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