forked from zxl0714/redis-mock
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding support for `scan` and `flushdb` commands. * Using Java Optional instead of returning null
- Loading branch information
Showing
6 changed files
with
179 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/github/fppt/jedismock/operations/RO_flushdb.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.fppt.jedismock.operations; | ||
|
||
import com.github.fppt.jedismock.server.Response; | ||
import com.github.fppt.jedismock.server.Slice; | ||
import com.github.fppt.jedismock.storage.RedisBase; | ||
|
||
import java.util.List; | ||
|
||
class RO_flushdb extends AbstractRedisOperation { | ||
RO_flushdb(RedisBase base, List<Slice> params) { | ||
super(base, params); | ||
} | ||
|
||
Slice response(){ | ||
base().clear(); | ||
return Response.OK; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/github/fppt/jedismock/operations/RO_scan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.github.fppt.jedismock.operations; | ||
|
||
import com.github.fppt.jedismock.Utils; | ||
import com.github.fppt.jedismock.server.Response; | ||
import com.github.fppt.jedismock.server.Slice; | ||
import com.github.fppt.jedismock.storage.RedisBase; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.github.fppt.jedismock.Utils.convertToLong; | ||
|
||
class RO_scan extends AbstractRedisOperation { | ||
|
||
private static final long CURSOR_START = 0; | ||
// From the Redis documentation, the default count if not specified: | ||
private static final long DEFAULT_COUNT = 10; | ||
|
||
private static final String MATCH = "match"; | ||
private static final String COUNT = "count"; | ||
|
||
RO_scan(RedisBase base, List<Slice> params) { | ||
super(base, params); | ||
} | ||
|
||
Slice response() { | ||
|
||
Slice cursorSlice = params().get(0); | ||
long cursor = cursorSlice != null ? convertToLong(cursorSlice.toString()) : CURSOR_START; | ||
|
||
String match = extractParameter(params(), MATCH).map(Slice::toString).orElse("*"); | ||
long count = extractParameter(params(), COUNT).map(s -> convertToLong(s.toString())).orElse(DEFAULT_COUNT); | ||
|
||
String regex = Utils.createRegexFromGlob(match); | ||
List<Slice> matchingKeys = base().keys().stream() | ||
.skip(cursor) | ||
.limit(count) | ||
.filter(x -> x.toString().matches(regex)) | ||
.map(Response::bulkString) | ||
.collect(Collectors.toList()); | ||
|
||
cursor = cursor + count; | ||
if (cursor >= base().keys().size()) { | ||
cursor = CURSOR_START; | ||
} | ||
|
||
List<Slice> response = Lists.newArrayList(Response.bulkString(Slice.create(String.valueOf(cursor))), Response.array(matchingKeys)); | ||
return Response.array(response); | ||
} | ||
|
||
private static Optional<Slice> extractParameter(List<Slice> params, String name) { | ||
for (int i = 0; i < params.size(); i++) { | ||
String param = new String(params.get(i).data()); | ||
if (name.equalsIgnoreCase(param)) { | ||
return Optional.of(params.get(i + 1)); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters