You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by so...@apache.org on 2024/08/17 06:28:18 UTC

(dubbo-spi-extensions) branch master updated: optimize: the handling logic of PathAndInvokerMapper (#473)

This is an automated email from the ASF dual-hosted git repository.

songxiaosheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git


The following commit(s) were added to refs/heads/master by this push:
     new 8df3affe optimize: the handling logic of PathAndInvokerMapper (#473)
8df3affe is described below

commit 8df3affe24fa474770983268691962bcd5f07b7c
Author: funkye <36...@qq.com>
AuthorDate: Sat Aug 17 14:28:13 2024 +0800

    optimize: the handling logic of PathAndInvokerMapper (#473)
    
    * optimize: the handling logic of PathAndInvokerMapper
    
    * optimize: the handling logic of PathAndInvokerMapper
    
    ---------
    
    Co-authored-by: xiaosheng <so...@apache.org>
---
 .../rpc/protocol/rest/PathAndInvokerMapper.java    | 45 ++++++++--------------
 1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/dubbo-rpc-extensions/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java b/dubbo-rpc-extensions/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
index 1cc9e6ab..097b3b4d 100644
--- a/dubbo-rpc-extensions/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
+++ b/dubbo-rpc-extensions/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
@@ -53,18 +53,13 @@ public class PathAndInvokerMapper {
      */
     public void addPathAndInvoker(Map<PathMatcher, RestMethodMetadata> metadataMap, Invoker invoker) {
 
-        metadataMap.entrySet().stream().forEach(entry -> {
-            PathMatcher pathMatcher = entry.getKey();
+        metadataMap.forEach((pathMatcher, value) -> {
             if (pathMatcher.hasPathVariable()) {
-                addPathMatcherToPathMap(
-                        pathMatcher,
-                        pathToServiceMapContainPathVariable,
-                        InvokerAndRestMethodMetadataPair.pair(invoker, entry.getValue()));
+                addPathMatcherToPathMap(pathMatcher, pathToServiceMapContainPathVariable,
+                    InvokerAndRestMethodMetadataPair.pair(invoker, value));
             } else {
-                addPathMatcherToPathMap(
-                        pathMatcher,
-                        pathToServiceMapNoPathVariable,
-                        InvokerAndRestMethodMetadataPair.pair(invoker, entry.getValue()));
+                addPathMatcherToPathMap(pathMatcher, pathToServiceMapNoPathVariable,
+                    InvokerAndRestMethodMetadataPair.pair(invoker, value));
             }
         });
     }
@@ -78,16 +73,13 @@ public class PathAndInvokerMapper {
     public InvokerAndRestMethodMetadataPair getRestMethodMetadata(PathMatcher pathMatcher) {
 
         // first search from pathToServiceMapNoPathVariable
-        if (pathToServiceMapNoPathVariable.containsKey(pathMatcher)) {
-            return pathToServiceMapNoPathVariable.get(pathMatcher);
+        InvokerAndRestMethodMetadataPair pair = pathToServiceMapNoPathVariable.get(pathMatcher);
+        if (pair == null) {
+            // second search from pathToServiceMapContainPathVariable
+            pair = pathToServiceMapContainPathVariable.get(pathMatcher);
         }
 
-        // second search from pathToServiceMapContainPathVariable
-        if (pathToServiceMapContainPathVariable.containsKey(pathMatcher)) {
-            return pathToServiceMapContainPathVariable.get(pathMatcher);
-        }
-
-        return null;
+        return pair;
     }
 
     /**
@@ -117,12 +109,12 @@ public class PathAndInvokerMapper {
             Map<PathMatcher, InvokerAndRestMethodMetadataPair> pathMatcherPairMap,
             InvokerAndRestMethodMetadataPair invokerRestMethodMetadataPair) {
 
-        if (pathMatcherPairMap.containsKey(pathMatcher)) {
+        InvokerAndRestMethodMetadataPair beforeMetadata = pathMatcherPairMap.get(pathMatcher);
+        if (beforeMetadata != null) {
 
             // cover the old service metadata when  current interface is old interface & current method desc equals
             // old`s method desc,else ,throw double check exception
 
-            InvokerAndRestMethodMetadataPair beforeMetadata = pathMatcherPairMap.get(pathMatcher);
             // true when reExport
             if (!invokerRestMethodMetadataPair.compareServiceMethod(beforeMetadata)) {
                 throw new DoublePathCheckException("dubbo rest double path check error, current path is: " + pathMatcher
@@ -145,15 +137,12 @@ public class PathAndInvokerMapper {
 
         PathMatcher newPathMatcher = PathMatcher.convertPathMatcher(pathMatcher);
 
-        if (!pathMatcherToHttpMethodMap.containsKey(newPathMatcher)) {
-            HashSet<String> httpMethods = new HashSet<>();
-
-            httpMethods.add(pathMatcher.getHttpMethod());
+        Set<String> httpMethods = pathMatcherToHttpMethodMap.computeIfAbsent(newPathMatcher, k -> {
+            HashSet<String> methods = new HashSet<>();
 
-            pathMatcherToHttpMethodMap.put(newPathMatcher, httpMethods);
-        }
-
-        Set<String> httpMethods = pathMatcherToHttpMethodMap.get(newPathMatcher);
+            methods.add(pathMatcher.getHttpMethod());
+            return methods;
+        });
 
         httpMethods.add(newPathMatcher.getHttpMethod());
     }