Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/tree_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -2810,9 +2810,14 @@ lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg,
}
}

if (lyds->rbn) {
/* insert node and try to reuse free lyds data */
lyds_insert2(parent_trg, first_trg, leader_p, dup_src, lyds);
if (lyds->rbn && !(dup_src->flags & LYD_EXT) && lyds_is_supported(dup_src)) {
/* insert node and try to reuse free lyds data, only for the nodes that lyd_insert_node() sorts as well */
r = lyds_insert2(parent_trg, first_trg, leader_p, dup_src, lyds);
if (r) {
/* insert the node anyway although the nodes will not be sorted, same as in lyd_insert_node() */
LOGWRN(LYD_CTX(dup_src), "Data in \"%s\" are not sorted.", dup_src->schema->name);
lyd_insert_node(parent_trg, first_trg, dup_src, LYD_INSERT_NODE_LAST_BY_SCHEMA);
}
} else {
/* generic insert node */
lyd_insert_node(parent_trg, first_trg, dup_src, LYD_INSERT_NODE_DEFAULT);
Expand Down
14 changes: 11 additions & 3 deletions src/tree_data_sorted.c
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ lyds_pool_add(struct lyd_node *leader, struct lyds_pool *pool)
rbt = lyds_get_rb_tree(leader, &root_meta);
if (root_meta) {
lyd_unlink_meta_single(root_meta);
/* the Red-black tree is owned by the pool now, the metadata must not refer to it anymore */
RBT_SET(root_meta, NULL);
if (pool->meta) {
tmp = pool->meta;
pool->meta = root_meta;
Expand Down Expand Up @@ -935,7 +937,6 @@ lyds_pool_clean(struct lyds_pool *pool)

for (meta = pool->meta; meta; meta = next) {
next = meta->next;
RBT_SET(meta, NULL);
lyd_free_meta_single(meta);
}
pool->meta = NULL;
Expand Down Expand Up @@ -1261,6 +1262,8 @@ lyds_insert2(struct lyd_node *parent, struct lyd_node **first_sibling, struct ly
struct lyd_meta *root_meta;

assert(pool && pool->rbn && first_sibling && node);
/* only for the nodes that lyd_insert_node() would sort as well */
assert(lyds_is_supported(node) && !(node->flags & LYD_EXT));

if (!*leader || ((*leader)->schema != node->schema)) {
/* leader has not been visited yet */
Expand Down Expand Up @@ -1297,7 +1300,7 @@ lyds_insert2(struct lyd_node *parent, struct lyd_node **first_sibling, struct ly
if (next) {
/* there is no free structure for red-black node, a new ones must be allocated */
ret = lyds_additionally_create_rb_nodes(first_sibling, leader, root_meta, &rbt, next);
LY_CHECK_RET(ret);
LY_CHECK_GOTO(ret, error);
}
}

Expand All @@ -1312,7 +1315,7 @@ lyds_insert2(struct lyd_node *parent, struct lyd_node **first_sibling, struct ly
} else {
/* allocate new node and insert */
ret = rb_insert(node, &rbt, &rbn);
LY_CHECK_RET(ret);
LY_CHECK_GOTO(ret, error);
}
/* connect @p node with siblings so that the order is maintained */
lyds_link_data_node(first_sibling, leader, node, root_meta, rbn);
Expand All @@ -1325,6 +1328,11 @@ lyds_insert2(struct lyd_node *parent, struct lyd_node **first_sibling, struct ly
*first_sibling = node->prev->next ? *first_sibling : node;

return LY_SUCCESS;

error:
/* keep the (partially) built Red-black tree in the leader, it is freed together with it */
RBT_SET(root_meta, rbt);
return ret;
}

void
Expand Down
52 changes: 52 additions & 0 deletions tests/utests/data/test_tree_data_sorted.c
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,57 @@ test_merge_siblings_destruct(void **state)
lyd_free_all(dst);
}

static void
test_merge_destruct_unsorted(void **state)
{
const char *schema, *trg, *data;
struct lyd_node *dst, *src, *node;

schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;revision 2014-05-08;"
"list lst {"
" key \"k\";"
" leaf k {type uint32;}"
" leaf-list ll {type uint32;}"
" list st {"
" config false;"
" key \"k\";"
" leaf k {type uint32;}"
" }"
"}}";
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);

/* the free lyds data of the source "lst" must not be reused for the nodes that are not sorted */
trg = "{\"a:lst\":[{\"k\":1}]}";

/* config false list */
CHECK_PARSE_LYD_PARAM(trg, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, dst);
data = "{\"a:lst\":[{\"k\":1,\"st\":[{\"k\":2},{\"k\":1}]},{\"k\":2}]}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, src);
assert_int_equal(lyd_merge_siblings(&dst, src, LYD_MERGE_DESTRUCT), LY_SUCCESS);
CHECK_LYD_STRING_PARAM(dst, data, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS);
assert_null(lyd_child_no_keys(dst)->meta);
lyd_free_all(dst);

/* opaque node */
CHECK_PARSE_LYD_PARAM(trg, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, dst);
data = "{\"a:lst\":[{\"k\":1,\"op\":\"x\"},{\"k\":2}]}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, LY_SUCCESS, src);
assert_int_equal(lyd_merge_siblings(&dst, src, LYD_MERGE_DESTRUCT), LY_SUCCESS);
CHECK_LYD_STRING_PARAM(dst, data, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS);
lyd_free_all(dst);

/* extension node (schema mount) */
CHECK_PARSE_LYD_PARAM(trg, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, dst);
data = "{\"a:lst\":[{\"k\":1,\"ll\":[1,2]},{\"k\":2}]}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, src);
LY_LIST_FOR(lyd_child_no_keys(src), node) {
node->flags |= LYD_EXT;
}
assert_int_equal(lyd_merge_siblings(&dst, src, LYD_MERGE_DESTRUCT), LY_SUCCESS);
assert_null(lyd_child_no_keys(dst)->meta);
lyd_free_all(dst);
}

static void
test_parse_data(void **state)
{
Expand Down Expand Up @@ -1671,6 +1722,7 @@ main(void)
UTEST(test_insert_multiple_keys),
UTEST(test_merge_siblings),
UTEST(test_merge_siblings_destruct),
UTEST(test_merge_destruct_unsorted),
UTEST(test_parse_data),
UTEST(test_parse_ordered_data),
UTEST(test_print_data),
Expand Down
Loading