Permalink
Browse files

Added getLock(key) method to RMap, RMultimap interfaces. #747

  • Loading branch information...
1 parent 41642fc commit 5cb8ab442c1234f199e90853a6ab11575cd6423a Nikita committed Jan 11, 2017
@@ -240,12 +240,12 @@ public RBuckets getBuckets(Codec codec) {
@Override
public <K, V> RListMultimap<K, V> getListMultimap(String name) {
- return new RedissonListMultimap<K, V>(connectionManager.getCommandExecutor(), name);
+ return new RedissonListMultimap<K, V>(this, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RListMultimap<K, V> getListMultimap(String name, Codec codec) {
- return new RedissonListMultimap<K, V>(codec, connectionManager.getCommandExecutor(), name);
+ return new RedissonListMultimap<K, V>(this, codec, connectionManager.getCommandExecutor(), name);
}
@Override
@@ -260,37 +260,37 @@ public RBuckets getBuckets(Codec codec) {
@Override
public <K, V> RMap<K, V> getMap(String name) {
- return new RedissonMap<K, V>(connectionManager.getCommandExecutor(), name);
+ return new RedissonMap<K, V>(this, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RSetMultimap<K, V> getSetMultimap(String name) {
- return new RedissonSetMultimap<K, V>(connectionManager.getCommandExecutor(), name);
+ return new RedissonSetMultimap<K, V>(this, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RSetMultimapCache<K, V> getSetMultimapCache(String name) {
- return new RedissonSetMultimapCache<K, V>(evictionScheduler, connectionManager.getCommandExecutor(), name);
+ return new RedissonSetMultimapCache<K, V>(this, evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RSetMultimapCache<K, V> getSetMultimapCache(String name, Codec codec) {
- return new RedissonSetMultimapCache<K, V>(evictionScheduler, codec, connectionManager.getCommandExecutor(), name);
+ return new RedissonSetMultimapCache<K, V>(this, evictionScheduler, codec, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RListMultimapCache<K, V> getListMultimapCache(String name) {
- return new RedissonListMultimapCache<K, V>(evictionScheduler, connectionManager.getCommandExecutor(), name);
+ return new RedissonListMultimapCache<K, V>(this, evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RListMultimapCache<K, V> getListMultimapCache(String name, Codec codec) {
- return new RedissonListMultimapCache<K, V>(evictionScheduler, codec, connectionManager.getCommandExecutor(), name);
+ return new RedissonListMultimapCache<K, V>(this, evictionScheduler, codec, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RSetMultimap<K, V> getSetMultimap(String name, Codec codec) {
- return new RedissonSetMultimap<K, V>(codec, connectionManager.getCommandExecutor(), name);
+ return new RedissonSetMultimap<K, V>(this, codec, connectionManager.getCommandExecutor(), name);
}
@Override
@@ -305,17 +305,17 @@ public RBuckets getBuckets(Codec codec) {
@Override
public <K, V> RMapCache<K, V> getMapCache(String name) {
- return new RedissonMapCache<K, V>(evictionScheduler, connectionManager.getCommandExecutor(), name);
+ return new RedissonMapCache<K, V>(this, evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RMapCache<K, V> getMapCache(String name, Codec codec) {
- return new RedissonMapCache<K, V>(codec, evictionScheduler, connectionManager.getCommandExecutor(), name);
+ return new RedissonMapCache<K, V>(this, codec, evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public <K, V> RMap<K, V> getMap(String name, Codec codec) {
- return new RedissonMap<K, V>(codec, connectionManager.getCommandExecutor(), name);
+ return new RedissonMap<K, V>(this, codec, connectionManager.getCommandExecutor(), name);
}
@Override
@@ -538,7 +538,7 @@ public RKeys getKeys() {
@Override
public RBatch createBatch() {
- RedissonBatch batch = new RedissonBatch(evictionScheduler, connectionManager);
+ RedissonBatch batch = new RedissonBatch(this, evictionScheduler, connectionManager);
if (config.isRedissonReferenceEnabled()) {
batch.enableRedissonReferenceSupport(this);
}
@@ -41,6 +41,7 @@
import org.redisson.api.RSetAsync;
import org.redisson.api.RSetCacheAsync;
import org.redisson.api.RTopicAsync;
+import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.command.CommandBatchService;
import org.redisson.connection.ConnectionManager;
@@ -55,10 +56,12 @@
private final EvictionScheduler evictionScheduler;
private final CommandBatchService executorService;
+ private final RedissonClient client;
- protected RedissonBatch(EvictionScheduler evictionScheduler, ConnectionManager connectionManager) {
+ protected RedissonBatch(RedissonClient client, EvictionScheduler evictionScheduler, ConnectionManager connectionManager) {
this.executorService = new CommandBatchService(connectionManager);
this.evictionScheduler = evictionScheduler;
+ this.client = client;
}
@Override
@@ -93,12 +96,12 @@ protected RedissonBatch(EvictionScheduler evictionScheduler, ConnectionManager c
@Override
public <K, V> RMapAsync<K, V> getMap(String name) {
- return new RedissonMap<K, V>(executorService, name);
+ return new RedissonMap<K, V>(client, executorService, name);
}
@Override
public <K, V> RMapAsync<K, V> getMap(String name, Codec codec) {
- return new RedissonMap<K, V>(codec, executorService, name);
+ return new RedissonMap<K, V>(client, codec, executorService, name);
}
@Override
@@ -193,12 +196,12 @@ public RBitSetAsync getBitSet(String name) {
@Override
public <K, V> RMapCacheAsync<K, V> getMapCache(String name, Codec codec) {
- return new RedissonMapCache<K, V>(codec, evictionScheduler, executorService, name);
+ return new RedissonMapCache<K, V>(client, codec, evictionScheduler, executorService, name);
}
@Override
public <K, V> RMapCacheAsync<K, V> getMapCache(String name) {
- return new RedissonMapCache<K, V>(evictionScheduler, executorService, name);
+ return new RedissonMapCache<K, V>(client, evictionScheduler, executorService, name);
}
@Override
@@ -243,22 +246,22 @@ public void executeSkipResult() {
@Override
public <K, V> RMultimapAsync<K, V> getSetMultimap(String name) {
- return new RedissonSetMultimap<K, V>(executorService, name);
+ return new RedissonSetMultimap<K, V>(client, executorService, name);
}
@Override
public <K, V> RMultimapAsync<K, V> getSetMultimap(String name, Codec codec) {
- return new RedissonSetMultimap<K, V>(codec, executorService, name);
+ return new RedissonSetMultimap<K, V>(client, codec, executorService, name);
}
@Override
public <K, V> RMultimapAsync<K, V> getListMultimap(String name) {
- return new RedissonListMultimap<K, V>(executorService, name);
+ return new RedissonListMultimap<K, V>(client, executorService, name);
}
@Override
public <K, V> RMultimapAsync<K, V> getListMultimap(String name, Codec codec) {
- return new RedissonListMultimap<K, V>(codec, executorService, name);
+ return new RedissonListMultimap<K, V>(client, codec, executorService, name);
}
@Override
@@ -273,22 +276,22 @@ public void executeSkipResult() {
@Override
public <K, V> RMultimapCacheAsync<K, V> getSetMultimapCache(String name) {
- return new RedissonSetMultimapCache<K, V>(evictionScheduler, executorService, name);
+ return new RedissonSetMultimapCache<K, V>(client, evictionScheduler, executorService, name);
}
@Override
public <K, V> RMultimapCacheAsync<K, V> getSetMultimapCache(String name, Codec codec) {
- return new RedissonSetMultimapCache<K, V>(evictionScheduler, codec, executorService, name);
+ return new RedissonSetMultimapCache<K, V>(client, evictionScheduler, codec, executorService, name);
}
@Override
public <K, V> RMultimapCacheAsync<K, V> getListMultimapCache(String name) {
- return new RedissonListMultimapCache<K, V>(evictionScheduler, executorService, name);
+ return new RedissonListMultimapCache<K, V>(client, evictionScheduler, executorService, name);
}
@Override
public <K, V> RMultimapCacheAsync<K, V> getListMultimapCache(String name, Codec codec) {
- return new RedissonListMultimapCache<K, V>(evictionScheduler, codec, executorService, name);
+ return new RedissonListMultimapCache<K, V>(client, evictionScheduler, codec, executorService, name);
}
protected void enableRedissonReferenceSupport(Redisson redisson) {
@@ -28,6 +28,7 @@
import org.redisson.api.RFuture;
import org.redisson.api.RList;
import org.redisson.api.RListMultimap;
+import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
@@ -44,12 +45,12 @@
private static final RedisStrictCommand<Boolean> LLEN_VALUE = new RedisStrictCommand<Boolean>("LLEN", new BooleanAmountReplayConvertor());
- RedissonListMultimap(CommandAsyncExecutor connectionManager, String name) {
- super(connectionManager, name);
+ RedissonListMultimap(RedissonClient client, CommandAsyncExecutor connectionManager, String name) {
+ super(client, connectionManager, name);
}
- RedissonListMultimap(Codec codec, CommandAsyncExecutor connectionManager, String name) {
- super(codec, connectionManager, name);
+ RedissonListMultimap(RedissonClient client, Codec codec, CommandAsyncExecutor connectionManager, String name) {
+ super(client, codec, connectionManager, name);
}
@Override
@@ -22,6 +22,7 @@
import org.redisson.api.RFuture;
import org.redisson.api.RList;
import org.redisson.api.RListMultimapCache;
+import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
@@ -36,14 +37,14 @@
private final RedissonMultimapCache<K> baseCache;
- RedissonListMultimapCache(EvictionScheduler evictionScheduler, CommandAsyncExecutor connectionManager, String name) {
- super(connectionManager, name);
+ RedissonListMultimapCache(RedissonClient client, EvictionScheduler evictionScheduler, CommandAsyncExecutor connectionManager, String name) {
+ super(client, connectionManager, name);
evictionScheduler.scheduleCleanMultimap(name, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, name, codec, getTimeoutSetName());
}
- RedissonListMultimapCache(EvictionScheduler evictionScheduler, Codec codec, CommandAsyncExecutor connectionManager, String name) {
- super(codec, connectionManager, name);
+ RedissonListMultimapCache(RedissonClient client, EvictionScheduler evictionScheduler, Codec codec, CommandAsyncExecutor connectionManager, String name) {
+ super(client, codec, connectionManager, name);
evictionScheduler.scheduleCleanMultimap(name, getTimeoutSetName());
baseCache = new RedissonMultimapCache<K>(connectionManager, name, codec, getTimeoutSetName());
}
@@ -190,12 +190,12 @@ public String toString() {
private int invalidationListenerId;
protected RedissonLocalCachedMap(RedissonClient redisson, CommandAsyncExecutor commandExecutor, String name, LocalCachedMapOptions options) {
- super(commandExecutor, name);
+ super(redisson, commandExecutor, name);
init(redisson, name, options);
}
protected RedissonLocalCachedMap(RedissonClient redisson, Codec codec, CommandAsyncExecutor connectionManager, String name, LocalCachedMapOptions options) {
- super(codec, connectionManager, name);
+ super(redisson, codec, connectionManager, name);
init(redisson, name, options);
}
@@ -15,6 +15,7 @@
*/
package org.redisson;
+import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.util.AbstractCollection;
@@ -29,7 +30,9 @@
import java.util.Set;
import org.redisson.api.RFuture;
+import org.redisson.api.RLock;
import org.redisson.api.RMap;
+import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.ScanCodec;
import org.redisson.client.codec.StringCodec;
@@ -42,6 +45,7 @@
import org.redisson.client.protocol.decoder.ScanObjectEntry;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.connection.decoder.MapGetAllDecoder;
+import org.redisson.misc.Hash;
/**
* Distributed and concurrent implementation of {@link java.util.concurrent.ConcurrentMap}
@@ -60,15 +64,34 @@
static final RedisCommand<Boolean> EVAL_REMOVE_VALUE = new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 4, ValueType.MAP);
static final RedisCommand<Object> EVAL_PUT = EVAL_REPLACE;
- protected RedissonMap(CommandAsyncExecutor commandExecutor, String name) {
+ private final RedissonClient client;
+
+ protected RedissonMap(RedissonClient client, CommandAsyncExecutor commandExecutor, String name) {
super(commandExecutor, name);
+ this.client = client;
}
- public RedissonMap(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
+ public RedissonMap(RedissonClient client, Codec codec, CommandAsyncExecutor commandExecutor, String name) {
super(codec, commandExecutor, name);
+ this.client = client;
}
@Override
+ public RLock getLock(K key) {
+ String lockName = getLockName(key);
+ return client.getLock(lockName);
+ }
+
+ private String getLockName(Object key) {
+ try {
+ byte[] keyState = codec.getMapKeyEncoder().encode(key);
+ return "{" + getName() + "}:" + Hash.hashToBase64(keyState) + ":key";
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
public int size() {
return get(sizeAsync());
}
@@ -27,6 +27,7 @@
import org.redisson.api.RFuture;
import org.redisson.api.RMapCache;
+import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.ScanCodec;
@@ -84,13 +85,13 @@
private static final RedisCommand<Boolean> EVAL_CONTAINS_VALUE = new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 7, ValueType.MAP_VALUE);
private static final RedisCommand<Long> EVAL_FAST_REMOVE = new RedisCommand<Long>("EVAL", 5, ValueType.MAP_KEY);
- protected RedissonMapCache(EvictionScheduler evictionScheduler, CommandAsyncExecutor commandExecutor, String name) {
- super(commandExecutor, name);
+ protected RedissonMapCache(RedissonClient redisson, EvictionScheduler evictionScheduler, CommandAsyncExecutor commandExecutor, String name) {
+ super(redisson, commandExecutor, name);
evictionScheduler.schedule(getName(), getTimeoutSetName(), getIdleSetName());
}
- public RedissonMapCache(Codec codec, EvictionScheduler evictionScheduler, CommandAsyncExecutor commandExecutor, String name) {
- super(codec, commandExecutor, name);
+ public RedissonMapCache(RedissonClient redisson, Codec codec, EvictionScheduler evictionScheduler, CommandAsyncExecutor commandExecutor, String name) {
+ super(redisson, codec, commandExecutor, name);
evictionScheduler.schedule(getName(), getTimeoutSetName(), getIdleSetName());
}
Oops, something went wrong.

0 comments on commit 5cb8ab4

Please sign in to comment.