Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elastic Binary Trees - macros to manipulate Indirect String data nodes. |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 3 | * Version 6.0 |
| 4 | * (C) 2002-2010 - Willy Tarreau <w@1wt.eu> |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* These functions and macros rely on Multi-Byte nodes */ |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include "ebtree.h" |
| 25 | #include "ebpttree.h" |
| 26 | |
| 27 | /* These functions and macros rely on Pointer nodes and use the <key> entry as |
| 28 | * a pointer to an indirect key. Most operations are performed using ebpt_*. |
| 29 | */ |
| 30 | |
| 31 | /* The following functions are not inlined by default. They are declared |
| 32 | * in ebistree.c, which simply relies on their inline version. |
| 33 | */ |
| 34 | REGPRM2 struct ebpt_node *ebis_lookup(struct eb_root *root, const char *x); |
Willy Tarreau | c9a31da | 2009-12-14 12:40:27 +0100 | [diff] [blame] | 35 | REGPRM3 struct ebpt_node *ebis_lookup_len(struct eb_root *root, const char *x, unsigned int len); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 36 | REGPRM2 struct ebpt_node *ebis_insert(struct eb_root *root, struct ebpt_node *new); |
| 37 | |
| 38 | /* Find the first occurence of a zero-terminated string <x> in the tree <root>. |
| 39 | * It's the caller's reponsibility to use this function only on trees which |
| 40 | * only contain zero-terminated strings. If none can be found, return NULL. |
| 41 | */ |
| 42 | static forceinline struct ebpt_node *__ebis_lookup(struct eb_root *root, const void *x) |
| 43 | { |
| 44 | struct ebpt_node *node; |
| 45 | eb_troot_t *troot; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 46 | int bit; |
| 47 | int node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 48 | |
| 49 | troot = root->b[EB_LEFT]; |
| 50 | if (unlikely(troot == NULL)) |
| 51 | return NULL; |
| 52 | |
| 53 | bit = 0; |
| 54 | while (1) { |
| 55 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 56 | node = container_of(eb_untag(troot, EB_LEAF), |
| 57 | struct ebpt_node, node.branches); |
| 58 | if (strcmp(node->key, x) == 0) |
| 59 | return node; |
| 60 | else |
| 61 | return NULL; |
| 62 | } |
| 63 | node = container_of(eb_untag(troot, EB_NODE), |
| 64 | struct ebpt_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 65 | node_bit = node->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 66 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 67 | if (node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 68 | /* We have a dup tree now. Either it's for the same |
| 69 | * value, and we walk down left, or it's a different |
| 70 | * one and we don't have our key. |
| 71 | */ |
| 72 | if (strcmp(node->key, x) != 0) |
| 73 | return NULL; |
| 74 | |
| 75 | troot = node->node.branches.b[EB_LEFT]; |
| 76 | while (eb_gettag(troot) != EB_LEAF) |
| 77 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 78 | node = container_of(eb_untag(troot, EB_LEAF), |
| 79 | struct ebpt_node, node.branches); |
| 80 | return node; |
| 81 | } |
| 82 | |
| 83 | /* OK, normal data node, let's walk down */ |
| 84 | bit = string_equal_bits(x, node->key, bit); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 85 | if (bit < node_bit) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 86 | return NULL; /* no more common bits */ |
| 87 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 88 | troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >> |
| 89 | (~node_bit & 7)) & 1]; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | /* Insert ebpt_node <new> into subtree starting at node root <root>. Only |
| 94 | * new->key needs be set with the zero-terminated string key. The ebpt_node is |
| 95 | * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
| 96 | * caller is responsible for properly terminating the key with a zero. |
| 97 | */ |
| 98 | static forceinline struct ebpt_node * |
| 99 | __ebis_insert(struct eb_root *root, struct ebpt_node *new) |
| 100 | { |
| 101 | struct ebpt_node *old; |
| 102 | unsigned int side; |
| 103 | eb_troot_t *troot; |
| 104 | eb_troot_t *root_right = root; |
| 105 | int diff; |
| 106 | int bit; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 107 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 108 | |
| 109 | side = EB_LEFT; |
| 110 | troot = root->b[EB_LEFT]; |
| 111 | root_right = root->b[EB_RGHT]; |
| 112 | if (unlikely(troot == NULL)) { |
| 113 | /* Tree is empty, insert the leaf part below the left branch */ |
| 114 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 115 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 116 | new->node.node_p = NULL; /* node part unused */ |
| 117 | return new; |
| 118 | } |
| 119 | |
| 120 | /* The tree descent is fairly easy : |
| 121 | * - first, check if we have reached a leaf node |
| 122 | * - second, check if we have gone too far |
| 123 | * - third, reiterate |
| 124 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 125 | * for the node we attach it to, and <old> for the node we are |
| 126 | * displacing below <new>. <troot> will always point to the future node |
| 127 | * (tagged with its type). <side> carries the side the node <new> is |
| 128 | * attached to below its parent, which is also where previous node |
| 129 | * was attached. |
| 130 | */ |
| 131 | |
| 132 | bit = 0; |
| 133 | while (1) { |
| 134 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 135 | eb_troot_t *new_left, *new_rght; |
| 136 | eb_troot_t *new_leaf, *old_leaf; |
| 137 | |
| 138 | old = container_of(eb_untag(troot, EB_LEAF), |
| 139 | struct ebpt_node, node.branches); |
| 140 | |
| 141 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 142 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 143 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 144 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 145 | |
| 146 | new->node.node_p = old->node.leaf_p; |
| 147 | |
| 148 | /* Right here, we have 3 possibilities : |
| 149 | * - the tree does not contain the key, and we have |
| 150 | * new->key < old->key. We insert new above old, on |
| 151 | * the left ; |
| 152 | * |
| 153 | * - the tree does not contain the key, and we have |
| 154 | * new->key > old->key. We insert new above old, on |
| 155 | * the right ; |
| 156 | * |
| 157 | * - the tree does contain the key, which implies it |
| 158 | * is alone. We add the new key next to it as a |
| 159 | * first duplicate. |
| 160 | * |
| 161 | * The last two cases can easily be partially merged. |
| 162 | */ |
| 163 | bit = string_equal_bits(new->key, old->key, bit); |
| 164 | diff = cmp_bits(new->key, old->key, bit); |
| 165 | |
| 166 | if (diff < 0) { |
| 167 | new->node.leaf_p = new_left; |
| 168 | old->node.leaf_p = new_rght; |
| 169 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 170 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 171 | } else { |
| 172 | /* we may refuse to duplicate this key if the tree is |
| 173 | * tagged as containing only unique keys. |
| 174 | */ |
| 175 | if (diff == 0 && eb_gettag(root_right)) |
| 176 | return old; |
| 177 | |
| 178 | /* new->key >= old->key, new goes the right */ |
| 179 | old->node.leaf_p = new_left; |
| 180 | new->node.leaf_p = new_rght; |
| 181 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 182 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 183 | |
| 184 | if (diff == 0) { |
| 185 | new->node.bit = -1; |
| 186 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 187 | return new; |
| 188 | } |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | /* OK we're walking down this link */ |
| 194 | old = container_of(eb_untag(troot, EB_NODE), |
| 195 | struct ebpt_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 196 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 197 | |
| 198 | /* Stop going down when we don't have common bits anymore. We |
| 199 | * also stop in front of a duplicates tree because it means we |
| 200 | * have to insert above. Note: we can compare more bits than |
| 201 | * the current node's because as long as they are identical, we |
| 202 | * know we descend along the correct side. |
| 203 | */ |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 204 | if (old_node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 205 | /* we're above a duplicate tree, we must compare till the end */ |
| 206 | bit = string_equal_bits(new->key, old->key, bit); |
| 207 | goto dup_tree; |
| 208 | } |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 209 | else if (bit < old_node_bit) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 210 | bit = string_equal_bits(new->key, old->key, bit); |
| 211 | } |
| 212 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 213 | if (bit < old_node_bit) { /* we don't have all bits in common */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 214 | /* The tree did not contain the key, so we insert <new> before the node |
| 215 | * <old>, and set ->bit to designate the lowest bit position in <new> |
| 216 | * which applies to ->branches.b[]. |
| 217 | */ |
| 218 | eb_troot_t *new_left, *new_rght; |
| 219 | eb_troot_t *new_leaf, *old_node; |
| 220 | dup_tree: |
| 221 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 222 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 223 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 224 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 225 | |
| 226 | new->node.node_p = old->node.node_p; |
| 227 | |
| 228 | diff = cmp_bits(new->key, old->key, bit); |
| 229 | if (diff < 0) { |
| 230 | new->node.leaf_p = new_left; |
| 231 | old->node.node_p = new_rght; |
| 232 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 233 | new->node.branches.b[EB_RGHT] = old_node; |
| 234 | } |
| 235 | else if (diff > 0) { |
| 236 | old->node.node_p = new_left; |
| 237 | new->node.leaf_p = new_rght; |
| 238 | new->node.branches.b[EB_LEFT] = old_node; |
| 239 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 240 | } |
| 241 | else { |
| 242 | struct eb_node *ret; |
| 243 | ret = eb_insert_dup(&old->node, &new->node); |
| 244 | return container_of(ret, struct ebpt_node, node); |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | /* walk down */ |
| 250 | root = &old->node.branches; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 251 | side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 252 | troot = root->b[side]; |
| 253 | } |
| 254 | |
| 255 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 256 | * parent is already set to <new>, and the <root>'s branch is still in |
| 257 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 258 | * find the side by checking the side of new->node.node_p. |
| 259 | */ |
| 260 | |
| 261 | /* We need the common higher bits between new->key and old->key. |
| 262 | * This number of bits is already in <bit>. |
| 263 | */ |
| 264 | new->node.bit = bit; |
| 265 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 266 | return new; |
| 267 | } |
| 268 | |