blob: a1fbb76c45729c29e1e2a08efbeb7fbe2996da9a [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate Indirect String data nodes.
Willy Tarreauf3bfede2011-07-25 11:38:17 +02003 * Version 6.0.6
Willy Tarreaue1ee9562011-01-04 14:33:13 +01004 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +02006 * 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 Tarreauc2186022009-10-26 19:48:54 +010010 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020011 * This library is distributed in the hope that it will be useful,
Willy Tarreauc2186022009-10-26 19:48:54 +010012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Willy Tarreauf3bfede2011-07-25 11:38:17 +020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
Willy Tarreauc2186022009-10-26 19:48:54 +010015 *
Willy Tarreauf3bfede2011-07-25 11:38:17 +020016 * 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 Tarreauc2186022009-10-26 19:48:54 +010019 */
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 Tarreaue1ee9562011-01-04 14:33:13 +010026#include "ebimtree.h"
Willy Tarreauc2186022009-10-26 19:48:54 +010027
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 */
35REGPRM2 struct ebpt_node *ebis_lookup(struct eb_root *root, const char *x);
Willy Tarreauc2186022009-10-26 19:48:54 +010036REGPRM2 struct ebpt_node *ebis_insert(struct eb_root *root, struct ebpt_node *new);
37
Willy Tarreaue1ee9562011-01-04 14:33:13 +010038/* 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 */
43static forceinline struct ebpt_node *
44ebis_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 Tarreauc2186022009-10-26 19:48:54 +010054/* 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 */
58static 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 Tarreau3a932442010-05-09 19:29:23 +020062 int bit;
63 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010064
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 Tarreau3a932442010-05-09 19:29:23 +020081 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010082
Willy Tarreau3a932442010-05-09 19:29:23 +020083 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010084 /* 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 Tarreaub55fcf22010-10-28 22:48:29 +020099 /* 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 Tarreau007257e2011-11-14 14:09:27 +0100116 /* 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 Tarreaub55fcf22010-10-28 22:48:29 +0200124 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100125
Willy Tarreau3a932442010-05-09 19:29:23 +0200126 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
127 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +0100128 }
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 */
136static 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 Tarreau6258f7b2011-09-19 20:48:00 +0200142 eb_troot_t *root_right;
Willy Tarreauc2186022009-10-26 19:48:54 +0100143 int diff;
144 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200145 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100146
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 Tarreaub55fcf22010-10-28 22:48:29 +0200201 if (bit >= 0)
202 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100203
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200204 if (bit < 0) {
205 /* key was already there */
206
Willy Tarreauc2186022009-10-26 19:48:54 +0100207 /* we may refuse to duplicate this key if the tree is
208 * tagged as containing only unique keys.
209 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200210 if (eb_gettag(root_right))
Willy Tarreauc2186022009-10-26 19:48:54 +0100211 return old;
212
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200213 /* new arbitrarily goes to the right and tops the dup tree */
Willy Tarreauc2186022009-10-26 19:48:54 +0100214 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 Tarreaub55fcf22010-10-28 22:48:29 +0200218 new->node.bit = -1;
219 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
220 return new;
221 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100222
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200223 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 Tarreauc2186022009-10-26 19:48:54 +0100236 }
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 Tarreau3a932442010-05-09 19:29:23 +0200243 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100244
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 Tarreaub55fcf22010-10-28 22:48:29 +0200251 if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
Willy Tarreauc2186022009-10-26 19:48:54 +0100252 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100253
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200254 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 Tarreauc2186022009-10-26 19:48:54 +0100277 */
278 eb_troot_t *new_left, *new_rght;
279 eb_troot_t *new_leaf, *old_node;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200280
Willy Tarreauc2186022009-10-26 19:48:54 +0100281 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 Tarreaub55fcf22010-10-28 22:48:29 +0200288 /* we can never match all bits here */
Willy Tarreauc2186022009-10-26 19:48:54 +0100289 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 Tarreaub55fcf22010-10-28 22:48:29 +0200296 else {
Willy Tarreauc2186022009-10-26 19:48:54 +0100297 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 Tarreauc2186022009-10-26 19:48:54 +0100302 break;
303 }
304
305 /* walk down */
306 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200307 side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100308 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 Tarreaub55fcf22010-10-28 22:48:29 +0200319 * NOTE: we can't get here whit bit < 0 since we found a dup !
Willy Tarreauc2186022009-10-26 19:48:54 +0100320 */
321 new->node.bit = bit;
322 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
323 return new;
324}
325