Skip to content

Commit

Permalink
Add deactive namespace support (#521)
Browse files Browse the repository at this point in the history
* fix

* fix
  • Loading branch information
kairbon authored Feb 18, 2020
1 parent 7ae7e46 commit df66c5b
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public void activeNamespace(String namespace) {
namespaceInterceptor.active(namespace);
}

@Override
public void deactiveNamespace() {
namespaceInterceptor.deactive();
}

@Override
public String getActivedNamespace() {
return namespaceInterceptor.getNamespace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public interface DistkvAsyncClient {
*/
void activeNamespace(String namespace);

/**
* Deactivate the already achieved Namespace
*/
void deactiveNamespace();

String getActivedNamespace();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public void activeNamespace(String namespace) {
asyncClient.activeNamespace(namespace);
}

@Override
public void deactiveNamespace() {
asyncClient.deactiveNamespace();
}

@Override
public String getActivedNamespace() {
return asyncClient.getActivedNamespace();
Expand Down
5 changes: 5 additions & 0 deletions client/src/main/java/com/distkv/client/DistkvClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public interface DistkvClient {
*/
void activeNamespace(String namespace);

/**
* Deactivate the already achieved Namespace
*/
void deactiveNamespace();

/**
* Get actived namespace
* @return the actived namespace's name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,13 @@ public static String activeNamespace(
return STATUS_OK;
}

public static String deactiveNamespace(
DistkvClient distkvClient, DistkvParsedResult parsedResult) {
if (distkvClient.getActivedNamespace() == null) {
return "Namespace has not been activated";
}
distkvClient.deactiveNamespace();
return STATUS_OK;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public String execute(DistkvParsedResult parsedResult) {
return null;
case ACTIVE_NAMESPACE:
return CommandExecutorHandler.activeNamespace(distkvClient, parsedResult);
case DEACTIVE_NAMESPACE:
return CommandExecutorHandler.deactiveNamespace(distkvClient, parsedResult);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public void active(String namespace) {
this.namespace = namespace;
}

public void deactive() {
this.namespace = null;
}

}
3 changes: 2 additions & 1 deletion parser/src/main/java/com/distkv/parser/DistkvNewSQL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ statement: (conceptStatement) EOF;
conceptStatement: basicOperationsStatement | strStatement | listStatement | setStatement | dictStatement | slistStatement;

// basic operations
basicOperationsStatement: exit | activeNamespace;
basicOperationsStatement: exit | activeNamespace | deactiveNamespace;
exit: 'exit';
activeNamespace: 'active namespace' namespace;
deactiveNamespace: 'deactive namespace';

// str concept
strStatement: strPut | strGet | strDrop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public void enterActiveNamespace(DistkvNewSQLParser.ActiveNamespaceContext ctx)
parsedResult = new DistkvParsedResult(RequestType.ACTIVE_NAMESPACE, request);
}

@Override
public void enterDeactiveNamespace(DistkvNewSQLParser.DeactiveNamespaceContext ctx) {
Preconditions.checkState(parsedResult == null);
Preconditions.checkState(ctx.children.size() == 1);
parsedResult = new DistkvParsedResult(RequestType.DEACTIVE_NAMESPACE, null);
}

@Override
public void enterStrPut(DistkvNewSQLParser.StrPutContext ctx) {
Preconditions.checkState(parsedResult == null);
Expand Down
2 changes: 2 additions & 0 deletions rpc/src/main/java/com/distkv/rpc/protobuf/distkv_pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ enum RequestType {

ACTIVE_NAMESPACE = 2;

DEACTIVE_NAMESPACE = 3;

// string concept
STR_PUT = 101;
STR_GET = 102;
Expand Down
32 changes: 32 additions & 0 deletions test/src/test/java/com/distkv/client/NamespaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,36 @@ public void testNamespaceDisabled()
Assert.assertEquals(value, "v2");
}


@Test
public void testNamespaceDeactive()
throws ExecutionException, InterruptedException, InvalidProtocolBufferException {
DistkvAsyncClient client1 = newAsyncDistkvClient();
DistkvAsyncClient client2 = newAsyncDistkvClient();

client1.activeNamespace("a");

CompletableFuture<DistkvProtocol.DistkvResponse> putFuture =
client1.strs().put("k1", "v1");
Assert.assertEquals(CommonProtocol.Status.OK, putFuture.get().getStatus());
putFuture = client2.strs().put("k1", "v2");
Assert.assertEquals(CommonProtocol.Status.OK, putFuture.get().getStatus());

CompletableFuture<DistkvProtocol.DistkvResponse> getFuture = client1.strs().get("k1");
String value = getFuture.get()
.getResponse().unpack(StringProtocol.StrGetResponse.class).getValue();
Assert.assertEquals(value, "v1");

getFuture = client2.strs().get("k1");
value = getFuture.get()
.getResponse().unpack(StringProtocol.StrGetResponse.class).getValue();
Assert.assertEquals(value, "v2");

client1.deactiveNamespace();
CompletableFuture<DistkvProtocol.DistkvResponse> getFuture1 = client1.strs().get("k1");
String value1 = getFuture.get()
.getResponse().unpack(StringProtocol.StrGetResponse.class).getValue();
Assert.assertEquals(value, "v2");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ public void testMain() {
Assert.assertEquals(
CommandExecutorHandler.activeNamespace(distkvClient, distKVParsedResult), STATUS_OK);

// Test Deactive namespace
command = "deactive namespace";
distKVParsedResult = distkvParser.parse(command);
Assert.assertEquals(
CommandExecutorHandler.deactiveNamespace(distkvClient, distKVParsedResult), STATUS_OK);

distkvClient.disconnect();
}

Expand Down

0 comments on commit df66c5b

Please sign in to comment.