Skip to content

Commit da49ece

Browse files
committed
Routing: remove instance of router matcher for static patterns
1 parent 7980984 commit da49ece

File tree

9 files changed

+80
-33
lines changed

9 files changed

+80
-33
lines changed

jooby/src/main/java/io/jooby/internal/Chi.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class Chi implements RouteTree {
3131
static final char ZERO_CHAR = (char) 0;
3232

3333
static class StaticRoute {
34-
private final Map<String, Route> methods;
34+
private final Map<String, StaticRouterMatch> methods;
3535

36-
public StaticRoute(Map<String, Route> methods) {
36+
public StaticRoute(Map<String, StaticRouterMatch> methods) {
3737
this.methods = methods;
3838
}
3939

@@ -42,7 +42,7 @@ public StaticRoute() {
4242
}
4343

4444
public void put(String method, Route route) {
45-
methods.put(method, route);
45+
methods.put(method, new StaticRouterMatch(route));
4646
}
4747
}
4848

@@ -807,14 +807,14 @@ public boolean find(String method, String path) {
807807
return true;
808808
}
809809

810-
public RouterMatch find(String method, String path, MessageEncoder encoder) {
811-
Route route = staticPaths.getOrDefault(path, NO_MATCH).methods.get(method);
812-
if (route == null) {
810+
public Router.Match find(String method, String path, MessageEncoder encoder) {
811+
StaticRouterMatch match = staticPaths.getOrDefault(path, NO_MATCH).methods.get(method);
812+
if (match == null) {
813813
// use radix tree
814814
RouterMatch result = new RouterMatch();
815-
route = root.findRoute(result, method, new ZeroCopyString(path));
815+
Route route = root.findRoute(result, method, new ZeroCopyString(path));
816816
return route == null ? result.missing(method, path, encoder) : result.found(route);
817817
}
818-
return new RouterMatch(route);
818+
return match;
819819
}
820820
}

jooby/src/main/java/io/jooby/internal/RouteTree.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
import io.jooby.MessageEncoder;
99
import io.jooby.Route;
10+
import io.jooby.Router;
1011

1112
interface RouteTree {
1213
void insert(String method, String pattern, Route route);
1314

1415
boolean find(String method, String path);
1516

16-
RouterMatch find(String method, String path, MessageEncoder encoder);
17+
Router.Match find(String method, String path, MessageEncoder encoder);
1718

1819
void destroy();
1920
}

jooby/src/main/java/io/jooby/internal/RouteTreeForwarding.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import io.jooby.MessageEncoder;
99
import io.jooby.Route;
10+
import io.jooby.Router;
1011

1112
public class RouteTreeForwarding implements RouteTree {
1213
private RouteTree tree;
@@ -23,7 +24,7 @@ public RouteTreeForwarding(RouteTree tree) {
2324
return tree.find(method, path);
2425
}
2526

26-
@Override public RouterMatch find(String method, String path, MessageEncoder encoder) {
27+
@Override public Router.Match find(String method, String path, MessageEncoder encoder) {
2728
return tree.find(method, path, encoder);
2829
}
2930

jooby/src/main/java/io/jooby/internal/RouteTreeIgnoreTrailingSlash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public RouteTreeIgnoreTrailingSlash(RouteTree tree) {
1717
return super.find(method, Router.noTrailingSlash(path));
1818
}
1919

20-
@Override public RouterMatch find(String method, String path, MessageEncoder encoder) {
20+
@Override public Router.Match find(String method, String path, MessageEncoder encoder) {
2121
return super.find(method, Router.noTrailingSlash(path), encoder);
2222
}
2323
}

jooby/src/main/java/io/jooby/internal/RouteTreeLowerCasePath.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.jooby.internal;
77

88
import io.jooby.MessageEncoder;
9+
import io.jooby.Router;
910

1011
public class RouteTreeLowerCasePath extends RouteTreeForwarding {
1112
public RouteTreeLowerCasePath(RouteTree tree) {
@@ -16,7 +17,7 @@ public RouteTreeLowerCasePath(RouteTree tree) {
1617
return super.find(method, path.toLowerCase());
1718
}
1819

19-
@Override public RouterMatch find(String method, String path, MessageEncoder encoder) {
20+
@Override public Router.Match find(String method, String path, MessageEncoder encoder) {
2021
return super.find(method, path.toLowerCase(), encoder);
2122
}
2223
}

jooby/src/main/java/io/jooby/internal/RouteTreeNormPath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public RouteTreeNormPath(RouteTree tree) {
1717
return super.find(method, Router.normalizePath(path));
1818
}
1919

20-
@Override public RouterMatch find(String method, String path, MessageEncoder encoder) {
20+
@Override public Router.Match find(String method, String path, MessageEncoder encoder) {
2121
return super.find(method, Router.normalizePath(path), encoder);
2222
}
2323
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ public void destroy() {
582582
if (predicateMap != null) {
583583
for (Map.Entry<Predicate<Context>, RouteTree> e : predicateMap.entrySet()) {
584584
if (e.getKey().test(ctx)) {
585-
RouterMatch match = e.getValue().find(ctx.getMethod(), ctx.getRequestPath(), encoder);
586-
if (match.matches) {
585+
Router.Match match = e.getValue().find(ctx.getMethod(), ctx.getRequestPath(), encoder);
586+
if (match.matches()) {
587587
return match;
588588
}
589589
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.internal;
7+
8+
import io.jooby.Context;
9+
import io.jooby.Route;
10+
import io.jooby.Router;
11+
12+
import javax.annotation.Nonnull;
13+
import java.util.Collections;
14+
import java.util.Map;
15+
16+
public class StaticRouterMatch implements Router.Match {
17+
private final Route route;
18+
19+
public StaticRouterMatch(Route route) {
20+
this.route = route;
21+
}
22+
23+
@Override public boolean matches() {
24+
return true;
25+
}
26+
27+
@Nonnull @Override public Route route() {
28+
return route;
29+
}
30+
31+
@Override public void execute(@Nonnull Context context) {
32+
context.setRoute(route);
33+
try {
34+
route.getPipeline().apply(context);
35+
} catch (Throwable x) {
36+
context.sendError(x);
37+
}
38+
}
39+
40+
@Nonnull @Override public Map<String, String> pathMap() {
41+
return Collections.emptyMap();
42+
}
43+
}

jooby/src/test/java/io/jooby/internal/ChiTest.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.jooby.Context;
44
import io.jooby.MessageEncoder;
55
import io.jooby.Route;
6+
import io.jooby.Router;
67
import io.jooby.SneakyThrows;
78
import org.junit.jupiter.api.Test;
89

@@ -21,9 +22,9 @@ public void routeOverride() {
2122
router.insert(foo);
2223
router.insert(bar);
2324

24-
RouterMatch result = router
25+
Router.Match result = router
2526
.find("GET", "/abcd", MessageEncoder.TO_STRING);
26-
assertTrue(result.matches);
27+
assertTrue(result.matches());
2728
assertEquals(bar, result.route());
2829
}
2930

@@ -35,9 +36,9 @@ public void routeCase() {
3536
router.insert(foo);
3637
router.insert(foos);
3738

38-
RouterMatch result = router
39+
Router.Match result = router
3940
.find("GET", "/abcd/", MessageEncoder.TO_STRING);
40-
assertTrue(result.matches);
41+
assertTrue(result.matches());
4142
assertEquals(foos, result.route());
4243
}
4344

@@ -50,37 +51,37 @@ public void wildOnRoot() throws Exception {
5051
router.insert(route("GET", "/*", stringHandler("root")));
5152

5253
find(router, "/", (ctx, result) -> {
53-
assertTrue(result.matches);
54+
assertTrue(result.matches());
5455
assertEquals("root", result.route().getPipeline().apply(ctx));
5556
});
5657

5758
find(router, "/foo", (ctx, result) -> {
58-
assertTrue(result.matches);
59+
assertTrue(result.matches());
5960
assertEquals("foo", result.route().getPipeline().apply(ctx));
6061
});
6162

6263
find(router, "/bar", (ctx, result) -> {
63-
assertTrue(result.matches);
64+
assertTrue(result.matches());
6465
assertEquals("root", result.route().getPipeline().apply(ctx));
6566
});
6667

6768
find(router, "/foox", (ctx, result) -> {
68-
assertTrue(result.matches);
69+
assertTrue(result.matches());
6970
assertEquals("root", result.route().getPipeline().apply(ctx));
7071
});
7172

7273
find(router, "/foo/", (ctx, result) -> {
73-
assertTrue(result.matches);
74+
assertTrue(result.matches());
7475
assertEquals("foo", result.route().getPipeline().apply(ctx));
7576
});
7677

7778
find(router, "/foo/x", (ctx, result) -> {
78-
assertTrue(result.matches);
79+
assertTrue(result.matches());
7980
assertEquals("foo", result.route().getPipeline().apply(ctx));
8081
});
8182

8283
find(router, "/bar/x", (ctx, result) -> {
83-
assertTrue(result.matches);
84+
assertTrue(result.matches());
8485
assertEquals("bar", result.route().getPipeline().apply(ctx));
8586
});
8687
}
@@ -97,17 +98,17 @@ public void searchString() throws Exception {
9798
router.insert(route("GET", "/articles/*", stringHandler("*")));
9899

99100
find(router, "/regex/678/edit", (ctx, result) -> {
100-
assertTrue(result.matches);
101+
assertTrue(result.matches());
101102
assertEquals("zid", result.route().getPipeline().apply(ctx));
102103
});
103104

104105
find(router, "/articles/tail/match", (ctx, result) -> {
105-
assertTrue(result.matches);
106+
assertTrue(result.matches());
106107
assertEquals("*", result.route().getPipeline().apply(ctx));
107108
});
108109

109110
find(router, "/articles/123", (ctx, result) -> {
110-
assertTrue(result.matches);
111+
assertTrue(result.matches());
111112
assertEquals("id", result.route().getPipeline().apply(ctx));
112113
});
113114
}
@@ -120,19 +121,19 @@ public void searchParam() throws Exception {
120121
router.insert(route("GET", "/articles/*", stringHandler("catchall")));
121122

122123
find(router, "/articles/123", (ctx, result) -> {
123-
assertTrue(result.matches);
124+
assertTrue(result.matches());
124125
assertEquals("id", result.route().getPipeline().apply(ctx));
125126
});
126127

127128
find(router, "/articles/a/b", (ctx, result) -> {
128-
assertTrue(result.matches);
129+
assertTrue(result.matches());
129130
assertEquals("catchall", result.route().getPipeline().apply(ctx));
130131
});
131132
}
132133

133134
private void find(Chi router, String pattern,
134-
SneakyThrows.Consumer2<Context, RouterMatch> consumer) {
135-
RouterMatch result = router
135+
SneakyThrows.Consumer2<Context, Router.Match> consumer) {
136+
Router.Match result = router
136137
.find("GET", pattern, MessageEncoder.TO_STRING);
137138
consumer.accept(ctx(pattern), result);
138139
}

0 commit comments

Comments
 (0)