condition_* => route_*
This commit is contained in:
parent
fcad767085
commit
60991c3d73
4 changed files with 71 additions and 70 deletions
34
include/r3.h
34
include/r3.h
|
@ -22,19 +22,19 @@
|
|||
|
||||
struct _edge;
|
||||
struct _node;
|
||||
struct _condition;
|
||||
struct _route;
|
||||
typedef struct _edge edge;
|
||||
typedef struct _node node;
|
||||
typedef struct _condition condition;
|
||||
typedef struct _route route;
|
||||
|
||||
struct _node {
|
||||
edge ** edges;
|
||||
int edge_len;
|
||||
int edge_cap;
|
||||
|
||||
condition ** conditions;
|
||||
int condition_len;
|
||||
int condition_cap;
|
||||
route ** routes;
|
||||
int route_len;
|
||||
int route_cap;
|
||||
|
||||
|
||||
/** compile-time variables here.... **/
|
||||
|
@ -48,7 +48,7 @@ struct _node {
|
|||
int * ov;
|
||||
|
||||
/**
|
||||
* the pointer of condition data
|
||||
* the pointer of route data
|
||||
*/
|
||||
void * data;
|
||||
|
||||
|
@ -68,7 +68,7 @@ typedef struct {
|
|||
int path_len; // the length of the current path
|
||||
int request_method; // current request method
|
||||
|
||||
void * data; // condition ptr
|
||||
void * data; // route ptr
|
||||
|
||||
char * host; // the request host
|
||||
int host_len;
|
||||
|
@ -77,7 +77,7 @@ typedef struct {
|
|||
int remote_addr_len;
|
||||
} match_entry;
|
||||
|
||||
struct _condition {
|
||||
struct _route {
|
||||
char * path;
|
||||
int path_len;
|
||||
|
||||
|
@ -107,9 +107,9 @@ edge * r3_node_find_edge(node * n, char * pat);
|
|||
|
||||
void r3_node_append_edge(node *n, edge *child);
|
||||
|
||||
node * r3_tree_insert_path(node *tree, char *path, condition * condition, void * data);
|
||||
node * r3_tree_insert_path(node *tree, char *path, route * route, void * data);
|
||||
|
||||
node * r3_tree_insert_pathl(node *tree, char *path, int path_len, condition * condition, void * data);
|
||||
node * r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route, void * data);
|
||||
|
||||
void r3_tree_dump(node * n, int level);
|
||||
|
||||
|
@ -137,7 +137,7 @@ void r3_edge_branch(edge *e, int dl);
|
|||
void r3_edge_free(edge * edge);
|
||||
|
||||
|
||||
node * r3_tree_insert_route(node *tree, condition * condition, void * data);
|
||||
node * r3_tree_insert_route(node *tree, route * route, void * data);
|
||||
|
||||
match_entry * match_entry_createl(char * path, int path_len);
|
||||
|
||||
|
@ -146,17 +146,17 @@ match_entry * match_entry_createl(char * path, int path_len);
|
|||
void match_entry_free(match_entry * entry);
|
||||
|
||||
|
||||
condition * condition_create(char * path);
|
||||
route * route_create(char * path);
|
||||
|
||||
condition * condition_createl(char * path, int path_len);
|
||||
route * route_createl(char * path, int path_len);
|
||||
|
||||
int condition_cmp(condition *r1, match_entry *r2);
|
||||
int route_cmp(route *r1, match_entry *r2);
|
||||
|
||||
void r3_node_append_condition(node * n, condition * condition);
|
||||
void r3_node_append_route(node * n, route * route);
|
||||
|
||||
void condition_free(condition * condition);
|
||||
void route_free(route * route);
|
||||
|
||||
condition * r3_node_match_condition(node *n, match_entry * entry);
|
||||
route * r3_node_match_route(node *n, match_entry * entry);
|
||||
|
||||
#define METHOD_GET 2
|
||||
#define METHOD_POST 2<<1
|
||||
|
|
80
src/node.c
80
src/node.c
|
@ -300,12 +300,12 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
condition * r3_node_match_condition(node *n, match_entry * entry) {
|
||||
if (n->conditions && n->condition_len > 0) {
|
||||
route * r3_node_match_route(node *n, match_entry * entry) {
|
||||
if (n->routes && n->route_len > 0) {
|
||||
int i;
|
||||
for (i = 0; i < n->condition_len ; i++ ) {
|
||||
if ( condition_cmp(n->conditions[i], entry) == 0 ) {
|
||||
return n->conditions[i];
|
||||
for (i = 0; i < n->route_len ; i++ ) {
|
||||
if ( route_cmp(n->routes[i], entry) == 0 ) {
|
||||
return n->routes[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -338,9 +338,9 @@ node * r3_node_create() {
|
|||
n->edge_len = 0;
|
||||
n->edge_cap = 0;
|
||||
|
||||
n->conditions = NULL;
|
||||
n->condition_len = 0;
|
||||
n->condition_cap = 0;
|
||||
n->routes = NULL;
|
||||
n->route_len = 0;
|
||||
n->route_cap = 0;
|
||||
|
||||
n->endpoint = 0;
|
||||
n->combined_pattern = NULL;
|
||||
|
@ -349,16 +349,16 @@ node * r3_node_create() {
|
|||
}
|
||||
|
||||
|
||||
condition * condition_create(char * path) {
|
||||
return condition_createl(path, strlen(path));
|
||||
route * route_create(char * path) {
|
||||
return route_createl(path, strlen(path));
|
||||
}
|
||||
|
||||
void condition_free(condition * condition) {
|
||||
free(condition);
|
||||
void route_free(route * route) {
|
||||
free(route);
|
||||
}
|
||||
|
||||
condition * condition_createl(char * path, int path_len) {
|
||||
condition * info = malloc(sizeof(condition));
|
||||
route * route_createl(char * path, int path_len) {
|
||||
route * info = malloc(sizeof(route));
|
||||
info->path = path;
|
||||
info->path_len = path_len;
|
||||
info->request_method = 0; // can be (GET || POST)
|
||||
|
@ -373,20 +373,20 @@ condition * condition_createl(char * path, int path_len) {
|
|||
return info;
|
||||
}
|
||||
|
||||
node * r3_tree_insert_route(node *tree, condition * condition, void * data) {
|
||||
return r3_tree_insert_pathl(tree, condition->path, condition->path_len, condition, data);
|
||||
node * r3_tree_insert_route(node *tree, route * route, void * data) {
|
||||
return r3_tree_insert_pathl(tree, route->path, route->path_len, route, data);
|
||||
}
|
||||
|
||||
node * r3_tree_insert_path(node *tree, char *path, condition * condition, void * data)
|
||||
node * r3_tree_insert_path(node *tree, char *path, route * route, void * data)
|
||||
{
|
||||
return r3_tree_insert_pathl(tree, path, strlen(path) , condition , data);
|
||||
return r3_tree_insert_pathl(tree, path, strlen(path) , route , data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the last inserted node.
|
||||
*/
|
||||
node * r3_tree_insert_pathl(node *tree, char *path, int path_len, condition * condition, void * data)
|
||||
node * r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route, void * data)
|
||||
{
|
||||
node * n = tree;
|
||||
edge * e = NULL;
|
||||
|
@ -423,9 +423,9 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, condition * co
|
|||
child->data = data;
|
||||
child->endpoint++;
|
||||
|
||||
if (condition) {
|
||||
condition->data = data;
|
||||
r3_node_append_condition(child, condition);
|
||||
if (route) {
|
||||
route->data = data;
|
||||
r3_node_append_route(child, route);
|
||||
}
|
||||
return child;
|
||||
} else if ( offset == e->pattern_len ) { // fully-equal to the pattern of the edge
|
||||
|
@ -435,14 +435,14 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, condition * co
|
|||
|
||||
// there are something more we can insert
|
||||
if ( subpath_len > 0 ) {
|
||||
return r3_tree_insert_pathl(e->child, subpath, subpath_len, condition, data);
|
||||
return r3_tree_insert_pathl(e->child, subpath, subpath_len, route, data);
|
||||
} else {
|
||||
// no more path to insert
|
||||
e->child->endpoint++; // make it as an endpoint
|
||||
e->child->data = data;
|
||||
if (condition) {
|
||||
condition->data = data;
|
||||
r3_node_append_condition(e->child, condition);
|
||||
if (route) {
|
||||
route->data = data;
|
||||
r3_node_append_route(e->child, route);
|
||||
}
|
||||
return e->child;
|
||||
}
|
||||
|
@ -479,13 +479,13 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, condition * co
|
|||
c2->endpoint++;
|
||||
c2->data = data;
|
||||
|
||||
if (condition) {
|
||||
condition->data = data;
|
||||
r3_node_append_condition(c2, condition);
|
||||
if (route) {
|
||||
route->data = data;
|
||||
r3_node_append_route(c2, route);
|
||||
}
|
||||
return c2;
|
||||
} else {
|
||||
printf("unexpected condition.");
|
||||
printf("unexpected route.");
|
||||
return NULL;
|
||||
}
|
||||
return n;
|
||||
|
@ -535,9 +535,9 @@ void r3_tree_dump(node * n, int level) {
|
|||
/**
|
||||
* return 0 == equal
|
||||
*
|
||||
* -1 == different condition
|
||||
* -1 == different route
|
||||
*/
|
||||
int condition_cmp(condition *r1, match_entry *r2) {
|
||||
int route_cmp(route *r1, match_entry *r2) {
|
||||
if (r1->request_method != 0) {
|
||||
if (0 == (r1->request_method & r2->request_method) ) {
|
||||
return -1;
|
||||
|
@ -568,16 +568,16 @@ int condition_cmp(condition *r1, match_entry *r2) {
|
|||
/**
|
||||
* Create a data only node.
|
||||
*/
|
||||
void r3_node_append_condition(node * n, condition * condition) {
|
||||
if (!n->conditions) {
|
||||
n->condition_cap = 3;
|
||||
n->conditions = malloc(sizeof(condition) * n->condition_cap);
|
||||
void r3_node_append_route(node * n, route * route) {
|
||||
if (!n->routes) {
|
||||
n->route_cap = 3;
|
||||
n->routes = malloc(sizeof(route) * n->route_cap);
|
||||
}
|
||||
if (n->condition_len >= n->condition_cap) {
|
||||
n->condition_cap *= 2;
|
||||
n->conditions = realloc(n->conditions, sizeof(condition) * n->condition_cap);
|
||||
if (n->route_len >= n->route_cap) {
|
||||
n->route_cap *= 2;
|
||||
n->routes = realloc(n->routes, sizeof(route) * n->route_cap);
|
||||
}
|
||||
n->conditions[ n->condition_len++ ] = condition;
|
||||
n->routes[ n->route_len++ ] = route;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -155,3 +155,4 @@
|
|||
1400389267,10864289.97
|
||||
1400389287,11080291.51
|
||||
1400389343,11017300.69
|
||||
1400389511,11126324.77
|
||||
|
|
|
|
@ -224,24 +224,24 @@ END_TEST
|
|||
|
||||
START_TEST(test_route_cmp)
|
||||
{
|
||||
condition *r1 = condition_create("/blog/post");
|
||||
route *r1 = route_create("/blog/post");
|
||||
match_entry * m = match_entry_create("/blog/post");
|
||||
|
||||
fail_if( condition_cmp(r1, m) == -1, "should match");
|
||||
fail_if( route_cmp(r1, m) == -1, "should match");
|
||||
|
||||
r1->request_method = METHOD_GET;
|
||||
m->request_method = METHOD_GET;
|
||||
fail_if( condition_cmp(r1, m) == -1, "should match");
|
||||
fail_if( route_cmp(r1, m) == -1, "should match");
|
||||
|
||||
r1->request_method = METHOD_GET;
|
||||
m->request_method = METHOD_POST;
|
||||
fail_if( condition_cmp(r1, m) == 0, "should be different");
|
||||
fail_if( route_cmp(r1, m) == 0, "should be different");
|
||||
|
||||
r1->request_method = METHOD_GET;
|
||||
m->request_method = METHOD_POST | METHOD_GET;
|
||||
fail_if( condition_cmp(r1, m) == -1, "should match");
|
||||
fail_if( route_cmp(r1, m) == -1, "should match");
|
||||
|
||||
condition_free(r1);
|
||||
route_free(r1);
|
||||
match_entry_free(m);
|
||||
}
|
||||
END_TEST
|
||||
|
@ -252,8 +252,8 @@ START_TEST(test_insert_route)
|
|||
{
|
||||
int var1 = 22;
|
||||
int var2 = 33;
|
||||
condition *r1 = condition_create("/blog/post");
|
||||
condition *r2 = condition_create("/blog/post");
|
||||
route *r1 = route_create("/blog/post");
|
||||
route *r2 = route_create("/blog/post");
|
||||
r1->request_method = METHOD_GET;
|
||||
r2->request_method = METHOD_POST;
|
||||
|
||||
|
@ -269,13 +269,13 @@ START_TEST(test_insert_route)
|
|||
|
||||
fail_if(m == NULL);
|
||||
fail_if(m->endpoint == 0);
|
||||
condition *c = r3_node_match_condition(m, entry);
|
||||
route *c = r3_node_match_route(m, entry);
|
||||
fail_if(c == NULL);
|
||||
|
||||
|
||||
match_entry_free(entry);
|
||||
condition_free(r1);
|
||||
condition_free(r2);
|
||||
route_free(r1);
|
||||
route_free(r2);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
@ -287,7 +287,7 @@ START_TEST(benchmark_str)
|
|||
node * n = r3_tree_create(1);
|
||||
|
||||
|
||||
int condition_data = 999;
|
||||
int route_data = 999;
|
||||
|
||||
r3_tree_insert_path(n, "/foo/bar/baz", NULL, NULL);
|
||||
r3_tree_insert_path(n, "/foo/bar/qux", NULL, NULL);
|
||||
|
@ -424,7 +424,7 @@ r3_tree_insert_path(n, "/qux/foo/garply", NULL, NULL);
|
|||
r3_tree_insert_path(n, "/qux/bar/foo", NULL, NULL);
|
||||
r3_tree_insert_path(n, "/qux/bar/baz", NULL, NULL);
|
||||
r3_tree_insert_path(n, "/qux/bar/quux", NULL, NULL);
|
||||
r3_tree_insert_path(n, "/qux/bar/corge", NULL, &condition_data);
|
||||
r3_tree_insert_path(n, "/qux/bar/corge", NULL, &route_data);
|
||||
r3_tree_insert_path(n, "/qux/bar/grault", NULL, NULL);
|
||||
r3_tree_insert_path(n, "/qux/bar/garply", NULL, NULL);
|
||||
r3_tree_insert_path(n, "/qux/baz/foo", NULL, NULL);
|
||||
|
|
Loading…
Reference in a new issue