/** * Copyright 2018 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.fr.third.org.redisson; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; import com.fr.third.org.redisson.api.ClusterNode; import com.fr.third.org.redisson.api.MapOptions; import com.fr.third.org.redisson.api.Node; import com.fr.third.org.redisson.api.NodesGroup; import com.fr.third.org.redisson.api.RAtomicDoubleReactive; import com.fr.third.org.redisson.api.RAtomicLongReactive; import com.fr.third.org.redisson.api.RBatchReactive; import com.fr.third.org.redisson.api.RBitSetReactive; import com.fr.third.org.redisson.api.RBlockingQueueReactive; import com.fr.third.org.redisson.api.RBucketReactive; import com.fr.third.org.redisson.api.RDequeReactive; import com.fr.third.org.redisson.api.RFuture; import com.fr.third.org.redisson.api.RHyperLogLogReactive; import com.fr.third.org.redisson.api.RKeysReactive; import com.fr.third.org.redisson.api.RLexSortedSetReactive; import com.fr.third.org.redisson.api.RListMultimapReactive; import com.fr.third.org.redisson.api.RListReactive; import com.fr.third.org.redisson.api.RLockReactive; import com.fr.third.org.redisson.api.RMapCacheReactive; import com.fr.third.org.redisson.api.RMapReactive; import com.fr.third.org.redisson.api.RPatternTopicReactive; import com.fr.third.org.redisson.api.RQueueReactive; import com.fr.third.org.redisson.api.RReadWriteLockReactive; import com.fr.third.org.redisson.api.RScoredSortedSetReactive; import com.fr.third.org.redisson.api.RScriptReactive; import com.fr.third.org.redisson.api.RSemaphoreReactive; import com.fr.third.org.redisson.api.RSetCacheReactive; import com.fr.third.org.redisson.api.RSetMultimapReactive; import com.fr.third.org.redisson.api.RSetReactive; import com.fr.third.org.redisson.api.RTopicReactive; import com.fr.third.org.redisson.api.RedissonReactiveClient; import com.fr.third.org.redisson.client.codec.Codec; import com.fr.third.org.redisson.client.protocol.RedisCommands; import com.fr.third.org.redisson.codec.ReferenceCodecProvider; import com.fr.third.org.redisson.config.Config; import com.fr.third.org.redisson.config.ConfigSupport; import com.fr.third.org.redisson.pubsub.SemaphorePubSub; import com.fr.third.org.redisson.api.ClusterNode; import com.fr.third.org.redisson.api.MapOptions; import com.fr.third.org.redisson.api.Node; import com.fr.third.org.redisson.api.NodesGroup; import com.fr.third.org.redisson.api.RAtomicDoubleReactive; import com.fr.third.org.redisson.api.RAtomicLongReactive; import com.fr.third.org.redisson.api.RBatchReactive; import com.fr.third.org.redisson.api.RBitSetReactive; import com.fr.third.org.redisson.api.RBlockingQueueReactive; import com.fr.third.org.redisson.api.RBucketReactive; import com.fr.third.org.redisson.api.RDequeReactive; import com.fr.third.org.redisson.api.RFuture; import com.fr.third.org.redisson.api.RHyperLogLogReactive; import com.fr.third.org.redisson.api.RKeysReactive; import com.fr.third.org.redisson.api.RLexSortedSetReactive; import com.fr.third.org.redisson.api.RListMultimapReactive; import com.fr.third.org.redisson.api.RListReactive; import com.fr.third.org.redisson.api.RLockReactive; import com.fr.third.org.redisson.api.RMapCacheReactive; import com.fr.third.org.redisson.api.RMapReactive; import com.fr.third.org.redisson.api.RPatternTopicReactive; import com.fr.third.org.redisson.api.RQueueReactive; import com.fr.third.org.redisson.api.RReadWriteLockReactive; import com.fr.third.org.redisson.api.RScoredSortedSetReactive; import com.fr.third.org.redisson.api.RScriptReactive; import com.fr.third.org.redisson.api.RSemaphoreReactive; import com.fr.third.org.redisson.api.RSetCacheReactive; import com.fr.third.org.redisson.api.RSetMultimapReactive; import com.fr.third.org.redisson.api.RSetReactive; import com.fr.third.org.redisson.api.RTopicReactive; import com.fr.third.org.redisson.api.RedissonReactiveClient; import com.fr.third.org.redisson.client.codec.Codec; import com.fr.third.org.redisson.client.protocol.RedisCommands; import com.fr.third.org.redisson.codec.ReferenceCodecProvider; import com.fr.third.org.redisson.command.CommandReactiveService; import com.fr.third.org.redisson.config.Config; import com.fr.third.org.redisson.config.ConfigSupport; import com.fr.third.org.redisson.connection.ConnectionManager; import com.fr.third.org.redisson.eviction.EvictionScheduler; import com.fr.third.org.redisson.pubsub.SemaphorePubSub; import com.fr.third.org.redisson.reactive.RedissonAtomicDoubleReactive; import com.fr.third.org.redisson.reactive.RedissonAtomicLongReactive; import com.fr.third.org.redisson.reactive.RedissonBatchReactive; import com.fr.third.org.redisson.reactive.RedissonBitSetReactive; import com.fr.third.org.redisson.reactive.RedissonBlockingQueueReactive; import com.fr.third.org.redisson.reactive.RedissonBucketReactive; import com.fr.third.org.redisson.reactive.RedissonDequeReactive; import com.fr.third.org.redisson.reactive.RedissonHyperLogLogReactive; import com.fr.third.org.redisson.reactive.RedissonKeysReactive; import com.fr.third.org.redisson.reactive.RedissonLexSortedSetReactive; import com.fr.third.org.redisson.reactive.RedissonListMultimapReactive; import com.fr.third.org.redisson.reactive.RedissonListReactive; import com.fr.third.org.redisson.reactive.RedissonLockReactive; import com.fr.third.org.redisson.reactive.RedissonMapCacheReactive; import com.fr.third.org.redisson.reactive.RedissonMapReactive; import com.fr.third.org.redisson.reactive.RedissonPatternTopicReactive; import com.fr.third.org.redisson.reactive.RedissonQueueReactive; import com.fr.third.org.redisson.reactive.RedissonReadWriteLockReactive; import com.fr.third.org.redisson.reactive.RedissonScoredSortedSetReactive; import com.fr.third.org.redisson.reactive.RedissonScriptReactive; import com.fr.third.org.redisson.reactive.RedissonSemaphoreReactive; import com.fr.third.org.redisson.reactive.RedissonSetCacheReactive; import com.fr.third.org.redisson.reactive.RedissonSetMultimapReactive; import com.fr.third.org.redisson.reactive.RedissonSetReactive; import com.fr.third.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); } @Override public RLockReactive getLock(String name) { return new RedissonLockReactive(commandExecutor, name); } @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 RAtomicDoubleReactive getAtomicDouble(String name) { return new RedissonAtomicDoubleReactive(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); 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); } }