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