Change mSetNX/multiSetIfAbsent return type to Boolean
DATAREDIS-177
This commit is contained in:
@@ -304,8 +304,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
|
||||
delegate.mSet(tuple);
|
||||
}
|
||||
|
||||
public void mSetNX(Map<byte[], byte[]> tuple) {
|
||||
delegate.mSetNX(tuple);
|
||||
public Boolean mSetNX(Map<byte[], byte[]> tuple) {
|
||||
return delegate.mSetNX(tuple);
|
||||
}
|
||||
|
||||
public void multi() {
|
||||
@@ -919,8 +919,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
|
||||
}
|
||||
|
||||
|
||||
public void mSetNXString(Map<String, String> tuple) {
|
||||
delegate.mSetNX(serialize(tuple));
|
||||
public Boolean mSetNXString(Map<String, String> tuple) {
|
||||
return delegate.mSetNX(serialize(tuple));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public interface RedisStringCommands {
|
||||
|
||||
void mSet(Map<byte[], byte[]> tuple);
|
||||
|
||||
void mSetNX(Map<byte[], byte[]> tuple);
|
||||
Boolean mSetNX(Map<byte[], byte[]> tuple);
|
||||
|
||||
Long incr(byte[] key);
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ public interface StringRedisConnection extends RedisConnection {
|
||||
|
||||
void mSetString(Map<String, String> tuple);
|
||||
|
||||
void mSetNXString(Map<String, String> tuple);
|
||||
Boolean mSetNXString(Map<String, String> tuple);
|
||||
|
||||
Long incr(String key);
|
||||
|
||||
|
||||
@@ -985,17 +985,17 @@ public class JedisConnection implements RedisConnection {
|
||||
}
|
||||
|
||||
|
||||
public void mSetNX(Map<byte[], byte[]> tuples) {
|
||||
public Boolean mSetNX(Map<byte[], byte[]> tuples) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.msetnx(JedisUtils.convert(tuples));
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
if (isPipelined()) {
|
||||
pipeline.msetnx(JedisUtils.convert(tuples));
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
jedis.msetnx(JedisUtils.convert(tuples));
|
||||
return JedisUtils.convertCodeReply(jedis.msetnx(JedisUtils.convert(tuples)));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -516,9 +516,9 @@ public class JredisConnection implements RedisConnection {
|
||||
}
|
||||
|
||||
|
||||
public void mSetNX(Map<byte[], byte[]> tuple) {
|
||||
public Boolean mSetNX(Map<byte[], byte[]> tuple) {
|
||||
try {
|
||||
jredis.msetnx(tuple);
|
||||
return jredis.msetnx(tuple);
|
||||
} catch (Exception ex) {
|
||||
throw convertJredisAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -873,13 +873,13 @@ public class LettuceConnection implements RedisConnection {
|
||||
}
|
||||
|
||||
|
||||
public void mSetNX(Map<byte[], byte[]> tuples) {
|
||||
public Boolean mSetNX(Map<byte[], byte[]> tuples) {
|
||||
try {
|
||||
if (isPipelined()) {
|
||||
pipeline(getAsyncConnection().msetnx(tuples));
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
getConnection().msetnx(tuples);
|
||||
return getConnection().msetnx(tuples);
|
||||
} catch (Exception ex) {
|
||||
throw convertLettuceAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -802,13 +802,13 @@ public class SrpConnection implements RedisConnection {
|
||||
}
|
||||
|
||||
|
||||
public void mSetNX(Map<byte[], byte[]> tuples) {
|
||||
public Boolean mSetNX(Map<byte[], byte[]> tuples) {
|
||||
try {
|
||||
if (isPipelined()) {
|
||||
pipeline(pipeline.msetnx((Object[]) SrpUtils.convert(tuples)));
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
client.msetnx((Object[]) SrpUtils.convert(tuples));
|
||||
return SrpUtils.asBoolean(client.msetnx((Object[]) SrpUtils.convert(tuples)));
|
||||
} catch (Exception ex) {
|
||||
throw convertSrpAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -149,9 +149,9 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
|
||||
}
|
||||
|
||||
|
||||
public void multiSetIfAbsent(Map<? extends K, ? extends V> m) {
|
||||
public Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m) {
|
||||
if (m.isEmpty()) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
final Map<byte[], byte[]> rawKeys = new LinkedHashMap<byte[], byte[]>(m.size());
|
||||
@@ -160,11 +160,10 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
|
||||
rawKeys.put(rawKey(entry.getKey()), rawValue(entry.getValue()));
|
||||
}
|
||||
|
||||
execute(new RedisCallback<Object>() {
|
||||
return execute(new RedisCallback<Boolean>() {
|
||||
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.mSetNX(rawKeys);
|
||||
return null;
|
||||
public Boolean doInRedis(RedisConnection connection) {
|
||||
return connection.mSetNX(rawKeys);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public interface ValueOperations<K, V> {
|
||||
|
||||
void multiSet(Map<? extends K, ? extends V> m);
|
||||
|
||||
void multiSetIfAbsent(Map<? extends K, ? extends V> m);
|
||||
Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
|
||||
|
||||
V get(Object key);
|
||||
|
||||
|
||||
@@ -865,9 +865,21 @@ public abstract class AbstractConnectionIntegrationTests {
|
||||
Map<String, String> vals = new HashMap<String, String>();
|
||||
vals.put("height", "5");
|
||||
vals.put("width", "1");
|
||||
connection.mSetNXString(vals);
|
||||
actual.add(connection.mSetNXString(vals));
|
||||
actual.add(connection.mGet("height", "width"));
|
||||
verifyResults(Arrays.asList(new Object[] { Arrays.asList(new String[] { "5", "1" }) }),
|
||||
verifyResults(Arrays.asList(new Object[] { true, Arrays.asList(new String[] { "5", "1" }) }),
|
||||
actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMSetNxFailure() {
|
||||
connection.set("height", "2");
|
||||
Map<String, String> vals = new HashMap<String, String>();
|
||||
vals.put("height", "5");
|
||||
vals.put("width", "1");
|
||||
actual.add(connection.mSetNXString(vals));
|
||||
actual.add(connection.mGet("height", "width"));
|
||||
verifyResults(Arrays.asList(new Object[] { false, Arrays.asList(new String[] { "2", null }) }),
|
||||
actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -339,6 +339,18 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
|
||||
actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMSetNxFailure() {
|
||||
connection.set("height", "2");
|
||||
Map<String, String> vals = new HashMap<String, String>();
|
||||
vals.put("height", "5");
|
||||
vals.put("width", "1");
|
||||
actual.add(connection.mSetNXString(vals));
|
||||
actual.add(connection.mGet("height", "width"));
|
||||
verifyResults(Arrays.asList(new Object[] { 0l, Arrays.asList(new String[] { "2", null }) }),
|
||||
actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNx() {
|
||||
actual.add(connection.setNX("notaround", "54"));
|
||||
|
||||
@@ -80,6 +80,10 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
|
||||
public void testMSetNx() {
|
||||
}
|
||||
|
||||
@Ignore("https://github.com/alphazero/jredis/issues/64 Protocol error: expected '$' got '*' on mset")
|
||||
public void testMSetNxFailure() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionClosesWhenNotPooled() {
|
||||
connection.close();
|
||||
|
||||
@@ -186,6 +186,18 @@ public class LettuceConnectionPipelineIntegrationTests extends
|
||||
actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMSetNxFailure() {
|
||||
connection.set("height", "2");
|
||||
Map<String, String> vals = new HashMap<String, String>();
|
||||
vals.put("height", "5");
|
||||
vals.put("width", "1");
|
||||
actual.add(connection.mSetNXString(vals));
|
||||
actual.add(connection.mGet("height", "width"));
|
||||
verifyResults(Arrays.asList(new Object[] { false, Arrays.asList(new String[] { "2", null }) }),
|
||||
actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNx() {
|
||||
actual.add(connection.setNX("notaround", "54"));
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -71,4 +76,23 @@ public class DefaultValueOperationsTests {
|
||||
valueOps.increment(key, -10d);
|
||||
assertEquals("1.9", valueOps.get(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiSetIfAbsent() {
|
||||
Map<String,String> keysAndValues = new HashMap<String,String>();
|
||||
keysAndValues.put("foo", "bar");
|
||||
keysAndValues.put("baz", "test");
|
||||
assertTrue(valueOps.multiSetIfAbsent(keysAndValues));
|
||||
assertEquals(new HashSet<String>(keysAndValues.values()),
|
||||
new HashSet<String>(valueOps.multiGet(keysAndValues.keySet())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiSetIfAbsentFailure() {
|
||||
valueOps.set("foo", "alreadyset");
|
||||
Map<String,String> keysAndValues = new HashMap<String,String>();
|
||||
keysAndValues.put("foo", "bar");
|
||||
keysAndValues.put("baz", "test");
|
||||
assertFalse(valueOps.multiSetIfAbsent(keysAndValues));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user