Skip to content

Commit

Permalink
feature: support setnx protocol (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes authored Apr 6, 2024
1 parent 43e0662 commit 4e71ba2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: "Test, Check style, Check PMD, Check license with Maven and Java8"
if: matrix.java == '8'
run: |
./mvnw -T 4C clean test
./mvnw -T 4C clean test && sh ./tools/check_format.sh
- name: "Test with Maven and Java${{ matrix.java }}"
if: matrix.java != '8'
run: |
Expand Down
45 changes: 43 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<main.user.dir>${user.dir}</main.user.dir>
<mock-jedis.version>0.3.1</mock-jedis.version>
<junit-jupiter.version>5.8.2</junit-jupiter.version>
<commons-cli.version>1.6.0</commons-cli.version>
<log.version>1.2.9</log.version>
<p3c-pmd.version>1.3.6</p3c-pmd.version>
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -138,6 +140,45 @@
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<!-- PMD-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven-pmd-plugin.version}</version>
<configuration>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
<minimumPriority>2</minimumPriority>
<printFailingErrors>true</printFailingErrors>
<rulesets>
<ruleset>rulesets/java/ali-comment.xml</ruleset>
<ruleset>rulesets/java/ali-concurrent.xml</ruleset>
<ruleset>rulesets/java/ali-constant.xml</ruleset>
<ruleset>rulesets/java/ali-exception.xml</ruleset>
<ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>
<ruleset>rulesets/java/ali-naming.xml</ruleset>
<ruleset>rulesets/java/ali-oop.xml</ruleset>
<ruleset>rulesets/java/ali-orm.xml</ruleset>
<ruleset>rulesets/java/ali-other.xml</ruleset>
<ruleset>rulesets/java/ali-set.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<id>pmd-check</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId>
<version>${p3c-pmd.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ private RedisRequest<?> convert2RedisRequest(List<String> params) {
return new GetRequest(params.get(1));
case "command":
return new CommandRequest();
case "setnx":
params.add("nx");
return new SetRequest(params);
case "set":
return new SetRequest(params.get(1), params.get(2), params);
return new SetRequest(params);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public void onFailure(AerospikeException ae) {
client.put(null, new WriteListener() {
@Override
public void onSuccess(Key key) {
setRequest.setResponse("OK".getBytes(StandardCharsets.UTF_8));
if (setRequest.getOriginalCommand().contains("nx")) {
setRequest.setResponse("1".getBytes(StandardCharsets.UTF_8));
} else {
setRequest.setResponse("OK".getBytes(StandardCharsets.UTF_8));
}
ctx.writeAndFlush(redisRequest.getResponse());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/redis2asp/protocol/RedisResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface RedisResponse<T> extends RemotingCommand {
byte[] CRLF = new byte[] {'\r', '\n'};

T data();

void setData(byte[] data);
void write(ByteBuf out) throws IOException;

default ProtocolCode getProtocolCode() {
Expand Down
54 changes: 41 additions & 13 deletions src/main/java/org/redis2asp/protocol/request/SetRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,28 @@
import org.redis2asp.protocol.RedisRequest;
import org.redis2asp.protocol.RedisResponse;
import org.redis2asp.protocol.response.BulkResponse;
import org.redis2asp.protocol.response.IntegerResponse;

public class SetRequest implements RedisRequest<byte[]> {

final String key;
final String originalCommand;

final String value;
final String key;

TtlType ttlType;
final String value;

Long ttl;
TtlType ttlType;

Operate operate;
Long ttl;

BulkResponse response = new BulkResponse();
Operate operate;

public SetRequest(String key, String value, List<String> params) {
this.key = key;
this.value = value;
RedisResponse<byte[]> response;

public SetRequest(List<String> params) {
this.originalCommand = params.get(0);
this.key = params.get(1);
this.value = params.get(2);
if (params.contains("nx")) {
this.operate = Operate.NX;
} else if (params.contains("xx")) {
Expand All @@ -51,6 +55,11 @@ public SetRequest(String key, String value, List<String> params) {
this.ttlType = TtlType.PX;
this.ttl = Long.parseLong(params.get(params.indexOf("px") + 1));
}
if (originalCommand.contains("nx")) {
this.response = new IntegerResponse();
} else {
this.response = new BulkResponse();
}
}

public String getKey() {
Expand Down Expand Up @@ -83,17 +92,36 @@ public RedisResponse<byte[]> getResponse() {
return response;
}

public String getOriginalCommand() {
return originalCommand;
}

public enum TtlType {
EX, PX
/**
* EX seconds -- Set the specified expire time, in seconds.
*/
EX,
/**
* PX milliseconds -- Set the specified expire time, in milliseconds.
*/
PX
}

public enum Operate {
NX, XX
/**
* NX -- Only set the key if it does not already exist.
*/
NX,
/**
* XX -- Only set the key if it already exist.
*/
XX
}

@Override
public String toString() {
return "SetRequest{" + "key='" + key + '\'' + ", value='" + value + '\'' + ", ttlType=" + ttlType + ", ttl="
+ ttl + ", operate=" + operate + ", response=" + response + '}';
return "SetRequest{" + "originalCommand='" + originalCommand + '\'' + ", key='" + key + '\'' + ", value='"
+ value + '\'' + ", ttlType=" + ttlType + ", ttl=" + ttl + ", operate=" + operate + ", response="
+ response + '}';
}
}
19 changes: 14 additions & 5 deletions src/main/java/org/redis2asp/protocol/response/IntegerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,37 @@
package org.redis2asp.protocol.response;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import io.netty.buffer.ByteBuf;
import org.redis2asp.protocol.RedisResponse;

public class IntegerResponse implements RedisResponse<Integer> {
public class IntegerResponse implements RedisResponse<byte[]> {

private static final char MARKER = ':';

private final int data;
private byte[] data;

public IntegerResponse(int data) {
this.data = data;
this.data = String.valueOf(data).getBytes(StandardCharsets.UTF_8);
}

public IntegerResponse() {
}

@Override
public Integer data() {
public byte[] data() {
return this.data;
}

@Override
public void setData(byte[] data) {
this.data = data;
}

@Override
public void write(ByteBuf out) throws IOException {
out.writeByte(MARKER);
out.writeBytes(String.valueOf(data).getBytes());
out.writeBytes(data == null ? "0".getBytes(StandardCharsets.UTF_8) : data);
out.writeBytes(CRLF);
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/redis2asp/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public void testSetNxNilAsp() {
Assertions.assertEquals(result, "OK");
result = jedis.set(key, "b", SetParams.setParams().nx());
Assertions.assertNull(result);
key = String.valueOf(ThreadLocalRandom.current().nextInt(50000));
result = String.valueOf(jedis.setnx(key, "b"));
Assertions.assertEquals(result, "1");
result = String.valueOf(jedis.setnx(key, "b"));
Assertions.assertEquals(result, "0");
}
}

Expand Down

0 comments on commit 4e71ba2

Please sign in to comment.