Skip to content

Commit

Permalink
Refactor & fix #452 (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
inponomarev authored Apr 23, 2024
1 parent 4a199d9 commit 2783e03
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public StreamId decrement() throws WrongStreamKeyException {

@Override
public int compareTo(StreamId other) {
if (other == null) {
return 1; // occurs in blocking (when nothing was added)
}

int firstPartComparison = compareUnsigned(firstPart, other.firstPart);
return firstPartComparison != 0
? firstPartComparison
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.storage.OperationExecutorState;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RedisCommand(value = "cluster", transactional = false)
Expand All @@ -33,17 +31,18 @@ public Slice execute() {
}
final String subcommand = params.get(0).toString();
if ("slots".equalsIgnoreCase(subcommand)) {
return Response.array(Collections.singletonList(
Response.array(Arrays.asList(
return Response.array(
Response.array(
Response.integer(0),
Response.integer(16383),
Response.array(Arrays.asList(
Response.array(
Response.bulkString(Slice.create(state.getHost())),
Response.integer(state.getPort()),
Response.bulkString(Slice.create(NODE_ID)),
Response.EMPTY_ARRAY
))
))));
)
)
);
} else if ("nodes".equalsIgnoreCase(subcommand)) {
return Response.bulkString(
Slice.create(String.format("%s %s:%d@%d myself,master - 0 1691313236000 1 connected 0-16383",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.fppt.jedismock.operations.hashes;

import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
import com.github.fppt.jedismock.operations.RedisCommand;
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -23,22 +23,17 @@ protected Slice response() {

Map<Slice, Slice> fieldAndValueMap = base().getFieldsAndValues(hash);

if(fieldAndValueMap == null) {
if (fieldAndValueMap == null) {
fieldAndValueMap = new HashMap<>();
}
int arraySize = fieldAndValueMap.size() * 2;
Slice [] fieldAndValueList = new Slice[arraySize];

int currentIndex = arraySize - 1;
for (Map.Entry<Slice, Slice> entry: fieldAndValueMap.entrySet()){
fieldAndValueList[currentIndex] = Response.bulkString(entry.getValue());
currentIndex--;
List<Slice> output = new ArrayList<>();

fieldAndValueList[currentIndex] = Response.bulkString(entry.getKey());
currentIndex--;
}
fieldAndValueMap.forEach((key, value) -> {
output.add(Response.bulkString(key));
output.add(Response.bulkString(value));
});

List<Slice> values = Arrays.asList(fieldAndValueList);
return Response.array(values);
return Response.array(output);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.fppt.jedismock.operations.hashes;

import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
import com.github.fppt.jedismock.operations.RedisCommand;
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RedisCommand("hkeys")
public class HKeys extends AbstractRedisOperation {
Expand All @@ -22,16 +22,10 @@ protected Slice response() {

Map<Slice, Slice> fieldAndValueMap = base().getFieldsAndValues(hash);

int arraySize = fieldAndValueMap.size();
Slice [] fkeys = new Slice[arraySize];

int currentIndex = 0;
for (Map.Entry<Slice, Slice> entry: fieldAndValueMap.entrySet()){
fkeys[currentIndex] = Response.bulkString(entry.getKey());
currentIndex++;
}

List<Slice> values = Arrays.asList(fkeys);
return Response.array(values);
return Response.array(
fieldAndValueMap.keySet().stream()
.map(Response::bulkString)
.collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ protected Slice response() {
ArrayList<Slice> result = new ArrayList<>();
Slice hash = params().get(0);

for(int i = 1; i < params().size(); i ++){
for (int i = 1; i < params().size(); i++) {
Slice field = params().get(i);
Slice value = base().getSlice(hash, field);

if(value == null){
if (value == null) {
result.add(Response.NULL);
} else {
result.add(Response.bulkString(value));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.fppt.jedismock.operations.hashes;

import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
import com.github.fppt.jedismock.operations.RedisCommand;
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RedisCommand("hvals")
public class HVals extends AbstractRedisOperation {
Expand All @@ -21,16 +21,11 @@ protected Slice response() {
Slice hash = params().get(0);

Map<Slice, Slice> fieldAndValueMap = base().getFieldsAndValues(hash);
int arraySize = fieldAndValueMap.size();
Slice [] fvals = new Slice[arraySize];

int currentIndex = 0;
for (Map.Entry<Slice, Slice> entry: fieldAndValueMap.entrySet()){
fvals[currentIndex] = Response.bulkString(entry.getValue());
currentIndex++;
}

List<Slice> values = Arrays.asList(fvals);
return Response.array(values);
return Response.array(
fieldAndValueMap.values().stream()
.map(Response::bulkString)
.collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.github.fppt.jedismock.operations.keys;

import com.github.fppt.jedismock.Utils;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
import com.github.fppt.jedismock.operations.RedisCommand;
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.ArrayList;
Expand All @@ -22,7 +22,7 @@ protected Slice response() {

base().keys().forEach(keyData -> {
String key = new String(keyData.data());
if(key.matches(regex)){
if (key.matches(regex)) {
matchingKeys.add(Response.bulkString(keyData));
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.github.fppt.jedismock.operations.keys;

import com.github.fppt.jedismock.Utils;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
import com.github.fppt.jedismock.operations.RedisCommand;
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -52,9 +50,10 @@ protected Slice response() {
cursor = CURSOR_START;
}

List<Slice> response = new ArrayList<>();
Collections.addAll(response, Response.bulkString(Slice.create(String.valueOf(cursor))), Response.array(matchingValues));
return Response.array(response);
return Response.array(
Response.bulkString(Slice.create(String.valueOf(cursor))),
Response.array(matchingValues)
);
}

private static Optional<Slice> extractParameter(List<Slice> params, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.storage.OperationExecutorState;

import java.util.Arrays;
import java.util.List;

@RedisCommand("blpop")
Expand All @@ -18,6 +17,6 @@ class BLPop extends BPop {
@Override
public Slice popper(List<Slice> params) {
Slice result = new LPop(base(), params).execute();
return Response.array(Arrays.asList(Response.bulkString(params.get(0)), result));
return Response.array(Response.bulkString(params.get(0)), result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.storage.OperationExecutorState;

import java.util.Arrays;
import java.util.List;

@RedisCommand("brpop")
Expand All @@ -18,6 +17,6 @@ class BRPop extends BPop {
@Override
public Slice popper(List<Slice> params) {
Slice result = new RPop(base(), params).execute();
return Response.array(Arrays.asList(Response.bulkString(params.get(0)), result));
return Response.array(Response.bulkString(params.get(0)), result);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.github.fppt.jedismock.operations.server;

import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.operations.AbstractRedisOperation;
import com.github.fppt.jedismock.operations.RedisCommand;
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.datastructures.Slice;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.Arrays;
import java.util.List;

@RedisCommand("time")
Expand All @@ -22,8 +21,9 @@ protected Slice response() {
long time = System.currentTimeMillis();
long seconds = time / 1000L;
long microseconds = (time % 1000L) * 1000L;
return Response.array(Arrays.asList(
return Response.array(
Response.bulkString(Slice.create(Long.toString(seconds))),
Response.bulkString(Slice.create(Long.toString(microseconds)))));
Response.bulkString(Slice.create(Long.toString(microseconds)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Slice response() {
if (number == 1) {
int index = ThreadLocalRandom.current().nextInt(list.size());
return params().size() > 1 ?
Response.array(Collections.singletonList(Response.bulkString(list.get(index)))) :
Response.array(Response.bulkString(list.get(index))) :
Response.bulkString(list.get(index));
} else if (number > 1) {
Collections.shuffle(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.storage.OperationExecutorState;

import java.util.Arrays;
import java.util.List;

import static com.github.fppt.jedismock.Utils.toNanoTimeout;
Expand All @@ -21,6 +20,6 @@ public class BZPopMax extends BZPop {
@Override
protected Slice popper(List<Slice> params) {
List<Slice> result = new ZPop(base(), params, true).pop();
return Response.array(Arrays.asList(Response.bulkString(params.get(0)), result.get(0), result.get(1)));
return Response.array(Response.bulkString(params.get(0)), result.get(0), result.get(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.storage.OperationExecutorState;

import java.util.Arrays;
import java.util.List;

import static com.github.fppt.jedismock.Utils.toNanoTimeout;
Expand All @@ -21,6 +20,6 @@ public class BZPopMin extends BZPop {
@Override
protected Slice popper(List<Slice> params) {
List<Slice> result = new ZPop(base(), params, false).pop();
return Response.array(Arrays.asList(Response.bulkString(params.get(0)), result.get(0), result.get(1)));
return Response.array(Response.bulkString(params.get(0)), result.get(0), result.get(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;

Expand Down Expand Up @@ -51,10 +50,13 @@ protected Slice getResult() {
for (int index = 0; index < result.size(); index += 2) {
Slice value = result.get(index);
Slice score = result.get(index + 1);
popedList.add(Response.array(Arrays.asList(value, score)));
popedList.add(Response.array(value, score));
}
Slice pop = Response.array(popedList);
return Response.array(Arrays.asList(Response.bulkString(key), pop));

return Response.array(
Response.bulkString(key),
Response.array(popedList)
);
}
}
return Response.NULL_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.github.fppt.jedismock.server.Response;
import com.github.fppt.jedismock.storage.RedisBase;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;

Expand All @@ -32,7 +31,7 @@ protected Slice response() {
mapDBObj = getZSetFromBaseOrCreateEmpty(key);
options.add(REV);
if (checkWrongIndex()) {
return Response.array(new ArrayList<>());
return Response.EMPTY_ARRAY;
}

NavigableSet<ZSetEntry> entries = getRange(
Expand Down
Loading

0 comments on commit 2783e03

Please sign in to comment.