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