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