All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.reactive.RedissonKeysReactive Maven / Gradle / Ivy

/**
 * Copyright 2016 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.reactive;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.LongConsumer;
import java.util.function.Supplier;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.redisson.RedissonKeys;
import org.redisson.api.RFuture;
import org.redisson.api.RKeysReactive;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.decoder.ListScanResult;
import org.redisson.command.CommandReactiveService;
import org.redisson.connection.MasterSlaveEntry;

import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class RedissonKeysReactive implements RKeysReactive {

    private final CommandReactiveService commandExecutor;

    private final RedissonKeys instance;

    public RedissonKeysReactive(CommandReactiveService commandExecutor) {
        super();
        instance = new RedissonKeys(commandExecutor);
        this.commandExecutor = commandExecutor;
    }

    @Override
    public Publisher getSlot(final String key) {
        return commandExecutor.reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.getSlotAsync(key);
            }
        });
    }

    @Override
    public Publisher getKeysByPattern(final String pattern) {
        List> publishers = new ArrayList>();
        for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
            publishers.add(createKeysIterator(entry, pattern));
        }
        return Flux.merge(publishers);
    }

    @Override
    public Publisher getKeys() {
        return getKeysByPattern(null);
    }

    private Publisher> scanIterator(MasterSlaveEntry entry, long startPos, String pattern) {
        if (pattern == null) {
            return commandExecutor.writeReactive(entry, StringCodec.INSTANCE, RedisCommands.SCAN, startPos);
        }
        return commandExecutor.writeReactive(entry, StringCodec.INSTANCE, RedisCommands.SCAN, startPos, "MATCH", pattern);
    }

    private Publisher createKeysIterator(final MasterSlaveEntry entry, final String pattern) {
        return Flux.create(new Consumer>() {
            
            @Override
            public void accept(FluxSink emitter) {
                emitter.onRequest(new LongConsumer() {
                    private List firstValues;
                    private long nextIterPos;
                    
                    private long currentIndex;
                    
                    @Override
                    public void accept(long value) {
                        currentIndex = value;
                        nextValues(emitter);
                    }
                    
                    protected void nextValues(FluxSink emitter) {
                        scanIterator(entry, nextIterPos, pattern).subscribe(new Subscriber>() {

                            @Override
                            public void onSubscribe(Subscription s) {
                                s.request(Long.MAX_VALUE);
                            }

                            @Override
                            public void onNext(ListScanResult res) {
                                long prevIterPos = nextIterPos;
                                if (nextIterPos == 0 && firstValues == null) {
                                    firstValues = res.getValues();
                                } else if (res.getValues().equals(firstValues)) {
                                    emitter.complete();
                                    currentIndex = 0;
                                    return;
                                }

                                nextIterPos = res.getPos();
                                if (prevIterPos == nextIterPos) {
                                    nextIterPos = -1;
                                }
                                for (String val : res.getValues()) {
                                    emitter.next(val);
                                    currentIndex--;
                                    if (currentIndex == 0) {
                                        emitter.complete();
                                        return;
                                    }
                                }
                                if (nextIterPos == -1) {
                                    emitter.complete();
                                    currentIndex = 0;
                                }
                            }

                            @Override
                            public void onError(Throwable error) {
                                emitter.error(error);
                            }

                            @Override
                            public void onComplete() {
                                if (currentIndex == 0) {
                                    return;
                                }
                                nextValues(emitter);
                            }
                        });
                    }

                });
            }
            

        });
    }

    @Override
    public Publisher> findKeysByPattern(final String pattern) {
        return commandExecutor.reactive(new Supplier>>() {
            @Override
            public RFuture> get() {
                return instance.findKeysByPatternAsync(pattern);
            }
        });
    }

    @Override
    public Publisher randomKey() {
        return commandExecutor.reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.randomKeyAsync();
            }
        });
    }

    @Override
    public Publisher deleteByPattern(final String pattern) {
        return commandExecutor.reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.deleteByPatternAsync(pattern);
            }
        });
    }

    @Override
    public Publisher delete(final String ... keys) {
        return commandExecutor.reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.deleteAsync(keys);
            }
        });
    }

    @Override
    public Publisher flushdb() {
        return commandExecutor.reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.flushdbAsync();
            }
        });
    }

    @Override
    public Publisher flushall() {
        return commandExecutor.reactive(new Supplier>() {
            @Override
            public RFuture get() {
                return instance.flushallAsync();
            }
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy