Search in sources :

Example 1 with ScanArgs

use of com.lambdaworks.redis.ScanArgs in project moleculer-java by moleculer-java.

the class RedisGetSetClient method clean.

private final CompletionStage<Object> clean(RedisFuture<KeyScanCursor<byte[]>> future, ScanArgs args, String match) {
    return future.thenCompose(keyScanCursor -> {
        List<byte[]> keys = keyScanCursor.getKeys();
        if (keys == null || keys.isEmpty()) {
            return CompletableFuture.completedFuture(keyScanCursor);
        }
        if (match != null) {
            Iterator<byte[]> i = keys.iterator();
            while (i.hasNext()) {
                if (!Matcher.matches(new String(i.next(), StandardCharsets.UTF_8), match)) {
                    i.remove();
                }
            }
        }
        if (keys.isEmpty()) {
            return CompletableFuture.completedFuture(keyScanCursor);
        }
        byte[][] array = new byte[keys.size()][];
        keys.toArray(array);
        return client.del(array).thenApply(nul -> keyScanCursor);
    }).thenApply(keyScanCursor -> {
        if (((KeyScanCursor<byte[]>) keyScanCursor).isFinished()) {
            return null;
        }
        return ((KeyScanCursor<byte[]>) keyScanCursor).getCursor();
    }).thenCompose(currentCursor -> {
        if (currentCursor == null) {
            return CompletableFuture.completedFuture(null);
        }
        return clean(new ScanCursor((String) currentCursor, false), args, match);
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SetArgs(com.lambdaworks.redis.SetArgs) ScanCursor(com.lambdaworks.redis.ScanCursor) ArrayList(java.util.ArrayList) Observable(rx.Observable) KeyScanCursor(com.lambdaworks.redis.KeyScanCursor) RedisFuture(com.lambdaworks.redis.RedisFuture) EventBus(com.lambdaworks.redis.event.EventBus) LinkedList(java.util.LinkedList) DefaultClientResources(com.lambdaworks.redis.resource.DefaultClientResources) Event(com.lambdaworks.redis.event.Event) ExecutorService(java.util.concurrent.ExecutorService) RedisClusterClient(com.lambdaworks.redis.cluster.RedisClusterClient) EventLoopGroup(io.netty.channel.EventLoopGroup) Iterator(java.util.Iterator) ByteArrayCodec(com.lambdaworks.redis.codec.ByteArrayCodec) ScanArgs(com.lambdaworks.redis.ScanArgs) RedisClient(com.lambdaworks.redis.RedisClient) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroupProvider(com.lambdaworks.redis.resource.EventLoopGroupProvider) Promise(services.moleculer.Promise) TimeUnit(java.util.concurrent.TimeUnit) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Matcher(services.moleculer.eventbus.Matcher) RedisURI(com.lambdaworks.redis.RedisURI) Future(io.netty.util.concurrent.Future) RedisAsyncCommands(com.lambdaworks.redis.api.async.RedisAsyncCommands) RedisAdvancedClusterAsyncCommands(com.lambdaworks.redis.cluster.api.async.RedisAdvancedClusterAsyncCommands) ScanCursor(com.lambdaworks.redis.ScanCursor) KeyScanCursor(com.lambdaworks.redis.KeyScanCursor)

Example 2 with ScanArgs

use of com.lambdaworks.redis.ScanArgs in project moleculer-java by moleculer-java.

the class RedisGetSetClient method clean.

/**
 * Deletes a group of items. Removes every key by a match string.
 *
 * @param match
 */
public final Promise clean(String match) {
    ScanArgs args = new ScanArgs();
    args.limit(100);
    boolean singleStar = match.indexOf('*') > -1;
    boolean doubleStar = match.contains("**");
    if (doubleStar) {
        args.match(match.replace("**", "*"));
    } else {
        args.match(match);
    }
    if (!singleStar || doubleStar) {
        match = null;
    }
    if (client != null) {
        return new Promise(clean(client.scan(args), args, match));
    }
    if (clusteredClient != null) {
        return new Promise(clean(clusteredClient.scan(args), args, match));
    }
    return Promise.resolve();
}
Also used : Promise(services.moleculer.Promise) ScanArgs(com.lambdaworks.redis.ScanArgs)

Aggregations

ScanArgs (com.lambdaworks.redis.ScanArgs)2 Promise (services.moleculer.Promise)2 KeyScanCursor (com.lambdaworks.redis.KeyScanCursor)1 RedisClient (com.lambdaworks.redis.RedisClient)1 RedisFuture (com.lambdaworks.redis.RedisFuture)1 RedisURI (com.lambdaworks.redis.RedisURI)1 ScanCursor (com.lambdaworks.redis.ScanCursor)1 SetArgs (com.lambdaworks.redis.SetArgs)1 RedisAsyncCommands (com.lambdaworks.redis.api.async.RedisAsyncCommands)1 RedisClusterClient (com.lambdaworks.redis.cluster.RedisClusterClient)1 RedisAdvancedClusterAsyncCommands (com.lambdaworks.redis.cluster.api.async.RedisAdvancedClusterAsyncCommands)1 ByteArrayCodec (com.lambdaworks.redis.codec.ByteArrayCodec)1 Event (com.lambdaworks.redis.event.Event)1 EventBus (com.lambdaworks.redis.event.EventBus)1 DefaultClientResources (com.lambdaworks.redis.resource.DefaultClientResources)1 EventLoopGroupProvider (com.lambdaworks.redis.resource.EventLoopGroupProvider)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 DefaultEventExecutor (io.netty.util.concurrent.DefaultEventExecutor)1 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)1