rax-radix-tree 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +574 -0
- package/benchmark/01.match-equals.js +21 -0
- package/benchmark/02.match-suffix.js +20 -0
- package/benchmark/03.match-prefix.js +28 -0
- package/benchmark/04.match-equals-prefix-suffix.js +36 -0
- package/benchmark/05.match-hosts.js +25 -0
- package/benchmark/06.match-wildcard-hosts.js +25 -0
- package/benchmark/07.match-remote-address.js +40 -0
- package/benchmark/08.match-exprs-headers.js +50 -0
- package/benchmark/09.match-exprs-args.js +50 -0
- package/benchmark/run.sh +3 -0
- package/benchmark/utility.js +56 -0
- package/binding.gyp +14 -0
- package/http-radix-tree.js +150 -0
- package/index.js +5 -0
- package/package.json +38 -0
- package/radix-tree.js +284 -0
- package/src/radix_tree.c +169 -0
- package/src/radix_tree.h +39 -0
- package/src/rax/COPYING +9 -0
- package/src/rax/Makefile +26 -0
- package/src/rax/README.md +488 -0
- package/src/rax/crc16.c +88 -0
- package/src/rax/rax-oom-test.c +175 -0
- package/src/rax/rax-test.c +1065 -0
- package/src/rax/rax.c +1996 -0
- package/src/rax/rax.h +217 -0
- package/src/rax/rax_malloc.h +43 -0
- package/src/rax/rax_oom_malloc.h +63 -0
- package/src/rax/rc4rand.c +77 -0
- package/src/rax/rc4rand.h +9 -0
- package/src/rax_wrapper.cpp +238 -0
- package/test/00.path.test.js +84 -0
- package/test/10.tree.test.js +217 -0
- package/test/20.http-tree.test.js +316 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/* Rax -- A radix tree implementation.
|
|
2
|
+
*
|
|
3
|
+
* Copyright (c) 2017-2018, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
4
|
+
* All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
|
7
|
+
* modification, are permitted provided that the following conditions are met:
|
|
8
|
+
*
|
|
9
|
+
* * Redistributions of source code must retain the above copyright notice,
|
|
10
|
+
* this list of conditions and the following disclaimer.
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
|
14
|
+
* * Neither the name of Redis nor the names of its contributors may be used
|
|
15
|
+
* to endorse or promote products derived from this software without
|
|
16
|
+
* specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
22
|
+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
23
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
24
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
25
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
26
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
27
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
#include <string.h>
|
|
32
|
+
#include <stdio.h>
|
|
33
|
+
#include <stdlib.h>
|
|
34
|
+
#include <errno.h>
|
|
35
|
+
|
|
36
|
+
#include "rax.h"
|
|
37
|
+
|
|
38
|
+
int oomtest(int cycle) {
|
|
39
|
+
printf("\n=========================\n");
|
|
40
|
+
printf("Cycle %d\n", cycle);
|
|
41
|
+
printf("=========================\n");
|
|
42
|
+
|
|
43
|
+
char *toadd[] = {"alligator","alien","baloon","chromodynamic","romane","romanus","romulus","rubens","ruber","rubicon","rubicundus","all","rub","ba",NULL};
|
|
44
|
+
|
|
45
|
+
rax *t = raxNew();
|
|
46
|
+
if (t == NULL) return 0; /* Ok... */
|
|
47
|
+
unsigned long items = 0;
|
|
48
|
+
while(toadd[items] != NULL) items++;
|
|
49
|
+
printf("%lu total items\n", items);
|
|
50
|
+
|
|
51
|
+
unsigned long failed_insertions = 0;
|
|
52
|
+
for (unsigned long i = 0; i < items; i++) {
|
|
53
|
+
int retval = raxInsert(t,(unsigned char*)toadd[i],strlen(toadd[i]),(void*)i,NULL);
|
|
54
|
+
if (retval == 0) {
|
|
55
|
+
if (errno != ENOMEM) {
|
|
56
|
+
printf("Insertion failed but errno != ENOMEM!\n");
|
|
57
|
+
return 1;
|
|
58
|
+
} else {
|
|
59
|
+
printf("Failed to insert %s for OOM\n", toadd[i]);
|
|
60
|
+
failed_insertions++;
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
printf("Added %s = %p\n", toadd[i], (void*)(long)i);
|
|
64
|
+
}
|
|
65
|
+
if (cycle == 307) raxShow(t);
|
|
66
|
+
|
|
67
|
+
if (t->numele != (i+1)-failed_insertions) {
|
|
68
|
+
printf("After insert: %d elements expected, got %d\n",
|
|
69
|
+
(int)((i+1)-failed_insertions), (int)t->numele);
|
|
70
|
+
printf("Offending layout:\n");
|
|
71
|
+
printf("---\n");
|
|
72
|
+
raxShow(t);
|
|
73
|
+
printf("---\n");
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/* Test that the number of items is ok. */
|
|
79
|
+
if (t->numele != items-failed_insertions) {
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
raxShow(t);
|
|
83
|
+
|
|
84
|
+
raxIterator iter;
|
|
85
|
+
raxStart(&iter,t);
|
|
86
|
+
|
|
87
|
+
struct {
|
|
88
|
+
char *str;
|
|
89
|
+
size_t len;
|
|
90
|
+
char *op;
|
|
91
|
+
} seek_places[] = {
|
|
92
|
+
{"rpxxx",5,"<="},
|
|
93
|
+
{"rom",3,">="},
|
|
94
|
+
{"rub",3,">="},
|
|
95
|
+
{"rub",3,">"},
|
|
96
|
+
{"rub",3,"<"},
|
|
97
|
+
{"rom",3,">"},
|
|
98
|
+
{"chro",4,">"},
|
|
99
|
+
{"chro",4,"<"},
|
|
100
|
+
{"chromz",6,"<"},
|
|
101
|
+
{NULL,0,"^"},
|
|
102
|
+
{"zorro",5,"<="},
|
|
103
|
+
{"zorro",5,"<"},
|
|
104
|
+
{NULL,0,"$"},
|
|
105
|
+
{"ro",2,">="}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
int r = rand() % (sizeof(seek_places) / sizeof(seek_places[0]));
|
|
109
|
+
|
|
110
|
+
printf("Seeking %s %s\n", seek_places[r].op, seek_places[r].str);
|
|
111
|
+
if (raxSeek(&iter,seek_places[r].op,(unsigned char*)seek_places[r].str,
|
|
112
|
+
seek_places[r].len) == 0)
|
|
113
|
+
{
|
|
114
|
+
goto cleanup;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
int check_iterator_count = 1;
|
|
118
|
+
unsigned long steps = 0;
|
|
119
|
+
|
|
120
|
+
while(raxNext(&iter)) {
|
|
121
|
+
printf("NEXT: %.*s, val %p\n", (int)iter.key_len,
|
|
122
|
+
(char*)iter.key,
|
|
123
|
+
iter.data);
|
|
124
|
+
steps++;
|
|
125
|
+
}
|
|
126
|
+
if (errno == ENOMEM) {
|
|
127
|
+
printf("raxNext() aborted on OOM\n");
|
|
128
|
+
check_iterator_count = 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
printf("Seeking %s %s\n", seek_places[r].op, seek_places[r].str);
|
|
132
|
+
if (raxSeek(&iter,seek_places[r].op,(unsigned char*)seek_places[r].str,
|
|
133
|
+
seek_places[r].len) == 0)
|
|
134
|
+
{
|
|
135
|
+
goto cleanup;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
while(raxPrev(&iter)) {
|
|
139
|
+
printf("PREV: %.*s, val %p\n", (int)iter.key_len,
|
|
140
|
+
(char*)iter.key,
|
|
141
|
+
iter.data);
|
|
142
|
+
steps++;
|
|
143
|
+
}
|
|
144
|
+
if (errno == ENOMEM) {
|
|
145
|
+
printf("raxNext() aborted on OOM\n");
|
|
146
|
+
check_iterator_count = 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* Check that we walked all the expected elements. */
|
|
150
|
+
if (check_iterator_count) {
|
|
151
|
+
if (steps != t->numele +1) {
|
|
152
|
+
printf("Expected %d total iteration steps, did %d instead\n",
|
|
153
|
+
(int)t->numele + 1, (int)steps);
|
|
154
|
+
return 1;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
cleanup:
|
|
159
|
+
raxStop(&iter);
|
|
160
|
+
raxFree(t);
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
int main(void) {
|
|
165
|
+
srand(1234); /* Make the test reproducible. */
|
|
166
|
+
for (int i = 0; i < 100000; i++) {
|
|
167
|
+
if (oomtest(i)) {
|
|
168
|
+
printf("Test failed\n");
|
|
169
|
+
exit(1);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
printf("Test OK!\n");
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
|