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

org.redisson.RedissonReactive 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;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import org.redisson.api.ClusterNode;
import org.redisson.api.MapOptions;
import org.redisson.api.Node;
import org.redisson.api.NodesGroup;
import org.redisson.api.RAtomicLongReactive;
import org.redisson.api.RBatchReactive;
import org.redisson.api.RBitSetReactive;
import org.redisson.api.RBlockingQueueReactive;
import org.redisson.api.RBucketReactive;
import org.redisson.api.RDequeReactive;
import org.redisson.api.RFuture;
import org.redisson.api.RHyperLogLogReactive;
import org.redisson.api.RKeysReactive;
import org.redisson.api.RLexSortedSetReactive;
import org.redisson.api.RListMultimapReactive;
import org.redisson.api.RListReactive;
import org.redisson.api.RLockReactive;
import org.redisson.api.RMapCacheReactive;
import org.redisson.api.RMapReactive;
import org.redisson.api.RPatternTopicReactive;
import org.redisson.api.RQueueReactive;
import org.redisson.api.RReadWriteLockReactive;
import org.redisson.api.RScoredSortedSetReactive;
import org.redisson.api.RScriptReactive;
import org.redisson.api.RSemaphoreReactive;
import org.redisson.api.RSetCacheReactive;
import org.redisson.api.RSetMultimapReactive;
import org.redisson.api.RSetReactive;
import org.redisson.api.RTopicReactive;
import org.redisson.api.RedissonReactiveClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.codec.ReferenceCodecProvider;
import org.redisson.command.CommandReactiveService;
import org.redisson.config.Config;
import org.redisson.config.ConfigSupport;
import org.redisson.connection.ConnectionManager;
import org.redisson.eviction.EvictionScheduler;
import org.redisson.pubsub.SemaphorePubSub;
import org.redisson.reactive.RedissonAtomicLongReactive;
import org.redisson.reactive.RedissonBatchReactive;
import org.redisson.reactive.RedissonBitSetReactive;
import org.redisson.reactive.RedissonBlockingQueueReactive;
import org.redisson.reactive.RedissonBucketReactive;
import org.redisson.reactive.RedissonDequeReactive;
import org.redisson.reactive.RedissonHyperLogLogReactive;
import org.redisson.reactive.RedissonKeysReactive;
import org.redisson.reactive.RedissonLexSortedSetReactive;
import org.redisson.reactive.RedissonListMultimapReactive;
import org.redisson.reactive.RedissonListReactive;
import org.redisson.reactive.RedissonLockReactive;
import org.redisson.reactive.RedissonMapCacheReactive;
import org.redisson.reactive.RedissonMapReactive;
import org.redisson.reactive.RedissonPatternTopicReactive;
import org.redisson.reactive.RedissonQueueReactive;
import org.redisson.reactive.RedissonReadWriteLockReactive;
import org.redisson.reactive.RedissonScoredSortedSetReactive;
import org.redisson.reactive.RedissonScriptReactive;
import org.redisson.reactive.RedissonSemaphoreReactive;
import org.redisson.reactive.RedissonSetCacheReactive;
import org.redisson.reactive.RedissonSetMultimapReactive;
import org.redisson.reactive.RedissonSetReactive;
import org.redisson.reactive.RedissonTopicReactive;

/**
 * Main infrastructure class allows to get access
 * to all Redisson objects on top of Redis server.
 *
 * @author Nikita Koksharov
 *
 */
public class RedissonReactive implements RedissonReactiveClient {

    protected final EvictionScheduler evictionScheduler;
    protected final CommandReactiveService commandExecutor;
    protected final ConnectionManager connectionManager;
    protected final Config config;
    protected final ReferenceCodecProvider codecProvider;
    
    protected final UUID id = UUID.randomUUID();
    protected final SemaphorePubSub semaphorePubSub = new SemaphorePubSub();
    
    protected RedissonReactive(Config config) {
        this.config = config;
        Config configCopy = new Config(config);
        
        connectionManager = ConfigSupport.createConnectionManager(configCopy);
        commandExecutor = new CommandReactiveService(connectionManager);
        evictionScheduler = new EvictionScheduler(commandExecutor);
        codecProvider = config.getReferenceCodecProvider();
    }

    @Override
    public RSemaphoreReactive getSemaphore(String name) {
        return new RedissonSemaphoreReactive(commandExecutor, name, semaphorePubSub);
    }
    
    @Override
    public RReadWriteLockReactive getReadWriteLock(String name) {
        return new RedissonReadWriteLockReactive(commandExecutor, name, id);
    }
    
    @Override
    public RLockReactive getLock(String name) {
        return new RedissonLockReactive(commandExecutor, name, id);
    }

    @Override
    public  RMapCacheReactive getMapCache(String name, Codec codec) {
        return new RedissonMapCacheReactive(evictionScheduler, codec, commandExecutor, name, null);
    }

    @Override
    public  RMapCacheReactive getMapCache(String name) {
        return new RedissonMapCacheReactive(evictionScheduler, commandExecutor, name, null);
    }

    @Override
    public  RBucketReactive getBucket(String name) {
        return new RedissonBucketReactive(commandExecutor, name);
    }

    @Override
    public  RBucketReactive getBucket(String name, Codec codec) {
        return new RedissonBucketReactive(codec, commandExecutor, name);
    }

    @Override
    public  List> findBuckets(String pattern) {
        RFuture> r = commandExecutor.readAllAsync(RedisCommands.KEYS, pattern);
        Collection keys = commandExecutor.get(r);

        List> buckets = new ArrayList>(keys.size());
        for (Object key : keys) {
            if(key != null) {
                buckets.add(this.getBucket(key.toString()));
            }
        }
        return buckets;
    }

    
    
    @Override
    public  RHyperLogLogReactive getHyperLogLog(String name) {
        return new RedissonHyperLogLogReactive(commandExecutor, name);
    }

    @Override
    public  RHyperLogLogReactive getHyperLogLog(String name, Codec codec) {
        return new RedissonHyperLogLogReactive(codec, commandExecutor, name);
    }

    @Override
    public  RListReactive getList(String name) {
        return new RedissonListReactive(commandExecutor, name);
    }

    @Override
    public  RListReactive getList(String name, Codec codec) {
        return new RedissonListReactive(codec, commandExecutor, name);
    }

    @Override
    public  RListMultimapReactive getListMultimap(String name) {
        return new RedissonListMultimapReactive(id, commandExecutor, name);
    }
    
    @Override
    public  RListMultimapReactive getListMultimap(String name, Codec codec) {
        return new RedissonListMultimapReactive(id, codec, commandExecutor, name);
    }

    @Override
    public  RSetMultimapReactive getSetMultimap(String name) {
        return new RedissonSetMultimapReactive(id, commandExecutor, name);
    }
    
    @Override
    public  RSetMultimapReactive getSetMultimap(String name, Codec codec) {
        return new RedissonSetMultimapReactive(id, codec, commandExecutor, name);
    }
    
    @Override
    public  RMapReactive getMap(String name) {
        return new RedissonMapReactive(commandExecutor, name, null);
    }

    @Override
    public  RMapReactive getMap(String name, Codec codec) {
        return new RedissonMapReactive(codec, commandExecutor, name, null);
    }

    @Override
    public  RSetReactive getSet(String name) {
        return new RedissonSetReactive(commandExecutor, name);
    }

    @Override
    public  RSetReactive getSet(String name, Codec codec) {
        return new RedissonSetReactive(codec, commandExecutor, name);
    }

    @Override
    public  RScoredSortedSetReactive getScoredSortedSet(String name) {
        return new RedissonScoredSortedSetReactive(commandExecutor, name);
    }

    @Override
    public  RScoredSortedSetReactive getScoredSortedSet(String name, Codec codec) {
        return new RedissonScoredSortedSetReactive(codec, commandExecutor, name);
    }

    @Override
    public RLexSortedSetReactive getLexSortedSet(String name) {
        return new RedissonLexSortedSetReactive(commandExecutor, name);
    }

    @Override
    public  RTopicReactive getTopic(String name) {
        return new RedissonTopicReactive(commandExecutor, name);
    }

    @Override
    public  RTopicReactive getTopic(String name, Codec codec) {
        return new RedissonTopicReactive(codec, commandExecutor, name);
    }

    @Override
    public  RPatternTopicReactive getPatternTopic(String pattern) {
        return new RedissonPatternTopicReactive(commandExecutor, pattern);
    }

    @Override
    public  RPatternTopicReactive getPatternTopic(String pattern, Codec codec) {
        return new RedissonPatternTopicReactive(codec, commandExecutor, pattern);
    }

    @Override
    public  RQueueReactive getQueue(String name) {
        return new RedissonQueueReactive(commandExecutor, name);
    }

    @Override
    public  RQueueReactive getQueue(String name, Codec codec) {
        return new RedissonQueueReactive(codec, commandExecutor, name);
    }

    @Override
    public  RBlockingQueueReactive getBlockingQueue(String name) {
        return new RedissonBlockingQueueReactive(commandExecutor, name);
    }

    @Override
    public  RBlockingQueueReactive getBlockingQueue(String name, Codec codec) {
        return new RedissonBlockingQueueReactive(codec, commandExecutor, name);
    }

    @Override
    public  RDequeReactive getDeque(String name) {
        return new RedissonDequeReactive(commandExecutor, name);
    }

    @Override
    public  RDequeReactive getDeque(String name, Codec codec) {
        return new RedissonDequeReactive(codec, commandExecutor, name);
    }

    @Override
    public  RSetCacheReactive getSetCache(String name) {
        return new RedissonSetCacheReactive(evictionScheduler, commandExecutor, name);
    }

    @Override
    public  RSetCacheReactive getSetCache(String name, Codec codec) {
        return new RedissonSetCacheReactive(codec, evictionScheduler, commandExecutor, name);
    }

    @Override
    public RAtomicLongReactive getAtomicLong(String name) {
        return new RedissonAtomicLongReactive(commandExecutor, name);
    }

    @Override
    public RBitSetReactive getBitSet(String name) {
        return new RedissonBitSetReactive(commandExecutor, name);
    }

    @Override
    public RScriptReactive getScript() {
        return new RedissonScriptReactive(commandExecutor);
    }

    @Override
    public RBatchReactive createBatch() {
        RedissonBatchReactive batch = new RedissonBatchReactive(evictionScheduler, connectionManager, commandExecutor);
        if (config.isReferenceEnabled()) {
            batch.enableRedissonReferenceSupport(this);
        }
        return batch;
    }

    @Override
    public RKeysReactive getKeys() {
        return new RedissonKeysReactive(commandExecutor);
    }

    @Override
    public Config getConfig() {
        return config;
    }

    @Override
    public ReferenceCodecProvider getCodecProvider() {
        return codecProvider;
    }
    
    @Override
    public NodesGroup getNodesGroup() {
        return new RedisNodes(connectionManager);
    }

    @Override
    public NodesGroup getClusterNodesGroup() {
        if (!connectionManager.isClusterMode()) {
            throw new IllegalStateException("Redisson not in cluster mode!");
        }
        return new RedisNodes(connectionManager);
    }

    @Override
    public void shutdown() {
        connectionManager.shutdown();
    }

    @Override
    public boolean isShutdown() {
        return connectionManager.isShutdown();
    }

    @Override
    public boolean isShuttingDown() {
        return connectionManager.isShuttingDown();
    }

    protected void enableRedissonReferenceSupport() {
        this.commandExecutor.enableRedissonReferenceSupport(this);
    }

    @Override
    public  RMapCacheReactive getMapCache(String name, Codec codec, MapOptions options) {
        return new RedissonMapCacheReactive(evictionScheduler, codec, commandExecutor, name, options);
    }


    @Override
    public  RMapCacheReactive getMapCache(String name, MapOptions options) {
        return new RedissonMapCacheReactive(evictionScheduler, commandExecutor, name, options);
    }


    @Override
    public  RMapReactive getMap(String name, MapOptions options) {
        return new RedissonMapReactive(commandExecutor, name, options);
    }


    @Override
    public  RMapReactive getMap(String name, Codec codec, MapOptions options) {
        return new RedissonMapReactive(codec, commandExecutor, name, options);
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy