Skip to content

Commit

Permalink
TairHash: support exhscanunorder command
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbodong22011 committed Nov 2, 2022
1 parent 3c7fcf9 commit f19f94f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/aliyun/tair/ModuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum ModuleCommand implements ProtocolCommand {
EXHGETALL("EXHGETALL"),
EXHMGETWITHVER("EXHMGETWITHVER"),
EXHSCAN("EXHSCAN"),
EXHSCANUNORDER("EXHSCANUNORDER"),

// CAS & CAD
CAS("CAS"),
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/aliyun/tair/tairhash/TairHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,57 @@ public ScanResult<Entry<byte[], byte[]>> exhscan(final byte[] key, final byte[]
releaseJedis(jedis);
}
}

/**
* EXHSCANUNORDER key cursor
* @param key the key
* @param cursor the cursor
* @return ScanResult
*/
public ScanResult<Entry<String, String>> exhscanunorder(final String key, final String cursor) {
return exhscanunorder(key, cursor, new ScanParams());
}

public ScanResult<Entry<byte[], byte[]>> exhscanunorder(final byte[] key, final byte[] cursor) {
return exhscanunorder(key, cursor, new ScanParams());
}

/**
* EXHSCANUNORDER key cursor [MATCH pattern] [COUNT count]
* @param key
* @param cursor
* @param params
* @return
*/
public ScanResult<Entry<String, String>> exhscanunorder(final String key, final String cursor,
final ScanParams params) {
final List<byte[]> args = new ArrayList<byte[]>();
args.add(SafeEncoder.encode(key));
args.add(SafeEncoder.encode(cursor));
args.addAll(params.getParams());

Jedis jedis = getJedis();
try {
Object obj = jedis.sendCommand(ModuleCommand.EXHSCANUNORDER, args.toArray(new byte[args.size()][]));
return HashBuilderFactory.EXHSCAN_RESULT_STRING.build(obj);
} finally {
releaseJedis(jedis);
}
}

public ScanResult<Entry<byte[], byte[]>> exhscanunorder(final byte[] key, final byte[] cursor,
final ScanParams params) {
final List<byte[]> args = new ArrayList<byte[]>();
args.add(key);
args.add(cursor);
args.addAll(params.getParams());

Jedis jedis = getJedis();
try {
Object obj = jedis.sendCommand(ModuleCommand.EXHSCANUNORDER, args.toArray(new byte[args.size()][]));
return HashBuilderFactory.EXHSCAN_RESULT_BYTE.build(obj);
} finally {
releaseJedis(jedis);
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/aliyun/tair/tairhash/TairHashCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.aliyun.tair.tairhash.params.*;
import com.aliyun.tair.util.JoinParameters;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
Expand Down Expand Up @@ -426,4 +427,35 @@ public ScanResult<Entry<byte[], byte[]>> exhscan(final byte[] key, final byte[]
Object obj = jc.sendCommand(key, ModuleCommand.EXHSCAN, args.toArray(new byte[args.size()][]));
return HashBuilderFactory.EXHSCAN_RESULT_BYTE.build(obj);
}

public ScanResult<Entry<String, String>> exhscanunorder(final String key, final String cursor) {
return exhscanunorder(key, cursor, new ScanParams());
}

public ScanResult<Entry<byte[], byte[]>> exhscanunorder(final byte[] key, final byte[] cursor) {
return exhscanunorder(key, cursor, new ScanParams());
}

public ScanResult<Entry<String, String>> exhscanunorder(final String key, final String cursor,
final ScanParams params) {
final List<byte[]> args = new ArrayList<byte[]>();
args.add(SafeEncoder.encode(key));
args.add(SafeEncoder.encode(cursor));
args.addAll(params.getParams());

Object obj = jc.sendCommand(SafeEncoder.encode(key), ModuleCommand.EXHSCANUNORDER,
args.toArray(new byte[args.size()][]));
return HashBuilderFactory.EXHSCAN_RESULT_STRING.build(obj);
}

public ScanResult<Entry<byte[], byte[]>> exhscanunorder(final byte[] key, final byte[] cursor,
final ScanParams params) {
final List<byte[]> args = new ArrayList<byte[]>();
args.add(key);
args.add(cursor);
args.addAll(params.getParams());

Object obj = jc.sendCommand(key, ModuleCommand.EXHSCANUNORDER, args.toArray(new byte[args.size()][]));
return HashBuilderFactory.EXHSCAN_RESULT_BYTE.build(obj);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/aliyun/tair/tairhash/TairHashPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.aliyun.tair.tairhash.factory.HashBuilderFactory;
import com.aliyun.tair.util.JoinParameters;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.ScanParams;
Expand Down Expand Up @@ -415,4 +416,34 @@ public Response<ScanResult<Entry<byte[], byte[]>>> exhscan(final byte[] key, fin
getClient("").sendCommand(ModuleCommand.EXHSCAN, args.toArray(new byte[args.size()][]));
return getResponse(HashBuilderFactory.EXHSCAN_RESULT_BYTE);
}

public Response<ScanResult<Entry<String, String>>> exhscanunorder(final String key, final String cursor) {
return exhscanunorder(key, cursor, new ScanParams());
}

public Response<ScanResult<Entry<byte[], byte[]>>> exhscanunorder(final byte[] key, final byte[] cursor) {
return exhscanunorder(key, cursor, new ScanParams());
}

public Response<ScanResult<Entry<String, String>>> exhscanunorder(final String key, final String cursor,
final ScanParams params) {
final List<byte[]> args = new ArrayList<byte[]>();
args.add(SafeEncoder.encode(key));
args.add(SafeEncoder.encode(cursor));
args.addAll(params.getParams());

getClient("").sendCommand(ModuleCommand.EXHSCANUNORDER, args.toArray(new byte[args.size()][]));
return getResponse(HashBuilderFactory.EXHSCAN_RESULT_STRING);
}

public Response<ScanResult<Entry<byte[], byte[]>>> exhscanunorder(final byte[] key, final byte[] cursor,
final ScanParams params) {
final List<byte[]> args = new ArrayList<byte[]>();
args.add(key);
args.add(cursor);
args.addAll(params.getParams());

getClient("").sendCommand(ModuleCommand.EXHSCANUNORDER, args.toArray(new byte[args.size()][]));
return getResponse(HashBuilderFactory.EXHSCAN_RESULT_BYTE);
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/aliyun/tair/tests/tairhash/TairHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,49 @@ public void exhscanException() {
}
}

@Test
public void exhscanunorderTest() {
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100; i++) {
map.put("field" + i, "val" + i);
}
tairHash.exhmset(foo, map);

map.clear();
String cursor = ScanParams.SCAN_POINTER_START;
do {
ScanResult<Entry<String, String>> scanResult = tairHash.exhscanunorder(foo, cursor);
cursor = scanResult.getCursor();
for (Entry<String, String> entry : scanResult.getResult()) {
map.put(entry.getKey(), entry.getValue());
}
} while (!cursor.equals("0"));

Assert.assertEquals(100, map.size());
}

@Test
public void exhscanunorderWithParamsTest() {
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100; i++) {
map.put("field" + i, "val" + i);
}
tairHash.exhmset(foo, map);

map.clear();
String cursor = ScanParams.SCAN_POINTER_START;
do {
ScanResult<Entry<String, String>> scanResult = tairHash.exhscanunorder(foo, cursor,
new ScanParams().match("*1*"));
cursor = scanResult.getCursor();
for (Entry<String, String> entry : scanResult.getResult()) {
map.put(entry.getKey(), entry.getValue());
}
} while (!cursor.equals("0"));

Assert.assertEquals(19, map.size());
}

// ======== common equal ========
public boolean arrayContains(Collection<byte[]> array, byte[] expected) {
for (byte[] a : array) {
Expand Down

0 comments on commit f19f94f

Please sign in to comment.