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 | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 3 | * Version 6.0.6 |
Willy Tarreau | e1ee956 | 2011-01-04 14:33:13 +0100 | [diff] [blame] | 4 | * (C) 2002-2011 - Willy Tarreau <w@1wt.eu> |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 5 | * |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 10 | * |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 11 | * This library is distributed in the hope that it will be useful, |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 15 | * |
Willy Tarreau | f3bfede | 2011-07-25 11:38:17 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 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" |
Willy Tarreau | e1ee956 | 2011-01-04 14:33:13 +0100 | [diff] [blame] | 26 | #include "ebimtree.h" |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 27 | |
| 28 | /* These functions and macros rely on Pointer nodes and use the <key> entry as |
| 29 | * a pointer to an indirect key. Most operations are performed using ebpt_*. |
| 30 | */ |
| 31 | |
| 32 | /* The following functions are not inlined by default. They are declared |
| 33 | * in ebistree.c, which simply relies on their inline version. |
| 34 | */ |
| 35 | REGPRM2 struct ebpt_node *ebis_lookup(struct eb_root *root, const char *x); |
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 | |
Willy Tarreau | e1ee956 | 2011-01-04 14:33:13 +0100 | [diff] [blame] | 38 | /* Find the first occurence of a length <len> 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, and that no null character is present |
| 41 | * in string <x> in the first <len> chars. If none can be found, return NULL. |
| 42 | */ |
| 43 | static forceinline struct ebpt_node * |
| 44 | ebis_lookup_len(struct eb_root *root, const char *x, unsigned int len) |
| 45 | { |
| 46 | struct ebpt_node *node; |
| 47 | |
| 48 | node = ebim_lookup(root, x, len); |
| 49 | if (!node || ((const char *)node->key)[len] != 0) |
| 50 | return NULL; |
| 51 | return node; |
| 52 | } |
| 53 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 54 | /* Find the first occurence of a zero-terminated string <x> in the tree <root>. |
| 55 | * It's the caller's reponsibility to use this function only on trees which |
| 56 | * only contain zero-terminated strings. If none can be found, return NULL. |
| 57 | */ |
| 58 | static forceinline struct ebpt_node *__ebis_lookup(struct eb_root *root, const void *x) |
| 59 | { |
| 60 | struct ebpt_node *node; |
| 61 | eb_troot_t *troot; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 62 | int bit; |
| 63 | int node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 64 | |
| 65 | troot = root->b[EB_LEFT]; |
| 66 | if (unlikely(troot == NULL)) |
| 67 | return NULL; |
| 68 | |
| 69 | bit = 0; |
| 70 | while (1) { |
| 71 | if ((eb_gettag(troot) == EB_LEAF)) { |
| 72 | node = container_of(eb_untag(troot, EB_LEAF), |
| 73 | struct ebpt_node, node.branches); |
| 74 | if (strcmp(node->key, x) == 0) |
| 75 | return node; |
| 76 | else |
| 77 | return NULL; |
| 78 | } |
| 79 | node = container_of(eb_untag(troot, EB_NODE), |
| 80 | struct ebpt_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 81 | node_bit = node->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 82 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 83 | if (node_bit < 0) { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 84 | /* We have a dup tree now. Either it's for the same |
| 85 | * value, and we walk down left, or it's a different |
| 86 | * one and we don't have our key. |
| 87 | */ |
| 88 | if (strcmp(node->key, x) != 0) |
| 89 | return NULL; |
| 90 | |
| 91 | troot = node->node.branches.b[EB_LEFT]; |
| 92 | while (eb_gettag(troot) != EB_LEAF) |
| 93 | troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT]; |
| 94 | node = container_of(eb_untag(troot, EB_LEAF), |
| 95 | struct ebpt_node, node.branches); |
| 96 | return node; |
| 97 | } |
| 98 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 99 | /* OK, normal data node, let's walk down but don't compare data |
| 100 | * if we already reached the end of the key. |
| 101 | */ |
| 102 | if (likely(bit >= 0)) { |
| 103 | bit = string_equal_bits(x, node->key, bit); |
| 104 | if (likely(bit < node_bit)) { |
| 105 | if (bit >= 0) |
| 106 | return NULL; /* no more common bits */ |
| 107 | |
| 108 | /* bit < 0 : we reached the end of the key. If we |
| 109 | * are in a tree with unique keys, we can return |
| 110 | * this node. Otherwise we have to walk it down |
| 111 | * and stop comparing bits. |
| 112 | */ |
| 113 | if (eb_gettag(root->b[EB_RGHT])) |
| 114 | return node; |
| 115 | } |
Willy Tarreau | 007257e | 2011-11-14 14:09:27 +0100 | [diff] [blame] | 116 | /* if the bit is larger than the node's, we must bound it |
| 117 | * because we might have compared too many bytes with an |
| 118 | * inappropriate leaf. For a test, build a tree from "0", |
| 119 | * "WW", "W", "S" inserted in this exact sequence and lookup |
| 120 | * "W" => "S" is returned without this assignment. |
| 121 | */ |
| 122 | else |
| 123 | bit = node_bit; |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 124 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 125 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 126 | troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >> |
| 127 | (~node_bit & 7)) & 1]; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Insert ebpt_node <new> into subtree starting at node root <root>. Only |
| 132 | * new->key needs be set with the zero-terminated string key. The ebpt_node is |
| 133 | * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
| 134 | * caller is responsible for properly terminating the key with a zero. |
| 135 | */ |
| 136 | static forceinline struct ebpt_node * |
| 137 | __ebis_insert(struct eb_root *root, struct ebpt_node *new) |
| 138 | { |
| 139 | struct ebpt_node *old; |
| 140 | unsigned int side; |
| 141 | eb_troot_t *troot; |
Willy Tarreau | 6258f7b | 2011-09-19 20:48:00 +0200 | [diff] [blame] | 142 | eb_troot_t *root_right; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 143 | int diff; |
| 144 | int bit; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 145 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 146 | |
| 147 | side = EB_LEFT; |
| 148 | troot = root->b[EB_LEFT]; |
| 149 | root_right = root->b[EB_RGHT]; |
| 150 | if (unlikely(troot == NULL)) { |
| 151 | /* Tree is empty, insert the leaf part below the left branch */ |
| 152 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 153 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 154 | new->node.node_p = NULL; /* node part unused */ |
| 155 | return new; |
| 156 | } |
| 157 | |
| 158 | /* The tree descent is fairly easy : |
| 159 | * - first, check if we have reached a leaf node |
| 160 | * - second, check if we have gone too far |
| 161 | * - third, reiterate |
| 162 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 163 | * for the node we attach it to, and <old> for the node we are |
| 164 | * displacing below <new>. <troot> will always point to the future node |
| 165 | * (tagged with its type). <side> carries the side the node <new> is |
| 166 | * attached to below its parent, which is also where previous node |
| 167 | * was attached. |
| 168 | */ |
| 169 | |
| 170 | bit = 0; |
| 171 | while (1) { |
| 172 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 173 | eb_troot_t *new_left, *new_rght; |
| 174 | eb_troot_t *new_leaf, *old_leaf; |
| 175 | |
| 176 | old = container_of(eb_untag(troot, EB_LEAF), |
| 177 | struct ebpt_node, node.branches); |
| 178 | |
| 179 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 180 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 181 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 182 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 183 | |
| 184 | new->node.node_p = old->node.leaf_p; |
| 185 | |
| 186 | /* Right here, we have 3 possibilities : |
| 187 | * - the tree does not contain the key, and we have |
| 188 | * new->key < old->key. We insert new above old, on |
| 189 | * the left ; |
| 190 | * |
| 191 | * - the tree does not contain the key, and we have |
| 192 | * new->key > old->key. We insert new above old, on |
| 193 | * the right ; |
| 194 | * |
| 195 | * - the tree does contain the key, which implies it |
| 196 | * is alone. We add the new key next to it as a |
| 197 | * first duplicate. |
| 198 | * |
| 199 | * The last two cases can easily be partially merged. |
| 200 | */ |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 201 | if (bit >= 0) |
| 202 | bit = string_equal_bits(new->key, old->key, bit); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 203 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 204 | if (bit < 0) { |
| 205 | /* key was already there */ |
| 206 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 207 | /* we may refuse to duplicate this key if the tree is |
| 208 | * tagged as containing only unique keys. |
| 209 | */ |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 210 | if (eb_gettag(root_right)) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 211 | return old; |
| 212 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 213 | /* new arbitrarily goes to the right and tops the dup tree */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 214 | old->node.leaf_p = new_left; |
| 215 | new->node.leaf_p = new_rght; |
| 216 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 217 | new->node.branches.b[EB_RGHT] = new_leaf; |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 218 | new->node.bit = -1; |
| 219 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 220 | return new; |
| 221 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 222 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 223 | diff = cmp_bits(new->key, old->key, bit); |
| 224 | if (diff < 0) { |
| 225 | /* new->key < old->key, new takes the left */ |
| 226 | new->node.leaf_p = new_left; |
| 227 | old->node.leaf_p = new_rght; |
| 228 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 229 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 230 | } else { |
| 231 | /* new->key > old->key, new takes the right */ |
| 232 | old->node.leaf_p = new_left; |
| 233 | new->node.leaf_p = new_rght; |
| 234 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 235 | new->node.branches.b[EB_RGHT] = new_leaf; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 236 | } |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | /* OK we're walking down this link */ |
| 241 | old = container_of(eb_untag(troot, EB_NODE), |
| 242 | struct ebpt_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 243 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 244 | |
| 245 | /* Stop going down when we don't have common bits anymore. We |
| 246 | * also stop in front of a duplicates tree because it means we |
| 247 | * have to insert above. Note: we can compare more bits than |
| 248 | * the current node's because as long as they are identical, we |
| 249 | * know we descend along the correct side. |
| 250 | */ |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 251 | if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0)) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 252 | bit = string_equal_bits(new->key, old->key, bit); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 253 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 254 | if (unlikely(bit < 0)) { |
| 255 | /* Perfect match, we must only stop on head of dup tree |
| 256 | * or walk down to a leaf. |
| 257 | */ |
| 258 | if (old_node_bit < 0) { |
| 259 | /* We know here that string_equal_bits matched all |
| 260 | * bits and that we're on top of a dup tree, then |
| 261 | * we can perform the dup insertion and return. |
| 262 | */ |
| 263 | struct eb_node *ret; |
| 264 | ret = eb_insert_dup(&old->node, &new->node); |
| 265 | return container_of(ret, struct ebpt_node, node); |
| 266 | } |
| 267 | /* OK so let's walk down */ |
| 268 | } |
| 269 | else if (bit < old_node_bit || old_node_bit < 0) { |
| 270 | /* The tree did not contain the key, or we stopped on top of a dup |
| 271 | * tree, possibly containing the key. In the former case, we insert |
| 272 | * <new> before the node <old>, and set ->bit to designate the lowest |
| 273 | * bit position in <new> which applies to ->branches.b[]. In the later |
| 274 | * case, we add the key to the existing dup tree. Note that we cannot |
| 275 | * enter here if we match an intermediate node's key that is not the |
| 276 | * head of a dup tree. |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 277 | */ |
| 278 | eb_troot_t *new_left, *new_rght; |
| 279 | eb_troot_t *new_leaf, *old_node; |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 280 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 281 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 282 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 283 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 284 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 285 | |
| 286 | new->node.node_p = old->node.node_p; |
| 287 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 288 | /* we can never match all bits here */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 289 | diff = cmp_bits(new->key, old->key, bit); |
| 290 | if (diff < 0) { |
| 291 | new->node.leaf_p = new_left; |
| 292 | old->node.node_p = new_rght; |
| 293 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 294 | new->node.branches.b[EB_RGHT] = old_node; |
| 295 | } |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 296 | else { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 297 | old->node.node_p = new_left; |
| 298 | new->node.leaf_p = new_rght; |
| 299 | new->node.branches.b[EB_LEFT] = old_node; |
| 300 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 301 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | |
| 305 | /* walk down */ |
| 306 | root = &old->node.branches; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 307 | 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] | 308 | troot = root->b[side]; |
| 309 | } |
| 310 | |
| 311 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 312 | * parent is already set to <new>, and the <root>'s branch is still in |
| 313 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 314 | * find the side by checking the side of new->node.node_p. |
| 315 | */ |
| 316 | |
| 317 | /* We need the common higher bits between new->key and old->key. |
| 318 | * This number of bits is already in <bit>. |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 319 | * NOTE: we can't get here whit bit < 0 since we found a dup ! |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 320 | */ |
| 321 | new->node.bit = bit; |
| 322 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 323 | return new; |
| 324 | } |
| 325 | |