Skip to content

Commit

Permalink
Append one more argument to r3_tree_insert_pathn
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed May 18, 2014
1 parent 64acfd8 commit 08a0594
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 368 deletions.
3 changes: 1 addition & 2 deletions gen_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
arr = ["foo", "bar", "baz", "qux", "quux", "corge", "grault", "garply"]
paths = arr.permutation(3).map { |a| "/#{a.join '/'}" }
paths.each do |path|
# puts "r3_tree_insert_path(n, \"#{path}\", NULL);"
puts path
puts "r3_tree_insert_path(n, \"#{path}\", NULL, NULL);"
end
4 changes: 2 additions & 2 deletions include/r3.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ edge * r3_node_find_edge(node * n, char * pat);

void r3_tree_append_edge(node *n, edge *child);

node * r3_tree_insert_path(node *tree, char *route, void * data);
node * r3_tree_insert_path(node *tree, char *path, route * route, void * data);

node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * data);
node * r3_tree_insert_pathn(node *tree, char *path, int path_len, route * route, void * data);

void r3_tree_dump(node * n, int level);

Expand Down
34 changes: 17 additions & 17 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,22 @@ node * r3_node_create() {
}


node * r3_tree_insert_path(node *tree, char *route, void * data)
node * r3_tree_insert_path(node *tree, char *path, route * route, void * data)
{
return r3_tree_insert_pathn(tree, route, strlen(route) , data);
return r3_tree_insert_pathn(tree, path, strlen(path) , route , data);
}

node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * data)
node * r3_tree_insert_pathn(node *tree, char *path, int path_len, route * route, void * data)
{
node * n = tree;
edge * e = NULL;

/* length of common prefix */
int offset = 0;
for( int i = 0 ; i < n->edge_len ; i++ ) {
offset = strndiff(route, n->edges[i]->pattern, n->edges[i]->pattern_len);
offset = strndiff(path, n->edges[i]->pattern, n->edges[i]->pattern_len);

// printf("offset: %d %s vs %s\n", offset, route, n->edges[i]->pattern );
// printf("offset: %d %s vs %s\n", offset, path, n->edges[i]->pattern );

// no common, consider insert a new edge
if ( offset > 0 ) {
Expand All @@ -389,34 +389,34 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * data)
}

// branch the edge at correct position (avoid broken slugs)
char *slug_s = strchr(route, '{');
char *slug_e = strchr(route, '}');
char *slug_s = strchr(path, '{');
char *slug_e = strchr(path, '}');
if ( slug_s && slug_e ) {
if ( offset > (slug_s - route) && offset < (slug_e - route) ) {
if ( offset > (slug_s - path) && offset < (slug_e - path) ) {
// break before '{'
offset = slug_s - route;
offset = slug_s - path;
}
}

if ( offset == 0 ) {
// not found, we should just insert a whole new edge
node * child = r3_tree_create(3);
r3_tree_add_child(n, strndup(route, route_len) , child);
info("edge not found, insert one: %s\n", route);
r3_tree_add_child(n, strndup(path, path_len) , child);
info("edge not found, insert one: %s\n", path);
child->data = data;
child->endpoint++;
return child;
} else if ( offset == e->pattern_len ) { // fully-equal to the pattern of the edge

char * subroute = route + offset;
int subroute_len = route_len - offset;
char * subroute = path + offset;
int subroute_len = path_len - offset;

// there are something more we can insert
if ( subroute_len > 0 ) {
return r3_tree_insert_pathn(e->child, subroute, subroute_len, data);
return r3_tree_insert_pathn(e->child, subroute, subroute_len, route, data);
} else {
// no more,
e->child->endpoint++; // make it as an endpoint, TODO: put the route value
e->child->endpoint++; // make it as an endpoint, TODO: put the path value
e->child->data = data;
return e->child;
}
Expand All @@ -430,14 +430,14 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * data)
*/
node *c2; // child 1, child 2
edge *e2; // edge 1, edge 2
char * s2 = route + offset;
char * s2 = path + offset;
int s2_len = 0;

r3_edge_branch(e, offset);

// here is the new edge from.
c2 = r3_tree_create(3);
s2_len = route_len - offset;
s2_len = path_len - offset;
e2 = r3_edge_create(strndup(s2, s2_len), s2_len, c2);
// printf("edge right: %s\n", e2->pattern);
r3_tree_append_edge(e->child, e2);
Expand Down
3 changes: 3 additions & 0 deletions tests/bench_str.csv
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@
1400382244,12226293.04
1400382299,11775631.24
1400382382,12331702.88
1400382578,13521992.76
1400382591,12607054.51
1400382780,12319337.31
Loading

0 comments on commit 08a0594

Please sign in to comment.