Search in sources :

Example 1 with CommandRxBatchService

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);
}
Also used : RedisException(org.redisson.client.RedisException) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) StringCodec(org.redisson.client.codec.StringCodec) CommandBatchService(org.redisson.command.CommandBatchService) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) CompositeIterable(org.redisson.misc.CompositeIterable) RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) CommandRxBatchService(org.redisson.rx.CommandRxBatchService) StreamSupport(java.util.stream.StreamSupport) RObject(org.redisson.api.RObject) CommandReactiveBatchService(org.redisson.reactive.CommandReactiveBatchService) RType(org.redisson.api.RType) ConnectionManager(org.redisson.connection.ConnectionManager) RedissonPromise(org.redisson.misc.RedissonPromise) CompletionException(java.util.concurrent.CompletionException) RedissonBaseIterator(org.redisson.iterator.RedissonBaseIterator) RKeys(org.redisson.api.RKeys) RedisCommands(org.redisson.client.protocol.RedisCommands) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisCommand(org.redisson.client.protocol.RedisCommand) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) CompletionException(java.util.concurrent.CompletionException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) CommandRxBatchService(org.redisson.rx.CommandRxBatchService) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) CompletionException(java.util.concurrent.CompletionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) RedisException(org.redisson.client.RedisException) CommandReactiveBatchService(org.redisson.reactive.CommandReactiveBatchService)

Aggregations

java.util (java.util)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 RFuture (org.redisson.api.RFuture)1 RKeys (org.redisson.api.RKeys)1 RObject (org.redisson.api.RObject)1 RType (org.redisson.api.RType)1 RedisClient (org.redisson.client.RedisClient)1 RedisException (org.redisson.client.RedisException)1 StringCodec (org.redisson.client.codec.StringCodec)1 RedisCommand (org.redisson.client.protocol.RedisCommand)1 RedisCommands (org.redisson.client.protocol.RedisCommands)1 CommandAsyncExecutor (org.redisson.command.CommandAsyncExecutor)1 CommandBatchService (org.redisson.command.CommandBatchService)1 ConnectionManager (org.redisson.connection.ConnectionManager)1 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)1