+ add RedisCollection interface

+ extend the use of KeyBound interface
This commit is contained in:
Costin Leau
2010-12-07 17:35:50 +02:00
parent 0c413b5c9e
commit 55ceaefd8d
17 changed files with 74 additions and 46 deletions

View File

@@ -19,8 +19,9 @@ package org.springframework.data.keyvalue.redis.connection;
import org.springframework.data.keyvalue.redis.UncategorizedRedisException;
/**
* A connection (session) to a Redis server.
* The methods namings follows as much as possible the Redis conventions.
* A connection to a Redis server.
*
* The methods follow as much as possible the Redis names and conventions.
*
* @author Costin Leau
*/

View File

@@ -16,16 +16,17 @@
package org.springframework.data.keyvalue.redis.core;
/**
* Redis store for a certain key. Useful for creating views into Redis 'collection' types.
* Contract defining the bind of the implementing entity to a Redis 'key'.
* Useful for executing 'bound' operations or operating over Redis 'collection' or 'views'.
*
* @author Costin Leau
*/
public interface KeyBound<K> {
/**
* Returns the key associated with this store.
* Returns the key associated with this entity.
*
* @return
* @return key associated with the implementing entity
*/
K getKey();
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.keyvalue.redis.support.atomic;
import java.io.Serializable;
import java.util.Collections;
import org.springframework.data.keyvalue.redis.core.KeyBound;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.ValueOperations;
@@ -28,7 +29,7 @@ import org.springframework.data.keyvalue.redis.core.ValueOperations;
* @see java.util.concurrent.atomic.AtomicInteger
* @author Costin Leau
*/
public class RedisAtomicInteger extends Number implements Serializable {
public class RedisAtomicInteger extends Number implements Serializable, KeyBound<String> {
private final String key;
private ValueOperations<String, Integer> operations;
@@ -58,6 +59,11 @@ public class RedisAtomicInteger extends Number implements Serializable {
this.operations.set(redisCounter, initialValue);
}
@Override
public String getKey() {
return key;
}
/**
* Get the current value.
*

View File

@@ -18,6 +18,7 @@ package org.springframework.data.keyvalue.redis.support.atomic;
import java.io.Serializable;
import java.util.Collections;
import org.springframework.data.keyvalue.redis.core.KeyBound;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.ValueOperations;
@@ -28,7 +29,7 @@ import org.springframework.data.keyvalue.redis.core.ValueOperations;
* @see java.util.concurrent.atomic.AtomicLong
* @author Costin Leau
*/
public class RedisAtomicLong extends Number implements Serializable {
public class RedisAtomicLong extends Number implements Serializable, KeyBound<String> {
private final String key;
private ValueOperations<String, Long> operations;
@@ -57,6 +58,11 @@ public class RedisAtomicLong extends Number implements Serializable {
this.operations.set(redisCounter, initialValue);
}
@Override
public String getKey() {
return key;
}
/**
* Gets the current value.
*
@@ -206,7 +212,7 @@ public class RedisAtomicLong extends Number implements Serializable {
}
public long longValue() {
return (long) get();
return get();
}
public float floatValue() {

View File

@@ -21,16 +21,17 @@ import java.util.Collection;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
/**
* Base implementation for Redis collections.
* Base implementation for {@link RedisCollection}.
* Provides a skeletal implementation.
*
* @author Costin Leau
*/
public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> implements RedisStore<String> {
public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> implements RedisCollection<E> {
public static final String ENCODING = "UTF-8";
protected final String key;
protected final RedisOperations<String, E> operations;
private final String key;
private final RedisOperations<String, E> operations;
public <K> AbstractRedisCollection(String key, RedisOperations<String, E> operations) {
this.key = key;

View File

@@ -38,10 +38,10 @@ abstract class CollectionUtils {
return (List<E>) Arrays.asList(reverse);
}
static Collection<String> extractKeys(Collection<? extends RedisStore<String>> stores) {
static Collection<String> extractKeys(Collection<? extends RedisStore> stores) {
Collection<String> keys = new ArrayList<String>(stores.size());
for (RedisStore<String> store : stores) {
for (RedisStore store : stores) {
keys.add(store.getKey());
}

View File

@@ -109,7 +109,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
// intersect the set with a non existing one
// TODO: find a safer way to clean the set
String randomKey = UUID.randomUUID().toString();
boundSetOps.intersectAndStore(key, Collections.singleton(randomKey));
boundSetOps.intersectAndStore(getKey(), Collections.singleton(randomKey));
}
@Override

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2010 the original author or authors.
*
* 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.springframework.data.keyvalue.redis.support.collections;
import java.util.Collection;
/**
* Redis extension for the {@link Collection} contract.
*
* @author Costin Leau
*/
public interface RedisCollection<E> extends RedisStore {
}

View File

@@ -25,7 +25,7 @@ import java.util.concurrent.BlockingDeque;
*
* @author Costin Leau
*/
public interface RedisList<E> extends RedisStore<String>, List<E>, BlockingDeque<E> {
public interface RedisList<E> extends RedisCollection<E>, List<E>, BlockingDeque<E> {
List<E> range(long start, long end);

View File

@@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentMap;
*
* @author Costin Leau
*/
public interface RedisMap<K, V> extends RedisStore<String>, ConcurrentMap<K, V> {
public interface RedisMap<K, V> extends RedisStore, ConcurrentMap<K, V> {
Long increment(K key, long delta);
}

View File

@@ -24,7 +24,7 @@ import java.util.Set;
*
* @author Costin Leau
*/
public interface RedisSet<E> extends RedisStore<String>, Set<E> {
public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
Set<E> intersect(Collection<? extends RedisSet<?>> sets);

View File

@@ -15,27 +15,23 @@
*/
package org.springframework.data.keyvalue.redis.support.collections;
import org.springframework.data.keyvalue.redis.core.KeyBound;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
/**
* Basic interface for Redis-based collections.
* Basic interface for Redis-based collections.
*
* Offers access to the {@link RedisOperations} entity
* used for executing commands against the backing store.
*
* @author Costin Leau
*/
public interface RedisStore<K> {
/**
* Returns the key used by the backing Redis store for this collection.
*
* @return Redis key
*/
K getKey();
public interface RedisStore extends KeyBound<String> {
/**
* Returns the underlying Redis operations used by the backing implementation.
*
* @return operations
*/
RedisOperations<K, ?> getOperations();
RedisOperations<String, ?> getOperations();
}

View File

@@ -27,7 +27,7 @@ import java.util.SortedSet;
*
* @author Costin Leau
*/
public interface RedisZSet<E> extends RedisStore<String>, Set<E> {
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
RedisZSet<E> intersectAndStore(String destKey, Collection<? extends RedisZSet<?>> sets);

View File

@@ -40,8 +40,6 @@ import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.data.keyvalue.redis.core.RedisCallback;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
import org.springframework.data.keyvalue.redis.support.collections.AbstractRedisCollection;
import org.springframework.data.keyvalue.redis.support.collections.RedisStore;
/**
@@ -65,7 +63,7 @@ public abstract class AbstractRedisCollectionTests<T> {
abstract AbstractRedisCollection<T> createCollection();
abstract RedisStore<T> copyStore(RedisStore<T> store);
abstract RedisStore copyStore(RedisStore store);
public AbstractRedisCollectionTests(ObjectFactory<T> factory, RedisTemplate template) {

View File

@@ -27,8 +27,6 @@ import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
import org.springframework.data.keyvalue.redis.support.collections.DefaultRedisList;
import org.springframework.data.keyvalue.redis.support.collections.RedisList;
/**
* Integration test for RedisList
@@ -281,7 +279,7 @@ public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionT
@Test
public void testCappedCollection() throws Exception {
RedisList<T> cappedList = new DefaultRedisList<T>(template.forList(collection.key + ":capped"), 1);
RedisList<T> cappedList = new DefaultRedisList<T>(template.forList(collection.getKey() + ":capped"), 1);
T first = getT();
cappedList.offer(first);
assertEquals(1, cappedList.size());

View File

@@ -43,9 +43,6 @@ import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory
import org.springframework.data.keyvalue.redis.core.RedisCallback;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
import org.springframework.data.keyvalue.redis.support.collections.DefaultRedisMap;
import org.springframework.data.keyvalue.redis.support.collections.RedisMap;
import org.springframework.data.keyvalue.redis.support.collections.RedisStore;
/**
* Integration test for Redis Map.
@@ -98,7 +95,7 @@ public abstract class AbstractRedisMapTests<K, V> {
return valueFactory.instance();
}
protected RedisStore<String> copyStore(RedisStore<String> store) {
protected RedisStore copyStore(RedisStore store) {
return new DefaultRedisMap(store.getKey(), store.getOperations());
}
@@ -158,7 +155,7 @@ public abstract class AbstractRedisMapTests<K, V> {
@Test
public void testEquals() {
RedisStore<String> clone = copyStore(map);
RedisStore clone = copyStore(map);
assertEquals(clone, map);
assertEquals(clone, clone);
assertEquals(map, map);
@@ -167,7 +164,7 @@ public abstract class AbstractRedisMapTests<K, V> {
@Test
public void testNotEquals() {
RedisOperations<String, ?> ops = map.getOperations();
RedisStore<String> newInstance = new DefaultRedisMap<K, V>(ops.<K, V> forHash(map.getKey() + ":new"));
RedisStore newInstance = new DefaultRedisMap<K, V>(ops.<K, V> forHash(map.getKey() + ":new"));
assertFalse(map.equals(newInstance));
assertFalse(newInstance.equals(map));
}

View File

@@ -16,9 +16,6 @@
package org.springframework.data.keyvalue.redis.support.collections;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
import org.springframework.data.keyvalue.redis.support.collections.AbstractRedisCollection;
import org.springframework.data.keyvalue.redis.support.collections.DefaultRedisZSet;
import org.springframework.data.keyvalue.redis.support.collections.RedisStore;
/**
* Parameterized instance of Redis sorted set tests.
@@ -38,7 +35,7 @@ public class RedisZSetTests extends AbstractRedisZSetTest<Object> {
}
@Override
RedisStore<Object> copyStore(RedisStore<Object> store) {
RedisStore copyStore(RedisStore store) {
return new DefaultRedisZSet(store.getKey().toString(), store.getOperations());
}