Skip to content

Commit

Permalink
Remove Request.nullArg(), part two
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Aug 16, 2024
1 parent 2c282c8 commit 6cf6c94
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 43 deletions.
13 changes: 7 additions & 6 deletions src/main/java/io/vertx/redis/client/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.util.Map;

/**
* Builder for REDIS requests that will be encoded according to the RESP protocol was introduced in Redis 1.2.
* Which became the standard way for talking with the Redis server in Redis 2.0.
* Builder for Redis requests that will be encoded according to the RESP protocol.
* RESP was introduced in Redis 1.2 and became the standard way for talking with the Redis server in Redis 2.0.
* <p>
* Redis <a href="https://redis.io/topics/protocol">protocol documentation</a> states:
*
Expand All @@ -45,7 +45,7 @@
public interface Request {

/**
* Creates a new request command. Requests can be reused to avoid GC.
* Creates a new request. Requests can be reused to avoid GC.
*
* @param command the command to use
* @return a new request instance
Expand All @@ -55,9 +55,10 @@ static Request cmd(Command command) {
}

/**
* Creates a new request command only with simple types: {@link Number}, {@link Boolean}, {@link String},
* {@code byte[]} or {@link Buffer}. This factory reduces the GC as it allocates the arguments array with
* the right size.
* Creates a new request only with simple types of arguments: {@link Number}, {@link Boolean},
* {@link String}, {@code byte[]} or {@link Buffer}. Other kinds of arguments throw an exception.
* <p>
* This factory reduces the GC pressure as it allocates the arguments array with the right size.
*
* @param command the command to use
* @param args the fixed list of arguments
Expand Down
60 changes: 23 additions & 37 deletions src/main/java/io/vertx/redis/client/impl/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public final class RequestImpl implements Request {

private static final byte[] EMPTY_BULK = "$0\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1);
private static final byte[] EMPTY_BYTES = new byte[0];
private static final byte[] NULL_BULK = "$4\r\nnull\r\n".getBytes(StandardCharsets.ISO_8859_1);
private static final byte[] EOL = "\r\n".getBytes(StandardCharsets.ISO_8859_1);
private static final byte[] TRUE = new byte[]{'t'};
private static final byte[] FALSE = new byte[]{'f'};
Expand All @@ -48,39 +47,31 @@ public RequestImpl(Command cmd) {

public RequestImpl(Command cmd, Object[] args) {
this.cmd = (CommandImpl) cmd;
if (args != null) {
final int len = args.length;
if (len > 0) {
for (int i = 0; i < args.length; i++) {
final Object o = args[i];
if (o != null) {
if (o instanceof Number) {
args[i] = o.toString().getBytes(StandardCharsets.US_ASCII);
continue;
}
if (o instanceof Boolean) {
args[i] = ((Boolean) o) ? TRUE : FALSE;
continue;
}
if (o instanceof String) {
args[i] = ((String) o).getBytes(StandardCharsets.UTF_8);
continue;
}
if (o instanceof byte[]) {
continue;
}
if (o instanceof Buffer) {
args[i] = ((Buffer) o).getBytes();
continue;
}
throw new IllegalArgumentException("Unsupported argument type: " + o.getClass());
}
}
this.args = (List) Arrays.asList(args);
return;

if (args == null) {
this.args = Collections.emptyList();
return;
}

for (int i = 0; i < args.length; i++) {
final Object o = args[i];
if (o == null) {
throw new IllegalArgumentException("Null argument at index " + i);
} else if (o instanceof Number) {
args[i] = o.toString().getBytes(StandardCharsets.US_ASCII);
} else if (o instanceof Boolean) {
args[i] = ((Boolean) o) ? TRUE : FALSE;
} else if (o instanceof String) {
args[i] = ((String) o).getBytes(StandardCharsets.UTF_8);
} else if (o instanceof byte[]) {
// already OK
} else if (o instanceof Buffer) {
args[i] = ((Buffer) o).getBytes();
} else {
throw new IllegalArgumentException("Unsupported argument type at index " + i + ": " + o.getClass());
}
}
this.args = Collections.emptyList();
this.args = (List) Arrays.asList(args);
}

@Override
Expand Down Expand Up @@ -146,11 +137,6 @@ Buffer encode(Buffer buffer) {
.appendBytes(cmd.getBytes());

for (final byte[] arg : args) {
if (arg == null) {
buffer.appendBytes(NULL_BULK);
continue;
}

if (arg.length == 0) {
buffer.appendBytes(EMPTY_BULK);
continue;
Expand Down

0 comments on commit 6cf6c94

Please sign in to comment.