blob: a82130aa36836b2ef83ed45d29c583f802a7beda [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate Indirect 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
23#include <string.h>
24#include "ebtree.h"
25#include "ebpttree.h"
26
27/* These functions and macros rely on Pointer nodes and use the <key> entry as
28 * a pointer to an indirect key. Most operations are performed using ebpt_*.
29 */
30
31/* The following functions are not inlined by default. They are declared
32 * in ebistree.c, which simply relies on their inline version.
33 */
34REGPRM2 struct ebpt_node *ebis_lookup(struct eb_root *root, const char *x);
Willy Tarreauc9a31da2009-12-14 12:40:27 +010035REGPRM3 struct ebpt_node *ebis_lookup_len(struct eb_root *root, const char *x, unsigned int len);
Willy Tarreauc2186022009-10-26 19:48:54 +010036REGPRM2 struct ebpt_node *ebis_insert(struct eb_root *root, struct ebpt_node *new);
37
38/* Find the first occurence of a zero-terminated 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. If none can be found, return NULL.
41 */
42static forceinline struct ebpt_node *__ebis_lookup(struct eb_root *root, const void *x)
43{
44 struct ebpt_node *node;
45 eb_troot_t *troot;
Willy Tarreau3a932442010-05-09 19:29:23 +020046 int bit;
47 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010048
49 troot = root->b[EB_LEFT];
50 if (unlikely(troot == NULL))
51 return NULL;
52
53 bit = 0;
54 while (1) {
55 if ((eb_gettag(troot) == EB_LEAF)) {
56 node = container_of(eb_untag(troot, EB_LEAF),
57 struct ebpt_node, node.branches);
58 if (strcmp(node->key, x) == 0)
59 return node;
60 else
61 return NULL;
62 }
63 node = container_of(eb_untag(troot, EB_NODE),
64 struct ebpt_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +020065 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010066
Willy Tarreau3a932442010-05-09 19:29:23 +020067 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010068 /* We have a dup tree now. Either it's for the same
69 * value, and we walk down left, or it's a different
70 * one and we don't have our key.
71 */
72 if (strcmp(node->key, x) != 0)
73 return NULL;
74
75 troot = node->node.branches.b[EB_LEFT];
76 while (eb_gettag(troot) != EB_LEAF)
77 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
78 node = container_of(eb_untag(troot, EB_LEAF),
79 struct ebpt_node, node.branches);
80 return node;
81 }
82
Willy Tarreaub55fcf22010-10-28 22:48:29 +020083 /* OK, normal data node, let's walk down but don't compare data
84 * if we already reached the end of the key.
85 */
86 if (likely(bit >= 0)) {
87 bit = string_equal_bits(x, node->key, bit);
88 if (likely(bit < node_bit)) {
89 if (bit >= 0)
90 return NULL; /* no more common bits */
91
92 /* bit < 0 : we reached the end of the key. If we
93 * are in a tree with unique keys, we can return
94 * this node. Otherwise we have to walk it down
95 * and stop comparing bits.
96 */
97 if (eb_gettag(root->b[EB_RGHT]))
98 return node;
99 }
100 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100101
Willy Tarreau3a932442010-05-09 19:29:23 +0200102 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
103 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +0100104 }
105}
106
107/* Insert ebpt_node <new> into subtree starting at node root <root>. Only
108 * new->key needs be set with the zero-terminated string key. The ebpt_node is
109 * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
110 * caller is responsible for properly terminating the key with a zero.
111 */
112static forceinline struct ebpt_node *
113__ebis_insert(struct eb_root *root, struct ebpt_node *new)
114{
115 struct ebpt_node *old;
116 unsigned int side;
117 eb_troot_t *troot;
118 eb_troot_t *root_right = root;
119 int diff;
120 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200121 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100122
123 side = EB_LEFT;
124 troot = root->b[EB_LEFT];
125 root_right = root->b[EB_RGHT];
126 if (unlikely(troot == NULL)) {
127 /* Tree is empty, insert the leaf part below the left branch */
128 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
129 new->node.leaf_p = eb_dotag(root, EB_LEFT);
130 new->node.node_p = NULL; /* node part unused */
131 return new;
132 }
133
134 /* The tree descent is fairly easy :
135 * - first, check if we have reached a leaf node
136 * - second, check if we have gone too far
137 * - third, reiterate
138 * Everywhere, we use <new> for the node node we are inserting, <root>
139 * for the node we attach it to, and <old> for the node we are
140 * displacing below <new>. <troot> will always point to the future node
141 * (tagged with its type). <side> carries the side the node <new> is
142 * attached to below its parent, which is also where previous node
143 * was attached.
144 */
145
146 bit = 0;
147 while (1) {
148 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
149 eb_troot_t *new_left, *new_rght;
150 eb_troot_t *new_leaf, *old_leaf;
151
152 old = container_of(eb_untag(troot, EB_LEAF),
153 struct ebpt_node, node.branches);
154
155 new_left = eb_dotag(&new->node.branches, EB_LEFT);
156 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
157 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
158 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
159
160 new->node.node_p = old->node.leaf_p;
161
162 /* Right here, we have 3 possibilities :
163 * - the tree does not contain the key, and we have
164 * new->key < old->key. We insert new above old, on
165 * the left ;
166 *
167 * - the tree does not contain the key, and we have
168 * new->key > old->key. We insert new above old, on
169 * the right ;
170 *
171 * - the tree does contain the key, which implies it
172 * is alone. We add the new key next to it as a
173 * first duplicate.
174 *
175 * The last two cases can easily be partially merged.
176 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200177 if (bit >= 0)
178 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100179
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200180 if (bit < 0) {
181 /* key was already there */
182
Willy Tarreauc2186022009-10-26 19:48:54 +0100183 /* we may refuse to duplicate this key if the tree is
184 * tagged as containing only unique keys.
185 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200186 if (eb_gettag(root_right))
Willy Tarreauc2186022009-10-26 19:48:54 +0100187 return old;
188
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200189 /* new arbitrarily goes to the right and tops the dup tree */
Willy Tarreauc2186022009-10-26 19:48:54 +0100190 old->node.leaf_p = new_left;
191 new->node.leaf_p = new_rght;
192 new->node.branches.b[EB_LEFT] = old_leaf;
193 new->node.branches.b[EB_RGHT] = new_leaf;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200194 new->node.bit = -1;
195 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
196 return new;
197 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100198
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200199 diff = cmp_bits(new->key, old->key, bit);
200 if (diff < 0) {
201 /* new->key < old->key, new takes the left */
202 new->node.leaf_p = new_left;
203 old->node.leaf_p = new_rght;
204 new->node.branches.b[EB_LEFT] = new_leaf;
205 new->node.branches.b[EB_RGHT] = old_leaf;
206 } else {
207 /* new->key > old->key, new takes the right */
208 old->node.leaf_p = new_left;
209 new->node.leaf_p = new_rght;
210 new->node.branches.b[EB_LEFT] = old_leaf;
211 new->node.branches.b[EB_RGHT] = new_leaf;
Willy Tarreauc2186022009-10-26 19:48:54 +0100212 }
213 break;
214 }
215
216 /* OK we're walking down this link */
217 old = container_of(eb_untag(troot, EB_NODE),
218 struct ebpt_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200219 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100220
221 /* Stop going down when we don't have common bits anymore. We
222 * also stop in front of a duplicates tree because it means we
223 * have to insert above. Note: we can compare more bits than
224 * the current node's because as long as they are identical, we
225 * know we descend along the correct side.
226 */
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200227 if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
Willy Tarreauc2186022009-10-26 19:48:54 +0100228 bit = string_equal_bits(new->key, old->key, bit);
Willy Tarreauc2186022009-10-26 19:48:54 +0100229
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200230 if (unlikely(bit < 0)) {
231 /* Perfect match, we must only stop on head of dup tree
232 * or walk down to a leaf.
233 */
234 if (old_node_bit < 0) {
235 /* We know here that string_equal_bits matched all
236 * bits and that we're on top of a dup tree, then
237 * we can perform the dup insertion and return.
238 */
239 struct eb_node *ret;
240 ret = eb_insert_dup(&old->node, &new->node);
241 return container_of(ret, struct ebpt_node, node);
242 }
243 /* OK so let's walk down */
244 }
245 else if (bit < old_node_bit || old_node_bit < 0) {
246 /* The tree did not contain the key, or we stopped on top of a dup
247 * tree, possibly containing the key. In the former case, we insert
248 * <new> before the node <old>, and set ->bit to designate the lowest
249 * bit position in <new> which applies to ->branches.b[]. In the later
250 * case, we add the key to the existing dup tree. Note that we cannot
251 * enter here if we match an intermediate node's key that is not the
252 * head of a dup tree.
Willy Tarreauc2186022009-10-26 19:48:54 +0100253 */
254 eb_troot_t *new_left, *new_rght;
255 eb_troot_t *new_leaf, *old_node;
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200256
Willy Tarreauc2186022009-10-26 19:48:54 +0100257 new_left = eb_dotag(&new->node.branches, EB_LEFT);
258 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
259 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
260 old_node = eb_dotag(&old->node.branches, EB_NODE);
261
262 new->node.node_p = old->node.node_p;
263
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200264 /* we can never match all bits here */
Willy Tarreauc2186022009-10-26 19:48:54 +0100265 diff = cmp_bits(new->key, old->key, bit);
266 if (diff < 0) {
267 new->node.leaf_p = new_left;
268 old->node.node_p = new_rght;
269 new->node.branches.b[EB_LEFT] = new_leaf;
270 new->node.branches.b[EB_RGHT] = old_node;
271 }
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200272 else {
Willy Tarreauc2186022009-10-26 19:48:54 +0100273 old->node.node_p = new_left;
274 new->node.leaf_p = new_rght;
275 new->node.branches.b[EB_LEFT] = old_node;
276 new->node.branches.b[EB_RGHT] = new_leaf;
277 }
Willy Tarreauc2186022009-10-26 19:48:54 +0100278 break;
279 }
280
281 /* walk down */
282 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200283 side = (((unsigned char *)new->key)[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100284 troot = root->b[side];
285 }
286
287 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
288 * parent is already set to <new>, and the <root>'s branch is still in
289 * <side>. Update the root's leaf till we have it. Note that we can also
290 * find the side by checking the side of new->node.node_p.
291 */
292
293 /* We need the common higher bits between new->key and old->key.
294 * This number of bits is already in <bit>.
Willy Tarreaub55fcf22010-10-28 22:48:29 +0200295 * NOTE: we can't get here whit bit < 0 since we found a dup !
Willy Tarreauc2186022009-10-26 19:48:54 +0100296 */
297 new->node.bit = bit;
298 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
299 return new;
300}
301