Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine parser executor #237

Merged
merged 6 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dst-common</artifactId>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
senyer marked this conversation as resolved.
Show resolved Hide resolved
</dependencies>
</project>
4 changes: 2 additions & 2 deletions parser/src/main/java/org/dst/parser/DstNewSQL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ listStatement: ;
setStatement: setPut | setGet | setDropByKey;
setPut: 'set.put' key valueArray;
setGet:'set.get' key;
setDropByKey: 'set.dropByKey' key key;
setDropByKey: 'set.dropByKey' key;

// dict concept
dictStatement: ;

// meta
key: STRING;
value: STRING;
valueArray :(STRING)+;
valueArray: (STRING)+;

STRING: '"'.*?'"' ;

Expand Down
54 changes: 31 additions & 23 deletions parser/src/main/java/org/dst/parser/DstNewSqlListener.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
package org.dst.parser;

import org.dst.parser.executor.AbstractExecutor;
import org.dst.parser.executor.DstSetExecutor;
import com.google.common.base.Preconditions;
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;

public class DstNewSqlListener extends DstNewSQLBaseListener {

private static final Logger LOGGER = LoggerFactory.getLogger(DstNewSqlListener.class);

private AbstractExecutor executor;
private DstParsedResult parsedResult = null;

public AbstractExecutor getExecutor() {
return executor;
public DstParsedResult getParsedResult() {
return parsedResult;
}

@Override
public void enterSetPut(DstNewSQLParser.SetPutContext ctx) {

executor = new DstSetExecutor();
//optimize method name;
executor.setMethod("put");
// The children of the `set_put` should be 3:
// `set.put key value_array`
Preconditions.checkState(parsedResult == null);
Preconditions.checkState(ctx.children.size() == 3);

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 enterSetGet(DstNewSQLParser.SetGetContext ctx) {
executor = new DstSetExecutor();
//optimize method name;
executor.setMethod("get");
}

@Override
public void enterKey(DstNewSQLParser.KeyContext ctx) {
executor.setKey(ctx.getText());
}

@Override
public void enterValueArray(DstNewSQLParser.ValueArrayContext ctx) {
executor.setValue(ctx.STRING());
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
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());
}

}
5 changes: 1 addition & 4 deletions parser/src/main/java/org/dst/parser/DstParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

}
41 changes: 0 additions & 41 deletions parser/src/main/java/org/dst/parser/executor/AbstractExecutor.java

This file was deleted.

30 changes: 0 additions & 30 deletions parser/src/main/java/org/dst/parser/executor/DstSetExecutor.java

This file was deleted.

8 changes: 4 additions & 4 deletions parser/src/main/java/org/dst/parser/po/DstParsedResult.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions parser/src/main/java/org/dst/parser/po/RequestTypeEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.dst.parser.po;

public enum RequestTypeEnum {
STR_PUT,
STR_GET,
SET_PUT,
SET_GET,
SET_DROP_BY_KEY,
}
31 changes: 0 additions & 31 deletions parser/src/main/java/org/dst/parser/util/CodeUtils.java

This file was deleted.

20 changes: 2 additions & 18 deletions parser/src/test/java/org/dst/parser/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@
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 {


@org.junit.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());
Assert.assertEquals(3, ((SetProtocol.PutRequest)result.getRequest()).getValuesCount());
Assert.assertEquals(4, ((SetProtocol.PutRequest)result.getRequest()).getValuesCount());

//get
command = "set.get \"k1\"";
Expand All @@ -33,17 +28,6 @@ public void testCmdParse() {
} catch (Exception e) {
System.out.println(e.toString());
}

}


@org.junit.Test
public void testExecuteExpression() {
Map<String, Object> map = new HashMap<>();
map.put("alive", "coding every day");
map.put("out", System.out);
String expression = "out.print(alive)";
executeExpression(expression, map);
}

}