From 5847d365484b4a198f5320974a6472cfc33a437a Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 6 Jan 2014 14:30:26 +0400 Subject: [PATCH] Additional interfaces added to all Redisson distributed objects --- src/main/java/org/redisson/Redisson.java | 27 +++++++++++++------ src/main/java/org/redisson/RedissonList.java | 13 +++++++-- src/main/java/org/redisson/RedissonLock.java | 5 ++-- src/main/java/org/redisson/RedissonMap.java | 21 +++++++++------ src/main/java/org/redisson/RedissonQueue.java | 5 ++-- src/main/java/org/redisson/RedissonSet.java | 17 ++++++++++-- src/main/java/org/redisson/core/RList.java | 7 +++++ src/main/java/org/redisson/core/RLock.java | 7 +++++ src/main/java/org/redisson/core/RMap.java | 7 +++++ src/main/java/org/redisson/core/RQueue.java | 7 +++++ src/main/java/org/redisson/core/RSet.java | 7 +++++ 11 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 src/main/java/org/redisson/core/RList.java create mode 100644 src/main/java/org/redisson/core/RLock.java create mode 100644 src/main/java/org/redisson/core/RMap.java create mode 100644 src/main/java/org/redisson/core/RQueue.java create mode 100644 src/main/java/org/redisson/core/RSet.java diff --git a/src/main/java/org/redisson/Redisson.java b/src/main/java/org/redisson/Redisson.java index a83b19788..b9763e59f 100644 --- a/src/main/java/org/redisson/Redisson.java +++ b/src/main/java/org/redisson/Redisson.java @@ -15,16 +15,18 @@ */ package org.redisson; -import java.util.List; -import java.util.Queue; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.locks.Lock; import org.redisson.core.RAtomicLong; import org.redisson.core.RCountDownLatch; +import org.redisson.core.RList; +import org.redisson.core.RLock; +import org.redisson.core.RMap; import org.redisson.core.RObject; +import org.redisson.core.RQueue; +import org.redisson.core.RSet; import org.redisson.core.RTopic; import com.lambdaworks.redis.RedisAsyncConnection; @@ -66,7 +68,7 @@ public class Redisson { return new Redisson(host, port); } - public List getList(String name) { + public RList getList(String name) { RedissonList list = listsMap.get(name); if (list == null) { RedisConnection connection = connect(); @@ -82,7 +84,7 @@ public class Redisson { return list; } - public ConcurrentMap getMap(String name) { + public RMap getMap(String name) { RedissonMap map = mapsMap.get(name); if (map == null) { RedisConnection connection = connect(); @@ -98,7 +100,7 @@ public class Redisson { return map; } - public Lock getLock(String name) { + public RLock getLock(String name) { RedissonLock lock = locksMap.get(name); if (lock == null) { RedisConnection connection = connect(); @@ -118,7 +120,7 @@ public class Redisson { return lock; } - public Set getSet(String name) { + public RSet getSet(String name) { RedissonSet set = setsMap.get(name); if (set == null) { RedisConnection connection = connect(); @@ -155,7 +157,7 @@ public class Redisson { } - public Queue getQueue(String name) { + public RQueue getQueue(String name) { RedissonQueue queue = queuesMap.get(name); if (queue == null) { RedisConnection connection = connect(); @@ -232,6 +234,15 @@ public class Redisson { if (robject instanceof RedissonCountDownLatch) { latchesMap.remove(robject.getName()); } + if (robject instanceof RedissonList) { + listsMap.remove(robject.getName()); + } + if (robject instanceof RedissonMap) { + mapsMap.remove(robject.getName()); + } + if (robject instanceof RedissonQueue) { + queuesMap.remove(robject.getName()); + } } public void shutdown() { diff --git a/src/main/java/org/redisson/RedissonList.java b/src/main/java/org/redisson/RedissonList.java index 712fa8a4c..b97d0a50a 100644 --- a/src/main/java/org/redisson/RedissonList.java +++ b/src/main/java/org/redisson/RedissonList.java @@ -22,9 +22,11 @@ import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; +import org.redisson.core.RList; + import com.lambdaworks.redis.RedisConnection; -public class RedissonList implements List { +public class RedissonList implements RList { private int batchSize = 50; @@ -363,8 +365,15 @@ public class RedissonList implements List { return connection; } - protected String getName() { + public String getName() { return name; } + @Override + public void destroy() { + connection.close(); + + redisson.remove(this); + } + } diff --git a/src/main/java/org/redisson/RedissonLock.java b/src/main/java/org/redisson/RedissonLock.java index 68347f4de..ca6a23dd5 100644 --- a/src/main/java/org/redisson/RedissonLock.java +++ b/src/main/java/org/redisson/RedissonLock.java @@ -20,16 +20,15 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; -import org.redisson.core.RObject; +import org.redisson.core.RLock; import com.lambdaworks.redis.RedisConnection; import com.lambdaworks.redis.pubsub.RedisPubSubAdapter; import com.lambdaworks.redis.pubsub.RedisPubSubConnection; // TODO make it reentrant -public class RedissonLock implements Lock, RObject { +public class RedissonLock implements RLock { private final Redisson redisson; private final RedisPubSubConnection pubSubConnection; diff --git a/src/main/java/org/redisson/RedissonMap.java b/src/main/java/org/redisson/RedissonMap.java index 7ce89e3da..1e4f306cb 100644 --- a/src/main/java/org/redisson/RedissonMap.java +++ b/src/main/java/org/redisson/RedissonMap.java @@ -22,10 +22,12 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentMap; +import org.redisson.core.RMap; + import com.lambdaworks.redis.RedisConnection; -//TODO make keys watching instead of map name -public class RedissonMap implements ConcurrentMap { +//TODO implement watching by keys instead of map name +public class RedissonMap implements RMap { private final RedisConnection connection; private final String name; @@ -37,14 +39,10 @@ public class RedissonMap implements ConcurrentMap { this.name = name; } - protected String getName() { + public String getName() { return name; } - protected RedisConnection getConnection() { - return connection; - } - @Override public int size() { return connection.hlen(name).intValue(); @@ -118,7 +116,7 @@ public class RedissonMap implements ConcurrentMap { @Override public V putIfAbsent(K key, V value) { while (true) { - Boolean res = getConnection().hsetnx(getName(), key, value); + Boolean res = connection.hsetnx(getName(), key, value); if (!res) { V result = get(key); if (result != null) { @@ -200,4 +198,11 @@ public class RedissonMap implements ConcurrentMap { } } + @Override + public void destroy() { + connection.close(); + + redisson.remove(this); + } + } diff --git a/src/main/java/org/redisson/RedissonQueue.java b/src/main/java/org/redisson/RedissonQueue.java index 4bdff8c27..d19964e70 100644 --- a/src/main/java/org/redisson/RedissonQueue.java +++ b/src/main/java/org/redisson/RedissonQueue.java @@ -16,11 +16,12 @@ package org.redisson; import java.util.NoSuchElementException; -import java.util.Queue; + +import org.redisson.core.RQueue; import com.lambdaworks.redis.RedisConnection; -public class RedissonQueue extends RedissonList implements Queue { +public class RedissonQueue extends RedissonList implements RQueue { RedissonQueue(Redisson redisson, RedisConnection connection, String name) { super(redisson, connection, name); diff --git a/src/main/java/org/redisson/RedissonSet.java b/src/main/java/org/redisson/RedissonSet.java index 7b7994952..e1022a10a 100644 --- a/src/main/java/org/redisson/RedissonSet.java +++ b/src/main/java/org/redisson/RedissonSet.java @@ -17,11 +17,12 @@ package org.redisson; import java.util.Collection; import java.util.Iterator; -import java.util.Set; + +import org.redisson.core.RSet; import com.lambdaworks.redis.RedisConnection; -public class RedissonSet implements Set { +public class RedissonSet implements RSet { private final Redisson redisson; private final RedisConnection connection; @@ -112,4 +113,16 @@ public class RedissonSet implements Set { connection.del(name); } + @Override + public String getName() { + return name; + } + + @Override + public void destroy() { + connection.close(); + + redisson.remove(this); + } + } diff --git a/src/main/java/org/redisson/core/RList.java b/src/main/java/org/redisson/core/RList.java new file mode 100644 index 000000000..92dc0567e --- /dev/null +++ b/src/main/java/org/redisson/core/RList.java @@ -0,0 +1,7 @@ +package org.redisson.core; + +import java.util.List; + +public interface RList extends List, RObject { + +} diff --git a/src/main/java/org/redisson/core/RLock.java b/src/main/java/org/redisson/core/RLock.java new file mode 100644 index 000000000..4704012ae --- /dev/null +++ b/src/main/java/org/redisson/core/RLock.java @@ -0,0 +1,7 @@ +package org.redisson.core; + +import java.util.concurrent.locks.Lock; + +public interface RLock extends Lock, RObject { + +} diff --git a/src/main/java/org/redisson/core/RMap.java b/src/main/java/org/redisson/core/RMap.java new file mode 100644 index 000000000..b416ec05c --- /dev/null +++ b/src/main/java/org/redisson/core/RMap.java @@ -0,0 +1,7 @@ +package org.redisson.core; + +import java.util.concurrent.ConcurrentMap; + +public interface RMap extends ConcurrentMap, RObject { + +} diff --git a/src/main/java/org/redisson/core/RQueue.java b/src/main/java/org/redisson/core/RQueue.java new file mode 100644 index 000000000..9a87abbfe --- /dev/null +++ b/src/main/java/org/redisson/core/RQueue.java @@ -0,0 +1,7 @@ +package org.redisson.core; + +import java.util.Queue; + +public interface RQueue extends Queue, RObject { + +} diff --git a/src/main/java/org/redisson/core/RSet.java b/src/main/java/org/redisson/core/RSet.java new file mode 100644 index 000000000..c1e393d31 --- /dev/null +++ b/src/main/java/org/redisson/core/RSet.java @@ -0,0 +1,7 @@ +package org.redisson.core; + +import java.util.Set; + +public interface RSet extends Set, RObject { + +}