Skip to content

Commit 1af5566

Browse files
authored
bugfix: reused the iterator object. (#13)
* bugfix: reused the iterator object.
1 parent 33497e3 commit 1af5566

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

lib/resty/radixtree.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ ffi_cdef[[
7474
int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
7575
void *data);
7676
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
77-
void *radix_tree_search(void *t, const unsigned char *buf, size_t len);
77+
void *radix_tree_search(void *t, void *it, const unsigned char *buf,
78+
size_t len);
7879
void *radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
7980
void *radix_tree_next(void *it, const unsigned char *buf, size_t len);
8081
int radix_tree_stop(void *it);
82+
83+
void *radix_tree_new_it();
8184
]]
8285

8386

@@ -429,6 +432,13 @@ end
429432

430433

431434
local matched_routes = {}
435+
local radix_it = radix.radix_tree_new_it()
436+
if radix_it == nil then
437+
error("failed to new radixtree it")
438+
end
439+
-- use gc to free
440+
ffi.gc(radix_it, ffi.C.free)
441+
432442
local function match_route(self, path, opts)
433443
clear_tab(matched_routes)
434444
local routes = self.hash_path[path]
@@ -448,7 +458,7 @@ local function match_route(self, path, opts)
448458
clear_tab(matched_routes)
449459
end
450460

451-
local it = radix.radix_tree_search(self.tree, path, #path)
461+
local it = radix.radix_tree_search(self.tree, radix_it, path, #path)
452462
if not it then
453463
return nil, "failed to match"
454464
end

src/easy_rax.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,31 @@ radix_tree_find(void *t, const unsigned char *buf, size_t len)
5555

5656

5757
void *
58-
radix_tree_search(void *t, const unsigned char *buf, size_t len)
58+
radix_tree_new_it()
5959
{
6060
raxIterator *it = malloc(sizeof(raxIterator));
6161
if (it == NULL) {
6262
return NULL;
6363
}
6464

65-
raxStart(it, t);
66-
raxSeek(it, "<=", (unsigned char *)buf, len);
6765
return (void *)it;
6866
}
6967

7068

69+
void *
70+
radix_tree_search(void *t, void *it, const unsigned char *buf, size_t len)
71+
{
72+
raxIterator *iter = (raxIterator *)it;
73+
if (it == NULL) {
74+
return NULL;
75+
}
76+
77+
raxStart(iter, t);
78+
raxSeek(iter, "<=", (unsigned char *)buf, len);
79+
return (void *)iter;
80+
}
81+
82+
7183
void *
7284
radix_tree_next(void *it, const unsigned char *buf, size_t len)
7385
{
@@ -117,6 +129,10 @@ radix_tree_pcre(void *it, const unsigned char *buf, size_t len)
117129
int
118130
radix_tree_stop(void *it)
119131
{
132+
if (!it) {
133+
return 0;
134+
}
135+
120136
raxStop(it);
121137
return 0;
122138
}

src/easy_rax.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ int radix_tree_destroy(void *t);
3434
int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
3535
void *data);
3636
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
37-
void *radix_tree_search(void *t, const unsigned char *buf, size_t len);
37+
void *radix_tree_search(void *t, void *it, const unsigned char *buf, size_t len);
3838
void *radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
3939
void *radix_tree_next(void *it, const unsigned char *buf, size_t len);
4040
int radix_tree_stop(void *it);
4141

42+
void *radix_tree_new_it();
43+
4244
#ifdef __cplusplus
4345
}
4446
#endif

0 commit comments

Comments
 (0)