Add route_cmp function to compare route

This commit is contained in:
c9s 2014-05-18 11:59:30 +08:00
parent 4efd1b9702
commit a4481a8ecb
4 changed files with 90 additions and 4 deletions

View file

@ -22,8 +22,10 @@
struct _edge;
struct _node;
struct _route;
typedef struct _edge edge;
typedef struct _node node;
typedef struct _route route;
struct _node {
edge ** edges;
@ -54,6 +56,7 @@ struct _edge {
int pattern_len;
bool has_slug;
node * child;
route * route;
};
typedef struct {
@ -71,7 +74,7 @@ typedef struct {
int remote_addr_len;
} match_entry;
typedef struct {
struct _route {
char * path;
int path_len;
@ -83,7 +86,7 @@ typedef struct {
char * remote_addr_pattern;
int remote_addr_pattern_len;
} route;
};
node * r3_tree_create(int cap);
@ -142,4 +145,9 @@ route * route_create(char * path);
route * route_createl(char * path, int path_len);
int route_cmp(route *r1, route *r2);
#define METHOD_GET 2
#define METHOD_POST 2<<1
#endif /* !NODE_H */

View file

@ -198,6 +198,10 @@ route * route_create(char * path) {
return route_createl(path, strlen(path));
}
void route_free(route * route) {
free(route);
}
route * route_createl(char * path, int path_len) {
route * info = malloc(sizeof(route));
info->path = path;
@ -415,8 +419,8 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route,
if ( subroute_len > 0 ) {
return r3_tree_insert_pathl(e->child, subroute, subroute_len, route, data);
} else {
// no more,
e->child->endpoint++; // make it as an endpoint, TODO: put the path value
// no more path to insert
e->child->endpoint++; // make it as an endpoint
e->child->data = data;
return e->child;
}
@ -500,6 +504,53 @@ void r3_tree_dump(node * n, int level) {
}
}
/**
* return 0 == equal
*
* -1 == different route
*/
int route_cmp(route *r1, route *r2) {
if ( r1 == r2 ) {
return 0;
}
if (r1->request_method != r2->request_method) {
return -1;
}
int ret;
if ( r1->path && r2->path ) {
ret = strcmp(r1->path, r2->path);
if (ret != 0) {
return -1;
}
} else if ((r1->path && !r2->path) || (!r1->path && r2->path )) {
return -1;
}
if ( r1->host && r2->host ) {
ret = strcmp(r1->host, r2->host);
if (ret != 0) {
return -1;
}
} else if ((r1->host && !r2->host) || (!r1->host && r2->host )) {
return -1;
}
if (r1->remote_addr_pattern && r2->remote_addr_pattern ) {
ret = strcmp(r1->remote_addr_pattern, r2->remote_addr_pattern);
if (ret != 0) {
return -1;
}
} else if ((r1->remote_addr_pattern && !r2->remote_addr_pattern) || (!r1->remote_addr_pattern && r2->remote_addr_pattern )) {
return -1;
}
return 0;
}
/*
char * r3_node_trace(node * n) {

View file

@ -93,3 +93,12 @@
1400382591,12607054.51
1400382780,12319337.31
1400382819,12453653.04
1400385205,13620526.43
1400385278,12130720.00
1400385318,13755788.28
1400385481,12663400.57
1400385502,12667686.69
1400385515,13077823.94
1400385528,13333901.32
1400385535,11961732.08
1400385563,12910309.71

1 1400242718 5649455.80
93 1400382591 12607054.51
94 1400382780 12319337.31
95 1400382819 12453653.04
96 1400385205 13620526.43
97 1400385278 12130720.00
98 1400385318 13755788.28
99 1400385481 12663400.57
100 1400385502 12667686.69
101 1400385515 13077823.94
102 1400385528 13333901.32
103 1400385535 11961732.08
104 1400385563 12910309.71

View file

@ -257,6 +257,23 @@ START_TEST (test_str_array)
}
END_TEST
START_TEST(test_route_cmp)
{
route *r1 = route_create("/blog/post");
route *r2 = route_create("/blog/post");
fail_if( route_cmp(r1, r2) == -1, "should be the same");
r1->request_method = METHOD_GET;
r2->request_method = METHOD_GET;
fail_if( route_cmp(r1, r2) == -1, "should be the same");
r1->request_method = METHOD_GET;
r2->request_method = METHOD_POST;
fail_if( route_cmp(r1, r2) == 0, "should be different");
}
END_TEST
START_TEST(test_insert_route)
{
@ -658,6 +675,7 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_r3_tree_insert_pathl);
tcase_add_test(tcase, test_compile_slug);
tcase_add_test(tcase, test_compile);
tcase_add_test(tcase, test_route_cmp);
tcase_add_test(tcase, benchmark_str);