Merge branch 'master' of github.com:SpringSource/spring-data-keyvalue
This commit is contained in:
@@ -34,6 +34,8 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
|
||||
|
||||
void set(HK key, HV value);
|
||||
|
||||
Collection<HV> multiGet(Set<HK> keys);
|
||||
|
||||
void multiSet(Map<? extends HK, ? extends HV> m);
|
||||
|
||||
Set<HK> keys();
|
||||
@@ -43,4 +45,5 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
|
||||
Integer length();
|
||||
|
||||
void delete(Object key);
|
||||
|
||||
}
|
||||
|
||||
@@ -49,6 +49,11 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
|
||||
return ops.get(getKey(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<HV> multiGet(Set<HK> hashKeys) {
|
||||
return ops.multiGet(getKey(), hashKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisOperations<H, ?> getOperations() {
|
||||
return ops.getOperations();
|
||||
|
||||
@@ -32,6 +32,8 @@ public interface HashOperations<H, HK, HV> {
|
||||
|
||||
HV get(H key, Object hashKey);
|
||||
|
||||
Collection<HV> multiGet(H key, Set<HK> hashKeys);
|
||||
|
||||
Integer increment(H key, HK hashKey, int delta);
|
||||
|
||||
Set<HK> keys(H key);
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -960,7 +961,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
}, true);
|
||||
|
||||
return (HV) deserializeHashValue(rawHashValue);
|
||||
return RedisTemplate.this.<HV> deserializeHashValue(rawHashValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1039,6 +1040,32 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}, true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<HV> multiGet(K key, Set<HK> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final byte[] rawKey = rawKey(key);
|
||||
|
||||
final byte[][] rawHashKeys = new byte[fields.size()][];
|
||||
|
||||
int counter = 0;
|
||||
for (HK hashKey : fields) {
|
||||
rawHashKeys[counter++] = rawHashKey(hashKey);
|
||||
}
|
||||
|
||||
List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
|
||||
@Override
|
||||
public List<byte[]> doInRedis(RedisConnection connection) {
|
||||
return connection.hMGet(rawKey, rawHashKeys);
|
||||
}
|
||||
}, true);
|
||||
|
||||
return (List<HV>) hashValues(rawValues, List.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(K key, HK hashKey, HV value) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
package org.springframework.data.keyvalue.redis.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -31,6 +33,32 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
|
||||
|
||||
private final BoundHashOperations<String, K, V> hashOps;
|
||||
|
||||
private class DefaultRedisMapEntry implements Map.Entry<K, V> {
|
||||
|
||||
private K key;
|
||||
private V value;
|
||||
|
||||
public DefaultRedisMapEntry(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <code>DefaultRedisMap</code> instance.
|
||||
*
|
||||
@@ -91,7 +119,18 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
|
||||
|
||||
@Override
|
||||
public Set<java.util.Map.Entry<K, V>> entrySet() {
|
||||
throw new UnsupportedOperationException();
|
||||
Set<K> keySet = keySet();
|
||||
Collection<V> multiGet = hashOps.multiGet(keySet);
|
||||
|
||||
Iterator<K> keys = keySet.iterator();
|
||||
Iterator<V> values = multiGet.iterator();
|
||||
|
||||
Set<Map.Entry<K, V>> entries = new LinkedHashSet<Entry<K, V>>();
|
||||
while (keys.hasNext()) {
|
||||
entries.add(new DefaultRedisMapEntry(keys.next(), values.next()));
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.matchers.JUnitMatchers.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -344,8 +345,34 @@ public abstract class AbstractRedisMapTests<K, V> {
|
||||
assertThat(values, hasItems(v1, v2, v3));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@Test
|
||||
public void testEntrySet() {
|
||||
map.entrySet();
|
||||
Set<Entry<K, V>> entries = map.entrySet();
|
||||
assertTrue(entries.isEmpty());
|
||||
|
||||
K k1 = getKey();
|
||||
K k2 = getKey();
|
||||
|
||||
V v1 = getValue();
|
||||
V v2 = getValue();
|
||||
|
||||
map.put(k1, v1);
|
||||
map.put(k2, v1);
|
||||
|
||||
entries = map.entrySet();
|
||||
|
||||
Set<K> keys = new LinkedHashSet<K>();
|
||||
Collection<V> values = new ArrayList<V>();
|
||||
|
||||
for (Entry<K, V> entry : entries) {
|
||||
keys.add(entry.getKey());
|
||||
values.add(entry.getValue());
|
||||
}
|
||||
|
||||
assertEquals(2, keys.size());
|
||||
|
||||
assertThat(keys, hasItems(k1, k2));
|
||||
assertThat(values, hasItem(v1));
|
||||
assertThat(values, not(hasItem(v2)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user