DATAKV-17
+ expose hGetAll on RedisTemplate (as entries() method)
This commit is contained in:
@@ -48,4 +48,5 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
|
||||
|
||||
void delete(Object key);
|
||||
|
||||
Map<HK, HV> entries();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
* 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.
|
||||
@@ -93,4 +93,9 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
|
||||
public Collection<HV> values() {
|
||||
return ops.values(getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<HK, HV> entries() {
|
||||
return ops.entries(getKey());
|
||||
}
|
||||
}
|
||||
@@ -46,5 +46,7 @@ public interface HashOperations<H, HK, HV> {
|
||||
|
||||
Collection<HV> values(H key);
|
||||
|
||||
Map<HK, HV> entries(H key);
|
||||
|
||||
RedisOperations<H, ?> getOperations();
|
||||
}
|
||||
|
||||
@@ -333,6 +333,19 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <HK, HV> Map<HK, HV> deserializeHashMap(Map<byte[], byte[]> entries) {
|
||||
Map<HK, HV> map = new LinkedHashMap<HK, HV>(entries.size());
|
||||
|
||||
for (Map.Entry<byte[], byte[]> entry : entries.entrySet()) {
|
||||
map.put((HK) deserializeHashKey(entry.getKey()), (HV) deserializeHashValue(entry.getValue()));
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<K> deserializeKeys(Collection<byte[]> rawKeys, Class<? extends Collection> type) {
|
||||
Collection<K> values = (List.class.isAssignableFrom(type) ? new ArrayList<K>(rawKeys.size())
|
||||
@@ -361,7 +374,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return (String) deserialize(value, stringSerializer);
|
||||
}
|
||||
|
||||
@SuppressWarnings( { "unchecked", "unused" })
|
||||
@SuppressWarnings( { "unchecked" })
|
||||
private <HK> HK deserializeHashKey(byte[] value) {
|
||||
return (HK) deserialize(value, hashKeySerializer);
|
||||
}
|
||||
@@ -1621,5 +1634,19 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<HK, HV> entries(K key) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
|
||||
Map<byte[], byte[]> entries = execute(new RedisCallback<Map<byte[], byte[]>>() {
|
||||
@Override
|
||||
public Map<byte[], byte[]> doInRedis(RedisConnection connection) {
|
||||
return connection.hGetAll(rawKey);
|
||||
}
|
||||
}, true);
|
||||
|
||||
return deserializeHashMap(entries);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user