Skip to content

Commit 3d0bf18

Browse files
committed
fix diff method can not be added to RadixTree
1 parent c31164a commit 3d0bf18

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/DefaultRequestMappingRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void register0(RequestMapping mapping, HandlerMeta handler, AtomicIntege
169169
try {
170170
Registration registration = new Registration(mapping, handler);
171171
for (PathExpression path : mapping.getPathCondition().getExpressions()) {
172-
Registration exists = tree.addPath(path, registration);
172+
Registration exists = tree.addPath(path, registration, Registration::equals);
173173
if (exists == null) {
174174
counter.incrementAndGet();
175175
if (LOGGER.isDebugEnabled()) {

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/RadixTree.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.Map.Entry;
3434
import java.util.function.BiConsumer;
35+
import java.util.function.BiFunction;
3536
import java.util.function.Predicate;
3637

3738
/**
@@ -63,7 +64,18 @@ public RadixTree() {
6364
this(true, '/');
6465
}
6566

67+
/**
68+
* This is a default implementation that does not check for equality.
69+
*/
6670
public T addPath(PathExpression path, T value) {
71+
return addPath(path, value, (t, t2) -> Boolean.TRUE);
72+
}
73+
74+
/**
75+
* When equalFunction is not null, if a node is found at the path, it will not be returned immediately; <br/>
76+
* instead, whether to return it depends on the result of the function.
77+
*/
78+
public T addPath(PathExpression path, T value, BiFunction<T, T, Boolean> equalFunction) {
6779
if (path.isDirect()) {
6880
KeyString key = new KeyString(path.getPath(), caseSensitive);
6981
List<Match<T>> matches = directPathMap.computeIfAbsent(key, k -> new ArrayList<>());
@@ -83,9 +95,16 @@ public T addPath(PathExpression path, T value) {
8395
Node<T> child = getChild(current, segments[i]);
8496
if (i == len - 1) {
8597
List<Pair<PathExpression, T>> values = child.values;
86-
for (int j = 0, size = values.size(); j < size; j++) {
87-
if (values.get(j).getLeft().equals(path)) {
88-
return values.get(j).getRight();
98+
for (Pair<PathExpression, T> pathExpressionTPair : values) {
99+
if (pathExpressionTPair.getLeft().equals(path)) {
100+
T rightVal = pathExpressionTPair.getRight();
101+
if (equalFunction == null) {
102+
return rightVal;
103+
} else {
104+
if (equalFunction.apply(rightVal, value)) {
105+
return rightVal;
106+
}
107+
}
89108
}
90109
}
91110
values.add(Pair.of(path, value));

0 commit comments

Comments
 (0)