DATAKV-44
DATAKV-45 DATAKV-52 + remove move and removeIfAbsent methods, as their semantics are too 'slippery' in some situations
This commit is contained in:
@@ -19,7 +19,6 @@ import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.data.keyvalue.redis.connection.DataType;
|
||||
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
|
||||
|
||||
/**
|
||||
* Operations over a Redis key.
|
||||
@@ -27,10 +26,7 @@ import org.springframework.data.keyvalue.redis.connection.RedisConnection;
|
||||
* Useful for executing common key-'bound' operations to all implementations.
|
||||
*
|
||||
* <p>As the rest of the APIs, if the underlying connection is pipelined or queued/in multi mode,
|
||||
* all methods will return null. In such scenarios, to prevent any data inconsistencies, mutative
|
||||
* methods that query the store (such as {@link #renameIfAbsent(Object)} or {@link #move(int)}) will throw
|
||||
* an exception.
|
||||
*
|
||||
* all methods will return null.
|
||||
* </p>
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@@ -86,28 +82,4 @@ public interface BoundKeyOperations<K> {
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Moves the key (if it exists) to the specified database. If the key already exists in the
|
||||
* destination database, or it does not exist in the source database, it does nothing.
|
||||
* <p/>
|
||||
* As opposed to the raw move command, the database of the underlying connection is switched as well
|
||||
* to the new database.
|
||||
*
|
||||
* @see RedisConnection#select(int)
|
||||
* @see RedisConnection#move(byte[], int)
|
||||
* @param dbIndex database index
|
||||
* @return true if the operation succeed, false otherwise
|
||||
*/
|
||||
Boolean move(int dbIndex);
|
||||
}
|
||||
@@ -69,24 +69,4 @@ abstract class DefaultBoundKeyOperations<K> implements BoundKeyOperations<K> {
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean move(int dbIndex) {
|
||||
Boolean move = ops.move(key, dbIndex);
|
||||
|
||||
if (Boolean.TRUE.equals(move)) {
|
||||
ops.select(dbIndex);
|
||||
}
|
||||
return move;
|
||||
}
|
||||
}
|
||||
@@ -87,8 +87,6 @@ public interface RedisOperations<K, V> {
|
||||
|
||||
K randomKey();
|
||||
|
||||
void select(int dbIndex);
|
||||
|
||||
void rename(K oldKey, K newKey);
|
||||
|
||||
Boolean renameIfAbsent(K oldKey, K newKey);
|
||||
@@ -207,13 +205,15 @@ public interface RedisOperations<K, V> {
|
||||
|
||||
List<V> sort(SortQuery<K> query);
|
||||
|
||||
|
||||
<T> List<T> sort(SortQuery<K> query, RedisSerializer<T> resultSerializer);
|
||||
|
||||
|
||||
<T> List<T> sort(SortQuery<K> query, BulkMapper<T, V> bulkMapper);
|
||||
|
||||
<T, S> List<T> sort(SortQuery<K> query, BulkMapper<T, S> bulkMapper, RedisSerializer<S> resultSerializer);
|
||||
|
||||
Long sort(SortQuery<K> query, K storeKey);
|
||||
|
||||
RedisSerializer<?> getValueSerializer();
|
||||
|
||||
RedisSerializer<?> getKeySerializer();
|
||||
}
|
||||
@@ -574,18 +574,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}, true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void select(final int dbIndex) {
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.select(dbIndex);
|
||||
return null;
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public K randomKey() {
|
||||
byte[] rawKey = execute(new RedisCallback<byte[]>() {
|
||||
|
||||
@@ -291,26 +291,6 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
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 Boolean move(int dbIndex) {
|
||||
Boolean move = generalOps.move(key, dbIndex);
|
||||
|
||||
if (Boolean.TRUE.equals(move)) {
|
||||
generalOps.select(dbIndex);
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataType getType() {
|
||||
return DataType.STRING;
|
||||
|
||||
@@ -294,26 +294,6 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
|
||||
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 Boolean move(int dbIndex) {
|
||||
Boolean move = generalOps.move(key, dbIndex);
|
||||
|
||||
if (Boolean.TRUE.equals(move)) {
|
||||
generalOps.select(dbIndex);
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataType getType() {
|
||||
return DataType.STRING;
|
||||
|
||||
@@ -144,24 +144,4 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
|
||||
CollectionUtils.rename(key, newKey, operations);
|
||||
key = newKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean renameIfAbsent(final String newKey) {
|
||||
Boolean result = CollectionUtils.renameIfAbsent(key, newKey, operations);
|
||||
|
||||
if (Boolean.TRUE.equals(result)) {
|
||||
key = newKey;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean move(int dbIndex) {
|
||||
Boolean move = operations.move(key, dbIndex);
|
||||
|
||||
if (Boolean.TRUE.equals(move)) {
|
||||
operations.select(dbIndex);
|
||||
}
|
||||
return move;
|
||||
}
|
||||
}
|
||||
@@ -325,17 +325,6 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
|
||||
hashOps.rename(newKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean renameIfAbsent(String newKey) {
|
||||
return hashOps.renameIfAbsent(newKey);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean move(int dbIndex) {
|
||||
return hashOps.move(dbIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataType getType() {
|
||||
return hashOps.getType();
|
||||
|
||||
@@ -72,18 +72,6 @@ public class BoundKeyOperationsTest {
|
||||
keyOps.rename(key);
|
||||
assertEquals(key, keyOps.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenameIfAbsent() throws Exception {
|
||||
Object key = keyOps.getKey();
|
||||
assertNotNull(key);
|
||||
Object newName = objFactory.instance();
|
||||
assertFalse(template.hasKey(newName));
|
||||
assertTrue("cannot rename to key " + newName, keyOps.renameIfAbsent(newName));
|
||||
assertEquals(newName, keyOps.getKey());
|
||||
keyOps.rename(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpire() throws Exception {
|
||||
assertEquals(Long.valueOf(-1), keyOps.getExpire());
|
||||
|
||||
@@ -105,18 +105,4 @@ public class RedisAtomicTests {
|
||||
int delta = 5;
|
||||
assertEquals(delta, intCounter.addAndGet(delta));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntMove() throws Exception {
|
||||
intCounter.set(5);
|
||||
intCounter.move(1);
|
||||
assertEquals(5, intCounter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongMove() throws Exception {
|
||||
longCounter.set(5);
|
||||
longCounter.move(2);
|
||||
assertEquals(5, longCounter.get());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user