Skip to content

Commit

Permalink
Separate GetAll、GetOne、GetRange operation in parser (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
ou-taku14 authored and jovany-wang committed Dec 10, 2019
1 parent 738bda3 commit 3d1c8bd
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions parser/src/main/java/com/distkv/dst/parser/DstNewSqlListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,35 +99,42 @@ public void enterListRput(DstNewSQLParser.ListRputContext ctx) {
}

@Override
public void enterListGet(DstNewSQLParser.ListGetContext ctx) {
public void enterListGetAll(DstNewSQLParser.ListGetAllContext ctx) {
Preconditions.checkState(parsedResult == null);
Preconditions.checkState(ctx.children.size() == 1);

ListProtocol.GetRequest.Builder getRequestBuilder = ListProtocol.GetRequest.newBuilder();
getRequestBuilder.setKey(ctx.getChild(0).getText());
getRequestBuilder.setType(ListProtocol.GetType.GET_ALL);

parsedResult = new DstParsedResult(RequestTypeEnum.LIST_GET, getRequestBuilder.build());
}

@Override
public void enterListGetOne(DstNewSQLParser.ListGetOneContext ctx) {
Preconditions.checkState(parsedResult == null);
Preconditions.checkState(ctx.children.size() == 2);
final ParseTree listGetArgumentsParseTree = ctx.children.get(1);
final int numArguments = listGetArgumentsParseTree.getChildCount();

ListProtocol.GetRequest.Builder builder = ListProtocol.GetRequest.newBuilder();
final String key = listGetArgumentsParseTree.getChild(0).getText();
builder.setKey(key);
if (1 == numArguments) {
// GET_ALL
Preconditions.checkState(listGetArgumentsParseTree.getChildCount() == 1);
builder.setType(ListProtocol.GetType.GET_ALL);
} else if (2 == numArguments) {
// GET_ONE
Preconditions.checkState(listGetArgumentsParseTree.getChildCount() == 2);
builder.setType(ListProtocol.GetType.GET_ONE);
builder.setIndex(Integer.valueOf(listGetArgumentsParseTree.getChild(1).getText()));
} else if (3 == numArguments) {
// GET_RANGE
Preconditions.checkState(listGetArgumentsParseTree.getChildCount() == 3);
builder.setType(ListProtocol.GetType.GET_RANGE);
builder.setFrom(Integer.valueOf(listGetArgumentsParseTree.getChild(1).getText()));
builder.setEnd(Integer.valueOf(listGetArgumentsParseTree.getChild(2).getText()));
} else {
throw new RuntimeException("Failed to parser the command.");
}
ListProtocol.GetRequest.Builder getRequestBuilder = ListProtocol.GetRequest.newBuilder();
getRequestBuilder.setKey(ctx.getChild(0).getText());
getRequestBuilder.setIndex(Integer.valueOf(ctx.getChild(1).getText()));
getRequestBuilder.setType(ListProtocol.GetType.GET_ONE);

parsedResult = new DstParsedResult(RequestTypeEnum.LIST_GET, getRequestBuilder.build());
}

@Override
public void enterListGetRange(DstNewSQLParser.ListGetRangeContext ctx) {
Preconditions.checkState(parsedResult == null);
Preconditions.checkState(ctx.children.size() == 3);

ListProtocol.GetRequest.Builder getRequestBuilder = ListProtocol.GetRequest.newBuilder();
getRequestBuilder.setKey(ctx.getChild(0).getText());
getRequestBuilder.setFrom(Integer.valueOf(ctx.getChild(1).getText()));
getRequestBuilder.setEnd(Integer.valueOf(ctx.getChild(2).getText()));
getRequestBuilder.setType(ListProtocol.GetType.GET_RANGE);

parsedResult = new DstParsedResult(RequestTypeEnum.LIST_GET, builder.build());
parsedResult = new DstParsedResult(RequestTypeEnum.LIST_GET, getRequestBuilder.build());
}

@Override
Expand Down

0 comments on commit 3d1c8bd

Please sign in to comment.