From 0409aaecb0e46c81eec0802fc725a0271420335a Mon Sep 17 00:00:00 2001 From: nkorange Date: Fri, 7 Dec 2018 11:24:22 +0800 Subject: [PATCH] #187 Add Java docs --- .../nacos/api/cmdb/spi/CmdbService.java | 2 + .../naming/selector/AbstractSelector.java | 1 + .../nacos/naming/selector/LabelSelector.java | 198 ++++++++++-------- .../naming/selector/SelectorJsonAdapter.java | 5 + 4 files changed, 114 insertions(+), 92 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java b/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java index 21d5ac054f1..7442ae129a7 100644 --- a/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java +++ b/api/src/main/java/com/alibaba/nacos/api/cmdb/spi/CmdbService.java @@ -24,6 +24,8 @@ import java.util.Set; /** + * Service to visit CMDB store + * * @author nkorange */ public interface CmdbService { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/selector/AbstractSelector.java b/naming/src/main/java/com/alibaba/nacos/naming/selector/AbstractSelector.java index 0c49b0081e7..cdea439017a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/selector/AbstractSelector.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/selector/AbstractSelector.java @@ -33,6 +33,7 @@ * * @author nkorange * @see SelectorType + * @see SelectorJsonAdapter */ public abstract class AbstractSelector { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java b/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java index 332d177ff94..3984506f0aa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/selector/LabelSelector.java @@ -57,7 +57,7 @@ public class LabelSelector extends AbstractSelector { private CmdbReader cmdbReader; /** - * The labels relevant to this the select strategy. + * The labels relevant to this the selector. * * @see com.alibaba.nacos.api.cmdb.pojo.Label */ @@ -108,138 +108,152 @@ public LabelSelector() { cmdbReader = context.getBean(CmdbReader.class); } - /** - * Parse the label expression. - *

- * Currently we support the very single type of expression: - *

-     *     consumer.labelA = provider.labelA & consumer.labelB = provider.labelB
-     * 
- * Later we will implement a interpreter to parse this expression in a standard LL parser way. - * - * @param expression the label expression to parse - * @return collection of labels - */ + public static Set parseExpression(String expression) { + return ExpressionInterpreter.parseExpression(expression); + } - String[] elements = expression.split(" "); - Set gotLabels = new HashSet<>(); - int index = 0; - index = checkInnerSyntax(elements, index); + @Override + public List select(String consumer, List providers) { + List ipAddressList = new ArrayList<>(); + for (IpAddress ipAddress : providers) { - if (index == -1) { - return new HashSet<>(); + boolean matched = true; + for (String labelName : getLabels()) { + if (!StringUtils.equals(cmdbReader.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName), + cmdbReader.queryLabel(ipAddress.getIp(), PreservedEntityTypes.ip.name(), labelName))) { + matched = false; + break; + } + } + if (matched) { + ipAddressList.add(ipAddress); + } } - gotLabels.add(elements[index++].split(PROVIDER_PREFIX)[1]); - - while (index < elements.length) { + if (ipAddressList.isEmpty()) { + return providers; + } - index = checkOuterSyntax(elements, index); + return ipAddressList; + } - if (index >= elements.length) { - return gotLabels; - } + /** + * Expression interpreter for label selector. + *

+ * For now it supports very limited set of syntax rules. + */ + public static class ExpressionInterpreter { + + /** + * Parse the label expression. + *

+ * Currently we support the very single type of expression: + *

+         *     consumer.labelA = provider.labelA & consumer.labelB = provider.labelB
+         * 
+ * Later we will implement a interpreter to parse this expression in a standard LL parser way. + * + * @param expression the label expression to parse + * @return collection of labels + */ + public static Set parseExpression(String expression) { + + String[] elements = expression.split(" "); + Set gotLabels = new HashSet<>(); + int index = 0; + + index = checkInnerSyntax(elements, index); if (index == -1) { return new HashSet<>(); } gotLabels.add(elements[index++].split(PROVIDER_PREFIX)[1]); - } - return gotLabels; - } + while (index < elements.length) { - private static int skipEmpty(String[] elements, int start) { - while (start < elements.length && StringUtils.isBlank(elements[start])) { - start++; - } - return start; - } + index = checkOuterSyntax(elements, index); - private static int checkOuterSyntax(String[] elements, int start) { + if (index >= elements.length) { + return gotLabels; + } - int index = start; + if (index == -1) { + return new HashSet<>(); + } - index = skipEmpty(elements, index); - if (index >= elements.length) { - return index; + gotLabels.add(elements[index++].split(PROVIDER_PREFIX)[1]); + } + + return gotLabels; } - if (!SUPPORTED_OUTER_CONNCETORS.contains(elements[index++])) { - return -1; + private static int skipEmpty(String[] elements, int start) { + while (start < elements.length && StringUtils.isBlank(elements[start])) { + start++; + } + return start; } - return checkInnerSyntax(elements, index); - } + private static int checkOuterSyntax(String[] elements, int start) { - private static int checkInnerSyntax(String[] elements, int start) { + int index = start; - int index = start; + index = skipEmpty(elements, index); + if (index >= elements.length) { + return index; + } - index = skipEmpty(elements, index); - if (index >= elements.length) { - return -1; - } + if (!SUPPORTED_OUTER_CONNCETORS.contains(elements[index++])) { + return -1; + } - if (!elements[index].startsWith(CONSUMER_PREFIX)) { - return -1; + return checkInnerSyntax(elements, index); } - String labelConsumer = elements[index++].split(CONSUMER_PREFIX)[1]; + private static int checkInnerSyntax(String[] elements, int start) { - index = skipEmpty(elements, index); - if (index >= elements.length) { - return -1; - } + int index = start; - if (!SUPPORTED_INNER_CONNCETORS.contains(elements[index++])) { - return -1; - } - - index = skipEmpty(elements, index); - if (index >= elements.length) { - return -1; - } + index = skipEmpty(elements, index); + if (index >= elements.length) { + return -1; + } - if (!elements[index].startsWith(PROVIDER_PREFIX)) { - return -1; - } + if (!elements[index].startsWith(CONSUMER_PREFIX)) { + return -1; + } - String labelProvider = elements[index].split(PROVIDER_PREFIX)[1]; + String labelConsumer = elements[index++].split(CONSUMER_PREFIX)[1]; - if (!labelConsumer.equals(labelProvider)) { - return -1; - } + index = skipEmpty(elements, index); + if (index >= elements.length) { + return -1; + } - return index; - } + if (!SUPPORTED_INNER_CONNCETORS.contains(elements[index++])) { + return -1; + } - @Override - public List select(String consumer, List providers) { - List ipAddressList = new ArrayList<>(); - for (IpAddress ipAddress : providers) { + index = skipEmpty(elements, index); + if (index >= elements.length) { + return -1; + } - boolean matched = true; - for (String labelName : getLabels()) { - if (!StringUtils.equals(cmdbReader.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName), - cmdbReader.queryLabel(ipAddress.getIp(), PreservedEntityTypes.ip.name(), labelName))) { - matched = false; - break; - } + if (!elements[index].startsWith(PROVIDER_PREFIX)) { + return -1; } - if (matched) { - ipAddressList.add(ipAddress); + + String labelProvider = elements[index].split(PROVIDER_PREFIX)[1]; + + if (!labelConsumer.equals(labelProvider)) { + return -1; } - } - if (ipAddressList.isEmpty()) { - return providers; + return index; } - - return ipAddressList; } public static void main(String[] args) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/selector/SelectorJsonAdapter.java b/naming/src/main/java/com/alibaba/nacos/naming/selector/SelectorJsonAdapter.java index 4081ef0020e..3329d4097c3 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/selector/SelectorJsonAdapter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/selector/SelectorJsonAdapter.java @@ -28,6 +28,11 @@ import java.lang.reflect.Type; /** + * Json adapter for class Selector. + *

+ * When deserializing object for class Selector, we should consider to convert the selector to + * its runtime child class object. And this adapter helps us to finish it. + * * @author nkorange */ public class SelectorJsonAdapter implements ObjectDeserializer, ObjectSerializer {