From a4481a8ecb0664b32008a368e0ac208e96e63f15 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 18 May 2014 11:59:30 +0800 Subject: [PATCH] Add route_cmp function to compare route --- include/r3.h | 12 ++++++++-- src/node.c | 55 +++++++++++++++++++++++++++++++++++++++++++-- tests/bench_str.csv | 9 ++++++++ tests/check_tree.c | 18 +++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/include/r3.h b/include/r3.h index 0c22293..ae83acc 100644 --- a/include/r3.h +++ b/include/r3.h @@ -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 */ diff --git a/src/node.c b/src/node.c index 0d31957..a13a821 100644 --- a/src/node.c +++ b/src/node.c @@ -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) { diff --git a/tests/bench_str.csv b/tests/bench_str.csv index f8b33de..0d80dc7 100644 --- a/tests/bench_str.csv +++ b/tests/bench_str.csv @@ -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 diff --git a/tests/check_tree.c b/tests/check_tree.c index 3f29a21..f8b1b10 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -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);