Skip to content

Commit 8aedf11

Browse files
committed
minor changes on radix tree + undertow
1 parent af07807 commit 8aedf11

4 files changed

Lines changed: 38 additions & 57 deletions

File tree

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

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
17-
import java.util.Objects;
1817
import java.util.regex.Pattern;
1918

2019
class $Chi implements RadixTree {
@@ -23,30 +22,6 @@ class $Chi implements RadixTree {
2322
private static final int ntParam = 2; // /{user}
2423
private static final int ntCatchAll = 3; // /api/v1/*
2524

26-
private static class StaticKey {
27-
private String method;
28-
private String pattern;
29-
30-
public StaticKey(String method, String pattern) {
31-
this.method = method;
32-
this.pattern = pattern;
33-
}
34-
35-
@Override public boolean equals(Object that) {
36-
if (this == that) {
37-
return true;
38-
} else {
39-
StaticKey staticKey = (StaticKey) that;
40-
return method.equals(staticKey.method) &&
41-
pattern.equals(staticKey.pattern);
42-
}
43-
}
44-
45-
@Override public int hashCode() {
46-
return Objects.hash(method, pattern);
47-
}
48-
}
49-
5025
static class Segment {
5126
int nodeType;
5227
String key = "";
@@ -628,7 +603,7 @@ public void destroy() {
628603
private Node root = new Node();
629604

630605
/** Not need to use a concurrent map, due we don't allow to add routes after application started. */
631-
private Map<StaticKey, Route> staticPaths = new HashMap<>();
606+
private Map<Object, Route> staticPaths = new HashMap<>();
632607

633608
public void insert(String method, String pattern, Route route) {
634609
String baseCatchAll = baseCatchAll(pattern);
@@ -642,7 +617,7 @@ public void insert(String method, String pattern, Route route) {
642617
pattern = "/*";
643618
}
644619
if (Router.pathKeys(pattern).isEmpty()) {
645-
staticPaths.put(new StaticKey(method, pattern), route);
620+
staticPaths.put(method + pattern, route);
646621
}
647622
root.insertRoute(method, pattern, route);
648623
}
@@ -663,28 +638,32 @@ public void insert(Route route) {
663638
root.destroy();
664639
}
665640

641+
public RouterMatch find(Context context, MessageEncoder encoder,
642+
List<RadixTree> more) {
643+
return find(context, context.pathString(), encoder, more);
644+
}
645+
666646
public RouterMatch find(Context context, String path, MessageEncoder encoder,
667647
List<RadixTree> more) {
668648
String method = context.getMethod();
669649
RouterMatch result = new RouterMatch();
670-
Route route = staticPaths.get(new StaticKey(method, path));
671-
if (route != null) {
672-
return result.found(route);
673-
}
674-
// use radix tree
675-
route = root.findRoute(result, method, path);
676-
if (route != null) {
677-
return result.found(route);
678-
}
679-
if (more != null) {
680-
// expand search
681-
for (RadixTree tree : more) {
682-
RouterMatch match = tree.find(context, path, encoder, null);
683-
if (match.matches) {
684-
return match;
650+
Route route = staticPaths.get(method + path);
651+
if (route == null) {
652+
// use radix tree
653+
route = root.findRoute(result, method, path);
654+
if (route == null) {
655+
if (more != null) {
656+
// expand search
657+
for (RadixTree tree : more) {
658+
RouterMatch match = tree.find(context, path, encoder, null);
659+
if (match.matches) {
660+
return match;
661+
}
662+
}
685663
}
664+
return result.missing(method, path, encoder);
686665
}
687666
}
688-
return result.missing(method, path, encoder);
667+
return result.found(route);
689668
}
690669
}

‎jooby/src/main/java/io/jooby/internal/RadixTree.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ interface RadixTree {
1818

1919
RouterMatch find(Context context, String path, MessageEncoder encoder, List<RadixTree> more);
2020

21-
default RouterMatch find(Context context, MessageEncoder encoder, List<RadixTree> more) {
22-
return find(context, context.pathString(), encoder, more);
23-
}
24-
2521
default RadixTree with(Predicate<Context> predicate) {
2622
return new RadixTree() {
2723
@Override public void insert(String method, String pattern, Route route) {
@@ -35,7 +31,7 @@ public RouterMatch find(Context context, String path, MessageEncoder encoder,
3531
return new RouterMatch()
3632
.missing(context.getMethod(), context.pathString(), encoder);
3733
}
38-
return RadixTree.this.find(context, encoder, more);
34+
return RadixTree.this.find(context, path, encoder, more);
3935
}
4036

4137
@Override public void destroy() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public void destroy() {
452452
}
453453

454454
@Nonnull @Override public Match match(@Nonnull Context ctx) {
455-
return chi.find(ctx, renderer, trees);
455+
return chi.find(ctx, ctx.pathString(), renderer, trees);
456456
}
457457

458458
@Nonnull @Override public Router errorCode(@Nonnull Class<? extends Throwable> type,

‎modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.concurrent.Executor;
5656

5757
import static io.undertow.server.handlers.form.FormDataParser.FORM_DATA;
58+
import static io.undertow.util.Headers.CONTENT_LENGTH;
5859
import static io.undertow.util.Headers.CONTENT_TYPE;
5960
import static io.undertow.util.Headers.RANGE;
6061
import static io.undertow.util.Headers.SET_COOKIE;
@@ -70,7 +71,7 @@ public class UtowContext implements DefaultContext, IoCallback {
7071
private Multipart multipart;
7172
private Value headers;
7273
private Map<String, String> pathMap = Collections.EMPTY_MAP;
73-
private Map<String, Object> attributes = new HashMap<>();
74+
private Map<String, Object> attributes;
7475
Body body;
7576
private MediaType responseType;
7677
private Map<String, String> cookies;
@@ -106,6 +107,9 @@ public UtowContext(HttpServerExchange exchange, Router router) {
106107
}
107108

108109
@Nonnull @Override public Map<String, Object> getAttributes() {
110+
if (attributes == null) {
111+
attributes = new HashMap<>();
112+
}
109113
return attributes;
110114
}
111115

@@ -259,7 +263,7 @@ public Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset
259263
}
260264

261265
@Nonnull @Override public Context setResponseLength(long length) {
262-
exchange.setResponseContentLength(length);
266+
exchange.getResponseHeaders().put(CONTENT_LENGTH, Long.toString(length));
263267
return this;
264268
}
265269

@@ -313,8 +317,8 @@ public Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset
313317
}
314318

315319
@Nonnull @Override public Context send(@Nonnull ByteBuffer data) {
316-
exchange.setResponseContentLength(data.remaining());
317-
exchange.getResponseSender().send(new ByteBuffer[]{data}, this);
320+
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, Long.toString(data.remaining()));
321+
exchange.getResponseSender().send(data, this);
318322
return this;
319323
}
320324

@@ -377,10 +381,12 @@ public void onException(HttpServerExchange exchange, Sender sender, IOException
377381
}
378382

379383
private void ifSaveSession() {
380-
Session session = (Session) getAttributes().get(Session.NAME);
381-
if (session != null && (session.isNew() || session.isModify())) {
382-
SessionStore store = getRouter().getSessionOptions().getStore();
383-
store.save(session);
384+
if (attributes != null) {
385+
Session session = (Session) attributes.get(Session.NAME);
386+
if (session != null && (session.isNew() || session.isModify())) {
387+
SessionStore store = getRouter().getSessionOptions().getStore();
388+
store.save(session);
389+
}
384390
}
385391
}
386392

0 commit comments

Comments
 (0)