From: Michael Tremer Date: Mon, 8 Jan 2018 18:06:13 +0000 (+0000) Subject: database: Stop walking down the tree when we hit a leaf node X-Git-Tag: 0.9.0~103 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=025ef4897d2a58e9dd3c8e0c9a6389d2b33f261b database: Stop walking down the tree when we hit a leaf node Signed-off-by: Michael Tremer --- diff --git a/src/database.c b/src/database.c index a6e7f18..2d930a6 100644 --- a/src/database.c +++ b/src/database.c @@ -421,14 +421,14 @@ static int loc_database_fetch_network(struct loc_database* db, struct loc_networ return r; } -static int __loc_database_lookup_leaf_node(struct loc_database* db, const struct in6_addr* address, +static int __loc_database_node_is_leaf(const struct loc_database_network_node_v0* node) { + return (node->zero == htobe32(0xffffffff)); +} + +static int __loc_database_lookup_handle_leaf(struct loc_database* db, const struct in6_addr* address, struct loc_network** network, struct in6_addr* network_address, const struct loc_database_network_node_v0* node) { - // Check if this node is a leaf node - if (node->zero != htobe32(0xffffffff)) - return 1; - - DEBUG(db->ctx, "Node is a leaf: %jd\n", node - db->network_nodes_v0); + DEBUG(db->ctx, "Handling leaf node at %jd\n", node - db->network_nodes_v0); // Fetch the network int r = loc_database_fetch_network(db, network, @@ -454,12 +454,11 @@ static int __loc_database_lookup_leaf_node(struct loc_database* db, const struct static int __loc_database_lookup_max(struct loc_database* db, const struct in6_addr* address, struct loc_network** network, struct in6_addr* network_address, const struct loc_database_network_node_v0* node, int level) { - // If the node is a leaf node, we end here - int r = __loc_database_lookup_leaf_node(db, address, network, network_address, node); - if (r <= 0) - return r; + if (__loc_database_node_is_leaf(node)) + return __loc_database_lookup_handle_leaf(db, address, network, network_address, node); + int r; off_t node_index; // Try to go down the ones path first @@ -504,10 +503,10 @@ static int __loc_database_lookup(struct loc_database* db, const struct in6_addr* struct loc_network** network, struct in6_addr* network_address, const struct loc_database_network_node_v0* node, int level) { // If the node is a leaf node, we end here - int r = __loc_database_lookup_leaf_node(db, address, network, network_address, node); - if (r <= 0) - return r; + if (__loc_database_node_is_leaf(node)) + return __loc_database_lookup_handle_leaf(db, address, network, network_address, node); + int r; off_t node_index; // Follow the path