DATAKV-44

+ renamed KeyBound to BoundKeyOperations (to be consistent with the other interfaces)
+ enhanced the number of methods available on BoundKeyOperations
This commit is contained in:
Costin Leau
2011-03-15 20:01:23 +02:00
parent a1bdf1b063
commit e4a805eb1c
22 changed files with 432 additions and 114 deletions

View File

@@ -24,7 +24,7 @@ import java.util.Set;
*
* @author Costin Leau
*/
public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
public interface BoundHashOperations<H, HK, HV> extends BoundKeyOperations<H> {
RedisOperations<H, ?> getOperations();

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2010-2011 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.core;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Operations over a Redis key.
*
* Useful for executing common key-'bound' operations to all implementations.
*
* @author Costin Leau
*/
public interface BoundKeyOperations<K> {
/**
* Returns the key associated with this entity.
*
* @return key associated with the implementing entity
*/
K getKey();
/**
* Returns the associated Redis type.
*
* @return key type
*/
DataType getType();
/**
* Returns the expiration of this key.
*
* @return expiration value (in seconds)
*/
Long getExpire();
/**
* Sets the key time-to-live/expiration.
*
* @param timeout expiration value
* @param unit expiration unit
* @return true if expiration was set, false otherwise
*/
Boolean expire(long timeout, TimeUnit unit);
/**
* Sets the key time-to-live/expiration.
*
* @param date expiration date
* @return true if expiration was set, false otherwise
*/
Boolean expireAt(Date date);
/**
* Removes the expiration (if any) of the key.
*/
void persist();
/**
* Renames the key.
*
* @param newKey new key
*/
void rename(K newKey);
/**
* Renames the key (if the new key does not exist). Note that the underlying key
* changes only if the operation returns true (which does not happen if the connection
* is pipelined or in multi mode).
*
* @param newKey new key
* @return true if rename was successful, false otherwise
*/
Boolean renameIfAbsent(K newKey);
}

View File

@@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit;
*
* @author Costin Leau
*/
public interface BoundListOperations<K, V> extends KeyBound<K> {
public interface BoundListOperations<K, V> extends BoundKeyOperations<K> {
RedisOperations<K, V> getOperations();

View File

@@ -24,7 +24,7 @@ import java.util.Set;
*
* @author Costin Leau
*/
public interface BoundSetOperations<K, V> extends KeyBound<K> {
public interface BoundSetOperations<K, V> extends BoundKeyOperations<K> {
RedisOperations<K, V> getOperations();

View File

@@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit;
*
* @author Costin Leau
*/
public interface BoundValueOperations<K, V> extends KeyBound<K> {
public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
RedisOperations<K, V> getOperations();

View File

@@ -25,7 +25,7 @@ import java.util.Set;
*
* @author Costin Leau
*/
public interface BoundZSetOperations<K, V> extends KeyBound<K> {
public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
RedisOperations<K, V> getOperations();

View File

@@ -19,12 +19,14 @@ import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Default implementation for {@link HashOperations}.
*
* @author Costin Leau
*/
class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implements BoundHashOperations<H, HK, HV> {
class DefaultBoundHashOperations<H, HK, HV> extends DefaultBoundKeyOperations<H> implements BoundHashOperations<H, HK, HV> {
private final HashOperations<H, HK, HV> ops;
@@ -35,7 +37,7 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
* @param template
*/
public DefaultBoundHashOperations(H key, RedisOperations<H, ?> operations) {
super(key);
super(key, operations);
this.ops = operations.opsForHash();
}
@@ -103,4 +105,9 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
public Map<HK, HV> entries() {
return ops.entries(getKey());
}
@Override
public DataType getType() {
return DataType.HASH;
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2010-2011 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.core;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* Default {@link BoundKeyOperations} implementation.
* Meant for internal usage.
*
* @author Costin Leau
*/
abstract class DefaultBoundKeyOperations<K> implements BoundKeyOperations<K> {
private K key;
private final RedisOperations<K, ?> ops;
public DefaultBoundKeyOperations(K key, RedisOperations<K, ?> operations) {
setKey(key);
this.ops = operations;
}
@Override
public K getKey() {
return key;
}
protected void setKey(K key) {
this.key = key;
}
@Override
public Boolean expire(long timeout, TimeUnit unit) {
return ops.expire(key, timeout, unit);
}
@Override
public Boolean expireAt(Date date) {
return ops.expireAt(key, date);
}
@Override
public Long getExpire() {
return ops.getExpire(key);
}
@Override
public void persist() {
ops.persist(key);
}
@Override
public void rename(K newKey) {
ops.rename(key, newKey);
key = newKey;
}
@Override
public Boolean renameIfAbsent(K newKey) {
Boolean result = ops.renameIfAbsent(key, newKey);
if (Boolean.TRUE.equals(result)) {
key = newKey;
}
return result;
}
}

View File

@@ -18,13 +18,15 @@ package org.springframework.data.keyvalue.redis.core;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Default implementation for {@link BoundListOperations}.
*
* @author Costin Leau
*/
class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements BoundListOperations<K, V> {
class DefaultBoundListOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundListOperations<K, V> {
private final ListOperations<K, V> ops;
@@ -35,7 +37,7 @@ class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements Bou
* @param operations
*/
public DefaultBoundListOperations(K key, RedisOperations<K, V> operations) {
super(key);
super(key, operations);
this.ops = operations.opsForList();
}
@@ -124,4 +126,9 @@ class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements Bou
public void set(long index, V value) {
ops.set(getKey(), index, value);
}
@Override
public DataType getType() {
return DataType.LIST;
}
}

View File

@@ -19,12 +19,14 @@ package org.springframework.data.keyvalue.redis.core;
import java.util.Collection;
import java.util.Set;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Default implementation for {@link BoundSetOperations}.
*
* @author Costin Leau
*/
class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements BoundSetOperations<K, V> {
class DefaultBoundSetOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundSetOperations<K, V> {
private final SetOperations<K, V> ops;
@@ -36,7 +38,7 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
* @param operations
*/
DefaultBoundSetOperations(K key, RedisOperations<K, V> operations) {
super(key);
super(key, operations);
this.ops = operations.opsForSet();
}
@@ -146,4 +148,9 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
public void unionAndStore(Collection<K> keys, K destKey) {
ops.unionAndStore(getKey(), keys, destKey);
}
@Override
public DataType getType() {
return DataType.SET;
}
}

View File

@@ -17,10 +17,12 @@ package org.springframework.data.keyvalue.redis.core;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* @author Costin Leau
*/
class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements BoundValueOperations<K, V> {
class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundValueOperations<K, V> {
private final ValueOperations<K, V> ops;
@@ -31,7 +33,7 @@ class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements Bo
* @param operations
*/
public DefaultBoundValueOperations(K key, RedisOperations<K, V> operations) {
super(key);
super(key, operations);
this.ops = operations.opsForValue();
}
@@ -89,4 +91,9 @@ class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements Bo
public RedisOperations<K, V> getOperations() {
return ops.getOperations();
}
@Override
public DataType getType() {
return DataType.STRING;
}
}

View File

@@ -19,12 +19,14 @@ package org.springframework.data.keyvalue.redis.core;
import java.util.Collection;
import java.util.Set;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Default implementation for {@link BoundZSetOperations}.
*
* @author Costin Leau
*/
class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements BoundZSetOperations<K, V> {
class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundZSetOperations<K, V> {
private final ZSetOperations<K, V> ops;
@@ -34,9 +36,9 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
* @param key
* @param oeprations
*/
public DefaultBoundZSetOperations(K key, RedisOperations<K, V> oeprations) {
super(key);
this.ops = oeprations.opsForZSet();
public DefaultBoundZSetOperations(K key, RedisOperations<K, V> operations) {
super(key, operations);
this.ops = operations.opsForZSet();
}
@Override
@@ -128,4 +130,9 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
public void unionAndStore(Collection<K> otherKeys, K destKey) {
ops.unionAndStore(getKey(), otherKeys, destKey);
}
@Override
public DataType getType() {
return DataType.ZSET;
}
}

View File

@@ -1,41 +0,0 @@
/*
* Copyright 2010-2011 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.core;
/**
* Default {@link KeyBound} implementation.
* Meant for internal usage.
*
* @author Costin Leau
*/
class DefaultKeyBound<K> implements KeyBound<K> {
private K key;
public DefaultKeyBound(K key) {
setKey(key);
}
@Override
public K getKey() {
return key;
}
protected void setKey(K key) {
this.key = key;
}
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright 2010-2011 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.core;
/**
* 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 entity.
*
* @return key associated with the implementing entity
*/
K getKey();
}

View File

@@ -17,9 +17,12 @@ package org.springframework.data.keyvalue.redis.support.atomic;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.data.keyvalue.redis.core.KeyBound;
import org.springframework.data.keyvalue.redis.core.BoundKeyOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
import org.springframework.data.keyvalue.redis.core.SessionCallback;
@@ -34,9 +37,9 @@ import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer;
* @see java.util.concurrent.atomic.AtomicInteger
* @author Costin Leau
*/
public class RedisAtomicInteger extends Number implements Serializable, KeyBound<String> {
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {
private final String key;
private volatile String key;
private ValueOperations<String, Integer> operations;
private RedisOperations<String, Integer> generalOps;
@@ -119,11 +122,6 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
this.operations.set(redisCounter, initialValue);
}
@Override
public String getKey() {
return key;
}
/**
* Get the current value.
*
@@ -261,4 +259,50 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
public double doubleValue() {
return (double) get();
}
@Override
public String getKey() {
return key;
}
@Override
public Boolean expire(long timeout, TimeUnit unit) {
return generalOps.expire(key, timeout, unit);
}
@Override
public Boolean expireAt(Date date) {
return generalOps.expireAt(key, date);
}
@Override
public Long getExpire() {
return generalOps.getExpire(key);
}
@Override
public void persist() {
generalOps.persist(key);
}
@Override
public void rename(String newKey) {
generalOps.rename(key, newKey);
key = newKey;
}
@Override
public Boolean renameIfAbsent(String newKey) {
Boolean result = generalOps.renameIfAbsent(key, newKey);
if (Boolean.TRUE.equals(result)) {
key = newKey;
}
return result;
}
@Override
public DataType getType() {
return DataType.STRING;
}
}

View File

@@ -17,9 +17,12 @@ package org.springframework.data.keyvalue.redis.support.atomic;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.data.keyvalue.redis.core.KeyBound;
import org.springframework.data.keyvalue.redis.core.BoundKeyOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
import org.springframework.data.keyvalue.redis.core.SessionCallback;
@@ -34,9 +37,9 @@ import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer;
* @see java.util.concurrent.atomic.AtomicLong
* @author Costin Leau
*/
public class RedisAtomicLong extends Number implements Serializable, KeyBound<String> {
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> {
private final String key;
private volatile String key;
private ValueOperations<String, Long> operations;
private RedisOperations<String, Long> generalOps;
@@ -118,11 +121,6 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
this.operations.set(redisCounter, initialValue);
}
@Override
public String getKey() {
return key;
}
/**
* Gets the current value.
*
@@ -264,4 +262,50 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
public double doubleValue() {
return (double) get();
}
@Override
public String getKey() {
return key;
}
@Override
public Boolean expire(long timeout, TimeUnit unit) {
return generalOps.expire(key, timeout, unit);
}
@Override
public Boolean expireAt(Date date) {
return generalOps.expireAt(key, date);
}
@Override
public Long getExpire() {
return generalOps.getExpire(key);
}
@Override
public void persist() {
generalOps.persist(key);
}
@Override
public void rename(String newKey) {
generalOps.rename(key, newKey);
key = newKey;
}
@Override
public Boolean renameIfAbsent(String newKey) {
Boolean result = generalOps.renameIfAbsent(key, newKey);
if (Boolean.TRUE.equals(result)) {
key = newKey;
}
return result;
}
@Override
public DataType getType() {
return DataType.STRING;
}
}

View File

@@ -17,6 +17,8 @@ package org.springframework.data.keyvalue.redis.support.collections;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
@@ -30,7 +32,7 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
public static final String ENCODING = "UTF-8";
private final String key;
private volatile String key;
private final RedisOperations<String, E> operations;
public <K> AbstractRedisCollection(String key, RedisOperations<String, E> operations) {
@@ -116,4 +118,40 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
sb.append(getKey());
return sb.toString();
}
@Override
public Boolean expire(long timeout, TimeUnit unit) {
return operations.expire(key, timeout, unit);
}
@Override
public Boolean expireAt(Date date) {
return operations.expireAt(key, date);
}
@Override
public Long getExpire() {
return operations.getExpire(key);
}
@Override
public void persist() {
operations.persist(key);
}
@Override
public void rename(String newKey) {
operations.rename(key, newKey);
key = newKey;
}
@Override
public Boolean renameIfAbsent(String newKey) {
Boolean result = operations.renameIfAbsent(key, newKey);
if (Boolean.TRUE.equals(result)) {
key = newKey;
}
return result;
}
}

View File

@@ -23,6 +23,7 @@ import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.core.BoundListOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
@@ -498,4 +499,9 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
public E takeLast() throws InterruptedException {
return pollLast(0, TimeUnit.SECONDS);
}
@Override
public DataType getType() {
return DataType.LIST;
}
}

View File

@@ -17,11 +17,14 @@ package org.springframework.data.keyvalue.redis.support.collections;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.core.BoundHashOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
@@ -84,11 +87,6 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
return hashOps.increment(key, delta);
}
@Override
public String getKey() {
return hashOps.getKey();
}
@Override
public RedisOperations<String, ?> getOperations() {
return hashOps.getOperations();
@@ -295,4 +293,45 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
// }
// }
}
@Override
public Boolean expire(long timeout, TimeUnit unit) {
return hashOps.expire(timeout, unit);
}
@Override
public Boolean expireAt(Date date) {
return hashOps.expireAt(date);
}
@Override
public Long getExpire() {
return hashOps.getExpire();
}
@Override
public void persist() {
hashOps.persist();
}
@Override
public String getKey() {
return hashOps.getKey();
}
@Override
public void rename(String newKey) {
hashOps.rename(newKey);
}
@Override
public Boolean renameIfAbsent(String newKey) {
return hashOps.renameIfAbsent(newKey);
}
@Override
public DataType getType() {
return hashOps.getType();
}
}

View File

@@ -21,6 +21,7 @@ import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.core.BoundSetOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
@@ -166,4 +167,9 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
public int size() {
return boundSetOps.size().intValue();
}
@Override
public DataType getType() {
return DataType.SET;
}
}

View File

@@ -20,6 +20,7 @@ import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.core.BoundZSetOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
@@ -211,4 +212,9 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
public Double score(Object o) {
return boundZSetOps.score(o);
}
@Override
public DataType getType() {
return DataType.ZSET;
}
}

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.data.keyvalue.redis.support.collections;
import org.springframework.data.keyvalue.redis.core.KeyBound;
import org.springframework.data.keyvalue.redis.core.BoundKeyOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
/**
@@ -26,7 +26,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
*
* @author Costin Leau
*/
public interface RedisStore extends KeyBound<String> {
public interface RedisStore extends BoundKeyOperations<String> {
/**
* Returns the underlying Redis operations used by the backing implementation.