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 | |
| 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include "common.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include "config.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include "hash_table.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include "log.h" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 23 | #include "plugins_types.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 |
| 29 | lyd_hash_keyless_list_dfs(struct lyd_node *child, uint32_t *hash) |
| 30 | { |
| 31 | struct lyd_node *iter; |
| 32 | |
| 33 | LY_LIST_FOR(child, iter) { |
| 34 | switch (iter->schema->nodetype) { |
| 35 | case LYS_CONTAINER: |
| 36 | case LYS_LIST: |
| 37 | lyd_hash_keyless_list_dfs(((struct lyd_node_inner*)iter)->child, hash); |
| 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) { |
| 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 */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 67 | struct lysc_node *key; |
| 68 | for (key = ((struct lysc_node_list*)node->schema)->child, iter = list->child; |
| 69 | key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY) && iter; |
| 70 | key = key->next, iter = iter->next) { |
| 71 | for ( ; iter && iter->schema != key; iter = iter->next); |
| 72 | if (!iter) { |
| 73 | break; |
| 74 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 75 | int dynamic = 0; |
| 76 | struct lysc_type *type = ((struct lysc_node_leaf*)iter->schema)->type; |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 77 | const char *value = type->plugin->print(&((struct lyd_node_term*)iter)->value, LYD_JSON, json_print_get_prefix, NULL, &dynamic); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 78 | node->hash = dict_hash_multi(node->hash, value, strlen(value)); |
| 79 | if (dynamic) { |
| 80 | free((char*)value); |
| 81 | } |
| 82 | } |
| 83 | } else { |
| 84 | /* keyless status list */ |
| 85 | lyd_hash_keyless_list_dfs(list->child, &node->hash); |
| 86 | } |
| 87 | } else if (node->schema->nodetype == LYS_LEAFLIST) { |
| 88 | struct lyd_node_term *llist = (struct lyd_node_term*)node; |
| 89 | int dynamic = 0; |
Radek Krejci | 13a57b6 | 2019-07-19 13:04:09 +0200 | [diff] [blame] | 90 | const char *value = ((struct lysc_node_leaflist*)node->schema)->type->plugin->print(&llist->value, LYD_JSON, |
| 91 | json_print_get_prefix, NULL, &dynamic); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 92 | node->hash = dict_hash_multi(node->hash, value, strlen(value)); |
| 93 | if (dynamic) { |
| 94 | free((char*)value); |
| 95 | } |
| 96 | } |
| 97 | /* finish the hash */ |
| 98 | node->hash = dict_hash_multi(node->hash, NULL, 0); |
| 99 | |
| 100 | return LY_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | static int |
| 104 | lyd_hash_table_val_equal(void *val1_p, void *val2_p, int mod, void *UNUSED(cb_data)) |
| 105 | { |
| 106 | struct lyd_node *val1, *val2; |
| 107 | |
| 108 | val1 = *((struct lyd_node **)val1_p); |
| 109 | val2 = *((struct lyd_node **)val2_p); |
| 110 | |
| 111 | if (mod) { |
| 112 | if (val1 == val2) { |
| 113 | return 1; |
| 114 | } else { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 119 | if (val1->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 120 | /* match on exact instance */ |
| 121 | if (!lyd_compare(val1, val2, 0)) { |
| 122 | return 1; |
| 123 | } |
| 124 | } else if (val1->schema == val2->schema) { |
| 125 | /* just schema match */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 126 | return 1; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 127 | } |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 128 | return 0; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | LY_ERR |
| 132 | lyd_insert_hash(struct lyd_node *node) |
| 133 | { |
| 134 | struct lyd_node *iter; |
| 135 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 136 | if (!node->parent || !node->schema || !node->parent->schema) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 137 | /* nothing to do */ |
| 138 | return LY_SUCCESS; |
| 139 | } |
| 140 | |
| 141 | /* create parent hash table if required, otherwise just add the new child */ |
| 142 | if (!node->parent->children_ht) { |
| 143 | unsigned int u; |
| 144 | |
| 145 | /* 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] | 146 | * defined minimal limit LYD_HT_MIN_ITEMS |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 147 | */ |
| 148 | for (u = 0, iter = node->parent->child; iter; ++u, iter = iter->next); |
| 149 | if (u >= LYD_HT_MIN_ITEMS) { |
| 150 | /* create hash table, insert all the children */ |
| 151 | node->parent->children_ht = lyht_new(1, sizeof(struct lyd_node *), lyd_hash_table_val_equal, NULL, 1); |
| 152 | LY_LIST_FOR(node->parent->child, iter) { |
| 153 | if (lyht_insert(node->parent->children_ht, &iter, iter->hash, NULL)) { |
| 154 | LOGINT(node->schema->module->ctx); |
| 155 | return LY_EINT; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } else { |
| 160 | if (lyht_insert(node->parent->children_ht, &node, node->hash, NULL)) { |
| 161 | LOGINT(node->schema->module->ctx); |
| 162 | return LY_EINT; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return LY_SUCCESS; |
| 167 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 168 | |
| 169 | void |
| 170 | lyd_unlink_hash(struct lyd_node *node) |
| 171 | { |
Michal Vasko | 413c7f2 | 2020-05-05 12:34:06 +0200 | [diff] [blame] | 172 | if (node->parent && node->parent->schema && node->parent->children_ht) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 173 | lyht_remove(node->parent->children_ht, &node, node->hash); |
| 174 | } |
| 175 | } |