Commit abdf2936 authored by Daniel Salzman's avatar Daniel Salzman

zone-tree: code cleanup

parent 0f81f7ab
......@@ -129,9 +129,8 @@ static int copy_signatures(zone_tree_t *from, zone_tree_t *to)
for (/* NOP */; !trie_it_finished(it); trie_it_next(it)) {
zone_node_t *node_from = (zone_node_t *)*trie_it_val(it);
zone_node_t *node_to = NULL;
zone_tree_get(to, node_from->owner, &node_to);
zone_node_t *node_to = zone_tree_get(to, node_from->owner);
if (node_to == NULL) {
continue;
}
......
......@@ -72,12 +72,7 @@ static int mark_nsec3(knot_rrset_t *rrset, zone_tree_t *nsec3_tree)
assert(nsec3_tree);
if (rrset->type == KNOT_RRTYPE_NSEC3) {
zone_node_t *node = NULL;
int ret = zone_tree_get(nsec3_tree, rrset->owner, &node);
if (ret != KNOT_EOK) {
return ret;
}
zone_node_t *node = zone_tree_get(nsec3_tree, rrset->owner);
if (node != NULL) {
node->flags |= NODE_FLAGS_REMOVED_NSEC;
}
......
......@@ -353,7 +353,7 @@ int apply_remove_rr(apply_ctx_t *ctx, const knot_rrset_t *rr)
node_remove_rdataset(node, rr->type);
// If node is empty now, delete it from zone tree.
if (node->rrset_count == 0 && node != contents->apex) {
zone_tree_delete_empty_node(tree, node);
zone_tree_delete_empty(tree, node);
}
}
......@@ -464,7 +464,7 @@ void update_free_zone(zone_contents_t **contents)
return;
}
zone_tree_apply((*contents)->nodes, free_additional, NULL);
(void)zone_tree_apply((*contents)->nodes, free_additional, NULL);
zone_tree_deep_free(&(*contents)->nodes);
zone_tree_deep_free(&(*contents)->nsec3_nodes);
......
......@@ -182,8 +182,8 @@ static void check_redundancy(zone_contents_t *counterpart, const knot_rrset_t *r
if (node->rrset_count == 0 && node != counterpart->apex) {
// Remove empty node.
zone_tree_t *t = knot_rrset_is_nsec3rel(rr) ?
counterpart->nsec3_nodes : counterpart->nodes;
zone_tree_delete_empty_node(t, node);
counterpart->nsec3_nodes : counterpart->nodes;
zone_tree_delete_empty(t, node);
}
}
......
......@@ -242,13 +242,11 @@ static int adjust_nsec3_pointers(zone_node_t **tnode, void *data)
zone_node_t *node = *tnode;
// Connect to NSEC3 node (only if NSEC3 tree is not empty)
zone_node_t *nsec3 = NULL;
knot_dname_t *nsec3_name = NULL;
int ret = create_nsec3_name(args->zone, node->owner, &nsec3_name);
if (ret == KNOT_EOK) {
assert(nsec3_name);
zone_tree_get(args->zone->nsec3_nodes, nsec3_name, &nsec3);
node->nsec3_node = nsec3;
node->nsec3_node = zone_tree_get(args->zone->nsec3_nodes, nsec3_name);
} else if (ret == KNOT_ENSEC3PAR) {
node->nsec3_node = NULL;
ret = KNOT_EOK;
......@@ -443,17 +441,10 @@ cleanup:
static zone_node_t *get_node(const zone_contents_t *zone, const knot_dname_t *name)
{
if (zone == NULL || name == NULL) {
return NULL;
}
zone_node_t *n;
int ret = zone_tree_get(zone->nodes, name, &n);
if (ret != KNOT_EOK) {
return NULL;
}
assert(zone);
assert(name);
return n;
return zone_tree_get(zone->nodes, name);
}
static int add_node(zone_contents_t *zone, zone_node_t *node, bool create_parents)
......@@ -563,17 +554,10 @@ static int add_nsec3_node(zone_contents_t *zone, zone_node_t *node)
static zone_node_t *get_nsec3_node(const zone_contents_t *zone,
const knot_dname_t *name)
{
if (zone == NULL || name == NULL) {
return NULL;
}
zone_node_t *n;
int ret = zone_tree_get(zone->nsec3_nodes, name, &n);
if (ret != KNOT_EOK) {
return NULL;
}
assert(zone);
assert(name);
return n;
return zone_tree_get(zone->nsec3_nodes, name);
}
static int insert_rr(zone_contents_t *z, const knot_rrset_t *rr,
......@@ -643,7 +627,7 @@ static int remove_rr(zone_contents_t *z, const knot_rrset_t *rr,
node_remove_rdataset(node, rr->type);
// If node is empty now, delete it from zone tree.
if (node->rrset_count == 0 && node != z->apex) {
zone_tree_delete_empty_node(nsec3 ? z->nsec3_nodes : z->nodes, node);
zone_tree_delete_empty(nsec3 ? z->nsec3_nodes : z->nodes, node);
}
}
......@@ -786,6 +770,10 @@ zone_node_t *zone_contents_get_node_for_rr(zone_contents_t *zone, const knot_rrs
const zone_node_t *zone_contents_find_node(const zone_contents_t *zone, const knot_dname_t *name)
{
if (zone == NULL || name == NULL) {
return NULL;
}
return get_node(zone, name);
}
......@@ -854,6 +842,10 @@ int zone_contents_find_dname(const zone_contents_t *zone,
const zone_node_t *zone_contents_find_nsec3_node(const zone_contents_t *zone,
const knot_dname_t *name)
{
if (zone == NULL || name == NULL) {
return NULL;
}
return get_nsec3_node(zone, name);
}
......@@ -1130,10 +1122,12 @@ void zone_contents_deep_free(zone_contents_t **contents)
if (*contents != NULL) {
// Delete NSEC3 tree
zone_tree_apply((*contents)->nsec3_nodes, destroy_node_rrsets_from_tree, NULL);
(void)zone_tree_apply((*contents)->nsec3_nodes,
destroy_node_rrsets_from_tree, NULL);
// Delete normal tree
zone_tree_apply((*contents)->nodes, destroy_node_rrsets_from_tree, NULL);
(void)zone_tree_apply((*contents)->nodes,
destroy_node_rrsets_from_tree, NULL);
}
zone_contents_free(contents);
......
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -210,12 +210,7 @@ static int knot_zone_diff_node(zone_node_t **node_ptr, void *data)
* First, we have to search the second tree to see if there's according
* node, if not, the whole node has been removed.
*/
zone_node_t *node_in_second_tree = NULL;
const knot_dname_t *node_owner = node->owner;
assert(node_owner);
zone_tree_get(param->nodes, node_owner, &node_in_second_tree);
zone_node_t *node_in_second_tree = zone_tree_get(param->nodes, node->owner);
if (node_in_second_tree == NULL) {
return remove_node(node, param->changeset);
}
......@@ -301,25 +296,13 @@ static int add_new_nodes(zone_node_t **node_ptr, void *data)
* and has to be added to changeset. Differencies on the RRSet level are
* already handled.
*/
const knot_dname_t *node_owner = node->owner;
/*
* Node should definitely have an owner, otherwise it would not be in
* the tree.
*/
assert(node_owner);
zone_node_t *new_node = NULL;
zone_tree_get(param->nodes, node_owner, &new_node);
int ret = KNOT_EOK;
if (!new_node) {
zone_node_t *new_node = zone_tree_get(param->nodes, node->owner);
if (new_node == NULL) {
assert(node);
ret = add_node(node, param->changeset);
return add_node(node, param->changeset);
}
return ret;
return KNOT_EOK;
}
static int load_trees(zone_tree_t *nodes1, zone_tree_t *nodes2,
......@@ -340,9 +323,7 @@ static int load_trees(zone_tree_t *nodes1, zone_tree_t *nodes2,
// Some nodes may have been added. Add missing nodes to changeset.
param.nodes = nodes1;
ret = zone_tree_apply(nodes2, add_new_nodes, &param);
return ret;
return zone_tree_apply(nodes2, add_new_nodes, &param);
}
int zone_contents_diff(const zone_contents_t *zone1, const zone_contents_t *zone2,
......
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -22,7 +22,7 @@
#include "libknot/errcode.h"
#include "contrib/macros.h"
zone_tree_t* zone_tree_create()
zone_tree_t *zone_tree_create(void)
{
return trie_create(NULL);
}
......@@ -36,47 +36,45 @@ size_t zone_tree_count(const zone_tree_t *tree)
return trie_weight(tree);
}
int zone_tree_is_empty(const zone_tree_t *tree)
bool zone_tree_is_empty(const zone_tree_t *tree)
{
return zone_tree_count(tree) == 0;
}
int zone_tree_insert(zone_tree_t *tree, zone_node_t *node)
{
if (tree == NULL) {
if (tree == NULL || node == NULL) {
return KNOT_EINVAL;
}
assert(tree && node && node->owner);
assert(node->owner);
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, node->owner, NULL);
*trie_get_ins(tree, (char*)lf+1, *lf) = node;
*trie_get_ins(tree, (char *)lf + 1, *lf) = node;
return KNOT_EOK;
}
int zone_tree_get(zone_tree_t *tree, const knot_dname_t *owner,
zone_node_t **found)
zone_node_t *zone_tree_get(zone_tree_t *tree, const knot_dname_t *owner)
{
if (owner == NULL) {
return KNOT_EINVAL;
return NULL;
}
if (zone_tree_is_empty(tree)) {
return KNOT_ENONODE;
return NULL;
}
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, owner, NULL);
trie_val_t *val = trie_get_try(tree, (char*)lf+1, *lf);
trie_val_t *val = trie_get_try(tree, (char *)lf + 1, *lf);
if (val == NULL) {
*found = NULL;
} else {
*found = (zone_node_t*)(*val);
return NULL;
}
return KNOT_EOK;
return *val;
}
int zone_tree_get_less_or_equal(zone_tree_t *tree,
......@@ -96,14 +94,14 @@ int zone_tree_get_less_or_equal(zone_tree_t *tree,
knot_dname_lf(lf, owner, NULL);
trie_val_t *fval = NULL;
int ret = trie_get_leq(tree, (char*)lf+1, *lf, &fval);
if (fval) {
int ret = trie_get_leq(tree, (char *)lf + 1, *lf, &fval);
if (fval != NULL) {
*found = (zone_node_t *)(*fval);
}
int exact_match = 0;
if (ret == KNOT_EOK) {
if (fval) {
if (fval != NULL) {
*previous = (*found)->prev;
}
exact_match = 1;
......@@ -126,30 +124,22 @@ int zone_tree_get_less_or_equal(zone_tree_t *tree,
return exact_match;
}
int zone_tree_remove(zone_tree_t *tree,
const knot_dname_t *owner,
zone_node_t **removed)
/*! \brief Removes node with the given owner from the zone tree. */
static void remove_node(zone_tree_t *tree, const knot_dname_t *owner)
{
if (owner == NULL) {
return KNOT_EINVAL;
}
assert(owner);
if (zone_tree_is_empty(tree)) {
return KNOT_ENONODE;
return;
}
uint8_t lf[KNOT_DNAME_MAXLEN];
knot_dname_lf(lf, owner, NULL);
trie_val_t *rval = trie_get_try(tree, (char*)lf+1, *lf);
if (rval == NULL) {
return KNOT_ENOENT;
} else {
*removed = (zone_node_t *)(*rval);
trie_val_t *rval = trie_get_try(tree, (char *)lf + 1, *lf);
if (rval != NULL) {
trie_del(tree, (char *)lf + 1, *lf, NULL);
}
trie_del(tree, (char*)lf+1, *lf, NULL);
return KNOT_EOK;
}
/*! \brief Clears wildcard child if set in parent node. */
......@@ -161,34 +151,27 @@ static void fix_wildcard_child(zone_node_t *node, const knot_dname_t *owner)
}
}
int zone_tree_delete_empty_node(zone_tree_t *tree, zone_node_t *node)
void zone_tree_delete_empty(zone_tree_t *tree, zone_node_t *node)
{
if (!tree || !node) {
return KNOT_EINVAL;
if (tree == NULL || node == NULL) {
return;
}
if (node->rrset_count == 0 && node->children == 0) {
zone_node_t *parent_node = node->parent;
if (parent_node) {
if (parent_node != NULL) {
parent_node->children--;
fix_wildcard_child(parent_node, node->owner);
if (parent_node->parent != NULL) { /* Is not apex */
// Recurse using the parent node, do not delete possibly empty parent.
int ret = zone_tree_delete_empty_node(tree, parent_node);
if (ret != KNOT_EOK) {
return ret;
}
zone_tree_delete_empty(tree, parent_node);
}
}
// Delete node
zone_node_t *removed_node = NULL;
zone_tree_remove(tree, node->owner, &removed_node);
UNUSED(removed_node);
remove_node(tree, node->owner);
node_free(&node, NULL);
}
return KNOT_EOK;
}
int zone_tree_apply(zone_tree_t *tree, zone_tree_apply_cb_t function, void *data)
......@@ -209,6 +192,7 @@ void zone_tree_free(zone_tree_t **tree)
if (tree == NULL || *tree == NULL) {
return;
}
trie_free(*tree);
*tree = NULL;
}
......@@ -216,9 +200,11 @@ void zone_tree_free(zone_tree_t **tree)
static int zone_tree_free_node(zone_node_t **node, void *data)
{
UNUSED(data);
if (node) {
node_free(node, NULL);
}
return KNOT_EOK;
}
......@@ -228,6 +214,6 @@ void zone_tree_deep_free(zone_tree_t **tree)
return;
}
zone_tree_apply(*tree, zone_tree_free_node, NULL);
(void)zone_tree_apply(*tree, zone_tree_free_node, NULL);
zone_tree_free(tree);
}
......@@ -13,14 +13,6 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief Zone trie structure and API for manipulating it.
*
* \addtogroup zone
* @{
*/
#pragma once
......@@ -39,11 +31,13 @@ typedef int (*zone_tree_apply_cb_t)(zone_node_t **node, void *data);
*
* \return created zone tree structure.
*/
zone_tree_t* zone_tree_create(void);
zone_tree_t *zone_tree_create(void);
/*!
* \brief Return number of nodes in the zone tree.
*
* \param tree Zone tree.
*
* \return number of nodes in tree.
*/
size_t zone_tree_count(const zone_tree_t *tree);
......@@ -55,7 +49,7 @@ size_t zone_tree_count(const zone_tree_t *tree);
*
* \return Nonzero if the zone tree is empty.
*/
int zone_tree_is_empty(const zone_tree_t *tree);
bool zone_tree_is_empty(const zone_tree_t *tree);
/*!
* \brief Inserts the given node into the zone tree.
......@@ -74,14 +68,10 @@ int zone_tree_insert(zone_tree_t *tree, zone_node_t *node);
*
* \param tree Zone tree to search in.
* \param owner Owner of the node to find.
* \param found Found node or NULL
*
* \retval KNOT_EOK
* \retval KNOT_EINVAL
* \retval KNOT_ENOMEM
* \retval Found node or NULL.
*/
int zone_tree_get(zone_tree_t *tree, const knot_dname_t *owner,
zone_node_t **found);
zone_node_t *zone_tree_get(zone_tree_t *tree, const knot_dname_t *owner);
/*!
* \brief Tries to find the given domain name in the zone tree and returns the
......@@ -107,28 +97,13 @@ int zone_tree_get_less_or_equal(zone_tree_t *tree,
zone_node_t **found,
zone_node_t **previous);
/*!
* \brief Removes node with the given owner from the zone tree and returns it.
*
* \param tree Zone tree to remove the node from.
* \param owner Owner of the node to find.
* \param removed The removed node.
*
* \retval The removed node.
*/
int zone_tree_remove(zone_tree_t *tree,
const knot_dname_t *owner,
zone_node_t **removed);
/*!
* \brief Delete a node that has no RRSets and no children.
*
* \param tree The tree to remove from.
* \param node The node to remove.
*
* \return KNOT_E*.
* \param tree The tree to remove from.
* \param node The node to remove.
*/
int zone_tree_delete_empty_node(zone_tree_t *tree, zone_node_t *node);
void zone_tree_delete_empty(zone_tree_t *tree, zone_node_t *node);
/*!
* \brief Applies the given function to each node in the zone in order.
......@@ -155,5 +130,3 @@ void zone_tree_free(zone_tree_t **tree);
* \param tree Zone tree to be destroyed.
*/
void zone_tree_deep_free(zone_tree_t **tree);
/*! @} */
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -88,10 +88,9 @@ int main(int argc, char *argv[])
/* 3. check data test */
passed = 1;
zone_node_t *node = NULL;
for (unsigned i = 0; i < NCOUNT; ++i) {
int r = zone_tree_get(t, NAME[i], &node);
if (r != KNOT_EOK || node != NODE + i) {
zone_node_t *node = zone_tree_get(t, NAME[i]);
if (node == NULL || node != NODE + i) {
passed = 0;
break;
}
......@@ -99,7 +98,7 @@ int main(int argc, char *argv[])
ok(passed, "ztree: lookup");
/* 4. ordered lookup */
node = NULL;
zone_node_t *node = NULL;
zone_node_t *prev = NULL;
knot_dname_t *tmp_dn = knot_dname_from_str_alloc("z.ac.");
zone_tree_get_less_or_equal(t, tmp_dn, &node, &prev);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment