Support hDel with multiple hash keys

DATAREDIS-99
This commit is contained in:
Jennifer Hickey
2013-07-31 16:36:25 -07:00
parent ce76ad1841
commit 8f362b481c
22 changed files with 99 additions and 38 deletions

View File

@@ -305,8 +305,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.getSubscription();
}
public Boolean hDel(byte[] key, byte[] field) {
Boolean result = delegate.hDel(key, field);
public Long hDel(byte[] key, byte[]... fields) {
Long result = delegate.hDel(key, fields);
if(isFutureConversion()) {
addResultConverter(identityConverter);
}
@@ -1345,8 +1345,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
}
public Boolean hDel(String key, String field) {
Boolean result = delegate.hDel(serialize(key), serialize(field));
public Long hDel(String key, String... fields) {
Long result = delegate.hDel(serialize(key), serializeMulti(fields));
if(isFutureConversion()) {
addResultConverter(identityConverter);
}

View File

@@ -43,7 +43,7 @@ public interface RedisHashCommands {
Boolean hExists(byte[] key, byte[] field);
Boolean hDel(byte[] key, byte[] field);
Long hDel(byte[] key, byte[]... fields);
Long hLen(byte[] key);

View File

@@ -257,7 +257,7 @@ public interface StringRedisConnection extends RedisConnection {
Boolean hExists(String key, String field);
Boolean hDel(String key, String field);
Long hDel(String key, String... fields);
Long hLen(String key);

View File

@@ -2364,17 +2364,21 @@ public class JedisConnection implements RedisConnection {
}
public Boolean hDel(byte[] key, byte[] field) {
public Long hDel(byte[] key, byte[]... fields) {
if((isPipelined() || isQueueing()) && fields.length > 1) {
throw new UnsupportedOperationException("hDel of multiple fields not supported " +
"in pipeline or transaction");
}
try {
if (isPipelined()) {
pipeline(new JedisResult(pipeline.hdel(key, field), JedisConverters.longToBoolean()));
pipeline(new JedisResult(pipeline.hdel(key, fields[0])));
return null;
}
if (isQueueing()) {
transaction(new JedisResult(transaction.hdel(key, field), JedisConverters.longToBoolean()));
transaction(new JedisResult(transaction.hdel(key, fields[0])));
return null;
}
return JedisConverters.toBoolean(jedis.hdel(key, field));
return jedis.hdel(key, fields);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}

View File

@@ -1116,9 +1116,12 @@ public class JredisConnection implements RedisConnection {
}
public Boolean hDel(byte[] key, byte[] field) {
public Long hDel(byte[] key, byte[]... fields) {
if(fields.length > 1) {
throw new UnsupportedOperationException("hDel of multiple fields not supported");
}
try {
return jredis.hdel(key, field);
return JredisUtils.toLong(jredis.hdel(key, fields[0]));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}

View File

@@ -123,4 +123,8 @@ public abstract class JredisUtils {
info.putAll(map);
return info;
}
static Long toLong(Boolean source) {
return source ? 1l : 0l;
}
}

View File

@@ -2460,17 +2460,17 @@ public class LettuceConnection implements RedisConnection {
}
public Boolean hDel(byte[] key, byte[] field) {
public Long hDel(byte[] key, byte[]... fields) {
try {
if (isPipelined()) {
pipeline(new LettuceResult(getAsyncConnection().hdel(key, field), LettuceConverters.longToBoolean()));
pipeline(new LettuceResult(getAsyncConnection().hdel(key, fields)));
return null;
}
if (isQueueing()) {
transaction(new LettuceTxResult(getConnection().hdel(key, field), LettuceConverters.longToBoolean()));
transaction(new LettuceTxResult(getConnection().hdel(key, fields)));
return null;
}
return LettuceConverters.toBoolean(getConnection().hdel(key, field));
return getConnection().hdel(key, fields);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}

View File

@@ -1887,13 +1887,13 @@ public class SrpConnection implements RedisConnection {
}
public Boolean hDel(byte[] key, byte[] field) {
public Long hDel(byte[] key, byte[]... fields) {
try {
if (isPipelined()) {
pipeline(new SrpResult(pipeline.hdel(key, new Object[] { field }), SrpConverters.longToBoolean()));
pipeline(new SrpResult(pipeline.hdel(key, (Object[])fields)));
return null;
}
return SrpConverters.toBoolean(client.hdel(key, new Object[] { field }).data());
return client.hdel(key, (Object[])fields).data();
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}

View File

@@ -33,6 +33,7 @@ import org.springframework.util.Assert;
* Internal base class used by various RedisTemplate XXXOperations implementations.
*
* @author Costin Leau
* @author Jennifer Hickey
*/
abstract class AbstractOperations<K, V> {
@@ -97,6 +98,7 @@ abstract class AbstractOperations<K, V> {
return keySerializer().serialize(key);
}
@SuppressWarnings("unchecked")
byte[] rawString(String key) {
return stringSerializer().serialize(key);
}
@@ -118,6 +120,15 @@ abstract class AbstractOperations<K, V> {
return hashKeySerializer().serialize(hashKey);
}
<HK> byte[][] rawHashKeys(HK... hashKeys) {
final byte[][] rawHashKeys = new byte[hashKeys.length][];
int i = 0;
for (HK hashKey : hashKeys) {
rawHashKeys[i++] = rawHashKey(hashKey);
}
return rawHashKeys;
}
@SuppressWarnings("unchecked")
<HV> byte[] rawHashValue(HV value) {
if(hashValueSerializer() == null & value instanceof byte[]) {
@@ -163,7 +174,7 @@ abstract class AbstractOperations<K, V> {
return SerializationUtils.deserialize(rawValues, valueSerializer());
}
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
Set<TypedTuple<V>> deserializeTupleValues(Set<Tuple> rawValues) {
if(rawValues == null) {
return null;

View File

@@ -51,7 +51,7 @@ public interface BoundHashOperations<H, HK, HV> extends BoundKeyOperations<H> {
Long size();
void delete(Object key);
void delete(Object... keys);
Map<HK, HV> entries();
}

View File

@@ -43,8 +43,8 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultBoundKeyOperations<H>
}
public void delete(Object key) {
ops.delete(getKey(), key);
public void delete(Object... keys) {
ops.delete(getKey(), keys);
}

View File

@@ -209,14 +209,14 @@ class DefaultHashOperations<K, HK, HV> extends AbstractOperations<K, Object> imp
}
public void delete(K key, Object hashKey) {
public void delete(K key, Object... hashKeys) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawHashKey(hashKey);
final byte[][] rawHashKeys = rawHashKeys(hashKeys);
execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.hDel(rawKey, rawHashKey);
connection.hDel(rawKey, rawHashKeys);
return null;
}
}, true);

View File

@@ -27,7 +27,7 @@ import java.util.Set;
*/
public interface HashOperations<H, HK, HV> {
void delete(H key, Object hashKey);
void delete(H key, Object... hashKeys);
Boolean hasKey(H key, Object hashKey);