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

com.github.lontime.shaded.org.redisson.Redisson Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2013-2021 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 com.github.lontime.shaded.org.redisson;

import com.github.lontime.shaded.org.redisson.api.*;
import com.github.lontime.shaded.org.redisson.api.redisnode.*;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.codec.JsonCodec;
import com.github.lontime.shaded.org.redisson.command.CommandAsyncExecutor;
import com.github.lontime.shaded.org.redisson.command.CommandSyncService;
import com.github.lontime.shaded.org.redisson.config.Config;
import com.github.lontime.shaded.org.redisson.config.ConfigSupport;
import com.github.lontime.shaded.org.redisson.connection.ConnectionManager;
import com.github.lontime.shaded.org.redisson.eviction.EvictionScheduler;
import com.github.lontime.shaded.org.redisson.liveobject.core.RedissonObjectBuilder;
import com.github.lontime.shaded.org.redisson.redisnode.RedissonClusterNodes;
import com.github.lontime.shaded.org.redisson.redisnode.RedissonMasterSlaveNodes;
import com.github.lontime.shaded.org.redisson.redisnode.RedissonSentinelMasterSlaveNodes;
import com.github.lontime.shaded.org.redisson.redisnode.RedissonSingleNode;
import com.github.lontime.shaded.org.redisson.remote.ResponseEntry;
import com.github.lontime.shaded.org.redisson.transaction.RedissonTransaction;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

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

    static {
        RedissonReference.warmUp();
    }

    protected final QueueTransferService queueTransferService = new QueueTransferService();
    protected final EvictionScheduler evictionScheduler;
    protected final WriteBehindService writeBehindService;
    protected final ConnectionManager connectionManager;
    protected final CommandAsyncExecutor commandExecutor;

    protected final ConcurrentMap, Class> liveObjectClassCache = new ConcurrentHashMap<>();
    protected final Config config;

    protected final ConcurrentMap responses = new ConcurrentHashMap<>();

    protected Redisson(Config config) {
        this.config = config;
        Config configCopy = new Config(config);

        connectionManager = ConfigSupport.createConnectionManager(configCopy);
        RedissonObjectBuilder objectBuilder = null;
        if (config.isReferenceEnabled()) {
            objectBuilder = new RedissonObjectBuilder(this);
        }
        commandExecutor = new CommandSyncService(connectionManager, objectBuilder);
        evictionScheduler = new EvictionScheduler(commandExecutor);
        writeBehindService = new WriteBehindService(commandExecutor);
    }

    public EvictionScheduler getEvictionScheduler() {
        return evictionScheduler;
    }

    public CommandAsyncExecutor getCommandExecutor() {
        return commandExecutor;
    }

    public ConnectionManager getConnectionManager() {
        return connectionManager;
    }

    /**
     * Create sync/async Redisson instance with default config
     *
     * @return Redisson instance
     */
    public static RedissonClient create() {
        Config config = new Config();
        config.useSingleServer()
        .setAddress("redis://127.0.0.1:6379");
        return create(config);
    }

    /**
     * Create sync/async Redisson instance with provided config
     *
     * @param config for Redisson
     * @return Redisson instance
     */
    public static RedissonClient create(Config config) {
        return new Redisson(config);
    }

    /*
     * Use Redisson.create().rxJava() method instead
     */
    @Deprecated
    public static RedissonRxClient createRx() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        return createRx(config);
    }

    /*
     * Use Redisson.create(config).rxJava() method instead
     */
    @Deprecated
    public static RedissonRxClient createRx(Config config) {
        return new RedissonRx(config);
    }

    @Override
    public RedissonRxClient rxJava() {
        return new RedissonRx(connectionManager, evictionScheduler, writeBehindService, responses);
    }

    /*
     * Use Redisson.create().reactive() method instead
     */
    @Deprecated
    public static RedissonReactiveClient createReactive() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        return createReactive(config);
    }

    /*
     * Use Redisson.create(config).reactive() method instead
     */
    @Deprecated
    public static RedissonReactiveClient createReactive(Config config) {
        return new RedissonReactive(config);
    }

    @Override
    public RedissonReactiveClient reactive() {
        return new RedissonReactive(connectionManager, evictionScheduler, writeBehindService, responses);
    }

    @Override
    public  RTimeSeries getTimeSeries(String name) {
        return new RedissonTimeSeries<>(evictionScheduler, commandExecutor, name);
    }

    @Override
    public  RTimeSeries getTimeSeries(String name, Codec codec) {
        return new RedissonTimeSeries<>(codec, evictionScheduler, commandExecutor, name);
    }

    @Override
    public  RStream getStream(String name) {
        return new RedissonStream(commandExecutor, name);
    }

    @Override
    public  RStream getStream(String name, Codec codec) {
        return new RedissonStream(codec, commandExecutor, name);
    }

    @Override
    public RBinaryStream getBinaryStream(String name) {
        return new RedissonBinaryStream(commandExecutor, name);
    }

    @Override
    public  RGeo getGeo(String name) {
        return new RedissonGeo(commandExecutor, name, this);
    }

    @Override
    public  RGeo getGeo(String name, Codec codec) {
        return new RedissonGeo(codec, commandExecutor, name, this);
    }

    @Override
    public  RBucket getBucket(String name) {
        return new RedissonBucket(commandExecutor, name);
    }

    @Override
    public RRateLimiter getRateLimiter(String name) {
        return new RedissonRateLimiter(commandExecutor, name);
    }

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

    @Override
    public RBuckets getBuckets() {
        return new RedissonBuckets(commandExecutor);
    }

    @Override
    public RBuckets getBuckets(Codec codec) {
        return new RedissonBuckets(codec, commandExecutor);
    }

    @Override
    public  RJsonBucket getJsonBucket(String name, JsonCodec codec) {
        return new RedissonJsonBucket<>(codec, commandExecutor, name);
    }

    @Override
    public  RHyperLogLog getHyperLogLog(String name) {
        return new RedissonHyperLogLog(commandExecutor, name);
    }

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

    @Override
    public  RList getList(String name) {
        return new RedissonList(commandExecutor, name, this);
    }

    @Override
    public  RList getList(String name, Codec codec) {
        return new RedissonList(codec, commandExecutor, name, this);
    }

    @Override
    public  RListMultimap getListMultimap(String name) {
        return new RedissonListMultimap(commandExecutor, name);
    }

    @Override
    public  RListMultimap getListMultimap(String name, Codec codec) {
        return new RedissonListMultimap(codec, commandExecutor, name);
    }

    @Override
    public  RLocalCachedMap getLocalCachedMap(String name, LocalCachedMapOptions options) {
        return new RedissonLocalCachedMap(commandExecutor, name,
                options, evictionScheduler, this, writeBehindService);
    }

    @Override
    public  RLocalCachedMap getLocalCachedMap(String name, Codec codec, LocalCachedMapOptions options) {
        return new RedissonLocalCachedMap(codec, commandExecutor, name,
                options, evictionScheduler, this, writeBehindService);
    }

    @Override
    public  RMap getMap(String name) {
        return new RedissonMap(commandExecutor, name, this, null, null);
    }

    @Override
    public  RMap getMap(String name, MapOptions options) {
        return new RedissonMap(commandExecutor, name, this, options, writeBehindService);
    }

    @Override
    public  RSetMultimap getSetMultimap(String name) {
        return new RedissonSetMultimap(commandExecutor, name);
    }

    @Override
    public  RSetMultimapCache getSetMultimapCache(String name) {
        return new RedissonSetMultimapCache(evictionScheduler, commandExecutor, name);
    }

    @Override
    public  RSetMultimapCache getSetMultimapCache(String name, Codec codec) {
        return new RedissonSetMultimapCache(evictionScheduler, codec, commandExecutor, name);
    }

    @Override
    public  RListMultimapCache getListMultimapCache(String name) {
        return new RedissonListMultimapCache(evictionScheduler, commandExecutor, name);
    }

    @Override
    public  RListMultimapCache getListMultimapCache(String name, Codec codec) {
        return new RedissonListMultimapCache(evictionScheduler, codec, commandExecutor, name);
    }

    @Override
    public  RSetMultimap getSetMultimap(String name, Codec codec) {
        return new RedissonSetMultimap(codec, commandExecutor, name);
    }

    @Override
    public  RSetCache getSetCache(String name) {
        return new RedissonSetCache(evictionScheduler, commandExecutor, name, this);
    }

    @Override
    public  RSetCache getSetCache(String name, Codec codec) {
        return new RedissonSetCache(codec, evictionScheduler, commandExecutor, name, this);
    }

    @Override
    public  RMapCache getMapCache(String name) {
        return new RedissonMapCache(evictionScheduler, commandExecutor, name, this, null, null);
    }

    @Override
    public  RMapCache getMapCache(String name, MapOptions options) {
        return new RedissonMapCache(evictionScheduler, commandExecutor, name, this, options, writeBehindService);
    }

    @Override
    public  RMapCache getMapCache(String name, Codec codec) {
        return new RedissonMapCache(codec, evictionScheduler, commandExecutor, name, this, null, null);
    }

    @Override
    public  RMapCache getMapCache(String name, Codec codec, MapOptions options) {
        return new RedissonMapCache(codec, evictionScheduler, commandExecutor, name, this, options, writeBehindService);
    }

    @Override
    public  RMap getMap(String name, Codec codec) {
        return new RedissonMap(codec, commandExecutor, name, this, null, null);
    }

    @Override
    public  RMap getMap(String name, Codec codec, MapOptions options) {
        return new RedissonMap(codec, commandExecutor, name, this, options, writeBehindService);
    }

    @Override
    public RLock getLock(String name) {
        return new RedissonLock(commandExecutor, name);
    }

    @Override
    public RLock getSpinLock(String name) {
        return getSpinLock(name, LockOptions.defaults());
    }

    @Override
    public RLock getSpinLock(String name, LockOptions.BackOff backOff) {
        return new RedissonSpinLock(commandExecutor, name, backOff);
    }

    @Override
    public RLock getMultiLock(RLock... locks) {
        return new RedissonMultiLock(locks);
    }
    
    @Override
    public RLock getRedLock(RLock... locks) {
        return new RedissonRedLock(locks);
    }

    @Override
    public RLock getFairLock(String name) {
        return new RedissonFairLock(commandExecutor, name);
    }

    @Override
    public RReadWriteLock getReadWriteLock(String name) {
        return new RedissonReadWriteLock(commandExecutor, name);
    }

    @Override
    public  RSet getSet(String name) {
        return new RedissonSet(commandExecutor, name, this);
    }

    @Override
    public  RSet getSet(String name, Codec codec) {
        return new RedissonSet(codec, commandExecutor, name, this);
    }

    @Override
    public RFunction getFunction() {
        return new RedissonFuction(commandExecutor);
    }

    @Override
    public RFunction getFunction(Codec codec) {
        return new RedissonFuction(commandExecutor, codec);
    }

    @Override
    public RScript getScript() {
        return new RedissonScript(commandExecutor);
    }
    
    @Override
    public RScript getScript(Codec codec) {
        return new RedissonScript(commandExecutor, codec);
    }

    @Override
    public RScheduledExecutorService getExecutorService(String name) {
        return getExecutorService(name, connectionManager.getCodec());
    }

    @Override
    public RScheduledExecutorService getExecutorService(String name, ExecutorOptions options) {
        return getExecutorService(name, connectionManager.getCodec(), options);
    }

    @Override
    public RScheduledExecutorService getExecutorService(String name, Codec codec) {
        return getExecutorService(name, codec, ExecutorOptions.defaults());
    }

    @Override
    public RScheduledExecutorService getExecutorService(String name, Codec codec, ExecutorOptions options) {
        return new RedissonExecutorService(codec, commandExecutor, this, name, queueTransferService, responses, options);
    }

    @Override
    public RRemoteService getRemoteService() {
        return getRemoteService("redisson_rs", connectionManager.getCodec());
    }

    @Override
    public RRemoteService getRemoteService(String name) {
        return getRemoteService(name, connectionManager.getCodec());
    }

    @Override
    public RRemoteService getRemoteService(Codec codec) {
        return getRemoteService("redisson_rs", codec);
    }

    @Override
    public RRemoteService getRemoteService(String name, Codec codec) {
        String executorId = connectionManager.getId();
        if (codec != connectionManager.getCodec()) {
            executorId = executorId + ":" + name;
        }
        return new RedissonRemoteService(codec, name, commandExecutor, executorId, responses);
    }

    @Override
    public  RSortedSet getSortedSet(String name) {
        return new RedissonSortedSet(commandExecutor, name, this);
    }

    @Override
    public  RSortedSet getSortedSet(String name, Codec codec) {
        return new RedissonSortedSet(codec, commandExecutor, name, this);
    }

    @Override
    public  RScoredSortedSet getScoredSortedSet(String name) {
        return new RedissonScoredSortedSet(commandExecutor, name, this);
    }

    @Override
    public  RScoredSortedSet getScoredSortedSet(String name, Codec codec) {
        return new RedissonScoredSortedSet(codec, commandExecutor, name, this);
    }

    @Override
    public RLexSortedSet getLexSortedSet(String name) {
        return new RedissonLexSortedSet(commandExecutor, name, this);
    }

    @Override
    public RShardedTopic getShardedTopic(String name) {
        return new RedissonShardedTopic(commandExecutor, name);
    }

    @Override
    public RShardedTopic getShardedTopic(String name, Codec codec) {
        return new RedissonShardedTopic(codec, commandExecutor, name);
    }

    @Override
    public RTopic getTopic(String name) {
        return new RedissonTopic(commandExecutor, name);
    }

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

    @Override
    public RReliableTopic getReliableTopic(String name) {
        return new RedissonReliableTopic(commandExecutor, name);
    }

    @Override
    public RReliableTopic getReliableTopic(String name, Codec codec) {
        return new RedissonReliableTopic(codec, commandExecutor, name);
    }

    @Override
    public RPatternTopic getPatternTopic(String pattern) {
        return new RedissonPatternTopic(commandExecutor, pattern);
    }

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

    @Override
    public  RDelayedQueue getDelayedQueue(RQueue destinationQueue) {
        if (destinationQueue == null) {
            throw new NullPointerException();
        }
        return new RedissonDelayedQueue(queueTransferService, destinationQueue.getCodec(), commandExecutor, destinationQueue.getName());
    }

    @Override
    public  RQueue getQueue(String name) {
        return new RedissonQueue(commandExecutor, name, this);
    }

    @Override
    public  RQueue getQueue(String name, Codec codec) {
        return new RedissonQueue(codec, commandExecutor, name, this);
    }

    @Override
    public  RTransferQueue getTransferQueue(String name) {
        String remoteName = RedissonObject.suffixName(name, "remoteService");
        RRemoteService service = getRemoteService(remoteName);
        return new RedissonTransferQueue(commandExecutor, name, service);
    }

    @Override
    public  RTransferQueue getTransferQueue(String name, Codec codec) {
        String remoteName = RedissonObject.suffixName(name, "remoteService");
        RRemoteService service = getRemoteService(remoteName);
        return new RedissonTransferQueue(codec, commandExecutor, name, service);
    }

    @Override
    public  RRingBuffer getRingBuffer(String name) {
        return new RedissonRingBuffer(commandExecutor, name, this);
    }
    
    @Override
    public  RRingBuffer getRingBuffer(String name, Codec codec) {
        return new RedissonRingBuffer(codec, commandExecutor, name, this);
    }
    
    @Override
    public  RBlockingQueue getBlockingQueue(String name) {
        return new RedissonBlockingQueue(commandExecutor, name, this);
    }

    @Override
    public  RBlockingQueue getBlockingQueue(String name, Codec codec) {
        return new RedissonBlockingQueue(codec, commandExecutor, name, this);
    }

    @Override
    public  RBoundedBlockingQueue getBoundedBlockingQueue(String name) {
        return new RedissonBoundedBlockingQueue(commandExecutor, name, this);
    }

    @Override
    public  RBoundedBlockingQueue getBoundedBlockingQueue(String name, Codec codec) {
        return new RedissonBoundedBlockingQueue(codec, commandExecutor, name, this);
    }

    @Override
    public  RDeque getDeque(String name) {
        return new RedissonDeque(commandExecutor, name, this);
    }

    @Override
    public  RDeque getDeque(String name, Codec codec) {
        return new RedissonDeque(codec, commandExecutor, name, this);
    }

    @Override
    public  RBlockingDeque getBlockingDeque(String name) {
        return new RedissonBlockingDeque(commandExecutor, name, this);
    }

    @Override
    public  RBlockingDeque getBlockingDeque(String name, Codec codec) {
        return new RedissonBlockingDeque(codec, commandExecutor, name, this);
    };

    @Override
    public RAtomicLong getAtomicLong(String name) {
        return new RedissonAtomicLong(commandExecutor, name);
    }

    @Override
    public RLongAdder getLongAdder(String name) {
        return new RedissonLongAdder(commandExecutor, name, this);
    }

    @Override
    public RDoubleAdder getDoubleAdder(String name) {
        return new RedissonDoubleAdder(commandExecutor, name, this);
    }

    @Override
    public RAtomicDouble getAtomicDouble(String name) {
        return new RedissonAtomicDouble(commandExecutor, name);
    }

    @Override
    public RCountDownLatch getCountDownLatch(String name) {
        return new RedissonCountDownLatch(commandExecutor, name);
    }

    @Override
    public RBitSet getBitSet(String name) {
        return new RedissonBitSet(commandExecutor, name);
    }

    @Override
    public RSemaphore getSemaphore(String name) {
        return new RedissonSemaphore(commandExecutor, name);
    }

    @Override
    public RPermitExpirableSemaphore getPermitExpirableSemaphore(String name) {
        return new RedissonPermitExpirableSemaphore(commandExecutor, name);
    }

    @Override
    public  RBloomFilter getBloomFilter(String name) {
        return new RedissonBloomFilter(commandExecutor, name);
    }

    @Override
    public  RBloomFilter getBloomFilter(String name, Codec codec) {
        return new RedissonBloomFilter(codec, commandExecutor, name);
    }

    @Override
    public RIdGenerator getIdGenerator(String name) {
        return new RedissonIdGenerator(commandExecutor, name);
    }

    @Override
    public RKeys getKeys() {
        return new RedissonKeys(commandExecutor);
    }

    @Override
    public RTransaction createTransaction(TransactionOptions options) {
        return new RedissonTransaction(commandExecutor, options);
    }

    @Override
    public RBatch createBatch(BatchOptions options) {
        return new RedissonBatch(evictionScheduler, commandExecutor, options);
    }

    @Override
    public RBatch createBatch() {
        return createBatch(BatchOptions.defaults());
    }

    @Override
    public RLiveObjectService getLiveObjectService() {
        return new RedissonLiveObjectService(liveObjectClassCache, commandExecutor);
    }

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


    @Override
    public void shutdown(long quietPeriod, long timeout, TimeUnit unit) {
        writeBehindService.stop();
        connectionManager.shutdown(quietPeriod, timeout, unit);
    }

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

    @Override
    public  T getRedisNodes(com.github.lontime.shaded.org.redisson.api.redisnode.RedisNodes nodes) {
        if (nodes.getClazz() == RedisSingle.class) {
            if (config.isSentinelConfig() || config.isClusterConfig()) {
                throw new IllegalArgumentException("Can't be used in non Redis single configuration");
            }
            return (T) new RedissonSingleNode(connectionManager, commandExecutor);
        }
        if (nodes.getClazz() == RedisCluster.class) {
            if (!config.isClusterConfig()) {
                throw new IllegalArgumentException("Can't be used in non Redis Cluster configuration");
            }
            return (T) new RedissonClusterNodes(connectionManager, commandExecutor);
        }
        if (nodes.getClazz() == RedisSentinelMasterSlave.class) {
            if (!config.isSentinelConfig()) {
                throw new IllegalArgumentException("Can't be used in non Redis Sentinel configuration");
            }
            return (T) new RedissonSentinelMasterSlaveNodes(connectionManager, commandExecutor);
        }
        if (nodes.getClazz() == RedisMasterSlave.class) {
            if (config.isSentinelConfig() || config.isClusterConfig()) {
                throw new IllegalArgumentException("Can't be used in non Redis Master Slave configuration");
            }
            return (T) new RedissonMasterSlaveNodes(connectionManager, commandExecutor);
        }
        throw new IllegalArgumentException();
    }

    @Override
    public NodesGroup getNodesGroup() {
        return new RedisNodes(connectionManager, commandExecutor);
    }

    @Override
    public ClusterNodesGroup getClusterNodesGroup() {
        if (!connectionManager.isClusterMode()) {
            throw new IllegalStateException("Redisson is not in cluster mode!");
        }
        return new RedisClusterNodes(connectionManager, commandExecutor);
    }

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

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

    @Override
    public  RPriorityQueue getPriorityQueue(String name) {
        return new RedissonPriorityQueue(commandExecutor, name, this);
    }

    @Override
    public  RPriorityQueue getPriorityQueue(String name, Codec codec) {
        return new RedissonPriorityQueue(codec, commandExecutor, name, this);
    }

    @Override
    public  RPriorityBlockingQueue getPriorityBlockingQueue(String name) {
        return new RedissonPriorityBlockingQueue(commandExecutor, name, this);
    }

    @Override
    public  RPriorityBlockingQueue getPriorityBlockingQueue(String name, Codec codec) {
        return new RedissonPriorityBlockingQueue(codec, commandExecutor, name, this);
    }

    @Override
    public  RPriorityBlockingDeque getPriorityBlockingDeque(String name) {
        return new RedissonPriorityBlockingDeque(commandExecutor, name, this);
    }

    @Override
    public  RPriorityBlockingDeque getPriorityBlockingDeque(String name, Codec codec) {
        return new RedissonPriorityBlockingDeque(codec, commandExecutor, name, this);
    }


    @Override
    public  RPriorityDeque getPriorityDeque(String name) {
        return new RedissonPriorityDeque(commandExecutor, name, this);
    }

    @Override
    public  RPriorityDeque getPriorityDeque(String name, Codec codec) {
        return new RedissonPriorityDeque(codec, commandExecutor, name, this);
    }

    @Override
    public String getId() {
        return connectionManager.getId();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy