You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/02/13 18:23:54 UTC

[camel] branch master updated: CAMEL-16203: camel-core - Optimize removeHeaders all

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cc63d9  CAMEL-16203: camel-core - Optimize removeHeaders all
0cc63d9 is described below

commit 0cc63d965d8be2248886f7764f211fc539ac2495
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Feb 13 19:19:42 2021 +0100

    CAMEL-16203: camel-core - Optimize removeHeaders all
---
 .../camel/processor/RemoveHeadersAllTest.java      | 107 +++++++++++++++++++++
 .../org/apache/camel/support/DefaultExchange.java  |  17 +++-
 .../org/apache/camel/support/DefaultMessage.java   |  28 ++++--
 3 files changed, 143 insertions(+), 9 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RemoveHeadersAllTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RemoveHeadersAllTest.java
new file mode 100644
index 0000000..65a2f2d
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/RemoveHeadersAllTest.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RemoveHeadersAllTest extends ContextTestSupport {
+
+    @Test
+    public void testRemoveHeadersAll() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:end");
+        mock.expectedBodiesReceived("Hello World");
+
+        Map<String, Object> headers = new HashMap<>();
+        headers.put("dudeCool", "cool");
+        headers.put("DudeWicket", "wicket");
+        headers.put("DUDEbig", "upper");
+        headers.put("duck", "Donald");
+        headers.put("foo", "bar");
+
+        template.sendBodyAndHeaders("direct:start", "Hello World", headers);
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(0, mock.getReceivedExchanges().get(0).getIn().getHeaders().size());
+    }
+
+    @Test
+    public void testRemoveHeadersAllExclude() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:end");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedHeaderReceived("duck", "Donald");
+
+        Map<String, Object> headers = new HashMap<>();
+        headers.put("dudeCool", "cool");
+        headers.put("DudeWicket", "wicket");
+        headers.put("DUDEbig", "upper");
+        headers.put("duck", "Donald");
+        headers.put("foo", "bar");
+
+        template.sendBodyAndHeaders("direct:startWithExclude", "Hello World", headers);
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(1, mock.getReceivedExchanges().get(0).getIn().getHeaders().size());
+    }
+
+    @Test
+    public void testRemoveHeadersAllExcludeNoMatch() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:end");
+        mock.expectedBodiesReceived("Hello World");
+
+        Map<String, Object> headers = new HashMap<>();
+        headers.put("dudeCool", "cool");
+        headers.put("DudeWicket", "wicket");
+        headers.put("DUDEbig", "upper");
+        headers.put("duck", "Donald");
+        headers.put("foo", "bar");
+
+        template.sendBodyAndHeaders("direct:startWithExcludeNoMatch", "Hello World", headers);
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(0, mock.getReceivedExchanges().get(0).getIn().getHeaders().size());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start")
+                        .removeHeaders("*")
+                        .to("mock:end");
+
+                from("direct:startWithExclude")
+                        .removeHeaders("*", "duck")
+                        .to("mock:end");
+
+                from("direct:startWithExcludeNoMatch")
+                        .removeHeaders("*", "dog")
+                        .to("mock:end");
+            }
+        };
+    }
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java
index eff55ce..986312b 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java
@@ -281,8 +281,14 @@ public final class DefaultExchange implements ExtendedExchange {
             return false;
         }
 
+        // special optimized
+        if (excludePatterns == null && "*".equals(pattern)) {
+            properties.clear();
+            return true;
+        }
+
         // store keys to be removed as we cannot loop and remove at the same time in implementations such as HashMap
-        Set<String> toBeRemoved = new HashSet<>();
+        Set<String> toBeRemoved = null;
         boolean matches = false;
         for (String key : properties.keySet()) {
             if (PatternHelper.matchPattern(key, pattern)) {
@@ -290,16 +296,21 @@ public final class DefaultExchange implements ExtendedExchange {
                     continue;
                 }
                 matches = true;
+                if (toBeRemoved == null) {
+                    toBeRemoved = new HashSet<>();
+                }
                 toBeRemoved.add(key);
             }
         }
 
-        if (!toBeRemoved.isEmpty()) {
+        if (matches) {
             if (toBeRemoved.size() == properties.size()) {
                 // special optimization when all should be removed
                 properties.clear();
             } else {
-                toBeRemoved.forEach(k -> properties.remove(k));
+                for (String key : toBeRemoved) {
+                    properties.remove(key);
+                }
             }
         }
 
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java
index c46922e..7e30d6f 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java
@@ -229,22 +229,38 @@ public class DefaultMessage extends MessageSupport {
             return false;
         }
 
+        // special optimized
+        if (excludePatterns == null && "*".equals(pattern)) {
+            headers.clear();
+            return true;
+        }
+
         boolean matches = false;
         // must use a set to store the keys to remove as we cannot walk using entrySet and remove at the same time
         // due concurrent modification error
-        Set<String> toRemove = new HashSet<>();
-        for (Map.Entry<String, Object> entry : headers.entrySet()) {
-            String key = entry.getKey();
+        Set<String> toBeRemoved = null;
+        for (String key : headers.keySet()) {
             if (PatternHelper.matchPattern(key, pattern)) {
                 if (excludePatterns != null && PatternHelper.isExcludePatternMatch(key, excludePatterns)) {
                     continue;
                 }
                 matches = true;
-                toRemove.add(entry.getKey());
+                if (toBeRemoved == null) {
+                    toBeRemoved = new HashSet<>();
+                }
+                toBeRemoved.add(key);
             }
         }
-        for (String key : toRemove) {
-            headers.remove(key);
+
+        if (matches) {
+            if (toBeRemoved.size() == headers.size()) {
+                // special optimization when all should be removed
+                headers.clear();
+            } else {
+                for (String key : toBeRemoved) {
+                    headers.remove(key);
+                }
+            }
         }
 
         return matches;