supporting endpoint
This commit is contained in:
parent
2348387f3c
commit
7326933065
3 changed files with 55 additions and 1 deletions
|
@ -37,6 +37,10 @@ void rnode_append_edge(rnode *n, redge *child);
|
||||||
|
|
||||||
rnode * rnode_insert_tokens(rnode * tree, token_array * tokens);
|
rnode * rnode_insert_tokens(rnode * tree, token_array * tokens);
|
||||||
|
|
||||||
|
rnode * rnode_insert_route(rnode *tree, char *route);
|
||||||
|
|
||||||
|
rnode * rnode_insert_routel(rnode *tree, char *route, int route_len);
|
||||||
|
|
||||||
void rnode_dump(rnode * n, int level);
|
void rnode_dump(rnode * n, int level);
|
||||||
|
|
||||||
redge * redge_create(char * pattern, int pattern_len, rnode * child);
|
redge * redge_create(char * pattern, int pattern_len, rnode * child);
|
||||||
|
|
45
src/node.c
45
src/node.c
|
@ -12,6 +12,7 @@
|
||||||
// Judy array
|
// Judy array
|
||||||
#include <Judy.h>
|
#include <Judy.h>
|
||||||
|
|
||||||
|
#include "str.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
|
|
||||||
|
@ -22,6 +23,8 @@ struct _rnode {
|
||||||
|
|
||||||
/* the combined regexp pattern string from pattern_tokens */
|
/* the combined regexp pattern string from pattern_tokens */
|
||||||
char* combined_pattern;
|
char* combined_pattern;
|
||||||
|
|
||||||
|
int endpoint;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _redge {
|
struct _redge {
|
||||||
|
@ -32,12 +35,18 @@ struct _redge {
|
||||||
|
|
||||||
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
|
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a rnode object
|
||||||
|
*/
|
||||||
rnode * rnode_create(int cap) {
|
rnode * rnode_create(int cap) {
|
||||||
rnode * n = (rnode*) calloc( sizeof(rnode) , 1);
|
rnode * n = (rnode*) calloc( sizeof(rnode) , 1);
|
||||||
|
|
||||||
n->children = (redge**) malloc( sizeof(redge*) * 10 );
|
n->children = (redge**) malloc( sizeof(redge*) * 10 );
|
||||||
n->children_len = 0;
|
n->children_len = 0;
|
||||||
n->children_cap = 10;
|
n->children_cap = 10;
|
||||||
|
n->endpoint = 0;
|
||||||
// n->edge_patterns = token_array_create(10);
|
// n->edge_patterns = token_array_create(10);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
@ -94,6 +103,28 @@ redge * rnode_find_edge(rnode * n, char * pat) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
rnode * rnode_lookup(rnode * tree, char * path, int path_len) {
|
||||||
|
token_array * tokens = split_route_pattern(path, path_len);
|
||||||
|
|
||||||
|
rnode * n = tree;
|
||||||
|
redge * e = NULL;
|
||||||
|
for ( int i = 0 ; i < tokens->len ; i++ ) {
|
||||||
|
e = rnode_find_edge(n, token_array_fetch(tokens, i) );
|
||||||
|
if (!e) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
n = e->child;
|
||||||
|
}
|
||||||
|
if (n->endpoint) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
rnode * rnode_insert_tokens(rnode * tree, token_array * tokens) {
|
rnode * rnode_insert_tokens(rnode * tree, token_array * tokens) {
|
||||||
rnode * n = tree;
|
rnode * n = tree;
|
||||||
redge * e = NULL;
|
redge * e = NULL;
|
||||||
|
@ -105,12 +136,24 @@ rnode * rnode_insert_tokens(rnode * tree, token_array * tokens) {
|
||||||
}
|
}
|
||||||
// insert node
|
// insert node
|
||||||
rnode * child = rnode_create(3);
|
rnode * child = rnode_create(3);
|
||||||
rnode_add_child(n, token_array_fetch(tokens,i) , child);
|
rnode_add_child(n, strdup(token_array_fetch(tokens,i)) , child);
|
||||||
n = child;
|
n = child;
|
||||||
}
|
}
|
||||||
|
n->endpoint++;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rnode * rnode_insert_route(rnode *tree, char *route)
|
||||||
|
{
|
||||||
|
return rnode_insert_routel(tree, route, strlen(route) );
|
||||||
|
}
|
||||||
|
|
||||||
|
rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
|
||||||
|
{
|
||||||
|
token_array * t = split_route_pattern(route, strlen(route));
|
||||||
|
return rnode_insert_tokens(tree, t);
|
||||||
|
}
|
||||||
|
|
||||||
void rnode_dump(rnode * n, int level) {
|
void rnode_dump(rnode * n, int level) {
|
||||||
if ( n->children_len ) {
|
if ( n->children_len ) {
|
||||||
print_indent(level);
|
print_indent(level);
|
||||||
|
|
|
@ -66,6 +66,13 @@ START_TEST (test_rnode_insert_tokens)
|
||||||
rnode_dump(n, 0);
|
rnode_dump(n, 0);
|
||||||
|
|
||||||
|
|
||||||
|
fail_if( rnode_lookup(n, "/a/jj/kk", strlen("/a/jj/kk") ) == NULL );
|
||||||
|
fail_if( rnode_lookup(n, "/a/jj", strlen("/a/jj") ) != NULL );
|
||||||
|
fail_if( rnode_lookup(n, "/a/jj/kk/ll", strlen("/a/jj/kk/ll") ) != NULL );
|
||||||
|
|
||||||
|
fail_if( rnode_lookup(n, "/xxxx", strlen("xxxx") ) != NULL );
|
||||||
|
|
||||||
|
|
||||||
// fail_if( rnode_find_edge(n, "/add") == NULL );
|
// fail_if( rnode_find_edge(n, "/add") == NULL );
|
||||||
// fail_if( rnode_find_edge(n, "/bar") != NULL );
|
// fail_if( rnode_find_edge(n, "/bar") != NULL );
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue