DATAKV-44

+ add missing key operations
This commit is contained in:
Costin Leau
2011-03-18 19:08:43 +02:00
parent b62abeec50
commit e9d582b15c
7 changed files with 83 additions and 2 deletions

View File

@@ -19,12 +19,19 @@ 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.
* Operations over a Redis key.
*
* 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.
*
* </p>
* @author Costin Leau
*/
public interface BoundKeyOperations<K> {
@@ -89,4 +96,18 @@ public interface BoundKeyOperations<K> {
* @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);
}

View File

@@ -79,4 +79,14 @@ abstract class DefaultBoundKeyOperations<K> implements BoundKeyOperations<K> {
}
return result;
}
@Override
public Boolean move(int dbIndex) {
Boolean move = ops.move(key, dbIndex);
if (Boolean.TRUE.equals(move)) {
ops.select(dbIndex);
}
return move;
}
}

View File

@@ -301,6 +301,16 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
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;

View File

@@ -304,6 +304,16 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
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;

View File

@@ -154,4 +154,14 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
}
return result;
}
@Override
public Boolean move(int dbIndex) {
Boolean move = operations.move(key, dbIndex);
if (Boolean.TRUE.equals(move)) {
operations.select(dbIndex);
}
return move;
}
}

View File

@@ -330,6 +330,12 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
return hashOps.renameIfAbsent(newKey);
}
@Override
public Boolean move(int dbIndex) {
return hashOps.move(dbIndex);
}
@Override
public DataType getType() {
return hashOps.getType();

View File

@@ -105,4 +105,18 @@ 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());
}
}