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 | |
| 119 | if (!lyd_compare(val1, val2, 0)) { |
| 120 | return 1; |
| 121 | } else { |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | LY_ERR |
| 127 | lyd_insert_hash(struct lyd_node *node) |
| 128 | { |
| 129 | struct lyd_node *iter; |
| 130 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 131 | if (!node->parent || !node->schema || !node->parent->schema) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 132 | /* nothing to do */ |
| 133 | return LY_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | /* create parent hash table if required, otherwise just add the new child */ |
| 137 | if (!node->parent->children_ht) { |
| 138 | unsigned int u; |
| 139 | |
| 140 | /* 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] | 141 | * defined minimal limit LYD_HT_MIN_ITEMS |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 142 | */ |
| 143 | for (u = 0, iter = node->parent->child; iter; ++u, iter = iter->next); |
| 144 | if (u >= LYD_HT_MIN_ITEMS) { |
| 145 | /* create hash table, insert all the children */ |
| 146 | node->parent->children_ht = lyht_new(1, sizeof(struct lyd_node *), lyd_hash_table_val_equal, NULL, 1); |
| 147 | LY_LIST_FOR(node->parent->child, iter) { |
| 148 | if (lyht_insert(node->parent->children_ht, &iter, iter->hash, NULL)) { |
| 149 | LOGINT(node->schema->module->ctx); |
| 150 | return LY_EINT; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } else { |
| 155 | if (lyht_insert(node->parent->children_ht, &node, node->hash, NULL)) { |
| 156 | LOGINT(node->schema->module->ctx); |
| 157 | return LY_EINT; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return LY_SUCCESS; |
| 162 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 163 | |
| 164 | void |
| 165 | lyd_unlink_hash(struct lyd_node *node) |
| 166 | { |
Michal Vasko | 413c7f2 | 2020-05-05 12:34:06 +0200 | [diff] [blame] | 167 | if (node->parent && node->parent->schema && node->parent->children_ht) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 168 | lyht_remove(node->parent->children_ht, &node, node->hash); |
| 169 | } |
| 170 | } |