Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_hash.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Functions to manipulate with the data node's hashes. |
| 5 | * |
| 6 | * Copyright (c) 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 14 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "common.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 21 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include "hash_table.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include "log.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "tree.h" |
| 25 | #include "tree_data.h" |
| 26 | #include "tree_schema.h" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 27 | |
| 28 | static void |
Christian Hopps | 5961897 | 2021-02-01 05:01:35 -0500 | [diff] [blame] | 29 | lyd_hash_keyless_list_dfs(const struct lyd_node *child, uint32_t *hash) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 30 | { |
Christian Hopps | 5961897 | 2021-02-01 05:01:35 -0500 | [diff] [blame] | 31 | const struct lyd_node *iter; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 32 | |
| 33 | LY_LIST_FOR(child, iter) { |
| 34 | switch (iter->schema->nodetype) { |
| 35 | case LYS_CONTAINER: |
| 36 | case LYS_LIST: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 37 | lyd_hash_keyless_list_dfs(lyd_child(iter), hash); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 38 | break; |
| 39 | case LYS_LEAFLIST: |
| 40 | case LYS_ANYXML: |
| 41 | case LYS_ANYDATA: |
| 42 | case LYS_LEAF: |
| 43 | *hash = dict_hash_multi(*hash, (char *)&iter->hash, sizeof iter->hash); |
| 44 | break; |
| 45 | default: |
| 46 | LOGINT(NULL); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | LY_ERR |
| 52 | lyd_hash(struct lyd_node *node) |
| 53 | { |
| 54 | struct lyd_node *iter; |
| 55 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 56 | if (!node->schema) { |
| 57 | return LY_SUCCESS; |
| 58 | } |
| 59 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 60 | node->hash = dict_hash_multi(0, node->schema->module->name, strlen(node->schema->module->name)); |
| 61 | node->hash = dict_hash_multi(node->hash, node->schema->name, strlen(node->schema->name)); |
| 62 | |
| 63 | if (node->schema->nodetype == LYS_LIST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 64 | struct lyd_node_inner *list = (struct lyd_node_inner *)node; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 65 | if (!(node->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 66 | /* list's hash is made of its keys */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 67 | for (iter = list->child; iter && (iter->schema->flags & LYS_KEY); iter = iter->next) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 68 | const char *value = LYD_CANON_VALUE(iter); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 69 | node->hash = dict_hash_multi(node->hash, value, strlen(value)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 70 | } |
| 71 | } else { |
| 72 | /* keyless status list */ |
| 73 | lyd_hash_keyless_list_dfs(list->child, &node->hash); |
| 74 | } |
| 75 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 76 | const char *value = LYD_CANON_VALUE(node); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 77 | node->hash = dict_hash_multi(node->hash, value, strlen(value)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 78 | } |
| 79 | /* finish the hash */ |
| 80 | node->hash = dict_hash_multi(node->hash, NULL, 0); |
| 81 | |
| 82 | return LY_SUCCESS; |
| 83 | } |
| 84 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 85 | /** |
| 86 | * @brief Compare callback for values in hash table. |
| 87 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 88 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 89 | */ |
| 90 | static ly_bool |
| 91 | lyd_hash_table_val_equal(void *val1_p, void *val2_p, ly_bool mod, void *UNUSED(cb_data)) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 92 | { |
| 93 | struct lyd_node *val1, *val2; |
| 94 | |
| 95 | val1 = *((struct lyd_node **)val1_p); |
| 96 | val2 = *((struct lyd_node **)val2_p); |
| 97 | |
| 98 | if (mod) { |
| 99 | if (val1 == val2) { |
| 100 | return 1; |
| 101 | } else { |
| 102 | return 0; |
| 103 | } |
| 104 | } |
| 105 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 106 | if (val1->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 107 | /* match on exact instance */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 108 | if (!lyd_compare_single(val1, val2, 0)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | } else if (val1->schema == val2->schema) { |
| 112 | /* just schema match */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 113 | return 1; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 114 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 115 | return 0; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 118 | /** |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 119 | * @brief Comparison callback for hash table that never considers 2 values equal. |
| 120 | * |
| 121 | * Implementation of ::lyht_value_equal_cb. |
| 122 | */ |
| 123 | static ly_bool |
| 124 | lyd_not_equal_value_cb(void *UNUSED(val1_p), void *UNUSED(val2_p), ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 130 | * @brief Add single node into children hash table. |
| 131 | * |
| 132 | * @param[in] ht Children hash table. |
| 133 | * @param[in] node Node to insert. |
| 134 | * @param[in] empty_ht Whether we started with an empty HT meaning no nodes were inserted yet. |
| 135 | * @return LY_ERR value. |
| 136 | */ |
| 137 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 138 | lyd_insert_hash_add(struct hash_table *ht, struct lyd_node *node, ly_bool empty_ht) |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 139 | { |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 140 | LY_ERR rc = LY_SUCCESS; |
| 141 | lyht_value_equal_cb orig_cb = NULL; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 142 | uint32_t hash; |
| 143 | |
| 144 | assert(ht && node && node->schema); |
| 145 | |
| 146 | /* add node itself */ |
| 147 | if (lyht_insert(ht, &node, node->hash, NULL)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 148 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 149 | rc = LY_EINT; |
| 150 | goto cleanup; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* add first instance of a (leaf-)list */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 154 | if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && |
| 155 | (!node->prev->next || (node->prev->schema != node->schema))) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 156 | /* get the simple hash */ |
| 157 | hash = dict_hash_multi(0, node->schema->module->name, strlen(node->schema->module->name)); |
| 158 | hash = dict_hash_multi(hash, node->schema->name, strlen(node->schema->name)); |
| 159 | hash = dict_hash_multi(hash, NULL, 0); |
| 160 | |
| 161 | /* remove any previous stored instance, only if we did not start with an empty HT */ |
| 162 | if (!empty_ht && node->next && (node->next->schema == node->schema)) { |
| 163 | if (lyht_remove(ht, &node->next, hash)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 164 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 165 | rc = LY_EINT; |
| 166 | goto cleanup; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 170 | if (hash == node->hash) { |
| 171 | /* special case, key-less list with no children hash is equal to the first instance list hash */ |
| 172 | assert((node->schema->nodetype == LYS_LIST) && (node->schema->flags & LYS_KEYLESS) && !lyd_child(node)); |
| 173 | |
| 174 | /* we must allow exact duplicates in the hash table */ |
| 175 | orig_cb = lyht_set_cb(ht, lyd_not_equal_value_cb); |
| 176 | } |
| 177 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 178 | /* insert this instance as the first (leaf-)list instance */ |
| 179 | if (lyht_insert(ht, &node, hash, NULL)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 180 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 181 | rc = LY_EINT; |
| 182 | goto cleanup; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 186 | cleanup: |
| 187 | if (orig_cb) { |
| 188 | lyht_set_cb(ht, orig_cb); |
| 189 | } |
| 190 | return rc; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 193 | LY_ERR |
| 194 | lyd_insert_hash(struct lyd_node *node) |
| 195 | { |
| 196 | struct lyd_node *iter; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 197 | uint32_t u; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 198 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 199 | if (!node->parent || !node->schema || !node->parent->schema) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 200 | /* nothing to do */ |
| 201 | return LY_SUCCESS; |
| 202 | } |
| 203 | |
| 204 | /* create parent hash table if required, otherwise just add the new child */ |
| 205 | if (!node->parent->children_ht) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 206 | /* the hash table is created only when the number of children in a node exceeds the |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 207 | * defined minimal limit LYD_HT_MIN_ITEMS |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 208 | */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 209 | u = 0; |
| 210 | LY_LIST_FOR(node->parent->child, iter) { |
| 211 | if (iter->schema) { |
| 212 | ++u; |
| 213 | } |
| 214 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 215 | if (u >= LYD_HT_MIN_ITEMS) { |
| 216 | /* create hash table, insert all the children */ |
| 217 | node->parent->children_ht = lyht_new(1, sizeof(struct lyd_node *), lyd_hash_table_val_equal, NULL, 1); |
| 218 | LY_LIST_FOR(node->parent->child, iter) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 219 | if (iter->schema) { |
| 220 | LY_CHECK_RET(lyd_insert_hash_add(node->parent->children_ht, iter, 1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | } |
| 224 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 225 | LY_CHECK_RET(lyd_insert_hash_add(node->parent->children_ht, node, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return LY_SUCCESS; |
| 229 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 230 | |
| 231 | void |
| 232 | lyd_unlink_hash(struct lyd_node *node) |
| 233 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 234 | uint32_t hash; |
| 235 | |
| 236 | if (!node->parent || !node->schema || !node->parent->schema || !node->parent->children_ht) { |
| 237 | /* not in any HT */ |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | /* remove from the parent HT */ |
| 242 | if (lyht_remove(node->parent->children_ht, &node, node->hash)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 243 | LOGINT(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
| 247 | /* first instance of the (leaf-)list, needs to be removed from HT */ |
| 248 | if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (!node->prev->next || (node->prev->schema != node->schema))) { |
| 249 | /* get the simple hash */ |
| 250 | hash = dict_hash_multi(0, node->schema->module->name, strlen(node->schema->module->name)); |
| 251 | hash = dict_hash_multi(hash, node->schema->name, strlen(node->schema->name)); |
| 252 | hash = dict_hash_multi(hash, NULL, 0); |
| 253 | |
| 254 | /* remove the instance */ |
| 255 | if (lyht_remove(node->parent->children_ht, &node, hash)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 256 | LOGINT(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | |
| 260 | /* add the next instance */ |
| 261 | if (node->next && (node->next->schema == node->schema)) { |
| 262 | if (lyht_insert(node->parent->children_ht, &node->next, hash, NULL)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 263 | LOGINT(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 267 | } |
| 268 | } |