use of org.redisson.rx.CommandRxBatchService in project redisson by redisson.
the class RedissonKeys method deleteByPatternAsync.
@Override
public RFuture<Long> deleteByPatternAsync(String pattern) {
if (commandExecutor instanceof CommandBatchService || commandExecutor instanceof CommandReactiveBatchService || commandExecutor instanceof CommandRxBatchService) {
if (getConnectionManager().isClusterMode()) {
throw new IllegalStateException("This method doesn't work in batch for Redis cluster mode. For Redis cluster execute it as non-batch method");
}
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;
List<CompletableFuture<Long>> futures = new ArrayList<>();
for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
commandExecutor.getConnectionManager().getExecutor().execute(() -> {
long count = 0;
try {
Iterator<String> keysIterator = createKeysIterator(entry, RedisCommands.SCAN, pattern, batchSize);
List<String> keys = new ArrayList<>();
while (keysIterator.hasNext()) {
String key = keysIterator.next();
keys.add(key);
if (keys.size() % batchSize == 0) {
count += delete(keys.toArray(new String[0]));
keys.clear();
}
}
if (!keys.isEmpty()) {
count += delete(keys.toArray(new String[0]));
keys.clear();
}
RFuture<Long> future = RedissonPromise.newSucceededFuture(count);
futures.add(future.toCompletableFuture());
} catch (Exception e) {
CompletableFuture<Long> future = new CompletableFuture<>();
future.completeExceptionally(e);
futures.add(future);
}
});
}
CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
CompletableFuture<Long> res = future.handle((r, e) -> {
long cc = futures.stream().filter(f -> f.isDone()).mapToLong(f -> f.getNow(0L)).sum();
if (e != null) {
if (cc > 0) {
RedisException ex = new RedisException(cc + " keys have been deleted. But one or more nodes has an error", e);
throw new CompletionException(ex);
} else {
throw new CompletionException(e);
}
}
return cc;
});
return new CompletableFutureWrapper<>(res);
}
Aggregations