blob: 5c52d6e94d0b7a773045fb2d49b4cc0ee0733864 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate String data nodes.
Willy Tarreau3a932442010-05-09 19:29:23 +02003 * Version 6.0
4 * (C) 2002-2010 - 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);
Willy Tarreauc9a31da2009-12-14 12:40:27 +010033REGPRM3 struct ebmb_node *ebst_lookup_len(struct eb_root *root, const char *x, unsigned int len);
Willy Tarreauc2186022009-10-26 19:48:54 +010034REGPRM2 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 */
40static 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 Tarreau3a932442010-05-09 19:29:23 +020044 int bit;
45 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010046
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 Tarreau4c848222009-10-29 12:00:11 +010056 if (strcmp((char *)node->key, x) == 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010057 return node;
58 else
59 return NULL;
60 }
61 node = container_of(eb_untag(troot, EB_NODE),
62 struct ebmb_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +020063 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010064
Willy Tarreau3a932442010-05-09 19:29:23 +020065 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010066 /* 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 Tarreau4c848222009-10-29 12:00:11 +010070 if (strcmp((char *)node->key, x) != 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010071 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
Willy Tarreaub55fcf22010-10-28 22:48:29 +020081 /* OK, normal data node, let's walk down but don't compare data
82 * if we already reached the end of the key.
83 */
84 if (likely(bit >= 0)) {
85 bit = string_equal_bits(x, node->key, bit);
86 if (likely(bit < node_bit)) {
87 if (bit >= 0)
88 return NULL; /* no more common bits */
89
90 /* bit < 0 : we reached the end of the key. If we
91 * are in a tree with unique keys, we can return
92 * this node. Otherwise we have to walk it down
93 * and stop comparing bits.
94 */
95 if (eb_gettag(root->b[EB_RGHT]))
96 return node;
97 }
98 }
Willy Tarreauc2186022009-10-26 19:48:54 +010099
Willy Tarreau3a932442010-05-09 19:29:23 +0200100 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
101 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +0100102 }
103}
104
105/* Insert ebmb_node <new> into subtree starting at node root <root>. Only
106 * new->key needs be set with the zero-terminated string key. The ebmb_node is
107 * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
108 * caller is responsible for properly terminating the key with a zero.
109 */
110static forceinline struct ebmb_node *
111__ebst_insert(struct eb_root *root, struct ebmb_node *new)
112{
113 struct ebmb_node *old;
114 unsigned int side;
115 eb_troot_t *troot;
116 eb_troot_t *root_right = root;
117 int diff;
118 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200119 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100120
121 side = EB_LEFT;
122 troot = root->b[EB_LEFT];
123 root_right = root->b[EB_RGHT];
124 if (unlikely(troot == NULL)) {
125 /* Tree is empty, insert the leaf part below the left branch */
126 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
127 new->node.leaf_p = eb_dotag(root, EB_LEFT);
128 new->node.node_p = NULL; /* node part unused */
129 return new;
130 }
131
132 /* The tree descent is fairly easy :
133 * - first, check if we have reached a leaf node
134 * - second, check if we have gone too far
135 * - third, reiterate
136 * Everywhere, we use <new> for the node node we are inserting, <root>
137 * for the node we attach it to, and <old> for the node we are
138 * displacing below <new>. <troot> will always point to the future node
139 * (tagged with its type). <side> carries the side the node <new> is
140 * attached to below its parent, which is also where previous node
141 * was attached.
142 */
143
144 bit = 0;
145 while (1) {
146 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
147 eb_troot_t *new_left, *new_rght;
148 eb_troot_t *new_leaf, *old_leaf;
149
150 old = container_of(eb_untag(troot, EB_LEAF),
151 struct ebmb_node, node.branches);
152
153 new_left = eb_dotag(&new->node.branches, EB_LEFT);
154 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
155 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
156 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
157
158 new->node.node_p = old->node.leaf_p;
159
160 /* Right here, we have 3 possibilities :
161 * - the tree does not contain the key, and we have
162 * new->key < old->key. We insert new above old, on
163 * the left ;
164 *
165 * - the tree does not contain the key, and we have
166 * new->key > old->key. We insert new above old, on
167 * the right ;
168 *
169 * - the tree does contain the key, which implies it
170 * is alone. We add the new key next to it as a
171 * first duplicate.
172 *
173 * The last two cases can easily be partially merged.
174 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200175 if (bit >= 0)
176 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100177
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200178 if (bit < 0) {
179 /* key was already there */
180
Willy Tarreauc2186022009-10-26 19:48:54 +0100181 /* we may refuse to duplicate this key if the tree is
182 * tagged as containing only unique keys.
183 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200184 if (eb_gettag(root_right))
Willy Tarreauc2186022009-10-26 19:48:54 +0100185 return old;
186
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200187 /* new arbitrarily goes to the right and tops the dup tree */
Willy Tarreauc2186022009-10-26 19:48:54 +0100188 old->node.leaf_p = new_left;
189 new->node.leaf_p = new_rght;
190 new->node.branches.b[EB_LEFT] = old_leaf;
191 new->node.branches.b[EB_RGHT] = new_leaf;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200192 new->node.bit = -1;
193 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
194 return new;
195 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100196
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200197 diff = cmp_bits(new->key, old->key, bit);
198 if (diff < 0) {
199 /* new->key < old->key, new takes the left */
200 new->node.leaf_p = new_left;
201 old->node.leaf_p = new_rght;
202 new->node.branches.b[EB_LEFT] = new_leaf;
203 new->node.branches.b[EB_RGHT] = old_leaf;
204 } else {
205 /* new->key > old->key, new takes the right */
206 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 Tarreauc2186022009-10-26 19:48:54 +0100210 }
211 break;
212 }
213
214 /* OK we're walking down this link */
215 old = container_of(eb_untag(troot, EB_NODE),
216 struct ebmb_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200217 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100218
219 /* Stop going down when we don't have common bits anymore. We
220 * also stop in front of a duplicates tree because it means we
221 * have to insert above. Note: we can compare more bits than
222 * the current node's because as long as they are identical, we
223 * know we descend along the correct side.
224 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200225 if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
Willy Tarreauc2186022009-10-26 19:48:54 +0100226 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100227
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200228 if (unlikely(bit < 0)) {
229 /* Perfect match, we must only stop on head of dup tree
230 * or walk down to a leaf.
231 */
232 if (old_node_bit < 0) {
233 /* We know here that string_equal_bits matched all
234 * bits and that we're on top of a dup tree, then
235 * we can perform the dup insertion and return.
236 */
237 struct eb_node *ret;
238 ret = eb_insert_dup(&old->node, &new->node);
239 return container_of(ret, struct ebmb_node, node);
240 }
241 /* OK so let's walk down */
242 }
243 else if (bit < old_node_bit || old_node_bit < 0) {
244 /* The tree did not contain the key, or we stopped on top of a dup
245 * tree, possibly containing the key. In the former case, we insert
246 * <new> before the node <old>, and set ->bit to designate the lowest
247 * bit position in <new> which applies to ->branches.b[]. In the later
248 * case, we add the key to the existing dup tree. Note that we cannot
249 * enter here if we match an intermediate node's key that is not the
250 * head of a dup tree.
Willy Tarreauc2186022009-10-26 19:48:54 +0100251 */
252 eb_troot_t *new_left, *new_rght;
253 eb_troot_t *new_leaf, *old_node;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200254
Willy Tarreauc2186022009-10-26 19:48:54 +0100255 new_left = eb_dotag(&new->node.branches, EB_LEFT);
256 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
257 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
258 old_node = eb_dotag(&old->node.branches, EB_NODE);
259
260 new->node.node_p = old->node.node_p;
261
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200262 /* we can never match all bits here */
Willy Tarreauc2186022009-10-26 19:48:54 +0100263 diff = cmp_bits(new->key, old->key, bit);
264 if (diff < 0) {
265 new->node.leaf_p = new_left;
266 old->node.node_p = new_rght;
267 new->node.branches.b[EB_LEFT] = new_leaf;
268 new->node.branches.b[EB_RGHT] = old_node;
269 }
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200270 else {
Willy Tarreauc2186022009-10-26 19:48:54 +0100271 old->node.node_p = new_left;
272 new->node.leaf_p = new_rght;
273 new->node.branches.b[EB_LEFT] = old_node;
274 new->node.branches.b[EB_RGHT] = new_leaf;
275 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100276 break;
277 }
278
279 /* walk down */
280 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200281 side = (new->key[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100282 troot = root->b[side];
283 }
284
285 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
286 * parent is already set to <new>, and the <root>'s branch is still in
287 * <side>. Update the root's leaf till we have it. Note that we can also
288 * find the side by checking the side of new->node.node_p.
289 */
290
291 /* We need the common higher bits between new->key and old->key.
292 * This number of bits is already in <bit>.
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200293 * NOTE: we can't get here whit bit < 0 since we found a dup !
Willy Tarreauc2186022009-10-26 19:48:54 +0100294 */
295 new->node.bit = bit;
296 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
297 return new;
298}
299
Willy Tarreau9e2e39e2009-11-02 14:43:39 +0100300#endif /* _EBSTTREE_H */
301