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 | e1ee956 | 2011-01-04 14:33:13 +0100 | [diff] [blame] | 3 | * Version 6.0.5 |
| 4 | * (C) 2002-2011 - 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" |
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 | } |
| 116 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 117 | |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 118 | troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >> |
| 119 | (~node_bit & 7)) & 1]; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Insert ebpt_node <new> into subtree starting at node root <root>. Only |
| 124 | * new->key needs be set with the zero-terminated string key. The ebpt_node is |
| 125 | * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The |
| 126 | * caller is responsible for properly terminating the key with a zero. |
| 127 | */ |
| 128 | static forceinline struct ebpt_node * |
| 129 | __ebis_insert(struct eb_root *root, struct ebpt_node *new) |
| 130 | { |
| 131 | struct ebpt_node *old; |
| 132 | unsigned int side; |
| 133 | eb_troot_t *troot; |
| 134 | eb_troot_t *root_right = root; |
| 135 | int diff; |
| 136 | int bit; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 137 | int old_node_bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 138 | |
| 139 | side = EB_LEFT; |
| 140 | troot = root->b[EB_LEFT]; |
| 141 | root_right = root->b[EB_RGHT]; |
| 142 | if (unlikely(troot == NULL)) { |
| 143 | /* Tree is empty, insert the leaf part below the left branch */ |
| 144 | root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF); |
| 145 | new->node.leaf_p = eb_dotag(root, EB_LEFT); |
| 146 | new->node.node_p = NULL; /* node part unused */ |
| 147 | return new; |
| 148 | } |
| 149 | |
| 150 | /* The tree descent is fairly easy : |
| 151 | * - first, check if we have reached a leaf node |
| 152 | * - second, check if we have gone too far |
| 153 | * - third, reiterate |
| 154 | * Everywhere, we use <new> for the node node we are inserting, <root> |
| 155 | * for the node we attach it to, and <old> for the node we are |
| 156 | * displacing below <new>. <troot> will always point to the future node |
| 157 | * (tagged with its type). <side> carries the side the node <new> is |
| 158 | * attached to below its parent, which is also where previous node |
| 159 | * was attached. |
| 160 | */ |
| 161 | |
| 162 | bit = 0; |
| 163 | while (1) { |
| 164 | if (unlikely(eb_gettag(troot) == EB_LEAF)) { |
| 165 | eb_troot_t *new_left, *new_rght; |
| 166 | eb_troot_t *new_leaf, *old_leaf; |
| 167 | |
| 168 | old = container_of(eb_untag(troot, EB_LEAF), |
| 169 | struct ebpt_node, node.branches); |
| 170 | |
| 171 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 172 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 173 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 174 | old_leaf = eb_dotag(&old->node.branches, EB_LEAF); |
| 175 | |
| 176 | new->node.node_p = old->node.leaf_p; |
| 177 | |
| 178 | /* Right here, we have 3 possibilities : |
| 179 | * - the tree does not contain the key, and we have |
| 180 | * new->key < old->key. We insert new above old, on |
| 181 | * the left ; |
| 182 | * |
| 183 | * - the tree does not contain the key, and we have |
| 184 | * new->key > old->key. We insert new above old, on |
| 185 | * the right ; |
| 186 | * |
| 187 | * - the tree does contain the key, which implies it |
| 188 | * is alone. We add the new key next to it as a |
| 189 | * first duplicate. |
| 190 | * |
| 191 | * The last two cases can easily be partially merged. |
| 192 | */ |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 193 | if (bit >= 0) |
| 194 | bit = string_equal_bits(new->key, old->key, bit); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 195 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 196 | if (bit < 0) { |
| 197 | /* key was already there */ |
| 198 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 199 | /* we may refuse to duplicate this key if the tree is |
| 200 | * tagged as containing only unique keys. |
| 201 | */ |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 202 | if (eb_gettag(root_right)) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 203 | return old; |
| 204 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 205 | /* new arbitrarily goes to the right and tops the dup tree */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 206 | old->node.leaf_p = new_left; |
| 207 | new->node.leaf_p = new_rght; |
| 208 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 209 | new->node.branches.b[EB_RGHT] = new_leaf; |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 210 | new->node.bit = -1; |
| 211 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 212 | return new; |
| 213 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 214 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 215 | diff = cmp_bits(new->key, old->key, bit); |
| 216 | if (diff < 0) { |
| 217 | /* new->key < old->key, new takes the left */ |
| 218 | new->node.leaf_p = new_left; |
| 219 | old->node.leaf_p = new_rght; |
| 220 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 221 | new->node.branches.b[EB_RGHT] = old_leaf; |
| 222 | } else { |
| 223 | /* new->key > old->key, new takes the right */ |
| 224 | old->node.leaf_p = new_left; |
| 225 | new->node.leaf_p = new_rght; |
| 226 | new->node.branches.b[EB_LEFT] = old_leaf; |
| 227 | new->node.branches.b[EB_RGHT] = new_leaf; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 228 | } |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | /* OK we're walking down this link */ |
| 233 | old = container_of(eb_untag(troot, EB_NODE), |
| 234 | struct ebpt_node, node.branches); |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 235 | old_node_bit = old->node.bit; |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 236 | |
| 237 | /* Stop going down when we don't have common bits anymore. We |
| 238 | * also stop in front of a duplicates tree because it means we |
| 239 | * have to insert above. Note: we can compare more bits than |
| 240 | * the current node's because as long as they are identical, we |
| 241 | * know we descend along the correct side. |
| 242 | */ |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 243 | if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0)) |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 244 | bit = string_equal_bits(new->key, old->key, bit); |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 245 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 246 | if (unlikely(bit < 0)) { |
| 247 | /* Perfect match, we must only stop on head of dup tree |
| 248 | * or walk down to a leaf. |
| 249 | */ |
| 250 | if (old_node_bit < 0) { |
| 251 | /* We know here that string_equal_bits matched all |
| 252 | * bits and that we're on top of a dup tree, then |
| 253 | * we can perform the dup insertion and return. |
| 254 | */ |
| 255 | struct eb_node *ret; |
| 256 | ret = eb_insert_dup(&old->node, &new->node); |
| 257 | return container_of(ret, struct ebpt_node, node); |
| 258 | } |
| 259 | /* OK so let's walk down */ |
| 260 | } |
| 261 | else if (bit < old_node_bit || old_node_bit < 0) { |
| 262 | /* The tree did not contain the key, or we stopped on top of a dup |
| 263 | * tree, possibly containing the key. In the former case, we insert |
| 264 | * <new> before the node <old>, and set ->bit to designate the lowest |
| 265 | * bit position in <new> which applies to ->branches.b[]. In the later |
| 266 | * case, we add the key to the existing dup tree. Note that we cannot |
| 267 | * enter here if we match an intermediate node's key that is not the |
| 268 | * head of a dup tree. |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 269 | */ |
| 270 | eb_troot_t *new_left, *new_rght; |
| 271 | eb_troot_t *new_leaf, *old_node; |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 272 | |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 273 | new_left = eb_dotag(&new->node.branches, EB_LEFT); |
| 274 | new_rght = eb_dotag(&new->node.branches, EB_RGHT); |
| 275 | new_leaf = eb_dotag(&new->node.branches, EB_LEAF); |
| 276 | old_node = eb_dotag(&old->node.branches, EB_NODE); |
| 277 | |
| 278 | new->node.node_p = old->node.node_p; |
| 279 | |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 280 | /* we can never match all bits here */ |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 281 | diff = cmp_bits(new->key, old->key, bit); |
| 282 | if (diff < 0) { |
| 283 | new->node.leaf_p = new_left; |
| 284 | old->node.node_p = new_rght; |
| 285 | new->node.branches.b[EB_LEFT] = new_leaf; |
| 286 | new->node.branches.b[EB_RGHT] = old_node; |
| 287 | } |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 288 | else { |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 289 | old->node.node_p = new_left; |
| 290 | new->node.leaf_p = new_rght; |
| 291 | new->node.branches.b[EB_LEFT] = old_node; |
| 292 | new->node.branches.b[EB_RGHT] = new_leaf; |
| 293 | } |
Willy Tarreau | c218602 | 2009-10-26 19:48:54 +0100 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | |
| 297 | /* walk down */ |
| 298 | root = &old->node.branches; |
Willy Tarreau | 3a93244 | 2010-05-09 19:29:23 +0200 | [diff] [blame] | 299 | 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] | 300 | troot = root->b[side]; |
| 301 | } |
| 302 | |
| 303 | /* Ok, now we are inserting <new> between <root> and <old>. <old>'s |
| 304 | * parent is already set to <new>, and the <root>'s branch is still in |
| 305 | * <side>. Update the root's leaf till we have it. Note that we can also |
| 306 | * find the side by checking the side of new->node.node_p. |
| 307 | */ |
| 308 | |
| 309 | /* We need the common higher bits between new->key and old->key. |
| 310 | * This number of bits is already in <bit>. |
Willy Tarreau | b55fcf2 | 2010-10-28 22:48:29 +0200 | [diff] [blame] | 311 | * 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] | 312 | */ |
| 313 | new->node.bit = bit; |
| 314 | root->b[side] = eb_dotag(&new->node.branches, EB_NODE); |
| 315 | return new; |
| 316 | } |
| 317 | |