blob: 62f42c6fdc2d0945de42e1420a90bf09e83af04e [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate Indirect String data nodes.
Willy Tarreauc9a31da2009-12-14 12:40:27 +01003 * Version 5.1
Willy Tarreauc2186022009-10-26 19:48:54 +01004 * (C) 2002-2009 - Willy Tarreau <w@1wt.eu>
5 *
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;
46 unsigned int bit;
47
48 troot = root->b[EB_LEFT];
49 if (unlikely(troot == NULL))
50 return NULL;
51
52 bit = 0;
53 while (1) {
54 if ((eb_gettag(troot) == EB_LEAF)) {
55 node = container_of(eb_untag(troot, EB_LEAF),
56 struct ebpt_node, node.branches);
57 if (strcmp(node->key, x) == 0)
58 return node;
59 else
60 return NULL;
61 }
62 node = container_of(eb_untag(troot, EB_NODE),
63 struct ebpt_node, node.branches);
64
65 if (node->node.bit < 0) {
66 /* 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 */
70 if (strcmp(node->key, x) != 0)
71 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 ebpt_node, node.branches);
78 return node;
79 }
80
81 /* OK, normal data node, let's walk down */
82 bit = string_equal_bits(x, node->key, bit);
83 if (bit < node->node.bit)
84 return NULL; /* no more common bits */
85
86 troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >>
87 (~node->node.bit & 7)) & 1];
88 }
89}
90
91/* Insert ebpt_node <new> into subtree starting at node root <root>. Only
92 * new->key needs be set with the zero-terminated string key. The ebpt_node is
93 * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
94 * caller is responsible for properly terminating the key with a zero.
95 */
96static forceinline struct ebpt_node *
97__ebis_insert(struct eb_root *root, struct ebpt_node *new)
98{
99 struct ebpt_node *old;
100 unsigned int side;
101 eb_troot_t *troot;
102 eb_troot_t *root_right = root;
103 int diff;
104 int bit;
105
106 side = EB_LEFT;
107 troot = root->b[EB_LEFT];
108 root_right = root->b[EB_RGHT];
109 if (unlikely(troot == NULL)) {
110 /* Tree is empty, insert the leaf part below the left branch */
111 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
112 new->node.leaf_p = eb_dotag(root, EB_LEFT);
113 new->node.node_p = NULL; /* node part unused */
114 return new;
115 }
116
117 /* The tree descent is fairly easy :
118 * - first, check if we have reached a leaf node
119 * - second, check if we have gone too far
120 * - third, reiterate
121 * Everywhere, we use <new> for the node node we are inserting, <root>
122 * for the node we attach it to, and <old> for the node we are
123 * displacing below <new>. <troot> will always point to the future node
124 * (tagged with its type). <side> carries the side the node <new> is
125 * attached to below its parent, which is also where previous node
126 * was attached.
127 */
128
129 bit = 0;
130 while (1) {
131 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
132 eb_troot_t *new_left, *new_rght;
133 eb_troot_t *new_leaf, *old_leaf;
134
135 old = container_of(eb_untag(troot, EB_LEAF),
136 struct ebpt_node, node.branches);
137
138 new_left = eb_dotag(&new->node.branches, EB_LEFT);
139 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
140 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
141 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
142
143 new->node.node_p = old->node.leaf_p;
144
145 /* Right here, we have 3 possibilities :
146 * - the tree does not contain the key, and we have
147 * new->key < old->key. We insert new above old, on
148 * the left ;
149 *
150 * - the tree does not contain the key, and we have
151 * new->key > old->key. We insert new above old, on
152 * the right ;
153 *
154 * - the tree does contain the key, which implies it
155 * is alone. We add the new key next to it as a
156 * first duplicate.
157 *
158 * The last two cases can easily be partially merged.
159 */
160 bit = string_equal_bits(new->key, old->key, bit);
161 diff = cmp_bits(new->key, old->key, bit);
162
163 if (diff < 0) {
164 new->node.leaf_p = new_left;
165 old->node.leaf_p = new_rght;
166 new->node.branches.b[EB_LEFT] = new_leaf;
167 new->node.branches.b[EB_RGHT] = old_leaf;
168 } else {
169 /* we may refuse to duplicate this key if the tree is
170 * tagged as containing only unique keys.
171 */
172 if (diff == 0 && eb_gettag(root_right))
173 return old;
174
175 /* new->key >= old->key, new goes the right */
176 old->node.leaf_p = new_left;
177 new->node.leaf_p = new_rght;
178 new->node.branches.b[EB_LEFT] = old_leaf;
179 new->node.branches.b[EB_RGHT] = new_leaf;
180
181 if (diff == 0) {
182 new->node.bit = -1;
183 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
184 return new;
185 }
186 }
187 break;
188 }
189
190 /* OK we're walking down this link */
191 old = container_of(eb_untag(troot, EB_NODE),
192 struct ebpt_node, node.branches);
193
194 /* Stop going down when we don't have common bits anymore. We
195 * also stop in front of a duplicates tree because it means we
196 * have to insert above. Note: we can compare more bits than
197 * the current node's because as long as they are identical, we
198 * know we descend along the correct side.
199 */
200 if (old->node.bit < 0) {
201 /* we're above a duplicate tree, we must compare till the end */
202 bit = string_equal_bits(new->key, old->key, bit);
203 goto dup_tree;
204 }
205 else if (bit < old->node.bit) {
206 bit = string_equal_bits(new->key, old->key, bit);
207 }
208
209 if (bit < old->node.bit) { /* we don't have all bits in common */
210 /* The tree did not contain the key, so we insert <new> before the node
211 * <old>, and set ->bit to designate the lowest bit position in <new>
212 * which applies to ->branches.b[].
213 */
214 eb_troot_t *new_left, *new_rght;
215 eb_troot_t *new_leaf, *old_node;
216 dup_tree:
217 new_left = eb_dotag(&new->node.branches, EB_LEFT);
218 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
219 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
220 old_node = eb_dotag(&old->node.branches, EB_NODE);
221
222 new->node.node_p = old->node.node_p;
223
224 diff = cmp_bits(new->key, old->key, bit);
225 if (diff < 0) {
226 new->node.leaf_p = new_left;
227 old->node.node_p = new_rght;
228 new->node.branches.b[EB_LEFT] = new_leaf;
229 new->node.branches.b[EB_RGHT] = old_node;
230 }
231 else if (diff > 0) {
232 old->node.node_p = new_left;
233 new->node.leaf_p = new_rght;
234 new->node.branches.b[EB_LEFT] = old_node;
235 new->node.branches.b[EB_RGHT] = new_leaf;
236 }
237 else {
238 struct eb_node *ret;
239 ret = eb_insert_dup(&old->node, &new->node);
240 return container_of(ret, struct ebpt_node, node);
241 }
242 break;
243 }
244
245 /* walk down */
246 root = &old->node.branches;
247 side = (((unsigned char *)new->key)[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1;
248 troot = root->b[side];
249 }
250
251 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
252 * parent is already set to <new>, and the <root>'s branch is still in
253 * <side>. Update the root's leaf till we have it. Note that we can also
254 * find the side by checking the side of new->node.node_p.
255 */
256
257 /* We need the common higher bits between new->key and old->key.
258 * This number of bits is already in <bit>.
259 */
260 new->node.bit = bit;
261 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
262 return new;
263}
264