-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for scan
and flushdb
commands.
#38
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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.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; | ||
|
||
Slice matchSlice = extractParameter(params(), MATCH); | ||
String match = matchSlice != null ? matchSlice.toString() : "*"; | ||
|
||
Slice countSlice = extractParameter(params(), COUNT); | ||
long count = countSlice != null ? convertToLong(countSlice.toString()) : 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 Slice extractParameter(List<Slice> params, String name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I am being a bit nitpicky but I would say rather return an
to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! It's a bit more involved as |
||
for (int i = 0; i < params.size(); i++) { | ||
String param = new String(params.get(i).data()); | ||
if (name.equalsIgnoreCase(param)) { | ||
return params.get(i + 1); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely belongs here!