This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8f40f0c Parse hint optimization. (#13681)
8f40f0c is described below
commit 8f40f0ca56d1e6f49dbaafee2a686df0cff50b50
Author: gin <[email protected]>
AuthorDate: Thu Nov 18 18:21:36 2021 +0800
Parse hint optimization. (#13681)
---
.../shadow/hint/HintShadowAlgorithmUtil.java | 93 ----------------
.../algorithm/shadow/hint/ShadowHintExtractor.java | 118 +++++++++++++++++++++
.../shadow/hint/SimpleHintShadowAlgorithm.java | 2 +-
3 files changed, 119 insertions(+), 94 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/HintShadowAlgorithmUtil.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/HintShadowAlgorithmUtil.java
deleted file mode 100644
index b95d1b4..0000000
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/HintShadowAlgorithmUtil.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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.shardingsphere.shadow.algorithm.shadow.hint;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-
-/**
- * Hint shadow algorithm util.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class HintShadowAlgorithmUtil {
-
- private static final String SIMPLE_SQL_NOTE_SPACE = ",";
-
- private static final String SIMPLE_SQL_NOTE_ELEMENT_SPACE = ":";
-
- private static final String SIMPLE_SQL_NOTE_PREFIX = "/*";
-
- private static final String SIMPLE_SQL_NOTE_SUFFIX = "*/";
-
- /**
- * Parse simple hint in SQL comment.
- *
- * @param sqlComment SQL comment
- * @return simple hint map
- */
- public static Optional<Map<String, String>> parseSimpleHint(final String
sqlComment) {
- String noteValue = sqlComment.trim();
- if (noteValue.startsWith(SIMPLE_SQL_NOTE_PREFIX)) {
- noteValue = removePrefix(noteValue);
- }
- if (noteValue.endsWith(SIMPLE_SQL_NOTE_SUFFIX)) {
- noteValue = removeSuffix(noteValue);
- }
- if (isBlank(noteValue)) {
- return Optional.empty();
- } else {
- noteValue = noteValue.trim();
- String[] noteElements = noteValue.split(SIMPLE_SQL_NOTE_SPACE);
- Map<String, String> result = new HashMap<>(noteElements.length);
- for (String each : noteElements) {
- String temp = each;
- temp = temp.trim();
- String[] split = temp.split(SIMPLE_SQL_NOTE_ELEMENT_SPACE);
- if (2 == split.length) {
- result.put(split[0].trim(), split[1].trim());
- }
- }
- return Optional.of(result);
- }
- }
-
- private static String removePrefix(final String input) {
- return input.substring(SIMPLE_SQL_NOTE_PREFIX.length());
- }
-
- private static String removeSuffix(final String input) {
- return input.substring(0, input.length() -
SIMPLE_SQL_NOTE_SUFFIX.length());
- }
-
- private static boolean isBlank(final String noteValue) {
- final int strLen = noteValue == null ? 0 : noteValue.length();
- if (strLen == 0) {
- return true;
- }
- for (int i = 0; i < strLen; i++) {
- if (!Character.isWhitespace(noteValue.charAt(i))) {
- return false;
- }
- }
- return true;
- }
-}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/ShadowHintExtractor.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/ShadowHintExtractor.java
new file mode 100644
index 0000000..87f0b25
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/ShadowHintExtractor.java
@@ -0,0 +1,118 @@
+/*
+ * 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.shardingsphere.shadow.algorithm.shadow.hint;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Hint shadow algorithm util.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShadowHintExtractor {
+
+ private static final String SHADOW_HINT_SPACE = ",";
+
+ private static final String SHADOW_HINT_ELEMENT_SPACE = ":";
+
+ private static final String SQL_COMMENT_PREFIX = "/*";
+
+ private static final String SQL_COMMENT_SUFFIX = "*/";
+
+ private static final String SQL_COMMENT_TRACE_SPAN = "@TRACE_CONTEXT@";
+
+ /**
+ * Extract simple hint map from SQL comment.
+ *
+ * @param sqlComment SQL comment
+ * @return simple hint map
+ */
+ public static Optional<Map<String, String>> extractSimpleHint(final String
sqlComment) {
+ String sqlCommentValue = trim(sqlComment);
+ if (isBlank(sqlCommentValue)) {
+ return Optional.empty();
+ }
+ return extractElements(sqlCommentValue);
+ }
+
+ private static String trim(final String sqlComment) {
+ String result = sqlComment.trim();
+ if (result.startsWith(SQL_COMMENT_PREFIX)) {
+ result = removePrefix(result);
+ }
+ if (result.endsWith(SQL_COMMENT_SUFFIX)) {
+ result = removeSuffix(result);
+ }
+ result = result.trim();
+ return trimTrace(result);
+ }
+
+ private static String removePrefix(final String input) {
+ return input.substring(SQL_COMMENT_PREFIX.length());
+ }
+
+ private static String removeSuffix(final String input) {
+ return input.substring(0, input.length() -
SQL_COMMENT_SUFFIX.length());
+ }
+
+ private static String trimTrace(final String sqlComment) {
+ int startIndex = sqlComment.indexOf(SQL_COMMENT_TRACE_SPAN);
+ if (startIndex == -1) {
+ return sqlComment;
+ }
+ int traceLen = SQL_COMMENT_TRACE_SPAN.length();
+ int fromIndex = startIndex + traceLen;
+ int endIndex = sqlComment.indexOf(SQL_COMMENT_TRACE_SPAN, fromIndex);
+ if (endIndex == -1) {
+ return sqlComment;
+ }
+ String result = sqlComment.substring(0, startIndex) +
sqlComment.substring(endIndex + traceLen);
+ return result.trim();
+ }
+
+ private static Optional<Map<String, String>> extractElements(final String
sqlComment) {
+ String[] noteElements = sqlComment.split(SHADOW_HINT_SPACE);
+ Map<String, String> result = new HashMap<>(noteElements.length);
+ for (String each : noteElements) {
+ String temp = each;
+ temp = temp.trim();
+ String[] split = temp.split(SHADOW_HINT_ELEMENT_SPACE);
+ if (2 == split.length) {
+ result.put(split[0].trim(), split[1].trim());
+ }
+ }
+ return Optional.of(result);
+ }
+
+ private static boolean isBlank(final String noteValue) {
+ final int strLen = noteValue == null ? 0 : noteValue.length();
+ if (strLen == 0) {
+ return true;
+ }
+ for (int i = 0; i < strLen; i++) {
+ if (!Character.isWhitespace(noteValue.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SimpleHintShadowAlgorithm.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SimpleHintShadowAlgorithm.java
index 484b6e3..c066fa7 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SimpleHintShadowAlgorithm.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SimpleHintShadowAlgorithm.java
@@ -53,7 +53,7 @@ public final class SimpleHintShadowAlgorithm implements
HintShadowAlgorithm<Stri
if (ShadowOperationType.HINT_MATCH !=
noteShadowValue.getShadowOperationType() &&
!shadowTableNames.contains(noteShadowValue.getLogicTableName())) {
return false;
}
- Optional<Map<String, String>> noteOptional =
HintShadowAlgorithmUtil.parseSimpleHint(noteShadowValue.getValue());
+ Optional<Map<String, String>> noteOptional =
ShadowHintExtractor.extractSimpleHint(noteShadowValue.getValue());
return noteOptional.filter(stringStringMap ->
props.entrySet().stream().allMatch(entry -> Objects.equals(entry.getValue(),
stringStringMap.get(String.valueOf(entry.getKey()))))).isPresent();
}