blob: 9c86f611e521ffeca09c38604fb2a5fb193d3521 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate 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
Willy Tarreau9e2e39e2009-11-02 14:43:39 +010023#ifndef _EBSTTREE_H
24#define _EBSTTREE_H
25
Willy Tarreauc2186022009-10-26 19:48:54 +010026#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 */
32REGPRM2 struct ebmb_node *ebst_lookup(struct eb_root *root, const char *x);
33REGPRM2 struct ebmb_node *ebst_insert(struct eb_root *root, struct ebmb_node *new);
34
Willy Tarreaue1ee9562011-01-04 14:33:13 +010035/* 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 */
40static forceinline struct ebmb_node *
41ebst_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 Tarreauc2186022009-10-26 19:48:54 +010051/* 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 */
55static 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 Tarreau3a932442010-05-09 19:29:23 +020059 int bit;
60 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010061
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 Tarreau4c848222009-10-29 12:00:11 +010071 if (strcmp((char *)node->key, x) == 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010072 return node;
73 else
74 return NULL;
75 }
76 node = container_of(eb_untag(troot, EB_NODE),
77 struct ebmb_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +020078 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010079
Willy Tarreau3a932442010-05-09 19:29:23 +020080 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010081 /* 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 Tarreau4c848222009-10-29 12:00:11 +010085 if (strcmp((char *)node->key, x) != 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010086 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 Tarreaub55fcf22010-10-28 22:48:29 +020096 /* 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 Tarreauc2186022009-10-26 19:48:54 +0100114
Willy Tarreau3a932442010-05-09 19:29:23 +0200115 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
116 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +0100117 }
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 */
125static 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 Tarreau3a932442010-05-09 19:29:23 +0200134 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100135
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 Tarreaub55fcf22010-10-28 22:48:29 +0200190 if (bit >= 0)
191 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100192
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200193 if (bit < 0) {
194 /* key was already there */
195
Willy Tarreauc2186022009-10-26 19:48:54 +0100196 /* we may refuse to duplicate this key if the tree is
197 * tagged as containing only unique keys.
198 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200199 if (eb_gettag(root_right))
Willy Tarreauc2186022009-10-26 19:48:54 +0100200 return old;
201
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200202 /* new arbitrarily goes to the right and tops the dup tree */
Willy Tarreauc2186022009-10-26 19:48:54 +0100203 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 Tarreaub55fcf22010-10-28 22:48:29 +0200207 new->node.bit = -1;
208 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
209 return new;
210 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100211
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200212 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 Tarreauc2186022009-10-26 19:48:54 +0100225 }
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 Tarreau3a932442010-05-09 19:29:23 +0200232 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100233
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 Tarreaub55fcf22010-10-28 22:48:29 +0200240 if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
Willy Tarreauc2186022009-10-26 19:48:54 +0100241 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100242
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200243 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 Tarreauc2186022009-10-26 19:48:54 +0100266 */
267 eb_troot_t *new_left, *new_rght;
268 eb_troot_t *new_leaf, *old_node;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200269
Willy Tarreauc2186022009-10-26 19:48:54 +0100270 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 Tarreaub55fcf22010-10-28 22:48:29 +0200277 /* we can never match all bits here */
Willy Tarreauc2186022009-10-26 19:48:54 +0100278 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 Tarreaub55fcf22010-10-28 22:48:29 +0200285 else {
Willy Tarreauc2186022009-10-26 19:48:54 +0100286 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 Tarreauc2186022009-10-26 19:48:54 +0100291 break;
292 }
293
294 /* walk down */
295 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200296 side = (new->key[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100297 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 Tarreaub55fcf22010-10-28 22:48:29 +0200308 * NOTE: we can't get here whit bit < 0 since we found a dup !
Willy Tarreauc2186022009-10-26 19:48:54 +0100309 */
310 new->node.bit = bit;
311 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
312 return new;
313}
314
Willy Tarreau9e2e39e2009-11-02 14:43:39 +0100315#endif /* _EBSTTREE_H */
316