This is an automated email from the ASF dual-hosted git repository.
jinrongtong pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new fa22a7838 [ISSUE #5507] Improve the speed of
AttributeParser#parseToMap parsing (#5508)
fa22a7838 is described below
commit fa22a78387f76d4ff04aa5b9527e03cf58caba01
Author: mxsm <[email protected]>
AuthorDate: Thu Jan 12 10:41:25 2023 +0800
[ISSUE #5507] Improve the speed of AttributeParser#parseToMap parsing
(#5508)
---
.../rocketmq/common/attribute/AttributeParser.java | 30 +++++++++++++---------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git
a/common/src/main/java/org/apache/rocketmq/common/attribute/AttributeParser.java
b/common/src/main/java/org/apache/rocketmq/common/attribute/AttributeParser.java
index 7ee7afca6..da98c6dab 100644
---
a/common/src/main/java/org/apache/rocketmq/common/attribute/AttributeParser.java
+++
b/common/src/main/java/org/apache/rocketmq/common/attribute/AttributeParser.java
@@ -16,15 +16,22 @@
*/
package org.apache.rocketmq.common.attribute;
-import com.google.common.base.Joiner;
import com.google.common.base.Strings;
-
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AttributeParser {
+
+ public static final String ATTR_ARRAY_SEPARATOR_COMMA = ",";
+
+ public static final String ATTR_KEY_VALUE_EQUAL_SIGN = "=";
+
+ public static final String ATTR_ADD_PLUS_SIGN = "+";
+
+ private static final String ATTR_DELETE_MINUS_SIGN = "-";
+
public static Map<String, String> parseToMap(String
attributesModification) {
if (Strings.isNullOrEmpty(attributesModification)) {
return new HashMap<>();
@@ -32,22 +39,21 @@ public class AttributeParser {
// format: +key1=value1,+key2=value2,-key3,+key4=value4
Map<String, String> attributes = new HashMap<>();
- String arraySeparator = ",";
- String kvSeparator = "=";
- String[] kvs = attributesModification.split(arraySeparator);
+ String[] kvs =
attributesModification.split(ATTR_ARRAY_SEPARATOR_COMMA);
for (String kv : kvs) {
String key;
String value;
- if (kv.contains(kvSeparator)) {
- key = kv.split(kvSeparator)[0];
- value = kv.split(kvSeparator)[1];
- if (!key.contains("+")) {
+ if (kv.contains(ATTR_KEY_VALUE_EQUAL_SIGN)) {
+ String[] splits = kv.split(ATTR_KEY_VALUE_EQUAL_SIGN);
+ key = splits[0];
+ value = splits[1];
+ if (!key.contains(ATTR_ADD_PLUS_SIGN)) {
throw new RuntimeException("add/alter attribute format is
wrong: " + key);
}
} else {
key = kv;
value = "";
- if (!key.contains("-")) {
+ if (!key.contains(ATTR_DELETE_MINUS_SIGN)) {
throw new RuntimeException("delete attribute format is
wrong: " + key);
}
}
@@ -71,9 +77,9 @@ public class AttributeParser {
if (Strings.isNullOrEmpty(value)) {
kvs.add(entry.getKey());
} else {
- kvs.add(entry.getKey() + "=" + entry.getValue());
+ kvs.add(entry.getKey() + ATTR_KEY_VALUE_EQUAL_SIGN +
entry.getValue());
}
}
- return Joiner.on(",").join(kvs);
+ return String.join(ATTR_ARRAY_SEPARATOR_COMMA, kvs);
}
}