forked from redis/jedis
/
ScanParams.java
84 lines (70 loc) · 2.17 KB
/
ScanParams.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.Keyword.COUNT;
import static redis.clients.jedis.Protocol.Keyword.MATCH;
import redis.clients.jedis.Protocol.Keyword;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.util.SafeEncoder;
public class ScanParams {
private final Map<Keyword, ByteBuffer> params = new EnumMap<Keyword, ByteBuffer>(Keyword.class);
public static final String SCAN_POINTER_START = String.valueOf(0);
public static final byte[] SCAN_POINTER_START_BINARY = SafeEncoder.encode(SCAN_POINTER_START);
public ScanParams match(final byte[] pattern) {
params.put(MATCH, ByteBuffer.wrap(pattern));
return this;
}
/**
* @see <a href="https://redis.io/commands/scan#the-match-option">MATCH option in Redis documentation</a>
*
* @param pattern
* @return
*/
public ScanParams match(final String pattern) {
params.put(MATCH, ByteBuffer.wrap(SafeEncoder.encode(pattern)));
return this;
}
/**
* @see <a href="https://redis.io/commands/scan#the-count-option">COUNT option in Redis documentation</a>
*
* @param count
* @return
*/
public ScanParams count(final Integer count) {
params.put(COUNT, ByteBuffer.wrap(Protocol.toByteArray(count)));
return this;
}
public Collection<byte[]> getParams() {
List<byte[]> paramsList = new ArrayList<byte[]>(params.size());
for (Map.Entry<Keyword, ByteBuffer> param : params.entrySet()) {
paramsList.add(param.getKey().raw);
paramsList.add(param.getValue().array());
}
return Collections.unmodifiableCollection(paramsList);
}
byte[] binaryMatch() {
if (params.containsKey(MATCH)) {
return params.get(MATCH).array();
} else {
return null;
}
}
String match() {
if (params.containsKey(MATCH)) {
return new String(params.get(MATCH).array());
} else {
return null;
}
}
Integer count() {
if (params.containsKey(COUNT)) {
return params.get(COUNT).getInt();
} else {
return null;
}
}
}