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