Fixed - RKeys.deleteByPatternAsync doesn't work in batch mode #2216

pull/2247/head^2
Nikita Koksharov 6 years ago
parent c856c35373
commit f9b7567263

@ -17,6 +17,7 @@ package org.redisson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -47,6 +48,8 @@ import org.redisson.connection.MasterSlaveEntry;
import org.redisson.misc.CompositeIterable;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
import org.redisson.reactive.CommandReactiveBatchService;
import org.redisson.rx.CommandRxBatchService;
/**
*
@ -210,6 +213,18 @@ public class RedissonKeys implements RKeys {
@Override
public RFuture<Long> deleteByPatternAsync(String pattern) {
if (commandExecutor instanceof CommandBatchService
|| commandExecutor instanceof CommandReactiveBatchService
|| commandExecutor instanceof CommandRxBatchService) {
return commandExecutor.evalWriteAsync((String)null, null, RedisCommands.EVAL_LONG,
"local keys = redis.call('keys', ARGV[1]) "
+ "local n = 0 "
+ "for i=1, #keys,5000 do "
+ "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) "
+ "end "
+ "return n;", Collections.emptyList(), pattern);
}
int batchSize = 500;
RPromise<Long> result = new RedissonPromise<Long>();
AtomicReference<Throwable> failed = new AtomicReference<Throwable>();

@ -12,6 +12,8 @@ import org.junit.Assert;
import org.junit.Test;
import org.redisson.ClusterRunner.ClusterProcesses;
import org.redisson.RedisRunner.FailedToStartRedisException;
import org.redisson.api.BatchResult;
import org.redisson.api.RBatch;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RType;
@ -185,6 +187,32 @@ public class RedissonKeysTest extends BaseTest {
Assert.assertEquals(0, redisson.getKeys().deleteByPattern("test?"));
}
@Test
public void testDeleteByPatternBatch() {
RBucket<String> bucket = redisson.getBucket("test0");
bucket.set("someValue3");
assertThat(bucket.isExists()).isTrue();
RBucket<String> bucket2 = redisson.getBucket("test9");
bucket2.set("someValue4");
assertThat(bucket.isExists()).isTrue();
RMap<String, String> map = redisson.getMap("test2");
map.fastPut("1", "2");
assertThat(map.isExists()).isTrue();
RMap<String, String> map2 = redisson.getMap("test3");
map2.fastPut("1", "5");
assertThat(map2.isExists()).isTrue();
RBatch batch = redisson.createBatch();
batch.getKeys().deleteByPatternAsync("test?");
BatchResult<?> r = batch.execute();
Assert.assertEquals(4L, r.getResponses().get(0));
}
@Test
public void testFindKeys() {
RBucket<String> bucket = redisson.getBucket("test1");

Loading…
Cancel
Save