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