Merge branch 'HeartSaVioR-expose-scan-commands-to-binary-jedis'
This commit is contained in:
@@ -4,6 +4,7 @@ import static redis.clients.jedis.Protocol.toByteArray;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.net.URI;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -3454,4 +3455,71 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands,
|
||||
return client.getIntegerReply();
|
||||
}
|
||||
|
||||
public ScanResult<byte[]> scan(final byte[] cursor) {
|
||||
return scan(cursor, new ScanParams());
|
||||
}
|
||||
|
||||
public ScanResult<byte[]> scan(final byte[] cursor, final ScanParams params) {
|
||||
checkIsInMulti();
|
||||
client.scan(cursor, params);
|
||||
List<Object> result = client.getObjectMultiBulkReply();
|
||||
byte[] newcursor = (byte[]) result.get(0);
|
||||
List<byte[]> rawResults = (List<byte[]>) result.get(1);
|
||||
return new ScanResult<byte[]>(newcursor, rawResults);
|
||||
}
|
||||
|
||||
public ScanResult<Map.Entry<byte[], byte[]>> hscan(final byte[] key,
|
||||
final byte[] cursor) {
|
||||
return hscan(key, cursor, new ScanParams());
|
||||
}
|
||||
|
||||
public ScanResult<Map.Entry<byte[], byte[]>> hscan(final byte[] key,
|
||||
final byte[] cursor, final ScanParams params) {
|
||||
checkIsInMulti();
|
||||
client.hscan(key, cursor, params);
|
||||
List<Object> result = client.getObjectMultiBulkReply();
|
||||
byte[] newcursor = (byte[]) result.get(0);
|
||||
List<Map.Entry<byte[], byte[]>> results = new ArrayList<Map.Entry<byte[], byte[]>>();
|
||||
List<byte[]> rawResults = (List<byte[]>) result.get(1);
|
||||
Iterator<byte[]> iterator = rawResults.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
results.add(new AbstractMap.SimpleEntry<byte[], byte[]>(iterator.next(),
|
||||
iterator.next()));
|
||||
}
|
||||
return new ScanResult<Map.Entry<byte[], byte[]>>(newcursor, results);
|
||||
}
|
||||
|
||||
public ScanResult<byte[]> sscan(final byte[] key, final byte[] cursor) {
|
||||
return sscan(key, cursor, new ScanParams());
|
||||
}
|
||||
|
||||
public ScanResult<byte[]> sscan(final byte[] key, final byte[] cursor,
|
||||
final ScanParams params) {
|
||||
checkIsInMulti();
|
||||
client.sscan(key, cursor, params);
|
||||
List<Object> result = client.getObjectMultiBulkReply();
|
||||
byte[] newcursor = (byte[]) result.get(0);
|
||||
List<byte[]> rawResults = (List<byte[]>) result.get(1);
|
||||
return new ScanResult<byte[]>(newcursor, rawResults);
|
||||
}
|
||||
|
||||
public ScanResult<Tuple> zscan(final byte[] key, final byte[] cursor) {
|
||||
return zscan(key, cursor, new ScanParams());
|
||||
}
|
||||
|
||||
public ScanResult<Tuple> zscan(final byte[] key, final byte[] cursor,
|
||||
final ScanParams params) {
|
||||
checkIsInMulti();
|
||||
client.zscan(key, cursor, params);
|
||||
List<Object> result = client.getObjectMultiBulkReply();
|
||||
byte[] newcursor = (byte[]) result.get(0);
|
||||
List<Tuple> results = new ArrayList<Tuple>();
|
||||
List<byte[]> rawResults = (List<byte[]>) result.get(1);
|
||||
Iterator<byte[]> iterator = rawResults.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
results.add(new Tuple(iterator.next(), Double
|
||||
.valueOf(SafeEncoder.encode(iterator.next()))));
|
||||
}
|
||||
return new ScanResult<Tuple>(newcursor, results);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,14 @@ import redis.clients.util.SafeEncoder;
|
||||
public class ScanParams {
|
||||
private List<byte[]> params = new ArrayList<byte[]>();
|
||||
public final static String SCAN_POINTER_START = String.valueOf(0);
|
||||
public final static byte[] SCAN_POINTER_START_BINARY = SafeEncoder.encode(SCAN_POINTER_START);
|
||||
|
||||
public ScanParams match(final byte[] pattern) {
|
||||
params.add(MATCH.raw);
|
||||
params.add(pattern);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ScanParams match(final String pattern) {
|
||||
params.add(MATCH.raw);
|
||||
params.add(SafeEncoder.encode(pattern));
|
||||
|
||||
@@ -2,8 +2,10 @@ package redis.clients.jedis;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import redis.clients.util.SafeEncoder;
|
||||
|
||||
public class ScanResult<T> {
|
||||
private String cursor;
|
||||
private byte[] cursor;
|
||||
private List<T> results;
|
||||
|
||||
@Deprecated
|
||||
@@ -13,15 +15,18 @@ public class ScanResult<T> {
|
||||
* @see https://github.com/xetorthio/jedis/issues/531
|
||||
*/
|
||||
public ScanResult(int cursor, List<T> results) {
|
||||
this.cursor = String.valueOf(cursor);
|
||||
this.results = results;
|
||||
this(Protocol.toByteArray(cursor), results);
|
||||
}
|
||||
|
||||
public ScanResult(String cursor, List<T> results) {
|
||||
this(SafeEncoder.encode(cursor), results);
|
||||
}
|
||||
|
||||
public ScanResult(byte[] cursor, List<T> results) {
|
||||
this.cursor = cursor;
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
/**
|
||||
* This method is deprecated due to bug (scan cursor should be unsigned long)
|
||||
@@ -30,13 +35,17 @@ public class ScanResult<T> {
|
||||
* @return int(currently), but will be changed to String, so be careful to prepare!
|
||||
*/
|
||||
public int getCursor() {
|
||||
return Integer.parseInt(cursor);
|
||||
return Integer.parseInt(getStringCursor());
|
||||
}
|
||||
|
||||
/**
|
||||
* FIXME: This method should be changed to getCursor() on next major release
|
||||
*/
|
||||
public String getStringCursor() {
|
||||
return SafeEncoder.encode(cursor);
|
||||
}
|
||||
|
||||
public byte[] getCursorAsBytes() {
|
||||
return cursor;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import redis.clients.jedis.ScanResult;
|
||||
import redis.clients.jedis.exceptions.JedisDataException;
|
||||
import redis.clients.util.SafeEncoder;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
|
||||
|
||||
public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
@@ -515,8 +516,14 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
ScanResult<byte[]> bResult = jedis.scan(SCAN_POINTER_START_BINARY);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void scanMatch() {
|
||||
ScanParams params = new ScanParams();
|
||||
@@ -529,6 +536,19 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.match(bfoostar);
|
||||
|
||||
jedis.set(bfoo1, bbar);
|
||||
jedis.set(bfoo2, bbar);
|
||||
jedis.set(bfoo3, bbar);
|
||||
|
||||
ScanResult<byte[]> bResult = jedis.scan(SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -543,5 +563,17 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
|
||||
ScanResult<String> result = jedis.scan(SCAN_POINTER_START, params);
|
||||
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.count(2);
|
||||
|
||||
jedis.set(bfoo1, bbar);
|
||||
jedis.set(bfoo2, bbar);
|
||||
jedis.set(bfoo3, bbar);
|
||||
|
||||
ScanResult<byte[]> bResult = jedis.scan(SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,18 @@ import org.junit.Test;
|
||||
import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.ScanResult;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
|
||||
|
||||
public class HashesCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
final byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
|
||||
final byte[] bcar = { 0x09, 0x0A, 0x0B, 0x0C };
|
||||
|
||||
|
||||
final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, 0x0A };
|
||||
final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, 0x0B };
|
||||
final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, 0x0C };
|
||||
final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' };
|
||||
|
||||
@Test
|
||||
public void hset() {
|
||||
long status = jedis.hset("foo", "bar", "car");
|
||||
@@ -300,6 +306,14 @@ public class HashesCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
jedis.hset(bfoo, bbar, bcar);
|
||||
|
||||
ScanResult<Map.Entry<byte[], byte[]>> bResult = jedis.hscan(bfoo, SCAN_POINTER_START_BINARY);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -315,6 +329,20 @@ public class HashesCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.match(bbarstar);
|
||||
|
||||
jedis.hset(bfoo, bbar, bcar);
|
||||
jedis.hset(bfoo, bbar1, bcar);
|
||||
jedis.hset(bfoo, bbar2, bcar);
|
||||
jedis.hset(bfoo, bbar3, bcar);
|
||||
|
||||
ScanResult<Map.Entry<byte[], byte[]>> bResult = jedis.hscan(bfoo, SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -330,5 +358,18 @@ public class HashesCommandsTest extends JedisCommandTestBase {
|
||||
SCAN_POINTER_START, params);
|
||||
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.count(2);
|
||||
|
||||
jedis.hset(bfoo, bbar, bcar);
|
||||
jedis.hset(bfoo, bbar1, bcar);
|
||||
jedis.hset(bfoo, bbar2, bcar);
|
||||
jedis.hset(bfoo, bbar3, bcar);
|
||||
|
||||
ScanResult<Map.Entry<byte[], byte[]>> bResult = jedis.hscan(bfoo, SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.junit.Test;
|
||||
import redis.clients.jedis.ScanParams;
|
||||
import redis.clients.jedis.ScanResult;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
|
||||
|
||||
public class SetCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
@@ -19,6 +20,11 @@ public class SetCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bc = { 0x0C };
|
||||
final byte[] bd = { 0x0D };
|
||||
final byte[] bx = { 0x42 };
|
||||
|
||||
final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, 0x0A };
|
||||
final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, 0x0B };
|
||||
final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, 0x0C };
|
||||
final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' };
|
||||
|
||||
@Test
|
||||
public void sadd() {
|
||||
@@ -462,6 +468,14 @@ public class SetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
jedis.sadd(bfoo, ba, bb);
|
||||
|
||||
ScanResult<byte[]> bResult = jedis.sscan(bfoo, SCAN_POINTER_START_BINARY);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -474,6 +488,16 @@ public class SetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.match(bbarstar);
|
||||
|
||||
jedis.sadd(bfoo, bbar1, bbar2, bbar3);
|
||||
ScanResult<byte[]> bResult = jedis.sscan(bfoo, SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -486,5 +510,14 @@ public class SetCommandsTest extends JedisCommandTestBase {
|
||||
ScanResult<String> result = jedis.sscan("foo", SCAN_POINTER_START, params);
|
||||
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.count(2);
|
||||
|
||||
jedis.sadd(bfoo, bbar1, bbar2, bbar3);
|
||||
ScanResult<byte[]> bResult = jedis.sscan(bfoo, SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import redis.clients.jedis.Tuple;
|
||||
import redis.clients.jedis.ZParams;
|
||||
import redis.clients.util.SafeEncoder;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START;
|
||||
import static redis.clients.jedis.ScanParams.SCAN_POINTER_START_BINARY;
|
||||
|
||||
public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
|
||||
@@ -19,6 +20,11 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
final byte[] ba = { 0x0A };
|
||||
final byte[] bb = { 0x0B };
|
||||
final byte[] bc = { 0x0C };
|
||||
|
||||
final byte[] bbar1 = { 0x05, 0x06, 0x07, 0x08, 0x0A };
|
||||
final byte[] bbar2 = { 0x05, 0x06, 0x07, 0x08, 0x0B };
|
||||
final byte[] bbar3 = { 0x05, 0x06, 0x07, 0x08, 0x0C };
|
||||
final byte[] bbarstar = { 0x05, 0x06, 0x07, 0x08, '*' };
|
||||
|
||||
@Test
|
||||
public void zadd() {
|
||||
@@ -899,6 +905,15 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
jedis.zadd(bfoo, 1, ba);
|
||||
jedis.zadd(bfoo, 1, bb);
|
||||
|
||||
ScanResult<Tuple> bResult = jedis.zscan(bfoo, SCAN_POINTER_START_BINARY);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -913,6 +928,19 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
|
||||
assertEquals(SCAN_POINTER_START, result.getStringCursor());
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.match(bbarstar);
|
||||
|
||||
jedis.zadd(bfoo, 2, bbar1);
|
||||
jedis.zadd(bfoo, 1, bbar2);
|
||||
jedis.zadd(bfoo, 11, bbar3);
|
||||
ScanResult<Tuple> bResult = jedis.zscan(bfoo, SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -929,5 +957,17 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
|
||||
ScanResult<Tuple> result = jedis.zscan("foo", SCAN_POINTER_START, params);
|
||||
|
||||
assertFalse(result.getResult().isEmpty());
|
||||
|
||||
// binary
|
||||
params = new ScanParams();
|
||||
params.count(2);
|
||||
|
||||
jedis.zadd(bfoo, 2, bbar1);
|
||||
jedis.zadd(bfoo, 1, bbar2);
|
||||
jedis.zadd(bfoo, 11, bbar3);
|
||||
|
||||
ScanResult<Tuple> bResult = jedis.zscan(bfoo, SCAN_POINTER_START_BINARY, params);
|
||||
|
||||
assertFalse(bResult.getResult().isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user