-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathChi.java
More file actions
1248 lines (1081 loc) · 35.7 KB
/
Copy pathChi.java
File metadata and controls
1248 lines (1081 loc) · 35.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import io.jooby.MessageEncoder;
import io.jooby.Route;
import io.jooby.Router;
/** Sync: May 22, 2020 Commit: 5704d7ee98edd3fe55169b506531bdd061667c70 */
class Chi implements RouteTree {
private static final String EMPTY_STRING = "";
private static final Slice EMPTY_SLICE = new Slice(EMPTY_STRING);
private static final byte ntStatic = 0; // /home
private static final byte ntRegexp = 1; // /{id:[0-9]+}
private static final byte ntParam = 2; // /{user}
private static final byte ntCatchAll = 3; // /api/v1/*
private static final int NODE_SIZE = ntCatchAll + 1;
static final char ZERO_CHAR = Character.MIN_VALUE;
private MessageEncoder encoder;
/** Avoid string allocation */
private static class Slice implements CharSequence {
private final String base;
private final int startIndex;
private final int endIndex;
public Slice(String base, int startIndex, int endIndex) {
this.base = base;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
public Slice(String base, int startIndex) {
this(base, startIndex, base.length());
}
public Slice(String base) {
this(base, 0);
}
@Override
public int length() {
return endIndex - startIndex;
}
@Override
public char charAt(int index) {
return base.charAt(startIndex + index);
}
@Override
public Slice subSequence(int start, int end) {
return new Slice(base, startIndex + start, startIndex + end);
}
@Override
public String toString() {
return base.substring(startIndex, endIndex);
}
public Slice substring(int start) {
return substring(start, length());
}
public Slice substring(int start, int end) {
return subSequence(start, end);
}
public int indexOf(int ch) {
for (int i = startIndex; i < endIndex; i++) {
if (base.charAt(i) == ch) {
return i - startIndex;
}
}
return -1;
}
public boolean startsWith(String prefix) {
return base.regionMatches(false, this.startIndex, prefix, 0, prefix.length());
}
}
private interface StaticMap {
StaticMap INIT =
new StaticMap() {
@Override
public StaticRoute get(String path) {
return null;
}
@Override
public StaticMap put(String path, StaticRoute staticRoute) {
return new StaticMap1(path, staticRoute);
}
};
StaticRoute get(String path);
StaticMap put(String path, StaticRoute staticRoute);
}
private static class StaticMap1 implements StaticMap {
private String pattern;
private StaticRoute route;
public StaticMap1(String pattern, StaticRoute route) {
this.pattern = pattern;
this.route = route;
}
@Override
public StaticMap put(String path, StaticRoute route) {
if (pattern.equals(path)) {
// Override
this.route = route;
return this;
}
return new StaticMap2(this, path, route);
}
public void release() {
this.pattern = null;
this.route = null;
}
public StaticRoute get(String path) {
if (pattern.equals(path)) {
return route;
} else {
return null;
}
}
}
private static class StaticMap2 implements StaticMap {
private String pattern1;
private StaticRoute route1;
private String pattern2;
private StaticRoute route2;
public StaticMap2(StaticMap1 staticMap, String path, StaticRoute route) {
this.pattern1 = staticMap.pattern;
this.route1 = staticMap.route;
this.pattern2 = path;
this.route2 = route;
staticMap.release();
}
public void release() {
this.pattern1 = null;
this.route1 = null;
this.pattern2 = null;
this.route2 = null;
}
@Override
public StaticMap put(String path, StaticRoute route) {
if (pattern1.equals(path)) {
route1 = route;
return this;
} else if (pattern2.equals(path)) {
route2 = route;
return this;
}
return new StaticMap3(this, path, route);
}
public StaticRoute get(String path) {
if (pattern1.equals(path)) {
return route1;
} else if (pattern2.equals(path)) {
return route2;
} else {
return null;
}
}
}
private static class StaticMap3 implements StaticMap {
private String pattern1;
private StaticRoute route1;
private String pattern2;
private StaticRoute route2;
private String pattern3;
private StaticRoute route3;
public StaticMap3(StaticMap2 staticMap, String path, StaticRoute route) {
this.pattern1 = staticMap.pattern1;
this.route1 = staticMap.route1;
this.pattern2 = staticMap.pattern2;
this.route2 = staticMap.route2;
this.pattern3 = path;
this.route3 = route;
staticMap.release();
}
public void release() {
this.pattern1 = null;
this.route1 = null;
this.pattern2 = null;
this.route2 = null;
this.pattern3 = null;
this.route3 = null;
}
@Override
public StaticMap put(String path, StaticRoute route) {
if (pattern1.equals(path)) {
route1 = route;
return this;
} else if (pattern2.equals(path)) {
route2 = route;
return this;
} else if (pattern3.equals(path)) {
route3 = route;
return this;
}
return new StaticMap4(this, path, route);
}
public StaticRoute get(String path) {
if (pattern1.equals(path)) {
return route1;
} else if (pattern2.equals(path)) {
return route2;
} else if (pattern3.equals(path)) {
return route3;
} else {
return null;
}
}
}
private static class StaticMap4 implements StaticMap {
private String pattern1;
private StaticRoute route1;
private String pattern2;
private StaticRoute route2;
private String pattern3;
private StaticRoute route3;
private String pattern4;
private StaticRoute route4;
public StaticMap4(StaticMap3 staticMap, String path, StaticRoute route) {
this.pattern1 = staticMap.pattern1;
this.route1 = staticMap.route1;
this.pattern2 = staticMap.pattern2;
this.route2 = staticMap.route2;
this.pattern3 = staticMap.pattern3;
this.route3 = staticMap.route3;
this.pattern4 = path;
this.route4 = route;
staticMap.release();
}
public void release() {
this.pattern1 = null;
this.route1 = null;
this.pattern2 = null;
this.route2 = null;
this.pattern3 = null;
this.route3 = null;
this.pattern4 = null;
this.route4 = null;
}
@Override
public StaticMap put(String path, StaticRoute route) {
if (pattern1.equals(path)) {
route1 = route;
return this;
} else if (pattern2.equals(path)) {
route2 = route;
return this;
} else if (pattern3.equals(path)) {
route3 = route;
return this;
} else if (pattern4.equals(path)) {
route4 = route;
return this;
}
return new StaticMap5(this, path, route);
}
public StaticRoute get(String path) {
if (pattern1.equals(path)) {
return route1;
} else if (pattern2.equals(path)) {
return route2;
} else if (pattern3.equals(path)) {
return route3;
} else if (pattern4.equals(path)) {
return route4;
} else {
return null;
}
}
}
private static class StaticMap5 implements StaticMap {
private String pattern1;
private StaticRoute route1;
private String pattern2;
private StaticRoute route2;
private String pattern3;
private StaticRoute route3;
private String pattern4;
private StaticRoute route4;
private String pattern5;
private StaticRoute route5;
public StaticMap5(StaticMap4 staticMap, String path, StaticRoute route) {
this.pattern1 = staticMap.pattern1;
this.route1 = staticMap.route1;
this.pattern2 = staticMap.pattern2;
this.route2 = staticMap.route2;
this.pattern3 = staticMap.pattern3;
this.route3 = staticMap.route3;
this.pattern4 = staticMap.pattern4;
this.route4 = staticMap.route4;
this.pattern5 = path;
this.route5 = route;
staticMap.release();
}
public void release() {
this.pattern1 = null;
this.route1 = null;
this.pattern2 = null;
this.route2 = null;
this.pattern3 = null;
this.route3 = null;
this.pattern4 = null;
this.route4 = null;
this.pattern5 = null;
this.route5 = null;
}
@Override
public StaticMap put(String path, StaticRoute route) {
if (pattern1.equals(path)) {
route1 = route;
return this;
} else if (pattern2.equals(path)) {
route2 = route;
return this;
} else if (pattern3.equals(path)) {
route3 = route;
return this;
} else if (pattern4.equals(path)) {
route4 = route;
return this;
} else if (pattern5.equals(path)) {
route5 = route;
return this;
}
return new StaticMap6(this, path, route);
}
public StaticRoute get(String path) {
if (pattern1.equals(path)) {
return route1;
} else if (pattern2.equals(path)) {
return route2;
} else if (pattern3.equals(path)) {
return route3;
} else if (pattern4.equals(path)) {
return route4;
} else if (pattern5.equals(path)) {
return route5;
} else {
return null;
}
}
}
private static class StaticMap6 implements StaticMap {
private String pattern1;
private StaticRoute route1;
private String pattern2;
private StaticRoute route2;
private String pattern3;
private StaticRoute route3;
private String pattern4;
private StaticRoute route4;
private String pattern5;
private StaticRoute route5;
private String pattern6;
private StaticRoute route6;
public StaticMap6(StaticMap5 staticMap, String path, StaticRoute route) {
this.pattern1 = staticMap.pattern1;
this.route1 = staticMap.route1;
this.pattern2 = staticMap.pattern2;
this.route2 = staticMap.route2;
this.pattern3 = staticMap.pattern3;
this.route3 = staticMap.route3;
this.pattern4 = staticMap.pattern4;
this.route4 = staticMap.route4;
this.pattern5 = staticMap.pattern5;
this.route5 = staticMap.route5;
this.pattern6 = path;
this.route6 = route;
staticMap.release();
}
public void release() {
this.pattern1 = null;
this.route1 = null;
this.pattern2 = null;
this.route2 = null;
this.pattern3 = null;
this.route3 = null;
this.pattern4 = null;
this.route4 = null;
this.pattern5 = null;
this.route5 = null;
this.pattern6 = null;
this.route6 = null;
}
@Override
public StaticMap put(String path, StaticRoute route) {
if (pattern1.equals(path)) {
route1 = route;
return this;
} else if (pattern2.equals(path)) {
route2 = route;
return this;
} else if (pattern3.equals(path)) {
route3 = route;
return this;
} else if (pattern4.equals(path)) {
route4 = route;
return this;
} else if (pattern5.equals(path)) {
route5 = route;
return this;
} else if (pattern6.equals(path)) {
route6 = route;
}
return new StaticMapN(this, path, route);
}
public StaticRoute get(String path) {
if (pattern1.equals(path)) {
return route1;
} else if (pattern2.equals(path)) {
return route2;
} else if (pattern3.equals(path)) {
return route3;
} else if (pattern4.equals(path)) {
return route4;
} else if (pattern5.equals(path)) {
return route5;
} else if (pattern6.equals(path)) {
return route6;
} else {
return null;
}
}
}
private static class StaticMapN implements StaticMap {
private final Map<String, StaticRoute> paths = new HashMap<>(10);
public StaticMapN(StaticMap6 staticMap, String path, StaticRoute staticRoute) {
put(staticMap.pattern1, staticMap.route1);
put(staticMap.pattern2, staticMap.route2);
put(staticMap.pattern3, staticMap.route3);
put(staticMap.pattern4, staticMap.route4);
put(staticMap.pattern5, staticMap.route5);
put(staticMap.pattern6, staticMap.route6);
put(path, staticRoute);
staticMap.release();
}
@Override
public StaticRoute get(String path) {
return paths.get(path);
}
@Override
public StaticMap put(String path, StaticRoute staticRoute) {
paths.computeIfAbsent(path, k -> staticRoute);
return this;
}
}
interface MethodMatcher {
StaticRouterMatch get(String method);
void put(String method, StaticRouterMatch route);
}
static class SingleMethodMatcher implements MethodMatcher {
private String method;
private StaticRouterMatch route;
@Override
public void put(String method, StaticRouterMatch route) {
this.method = method;
this.route = route;
}
@Override
public StaticRouterMatch get(String method) {
return this.method.equals(method) ? route : null;
}
public void clear() {
this.method = null;
this.route = null;
}
}
static class MultipleMethodMatcher implements MethodMatcher {
private final Map<String, StaticRouterMatch> methods = new HashMap<>();
public MultipleMethodMatcher(SingleMethodMatcher matcher) {
methods.put(matcher.method, matcher.route);
matcher.clear();
}
@Override
public StaticRouterMatch get(String method) {
return methods.get(method);
}
@Override
public void put(String method, StaticRouterMatch route) {
methods.put(method, route);
}
}
static class StaticRoute {
MethodMatcher matcher;
public void put(String method, Route route) {
if (matcher == null) {
matcher = new SingleMethodMatcher();
} else if (matcher instanceof SingleMethodMatcher) {
matcher = new MultipleMethodMatcher((SingleMethodMatcher) matcher);
}
matcher.put(method, new StaticRouterMatch(route));
}
}
static class Segment {
byte nodeType;
// String key = "";
String rexPat = EMPTY_STRING;
char tail;
int startIndex;
int endIndex;
public Segment() {}
public Segment(
byte nodeType, /*String key,*/ String regex, char tail, int startIndex, int endIndex) {
this.nodeType = nodeType;
// this.key = key;
this.rexPat = regex;
this.tail = tail;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
}
private static class Node implements Comparable<Node> {
// node type: static, regexp, param, catchAll
byte typ;
// first byte of the prefix
char label;
// first byte of the child prefix
char tail;
// prefix is the common prefix we ignore
String prefix;
// regexp matcher for regexp nodes
Pattern rex;
// HTTP handler endpoints on the leaf node
Map<String, Route> endpoints;
// subroutes on the leaf node
// Routes subroutes;
// child nodes should be stored in-order for iteration,
// in groups of the node type.
Node[][] children = new Node[NODE_SIZE][];
public Node typ(byte typ) {
this.typ = typ;
return this;
}
@Override
public int compareTo(Node o) {
return label - o.label;
}
public Node label(char label) {
this.label = label;
return this;
}
public Node tail(char tail) {
this.tail = tail;
return this;
}
public Node prefix(String prefix) {
this.prefix = prefix;
return this;
}
Node insertRoute(String method, String pattern, Route route, boolean failOnDuplicateRoutes) {
Node n = this;
Node parent;
String search = pattern;
while (true) {
// Handle key exhaustion
if (search.isEmpty()) {
// Insert or update the node's leaf handler
n.setEndpoint(method, route, failOnDuplicateRoutes);
return n;
}
// We're going to be searching for a wild node next,
// in this case, we need to get the tail
char label = search.charAt(0);
// char segTail;
// int segEndIdx;
// int segTyp;
Segment seg;
if (label == '{' || label == '*') {
// segTyp, _, segRexpat, segTail, _, segEndIdx = patNextSegment(search)
seg = patNextSegment(search);
} else {
seg = new Segment();
}
String prefix;
if (seg.nodeType == ntRegexp) {
prefix = seg.rexPat;
} else {
prefix = EMPTY_STRING;
}
// Look for the edge to attach to
parent = n;
n = n.getEdge(seg.nodeType, label, seg.tail, prefix);
// No edge, create one
if (n == null) {
Node child = new Node().label(label).tail(seg.tail).prefix(search);
Node hn = parent.addChild(child, search);
hn.setEndpoint(method, route, failOnDuplicateRoutes);
return hn;
}
// Found an edge to newRuntimeRoute the pattern
if (n.typ > ntStatic) {
// We found a param node, trim the param from the search path and continue.
// This param/wild pattern segment would already be on the tree from a previous
// call to addChild when creating a new node.
search = search.substring(seg.endIndex);
continue;
}
// Static nodes fall below here.
// Determine longest prefix of the search key on newRuntimeRoute.
int commonPrefix = longestPrefix(search, n.prefix);
if (commonPrefix == n.prefix.length()) {
// the common prefix is as long as the current node's prefix we're attempting to insert.
// keep the search going.
search = search.substring(commonPrefix);
continue;
}
// Split the node
Node child = new Node().typ(ntStatic).prefix(search.substring(0, commonPrefix));
parent.replaceChild(search.charAt(0), seg.tail, child);
// Restore the existing node
n.label = n.prefix.charAt(commonPrefix);
n.prefix = n.prefix.substring(commonPrefix);
child.addChild(n, n.prefix);
// If the new key is a subset, set the method/handler on this node and finish.
search = search.substring(commonPrefix);
if (search.isEmpty()) {
child.setEndpoint(method, route, failOnDuplicateRoutes);
return child;
}
// Create a new edge for the node
Node subchild = new Node().typ(ntStatic).label(search.charAt(0)).prefix(search);
Node hn = child.addChild(subchild, search);
hn.setEndpoint(method, route, failOnDuplicateRoutes);
return hn;
}
}
// addChild appends the new `child` node to the tree using the `pattern` as the trie key.
// For a URL router like chi's, we split the static, param, regexp and wildcard segments
// into different nodes. In addition, addChild will recursively call itself until every
// pattern segment is added to the url pattern tree as individual nodes, depending on type.
Node addChild(Node child, String search) {
Node n = this;
// String search = prefix.toString();
// handler leaf node added to the tree is the child.
// this may be overridden later down the flow
Node hn = child;
// Parse next segment
// segTyp, _, segRexpat, segTail, segStartIdx, segEndIdx := patNextSegment(search)
Segment seg = patNextSegment(search);
byte segTyp = seg.nodeType;
int segStartIdx = seg.startIndex;
int segEndIdx = seg.endIndex;
// Add child depending on next up segment
switch (segTyp) {
case ntStatic:
// Search prefix is all static (that is, has no params in path)
// noop
break;
default:
// Search prefix contains a param, regexp or wildcard
if (segTyp == ntRegexp) {
child.prefix = seg.rexPat;
child.rex = Pattern.compile(seg.rexPat);
}
if (segStartIdx == 0) {
// Route starts with a param
child.typ = segTyp;
if (segTyp == ntCatchAll) {
segStartIdx = -1;
} else {
segStartIdx = segEndIdx;
}
if (segStartIdx < 0) {
segStartIdx = search.length();
}
child.tail = seg.tail; // for params, we set the tail
if (segStartIdx != search.length()) {
// add static edge for the remaining part, split the end.
// its not possible to have adjacent param nodes, so its certainly
// going to be a static node next.
search = search.substring(segStartIdx); // advance search position
Node nn = new Node().typ(ntStatic).label(search.charAt(0)).prefix(search);
hn = child.addChild(nn, search);
}
} else if (segStartIdx > 0) {
// Route has some param
// starts with a static segment
child.typ = ntStatic;
child.prefix = search.substring(0, segStartIdx);
child.rex = null;
// add the param edge node
search = search.substring(segStartIdx);
Node nn = new Node().typ(segTyp).label(search.charAt(0)).tail(seg.tail);
hn = child.addChild(nn, search);
}
}
n.children[child.typ] = append(n.children[child.typ], child);
tailSort(n.children[child.typ]);
return hn;
}
void replaceChild(char label, char tail, Node child) {
Node n = this;
Node[] children = n.children[child.typ];
for (int i = 0; children != null && i < children.length; i++) {
if (children[i].label == label && children[i].tail == tail) {
children[i] = child;
children[i].label = label;
children[i].tail = tail;
return;
}
}
throw new IllegalArgumentException("Router: replacing missing child");
}
Node getEdge(int ntyp, char label, char tail, String prefix) {
Node n = this;
Node[] nds = n.children[ntyp];
for (int i = 0; nds != null && i < nds.length; i++) {
if (nds[i].label == label && nds[i].tail == tail) {
if (ntyp == ntRegexp && !nds[i].prefix.equals(prefix)) {
continue;
}
return nds[i];
}
}
return null;
}
void setEndpoint(String method, Route route, boolean failOnDuplicateRoutes) {
Node n = this;
// Set the handler for the method type on the node
if (n.endpoints == null) {
n.endpoints = new HashMap<>();
}
// if ((method & mSTUB) == mSTUB) {
// n.endpoints.put(mSTUB, new Endpoint(handler));
// }
// if ((method & mALL) == mALL) {
// Endpoint h = n.endpoints.get(mALL);
// h.handler = handler;
// h.pattern = pattern;
// h.paramKeys = paramKeys;
// for (Integer m : methodMap.values()) {
// h = n.endpoints.computeIfAbsent(m, k -> new Endpoint(handler));
// h.handler = handler;
// h.pattern = pattern;
// h.paramKeys = paramKeys;
// }
// } else {
if (failOnDuplicateRoutes) {
var existing = n.endpoints.get(method);
if (existing != null) {
throw new IllegalArgumentException(
"Route already exists: "
+ method
+ " "
+ existing.getPattern()
+ " at "
+ existing.getLocation().filename()
+ ":"
+ existing.getLocation().line());
}
}
n.endpoints.put(method, route);
// Endpoint h = n.endpoints.computeIfAbsent(method, k -> new Endpoint(handler));
// h.handler = handler;
// h.pattern = pattern;
// h.paramKeys = paramKeys;
// }
}
// Recursive edge traversal by checking all nodeTyp groups along the way.
// It's like searching through a multi-dimensional radix trie.
Route findRoute(RouterMatch rctx, String method, Slice path) {
for (int ntyp = 0; ntyp < NODE_SIZE; ntyp++) {
Node[] nds = this.children[ntyp];
if (nds != null) {
Node xn = null;
Slice xsearch = path;
char label = xsearch.isEmpty() ? ZERO_CHAR : xsearch.charAt(0);
switch (ntyp) {
case ntStatic:
xn = findEdge(nds, label);
if (xn == null || !xsearch.startsWith(xn.prefix)) {
continue;
}
xsearch = xsearch.substring(xn.prefix.length());
break;
case ntParam:
case ntRegexp:
// short-circuit and return no matching route for empty param values
if (xsearch.isEmpty()) {
continue;
}
// serially loop through each node grouped by the tail delimiter
for (int idx = 0; idx < nds.length; idx++) {
xn = nds[idx];
// label for param nodes is the delimiter byte
int p = xsearch.indexOf(xn.tail);
if (p < 0) {
if (xn.tail == '/') {
p = xsearch.length();
} else {
continue;
}
}
if (ntyp == ntRegexp && xn.rex != null) {
// 12, ar, page, arx
if (!xn.rex.matcher(xsearch.substring(0, p)).matches()) {
continue;
}
} else if (xsearch.substring(0, p).indexOf('/') > 0) {
// avoid a newRuntimeRoute across path segments
continue;
}
// rctx.routeParams.Values = append(rctx.routeParams.Values, xsearch[:p])
int prevlen = rctx.size();
rctx.value(xsearch.substring(0, p).toString());
xsearch = xsearch.substring(p);
if (xsearch.isEmpty()) {
if (xn.isLeaf()) {
Route h = xn.endpoints.get(method);
if (h != null) {
rctx.key(h.getPathKeys());
return h;
}
rctx.methodNotAllowed(xn.endpoints.keySet());
}
}
// recursively find the next node on this branch
Route fin = xn.findRoute(rctx, method, xsearch);
if (fin != null) {
return fin;
}
// not found on this branch, reset vars
rctx.truncate(prevlen);
xsearch = path;
}
break;
default:
// catch-all nodes
// rctx.routeParams.Values = append(rctx.routeParams.Values, search)
if (!xsearch.isEmpty()) {
rctx.value(xsearch.toString());
}
xn = nds[0];
xsearch = EMPTY_SLICE;
}
if (xn == null) {
continue;
}
// did we returnType it yet?
if (xsearch.length() == 0) {
if (xn.isLeaf()) {
Route h = xn.endpoints.get(method);
if (h != null) {
// rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
rctx.key(h.getPathKeys());
return h;
}
// flag that the routing context found a route, but not a corresponding
// supported method
rctx.methodNotAllowed(xn.endpoints.keySet());
}
}
// recursively returnType the next node..
Route fin = xn.findRoute(rctx, method, xsearch);
if (fin != null) {
return fin;
}
// Did not returnType final handler, let's remove the param here if it was set
if (xn.typ > ntStatic) {
// if len(rctx.routeParams.Values) > 0 {
// rctx.routeParams.Values =
// rctx.routeParams.Values[:len(rctx.routeParams.Values) - 1]
// }
rctx.pop();
}
}
}
return null;