From 0344e5f70c5cd90a614ed541a50e95b973f79ae8 Mon Sep 17 00:00:00 2001 From: jovany-wang Date: Wed, 6 Nov 2019 21:01:36 +0800 Subject: [PATCH 1/6] Fix --- common/pom.xml | 7 +++++++ parser/src/main/java/org/dst/parser/DstNewSQL.g4 | 15 +++++---------- .../java/org/dst/parser/DstNewSqlListener.java | 6 +++++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/common/pom.xml b/common/pom.xml index fecbdafbd..23117b394 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -9,4 +9,11 @@ 4.0.0 dst-common + + + com.google.guava + guava + 20.0 + + diff --git a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 index bb0d9536d..bb0678a04 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 +++ b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 @@ -9,26 +9,21 @@ conceptStatement: strStatement | listStatement | setStatement | dictStatement; // str concept strStatement: strPut | strGet; -strPut: 'str.put' key value; -strGet: 'str.get' key ; +strPut: 'str.put' STRING STRING; +strGet: 'str.get' STRING ; // list concept listStatement: ; // set concept setStatement: setPut | setGet | setDropByKey; -setPut: 'set.put' key valueArray; -setGet:'set.get' key; -setDropByKey: 'set.dropByKey' key key; +setPut: 'set.put' STRING (STRING)+; +setGet:'set.get' STRING; +setDropByKey: 'set.dropByKey' STRING STRING; // dict concept dictStatement: ; -// meta -key: STRING; -value: STRING; -valueArray :(STRING)+; - STRING: '"'.*?'"' ; // Skip spaces, tabs, and newlines. diff --git a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java index 1a42e2e98..49926878c 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java +++ b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java @@ -1,5 +1,6 @@ package org.dst.parser; +import com.google.common.base.Preconditions; import org.dst.parser.executor.AbstractExecutor; import org.dst.parser.executor.DstSetExecutor; import org.dst.parser.generated.DstNewSQLBaseListener; @@ -19,7 +20,10 @@ public AbstractExecutor getExecutor() { @Override public void enterSetPut(DstNewSQLParser.SetPutContext ctx) { - + // The children of the `set_put` should be 3: + // `set.put key value_array` + Preconditions.checkState(ctx.children.size() == 3); + final String key = ctx.children.get(1); executor = new DstSetExecutor(); //optimize method name; executor.setMethod("put"); From 71bb2a6549018379499eefdc3ed2dafdf1277cf9 Mon Sep 17 00:00:00 2001 From: jovany-wang Date: Wed, 6 Nov 2019 21:40:02 +0800 Subject: [PATCH 2/6] Refine --- .../src/main/java/org/dst/parser/DstNewSQL.g4 | 15 ++++--- .../org/dst/parser/DstNewSqlListener.java | 44 +++++++++---------- .../main/java/org/dst/parser/DstParser.java | 5 +-- .../dst/parser/executor/AbstractExecutor.java | 41 ----------------- .../dst/parser/executor/DstSetExecutor.java | 30 ------------- .../org/dst/parser/po/DstParsedResult.java | 8 ++-- .../org/dst/parser/po/RequestTypeEnum.java | 9 ++++ .../java/org/dst/parser/util/CodeUtils.java | 31 ------------- parser/src/test/java/org/dst/parser/Test.java | 18 +------- 9 files changed, 47 insertions(+), 154 deletions(-) delete mode 100644 parser/src/main/java/org/dst/parser/executor/AbstractExecutor.java delete mode 100644 parser/src/main/java/org/dst/parser/executor/DstSetExecutor.java create mode 100644 parser/src/main/java/org/dst/parser/po/RequestTypeEnum.java delete mode 100644 parser/src/main/java/org/dst/parser/util/CodeUtils.java diff --git a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 index bb0678a04..11c71f0e7 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 +++ b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 @@ -9,21 +9,26 @@ conceptStatement: strStatement | listStatement | setStatement | dictStatement; // str concept strStatement: strPut | strGet; -strPut: 'str.put' STRING STRING; -strGet: 'str.get' STRING ; +strPut: 'str.put' key value; +strGet: 'str.get' key ; // list concept listStatement: ; // set concept setStatement: setPut | setGet | setDropByKey; -setPut: 'set.put' STRING (STRING)+; -setGet:'set.get' STRING; -setDropByKey: 'set.dropByKey' STRING STRING; +setPut: 'set.put' key value_array; +setGet:'set.get' key; +setDropByKey: 'set.dropByKey' key key; // dict concept dictStatement: ; +// meta +key: STRING; +value: STRING; +value_array: (STRING)+; + STRING: '"'.*?'"' ; // Skip spaces, tabs, and newlines. diff --git a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java index 49926878c..4adb70d8e 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java +++ b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java @@ -1,10 +1,11 @@ package org.dst.parser; import com.google.common.base.Preconditions; -import org.dst.parser.executor.AbstractExecutor; -import org.dst.parser.executor.DstSetExecutor; import org.dst.parser.generated.DstNewSQLBaseListener; import org.dst.parser.generated.DstNewSQLParser; +import org.dst.parser.po.DstParsedResult; +import org.dst.parser.po.RequestTypeEnum; +import org.dst.rpc.protobuf.generated.SetProtocol; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,38 +13,37 @@ public class DstNewSqlListener extends DstNewSQLBaseListener { private static final Logger LOGGER = LoggerFactory.getLogger(DstNewSqlListener.class); - private AbstractExecutor executor; + DstParsedResult parsedResult = null; - public AbstractExecutor getExecutor() { - return executor; + public DstParsedResult getParsedResult() { + return parsedResult; } @Override public void enterSetPut(DstNewSQLParser.SetPutContext ctx) { // The children of the `set_put` should be 3: // `set.put key value_array` + Preconditions.checkState(parsedResult == null); Preconditions.checkState(ctx.children.size() == 3); - final String key = ctx.children.get(1); - executor = new DstSetExecutor(); - //optimize method name; - executor.setMethod("put"); - } - @Override - public void enterSetGet(DstNewSQLParser.SetGetContext ctx) { - executor = new DstSetExecutor(); - //optimize method name; - executor.setMethod("get"); + SetProtocol.PutRequest.Builder builder = SetProtocol.PutRequest.newBuilder(); + final String key = ctx.children.get(1).getText(); + builder.setKey(key); + final int valueSize = ctx.children.get(2).getChildCount(); + for (int i = 0; i < valueSize; ++i) { + builder.addValues(ctx.children.get(2).getChild(i).getText()); + } + SetProtocol.PutRequest request = builder.build(); + parsedResult = new DstParsedResult(RequestTypeEnum.SET_PUT, request); } @Override - public void enterKey(DstNewSQLParser.KeyContext ctx) { - executor.setKey(ctx.getText()); - } - - @Override - public void enterValueArray(DstNewSQLParser.ValueArrayContext ctx) { - executor.setValue(ctx.STRING()); + public void enterSetGet(DstNewSQLParser.SetGetContext ctx) { + Preconditions.checkState(parsedResult == null); + Preconditions.checkState(ctx.children.size() == 2); + SetProtocol.GetRequest.Builder builder = SetProtocol.GetRequest.newBuilder(); + builder.setKey(ctx.children.get(1).getText()); + parsedResult = new DstParsedResult(RequestTypeEnum.SET_GET, builder.build()); } @Override diff --git a/parser/src/main/java/org/dst/parser/DstParser.java b/parser/src/main/java/org/dst/parser/DstParser.java index 0dd817d8f..733abe1e8 100644 --- a/parser/src/main/java/org/dst/parser/DstParser.java +++ b/parser/src/main/java/org/dst/parser/DstParser.java @@ -3,7 +3,6 @@ import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.dst.parser.executor.AbstractExecutor; import org.dst.parser.generated.DstNewSQLLexer; import org.dst.parser.generated.DstNewSQLParser; import org.dst.parser.po.DstParsedResult; @@ -27,9 +26,7 @@ public DstParsedResult parse(String command) { ParseTreeWalker parseTreeWalker = new ParseTreeWalker(); parseTreeWalker.walk(dstNewSqlHandler, statement); - final AbstractExecutor executor = dstNewSqlHandler.getExecutor(); - Object request = executor.execute(); - return new DstParsedResult(executor.getRequestType(), request); + return dstNewSqlHandler.getParsedResult(); } } diff --git a/parser/src/main/java/org/dst/parser/executor/AbstractExecutor.java b/parser/src/main/java/org/dst/parser/executor/AbstractExecutor.java deleted file mode 100644 index 1b287e302..000000000 --- a/parser/src/main/java/org/dst/parser/executor/AbstractExecutor.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.dst.parser.executor; - -import org.dst.parser.util.CodeUtils; -import java.util.HashMap; -import java.util.Map; - -public abstract class AbstractExecutor { - - protected String key; - protected Object value; - protected String method; - protected String requestType; - - public Object execute() { - try { - String exec = "this." + method + "()"; - Map map = new HashMap(); - map.put("this", this); - return CodeUtils.executeExpression(exec, map); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public void setKey(String key) { - this.key = key; - } - - public void setValue(Object value) { - this.value = value; - } - - public void setMethod(String method) { - this.method = method; - } - - public String getRequestType() { - return requestType; - } -} diff --git a/parser/src/main/java/org/dst/parser/executor/DstSetExecutor.java b/parser/src/main/java/org/dst/parser/executor/DstSetExecutor.java deleted file mode 100644 index bdc80e36d..000000000 --- a/parser/src/main/java/org/dst/parser/executor/DstSetExecutor.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.dst.parser.executor; - -import org.antlr.v4.runtime.tree.TerminalNode; -import org.dst.common.exception.DstException; -import org.dst.rpc.protobuf.generated.SetProtocol; -import java.util.List; - -public class DstSetExecutor extends AbstractExecutor { - - - public SetProtocol.PutRequest put() { - this.requestType = "SetProtocol.PutRequest"; - SetProtocol.PutRequest.Builder request; - request = SetProtocol.PutRequest.newBuilder(); - request.setKey(key); - List node = (List) value; - node.forEach(terminalNode -> request.addValues(terminalNode.getText())); - return request.build(); - } - - public SetProtocol.GetRequest get() throws DstException { - this.requestType = "SetProtocol.GetRequest"; - SetProtocol.GetRequest request = - SetProtocol.GetRequest.newBuilder() - .setKey(key) - .build(); - return request; - } - -} diff --git a/parser/src/main/java/org/dst/parser/po/DstParsedResult.java b/parser/src/main/java/org/dst/parser/po/DstParsedResult.java index 3ca8e08fc..6bbd2d06b 100644 --- a/parser/src/main/java/org/dst/parser/po/DstParsedResult.java +++ b/parser/src/main/java/org/dst/parser/po/DstParsedResult.java @@ -1,22 +1,22 @@ package org.dst.parser.po; public class DstParsedResult { - public String requestType; + public RequestTypeEnum requestType; public Object request; public DstParsedResult() { } - public DstParsedResult(String requestType, Object request) { + public DstParsedResult(RequestTypeEnum requestType, Object request) { this.requestType = requestType; this.request = request; } - public String getRequestType() { + public RequestTypeEnum getRequestType() { return requestType; } - public void setRequestType(String requestType) { + public void setRequestType(RequestTypeEnum requestType) { this.requestType = requestType; } diff --git a/parser/src/main/java/org/dst/parser/po/RequestTypeEnum.java b/parser/src/main/java/org/dst/parser/po/RequestTypeEnum.java new file mode 100644 index 000000000..3eba7cf19 --- /dev/null +++ b/parser/src/main/java/org/dst/parser/po/RequestTypeEnum.java @@ -0,0 +1,9 @@ +package org.dst.parser.po; + +public enum RequestTypeEnum { + STR_PUT, + STR_GET, + SET_PUT, + SET_GET, + SET_DROP_BY_KEY, +} diff --git a/parser/src/main/java/org/dst/parser/util/CodeUtils.java b/parser/src/main/java/org/dst/parser/util/CodeUtils.java deleted file mode 100644 index fa5e4434a..000000000 --- a/parser/src/main/java/org/dst/parser/util/CodeUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.dst.parser.util; - -import org.apache.commons.jexl3.JexlContext; -import org.apache.commons.jexl3.JexlEngine; -import org.apache.commons.jexl3.JexlExpression; -import org.apache.commons.jexl3.MapContext; -import org.apache.commons.jexl3.internal.Engine; - -import java.util.Map; - -public class CodeUtils { - - private static JexlEngine jexlEngine = new Engine(); - - /** - * Convert String to java code ,then execute java code and return java code result. - * - * @param jexlExpression Code string. - * @param map key =StringExpression's parameterName, value=parameterValue - * @return The return value of the code we invoked. - */ - public static Object executeExpression(String jexlExpression, Map map) { - JexlExpression expression = jexlEngine.createExpression(jexlExpression); - JexlContext context = new MapContext(); - if (!map.isEmpty()) { - map.forEach(context::set); - } - return expression.evaluate(context); - } - -} diff --git a/parser/src/test/java/org/dst/parser/Test.java b/parser/src/test/java/org/dst/parser/Test.java index 3c158eb16..5559f18be 100644 --- a/parser/src/test/java/org/dst/parser/Test.java +++ b/parser/src/test/java/org/dst/parser/Test.java @@ -4,11 +4,6 @@ import org.dst.rpc.protobuf.generated.SetProtocol; import org.junit.Assert; -import java.util.HashMap; -import java.util.Map; - -import static org.dst.parser.util.CodeUtils.executeExpression; - public class Test { @@ -16,7 +11,7 @@ public class Test { public void testCmdParse() { //put DstParser dstParser = new DstParser(); - String command = "set.put \"k1\" \"v1\" \"v2\" \"v3\""; + String command = "set.put \"k1\" \"v1\" \"v2\" \"v3\" \"v4\""; DstParsedResult result = dstParser.parse(command); Assert.assertEquals(result.getRequest().getClass(), SetProtocol.PutRequest.class); Assert.assertEquals("\"k1\"", ((SetProtocol.PutRequest)result.getRequest()).getKey()); @@ -33,17 +28,6 @@ public void testCmdParse() { } catch (Exception e) { System.out.println(e.toString()); } - - } - - - @org.junit.Test - public void testExecuteExpression() { - Map map = new HashMap<>(); - map.put("alive", "coding every day"); - map.put("out", System.out); - String expression = "out.print(alive)"; - executeExpression(expression, map); } } From 92cf5f1a58221d42a005c81f1cf58ef4d38ef98a Mon Sep 17 00:00:00 2001 From: jovany-wang Date: Wed, 6 Nov 2019 21:45:34 +0800 Subject: [PATCH 3/6] Fix --- parser/src/main/java/org/dst/parser/DstNewSQL.g4 | 2 +- parser/src/main/java/org/dst/parser/DstNewSqlListener.java | 6 +++++- parser/src/test/java/org/dst/parser/Test.java | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 index 11c71f0e7..81bffe770 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 +++ b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 @@ -19,7 +19,7 @@ listStatement: ; setStatement: setPut | setGet | setDropByKey; setPut: 'set.put' key value_array; setGet:'set.get' key; -setDropByKey: 'set.dropByKey' key key; +setDropByKey: 'set.dropByKey' key; // dict concept dictStatement: ; diff --git a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java index 4adb70d8e..78f748a1b 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java +++ b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java @@ -48,7 +48,11 @@ public void enterSetGet(DstNewSQLParser.SetGetContext ctx) { @Override public void enterSetDropByKey(DstNewSQLParser.SetDropByKeyContext ctx) { - + Preconditions.checkState(parsedResult == null); + Preconditions.checkState(ctx.children.size() == 2); + SetProtocol.DropByKeyRequest.Builder builder = SetProtocol.DropByKeyRequest.newBuilder(); + builder.setKey(ctx.children.get(1).getText()); + parsedResult = new DstParsedResult(RequestTypeEnum.SET_DROP_BY_KEY, builder.build()); } } diff --git a/parser/src/test/java/org/dst/parser/Test.java b/parser/src/test/java/org/dst/parser/Test.java index 5559f18be..0c0f4d89c 100644 --- a/parser/src/test/java/org/dst/parser/Test.java +++ b/parser/src/test/java/org/dst/parser/Test.java @@ -15,7 +15,7 @@ public void testCmdParse() { DstParsedResult result = dstParser.parse(command); Assert.assertEquals(result.getRequest().getClass(), SetProtocol.PutRequest.class); Assert.assertEquals("\"k1\"", ((SetProtocol.PutRequest)result.getRequest()).getKey()); - Assert.assertEquals(3, ((SetProtocol.PutRequest)result.getRequest()).getValuesCount()); + Assert.assertEquals(4, ((SetProtocol.PutRequest)result.getRequest()).getValuesCount()); //get command = "set.get \"k1\""; From 052963072fcb23905e8e750becb7b938a8b026ab Mon Sep 17 00:00:00 2001 From: jovany-wang Date: Wed, 6 Nov 2019 21:46:32 +0800 Subject: [PATCH 4/6] Fix --- parser/src/main/java/org/dst/parser/DstNewSQL.g4 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 index 81bffe770..0d165bf8d 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSQL.g4 +++ b/parser/src/main/java/org/dst/parser/DstNewSQL.g4 @@ -17,7 +17,7 @@ listStatement: ; // set concept setStatement: setPut | setGet | setDropByKey; -setPut: 'set.put' key value_array; +setPut: 'set.put' key valueArray; setGet:'set.get' key; setDropByKey: 'set.dropByKey' key; @@ -27,7 +27,7 @@ dictStatement: ; // meta key: STRING; value: STRING; -value_array: (STRING)+; +valueArray: (STRING)+; STRING: '"'.*?'"' ; From 6953512604089d53fe355d9b8964d8e19a322725 Mon Sep 17 00:00:00 2001 From: jovany-wang Date: Wed, 6 Nov 2019 22:14:35 +0800 Subject: [PATCH 5/6] Address --- parser/src/main/java/org/dst/parser/DstNewSqlListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java index 78f748a1b..dd49e0ad4 100644 --- a/parser/src/main/java/org/dst/parser/DstNewSqlListener.java +++ b/parser/src/main/java/org/dst/parser/DstNewSqlListener.java @@ -13,7 +13,7 @@ public class DstNewSqlListener extends DstNewSQLBaseListener { private static final Logger LOGGER = LoggerFactory.getLogger(DstNewSqlListener.class); - DstParsedResult parsedResult = null; + private DstParsedResult parsedResult = null; public DstParsedResult getParsedResult() { return parsedResult; From 091fcfb93e49188e7b750d74653f5bed7f9b6353 Mon Sep 17 00:00:00 2001 From: jovany-wang Date: Wed, 6 Nov 2019 22:43:49 +0800 Subject: [PATCH 6/6] incr timeout --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 39b0975fe..656197a98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ matrix: - os: linux dist: xenial script: - - travis_wait 10 mvn test + - travis_wait 20 mvn test # for linux: This job to check code format. - os: linux