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 | 813c02d | 2021-04-26 10:29:19 +0200 | [diff] [blame] | 24 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "tree.h" |
| 26 | #include "tree_data.h" |
| 27 | #include "tree_schema.h" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 29 | LY_ERR |
| 30 | lyd_hash(struct lyd_node *node) |
| 31 | { |
| 32 | struct lyd_node *iter; |
Radek Krejci | 813c02d | 2021-04-26 10:29:19 +0200 | [diff] [blame] | 33 | const void *hash_key; |
| 34 | ly_bool dyn; |
| 35 | size_t key_len; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 36 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 37 | if (!node->schema) { |
| 38 | return LY_SUCCESS; |
| 39 | } |
| 40 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 41 | /* hash always starts with the module and schema name */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 42 | node->hash = dict_hash_multi(0, node->schema->module->name, strlen(node->schema->module->name)); |
| 43 | node->hash = dict_hash_multi(node->hash, node->schema->name, strlen(node->schema->name)); |
| 44 | |
| 45 | if (node->schema->nodetype == LYS_LIST) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 46 | if (node->schema->flags & LYS_KEYLESS) { |
aPiecek | 4f07c3e | 2021-06-11 10:53:07 +0200 | [diff] [blame] | 47 | /* key-less list simply calls hash function again with empty key, |
| 48 | * just so that it differs from the first-instance hash |
| 49 | */ |
| 50 | node->hash = dict_hash_multi(node->hash, NULL, 0); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 51 | } else { |
| 52 | struct lyd_node_inner *list = (struct lyd_node_inner *)node; |
| 53 | |
| 54 | /* list hash is made up from its keys */ |
Michal Vasko | 5d3eaa7 | 2021-05-21 15:36:19 +0200 | [diff] [blame] | 55 | for (iter = list->child; iter && iter->schema && (iter->schema->flags & LYS_KEY); iter = iter->next) { |
Radek Krejci | 813c02d | 2021-04-26 10:29:19 +0200 | [diff] [blame] | 56 | struct lyd_node_term *key = (struct lyd_node_term *)iter; |
| 57 | |
Michal Vasko | dcfac2c | 2021-05-10 11:36:37 +0200 | [diff] [blame] | 58 | hash_key = key->value.realtype->plugin->print(NULL, &key->value, LY_VALUE_LYB, NULL, &dyn, &key_len); |
Radek Krejci | 813c02d | 2021-04-26 10:29:19 +0200 | [diff] [blame] | 59 | node->hash = dict_hash_multi(node->hash, hash_key, key_len); |
| 60 | if (dyn) { |
| 61 | free((void *)hash_key); |
| 62 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 63 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 64 | } |
| 65 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 813c02d | 2021-04-26 10:29:19 +0200 | [diff] [blame] | 66 | /* leaf-list adds its hash key */ |
| 67 | struct lyd_node_term *llist = (struct lyd_node_term *)node; |
| 68 | |
Michal Vasko | dcfac2c | 2021-05-10 11:36:37 +0200 | [diff] [blame] | 69 | hash_key = llist->value.realtype->plugin->print(NULL, &llist->value, LY_VALUE_LYB, NULL, &dyn, &key_len); |
Radek Krejci | 813c02d | 2021-04-26 10:29:19 +0200 | [diff] [blame] | 70 | node->hash = dict_hash_multi(node->hash, hash_key, key_len); |
| 71 | if (dyn) { |
| 72 | free((void *)hash_key); |
| 73 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 74 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 75 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 76 | /* finish the hash */ |
| 77 | node->hash = dict_hash_multi(node->hash, NULL, 0); |
| 78 | |
| 79 | return LY_SUCCESS; |
| 80 | } |
| 81 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 82 | /** |
| 83 | * @brief Compare callback for values in hash table. |
| 84 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 85 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 86 | */ |
| 87 | static ly_bool |
| 88 | 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] | 89 | { |
| 90 | struct lyd_node *val1, *val2; |
| 91 | |
| 92 | val1 = *((struct lyd_node **)val1_p); |
| 93 | val2 = *((struct lyd_node **)val2_p); |
| 94 | |
| 95 | if (mod) { |
| 96 | if (val1 == val2) { |
| 97 | return 1; |
| 98 | } else { |
| 99 | return 0; |
| 100 | } |
| 101 | } |
| 102 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 103 | if (val1->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 104 | /* match on exact instance */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 105 | if (!lyd_compare_single(val1, val2, 0)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 106 | return 1; |
| 107 | } |
| 108 | } else if (val1->schema == val2->schema) { |
| 109 | /* just schema match */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 110 | return 1; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 111 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 112 | return 0; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 115 | /** |
| 116 | * @brief Add single node into children hash table. |
| 117 | * |
| 118 | * @param[in] ht Children hash table. |
| 119 | * @param[in] node Node to insert. |
| 120 | * @param[in] empty_ht Whether we started with an empty HT meaning no nodes were inserted yet. |
| 121 | * @return LY_ERR value. |
| 122 | */ |
| 123 | static LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 124 | lyd_insert_hash_add(struct ly_ht *ht, struct lyd_node *node, ly_bool empty_ht) |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 125 | { |
| 126 | uint32_t hash; |
| 127 | |
| 128 | assert(ht && node && node->schema); |
| 129 | |
| 130 | /* add node itself */ |
| 131 | if (lyht_insert(ht, &node, node->hash, NULL)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 132 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* add first instance of a (leaf-)list */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 136 | if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && |
| 137 | (!node->prev->next || (node->prev->schema != node->schema))) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 138 | /* get the simple hash */ |
| 139 | hash = dict_hash_multi(0, node->schema->module->name, strlen(node->schema->module->name)); |
| 140 | hash = dict_hash_multi(hash, node->schema->name, strlen(node->schema->name)); |
| 141 | hash = dict_hash_multi(hash, NULL, 0); |
| 142 | |
| 143 | /* remove any previous stored instance, only if we did not start with an empty HT */ |
| 144 | if (!empty_ht && node->next && (node->next->schema == node->schema)) { |
| 145 | if (lyht_remove(ht, &node->next, hash)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 146 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 150 | /* in this case there would be the exact same value twice in the hash table, not supported (by the HT) */ |
| 151 | assert(hash != node->hash); |
Michal Vasko | 4771829 | 2021-02-26 10:12:44 +0100 | [diff] [blame] | 152 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 153 | /* insert this instance as the first (leaf-)list instance */ |
| 154 | if (lyht_insert(ht, &node, hash, NULL)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 155 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 159 | return LY_SUCCESS; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 162 | LY_ERR |
| 163 | lyd_insert_hash(struct lyd_node *node) |
| 164 | { |
| 165 | struct lyd_node *iter; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 166 | uint32_t u; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 167 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 168 | if (!node->parent || !node->schema || !node->parent->schema) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 169 | /* nothing to do */ |
| 170 | return LY_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | /* create parent hash table if required, otherwise just add the new child */ |
| 174 | if (!node->parent->children_ht) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 175 | /* 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] | 176 | * defined minimal limit LYD_HT_MIN_ITEMS |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 177 | */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 178 | u = 0; |
| 179 | LY_LIST_FOR(node->parent->child, iter) { |
| 180 | if (iter->schema) { |
| 181 | ++u; |
| 182 | } |
| 183 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 184 | if (u >= LYD_HT_MIN_ITEMS) { |
| 185 | /* create hash table, insert all the children */ |
| 186 | node->parent->children_ht = lyht_new(1, sizeof(struct lyd_node *), lyd_hash_table_val_equal, NULL, 1); |
| 187 | LY_LIST_FOR(node->parent->child, iter) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 188 | if (iter->schema) { |
| 189 | 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] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 194 | 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] | 195 | } |
| 196 | |
| 197 | return LY_SUCCESS; |
| 198 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 199 | |
| 200 | void |
| 201 | lyd_unlink_hash(struct lyd_node *node) |
| 202 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 203 | uint32_t hash; |
| 204 | |
| 205 | if (!node->parent || !node->schema || !node->parent->schema || !node->parent->children_ht) { |
| 206 | /* not in any HT */ |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | /* remove from the parent HT */ |
| 211 | if (lyht_remove(node->parent->children_ht, &node, node->hash)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 212 | LOGINT(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
| 216 | /* first instance of the (leaf-)list, needs to be removed from HT */ |
| 217 | if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (!node->prev->next || (node->prev->schema != node->schema))) { |
| 218 | /* get the simple hash */ |
| 219 | hash = dict_hash_multi(0, node->schema->module->name, strlen(node->schema->module->name)); |
| 220 | hash = dict_hash_multi(hash, node->schema->name, strlen(node->schema->name)); |
| 221 | hash = dict_hash_multi(hash, NULL, 0); |
| 222 | |
| 223 | /* remove the instance */ |
| 224 | if (lyht_remove(node->parent->children_ht, &node, hash)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 225 | LOGINT(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
| 229 | /* add the next instance */ |
| 230 | if (node->next && (node->next->schema == node->schema)) { |
| 231 | if (lyht_insert(node->parent->children_ht, &node->next, hash, NULL)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 232 | LOGINT(LYD_CTX(node)); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 236 | } |
| 237 | } |