blob: 1110eea3d416eeb17945ec4f9b0836b8ee14c560 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate Indirect String data nodes.
Willy Tarreaue1ee9562011-01-04 14:33:13 +01003 * Version 6.0.5
4 * (C) 2002-2011 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
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 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 }
116 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100117
Willy Tarreau3a932442010-05-09 19:29:23 +0200118 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
119 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +0100120 }
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 */
128static 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 Tarreau3a932442010-05-09 19:29:23 +0200137 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100138
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 Tarreaub55fcf22010-10-28 22:48:29 +0200193 if (bit >= 0)
194 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100195
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200196 if (bit < 0) {
197 /* key was already there */
198
Willy Tarreauc2186022009-10-26 19:48:54 +0100199 /* we may refuse to duplicate this key if the tree is
200 * tagged as containing only unique keys.
201 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200202 if (eb_gettag(root_right))
Willy Tarreauc2186022009-10-26 19:48:54 +0100203 return old;
204
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200205 /* new arbitrarily goes to the right and tops the dup tree */
Willy Tarreauc2186022009-10-26 19:48:54 +0100206 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 Tarreaub55fcf22010-10-28 22:48:29 +0200210 new->node.bit = -1;
211 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
212 return new;
213 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100214
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200215 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 Tarreauc2186022009-10-26 19:48:54 +0100228 }
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 Tarreau3a932442010-05-09 19:29:23 +0200235 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100236
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 Tarreaub55fcf22010-10-28 22:48:29 +0200243 if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
Willy Tarreauc2186022009-10-26 19:48:54 +0100244 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100245
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200246 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 Tarreauc2186022009-10-26 19:48:54 +0100269 */
270 eb_troot_t *new_left, *new_rght;
271 eb_troot_t *new_leaf, *old_node;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200272
Willy Tarreauc2186022009-10-26 19:48:54 +0100273 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 Tarreaub55fcf22010-10-28 22:48:29 +0200280 /* we can never match all bits here */
Willy Tarreauc2186022009-10-26 19:48:54 +0100281 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 Tarreaub55fcf22010-10-28 22:48:29 +0200288 else {
Willy Tarreauc2186022009-10-26 19:48:54 +0100289 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 Tarreauc2186022009-10-26 19:48:54 +0100294 break;
295 }
296
297 /* walk down */
298 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200299 side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100300 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 Tarreaub55fcf22010-10-28 22:48:29 +0200311 * NOTE: we can't get here whit bit < 0 since we found a dup !
Willy Tarreauc2186022009-10-26 19:48:54 +0100312 */
313 new->node.bit = bit;
314 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
315 return new;
316}
317