Skip to content

Commit

Permalink
Minor fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Oct 24, 2023
1 parent 917dd0e commit 9e62324
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javababushka.benchmarks.utils.ConnectionSettings;
import javababushka.client.RedisClient;
import org.apache.commons.lang3.tuple.MutablePair;
Expand Down Expand Up @@ -141,7 +142,7 @@ public String get(String key) {

makeRedisRequest(getStringRequest.toByteArray());
ResponseOuterClass.Response response = receiveRedisResponse();
return response.toString();
return response == null ? null : response.toString();
}

@Override
Expand All @@ -159,7 +160,7 @@ private static Pair<Long, Integer> decodeVarint(byte[] buffer, int pos) throws E
long result = 0;
while (true) {
byte b = buffer[pos];
result |= (b & 0x7F) << shift;
result |= (long) (b & 0x7F) << shift;
pos += 1;
if ((b & 0x80) == 0) {
result &= mask;
Expand All @@ -175,23 +176,22 @@ private static Pair<Long, Integer> decodeVarint(byte[] buffer, int pos) throws E

private static ResponseOuterClass.Response decodeMessage(byte[] buffer) throws Exception {
Pair<Long, Integer> pair = decodeVarint(buffer, 0);
int startIdx = (int) pair.getRight();
int startIdx = pair.getRight();
byte[] responseBytes =
Arrays.copyOfRange(buffer, startIdx, startIdx + (int) (long) pair.getLeft());
ResponseOuterClass.Response response = ResponseOuterClass.Response.parseFrom(responseBytes);
return response;
return ResponseOuterClass.Response.parseFrom(responseBytes);
}

private static Byte[] varintBytes(int value) {
ArrayList<Byte> output = new ArrayList();
List<Byte> output = new ArrayList<>();
int bits = value & 0x7F;
value >>= 7;
while (value > 0) {
output.add(new Byte((byte) (0x80 | bits)));
output.add((byte) (0x80 | bits));
bits = value & 0x7F;
value >>= 7;
}
output.add(new Byte((byte) bits));
output.add((byte) bits);
Byte[] arr = new Byte[] {};
return output.toArray(arr);
}
Expand All @@ -200,7 +200,9 @@ private void makeRedisRequest(byte[] request) {
Byte[] varint = varintBytes(request.length);

// System.out.println("Request: \n" + request.toString());
ByteBuffer buffer = ByteBuffer.allocate(1024);
// javadocs: https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#putInt%28int%29
// BufferOverflowException - If there are fewer than four bytes remaining in this buffer
ByteBuffer buffer = ByteBuffer.allocate(request.length + 4);
buffer.clear();
for (Byte b : varint) {
buffer.put(b);
Expand All @@ -220,6 +222,7 @@ private void makeRedisRequest(byte[] request) {
}

private ResponseOuterClass.Response receiveRedisResponse() {
// TODO what if buffer is too small? re-allocate?
ByteBuffer readBuffer = ByteBuffer.allocate(1024);

int timeout = 0;
Expand All @@ -233,7 +236,7 @@ private ResponseOuterClass.Response receiveRedisResponse() {
throw new RuntimeException("Max timeout reached");
}

bytesRead = channel.read(readBuffer);
bytesRead += channel.read(readBuffer);
Thread.sleep(TIMEOUT_INTERVAL);
}
}
Expand All @@ -247,7 +250,7 @@ private ResponseOuterClass.Response receiveRedisResponse() {
try {
response = decodeMessage(bytes);
} catch (Exception e) {
e.printStackTrace();
// e.printStackTrace();
}
return response;
}
Expand Down Expand Up @@ -305,7 +308,7 @@ private ResponseOuterClass.Response makeRedisRequest(
try {
response = decodeMessage(responseBuffer);
} catch (Exception e) {
e.printStackTrace();
// e.printStackTrace();
}
return response;
}
Expand Down

0 comments on commit 9e62324

Please sign in to comment.