Merge branch 'master' of github.com:SpringSource/spring-data-keyvalue

This commit is contained in:
J. Brisbin
2010-11-26 13:50:07 -06:00
12 changed files with 545 additions and 34 deletions

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.redis.core;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/**
* @author Costin Leau
*/
public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
RedisOperations<H, ?> getOperations();
boolean hasKey(Object key);
Integer increment(HK key, int delta);
HV get(Object key);
void set(HK key, HV value);
void multiSet(Map<? extends HK, ? extends HV> m);
Set<HK> keys();
Collection<HV> values();
Integer length();
void delete(Object key);
}

View File

@@ -24,6 +24,8 @@ import java.util.List;
*/
public interface BoundListOperations<K, V> extends KeyBound<K> {
RedisOperations<K, V> getOperations();
List<V> range(int start, int end);
void trim(int start, int end);
@@ -41,4 +43,6 @@ public interface BoundListOperations<K, V> extends KeyBound<K> {
Integer remove(int i, Object value);
V index(int index);
void set(int index, V value);
}

View File

@@ -25,12 +25,12 @@ import java.util.Set;
*/
public interface BoundSetOperations<K, V> extends KeyBound<K> {
RedisOperations<K, V> getOperations();
Set<V> diff(K... keys);
void diffAndStore(K destKey, K... keys);
RedisOperations<K, V> getOperations();
Set<V> intersect(K... keys);
void intersectAndStore(K destKey, K... keys);

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.redis.core;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/**
* Default implementation for {@link HashOperations}.
*
* @author Costin Leau
*/
class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implements BoundHashOperations<H, HK, HV> {
private final HashOperations<H, HK, HV> ops;
private RedisOperations<H, ?> template;
/**
* Constructs a new <code>DefaultBoundHashOperations</code> instance.
*
* @param key
* @param template
*/
public DefaultBoundHashOperations(H key, RedisTemplate<H, ?> template) {
super(key);
this.ops = template.hashOps();
}
@Override
public void delete(Object key) {
ops.delete(getKey(), key);
}
@Override
public HV get(Object key) {
return ops.get(getKey(), key);
}
@Override
public RedisOperations<H, ?> getOperations() {
return template;
}
@Override
public boolean hasKey(Object key) {
return ops.hasKey(getKey(), key);
}
@Override
public Integer increment(HK key, int delta) {
return ops.increment(getKey(), key, delta);
}
@Override
public Set<HK> keys() {
return ops.keys(getKey());
}
@Override
public Integer length() {
return ops.length(getKey());
}
@Override
public void multiSet(Map<? extends HK, ? extends HV> m) {
ops.multiSet(getKey(), m);
}
@Override
public void set(HK key, HV value) {
ops.set(getKey(), key, value);
}
@Override
public Collection<HV> values() {
return ops.values(getKey());
}
}

View File

@@ -26,54 +26,65 @@ import java.util.List;
public class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements BoundListOperations<K, V> {
private final ListOperations<K, V> ops;
public DefaultBoundListOperations(K key, RedisTemplate<K, V> template) {
super(key);
this.ops = template.listOps();
}
@Override
public RedisOperations<K, V> getOperations() {
return ops.getOperations();
}
@Override
public V index(int index) {
throw new UnsupportedOperationException();
return ops.index(getKey(), index);
}
@Override
public V leftPop() {
throw new UnsupportedOperationException();
return ops.leftPop(getKey());
}
@Override
public Integer leftPush(V value) {
throw new UnsupportedOperationException();
return ops.leftPush(getKey(), value);
}
@Override
public Integer length() {
throw new UnsupportedOperationException();
return ops.length(getKey());
}
@Override
public List<V> range(int start, int end) {
throw new UnsupportedOperationException();
return ops.range(getKey(), start, end);
}
@Override
public Integer remove(int i, Object value) {
throw new UnsupportedOperationException();
return ops.remove(getKey(), i, value);
}
@Override
public V rightPop() {
throw new UnsupportedOperationException();
return ops.rightPop(getKey());
}
@Override
public Integer rightPush(V value) {
throw new UnsupportedOperationException();
return ops.rightPush(getKey(), value);
}
@Override
public void trim(int start, int end) {
throw new UnsupportedOperationException();
ops.trim(getKey(), start, end);
}
@Override
public void set(int index, V value) {
ops.set(getKey(), index, value);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.redis.core;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/**
* Redis map specific operations working on a hash.
*
* @author Costin Leau
*/
public interface HashOperations<H, HK, HV> {
void delete(H key, Object hashKey);
Boolean hasKey(H key, Object hashKey);
HV get(H key, Object hashKey);
Integer increment(H key, HK hashKey, int delta);
Set<HK> keys(H key);
Integer length(H key);
void multiSet(H key, Map<? extends HK, ? extends HV> m);
void set(H key, HK hashKey, HV value);
Collection<HV> values(H key);
}

View File

@@ -47,4 +47,6 @@ public interface ListOperations<K, V> {
List<V> blockingLeftPop(int timeout, K... keys);
List<V> blockingRightPop(int timeout, K... keys);
RedisOperations<K, V> getOperations();
}

View File

@@ -50,4 +50,8 @@ public interface RedisOperations<K, V> {
ZSetOperations<K, V> zSetOps();
BoundZSetOperations<K, V> forZSet(K key);
<HK, HV> HashOperations<K, HK, HV> hashOps();
<HK, HV> BoundHashOperations<K, HK, HV> forHash(K key);
}

View File

@@ -21,8 +21,10 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
@@ -211,6 +213,17 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return (T) values;
}
@SuppressWarnings("unchecked")
private <H> Collection<H> arbitraryValues(Collection<byte[]> rawValues, Class<? extends Collection> type) {
Collection<H> values = (List.class.isAssignableFrom(type) ? new ArrayList<H>(rawValues.size())
: new LinkedHashSet<H>(rawValues.size()));
for (byte[] bs : rawValues) {
values.add((H) valueSerializer.deserialize(bs));
}
return values;
}
// utility methods for the template internal methods
private abstract class ValueDeserializingRedisCallback implements RedisCallback<V> {
private K key;
@@ -483,6 +496,11 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
}, true);
}
@Override
public RedisOperations<K, V> getOperations() {
return RedisTemplate.this;
}
}
//
@@ -571,7 +589,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
public void intersectAndStore(K key, K destKey, K... keys) {
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
final byte[] rawDestKey = rawKey(destKey);
Object rawValues = execute(new RedisCallback<Object>() {
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.sInterStore(rawDestKey, rawKeys);
@@ -845,4 +863,152 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
}
//
// Hash Operations
//
@Override
public <HK, HV> BoundHashOperations<K, HK, HV> forHash(K key) {
return new DefaultBoundHashOperations<K, HK, HV>(key, this);
}
@Override
public <HK, HV> HashOperations<K, HK, HV> hashOps() {
return new DefaultHashOperations<HK, HV>();
}
private class DefaultHashOperations<HK, HV> implements HashOperations<K, HK, HV> {
@Override
public HV get(K key, Object hashKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
byte[] rawHashValue = execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) {
return connection.hGet(rawKey, rawHashKey);
}
}, true);
return (HV) valueSerializer.deserialize(rawHashValue);
}
@Override
public Boolean hasKey(K key, Object hashKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) {
return connection.hExists(rawKey, rawHashKey);
}
}, true);
}
@Override
public Integer increment(K key, HK hashKey, final int delta) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
return execute(new RedisCallback<Integer>() {
@Override
public Integer doInRedis(RedisConnection connection) {
return connection.hIncrBy(rawKey, rawHashKey, delta);
}
}, true);
}
@Override
public Set<HK> keys(K key) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@Override
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.hKeys(rawKey);
}
}, true);
return (Set<HK>) arbitraryValues(rawValues, Set.class);
}
@Override
public Integer length(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Integer>() {
@Override
public Integer doInRedis(RedisConnection connection) {
return connection.hLen(rawKey);
}
}, true);
}
@Override
public void multiSet(K key, Map<? extends HK, ? extends HV> m) {
final byte[] rawKey = rawKey(key);
final Map<byte[], byte[]> hashes = new LinkedHashMap<byte[], byte[]>(m.size());
for (Map.Entry<byte[], byte[]> entry : hashes.entrySet()) {
hashes.put(rawValue(entry.getKey()), rawValue(entry.getValue()));
}
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.hMSet(rawKey, hashes);
return null;
}
}, true);
}
@Override
public void set(K key, HK hashKey, HV value) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
final byte[] rawHashValue = rawValue(value);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.hSet(rawKey, rawHashKey, rawHashValue);
return null;
}
}, true);
}
@Override
public List<HV> values(K key) {
final byte[] rawKey = rawKey(key);
List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) {
return connection.hVals(rawKey);
}
}, true);
return (List<HV>) arbitraryValues(rawValues, List.class);
}
@Override
public void delete(K key, Object hashKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.hDel(rawKey, rawHashKey);
return null;
}
}, true);
}
}
}

View File

@@ -21,7 +21,7 @@ import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.springframework.data.keyvalue.redis.core.ListOperations;
import org.springframework.data.keyvalue.redis.core.BoundListOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
/**
@@ -31,7 +31,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
*/
public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements RedisList<E> {
private final ListOperations<String, E> listOps;
private final BoundListOperations<String, E> listOps;
private class DefaultRedisListIterator<E> extends RedisIterator<E> {
@@ -45,24 +45,35 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
}
}
/**
* Constructs a new <code>DefaultRedisList</code> instance.
*
* @param key
* @param operations
*/
public DefaultRedisList(String key, RedisOperations<String, E> operations) {
super(key, operations);
listOps = operations.listOps();
listOps = operations.forList(key);
}
public DefaultRedisList(BoundListOperations<String, E> boundOps) {
super(boundOps.getKey(), boundOps.getOperations());
listOps = boundOps;
}
@Override
public List<E> range(int start, int end) {
return listOps.range(key, start, end);
return listOps.range(start, end);
}
@Override
public RedisList<E> trim(int start, int end) {
listOps.trim(key, start, end);
listOps.trim(start, end);
return this;
}
private List<E> content() {
return listOps.range(key, 0, -1);
return listOps.range(0, -1);
}
@Override
@@ -72,38 +83,38 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
public int size() {
return listOps.length(key);
return listOps.length();
}
@Override
public boolean add(E value) {
listOps.rightPush(key, value);
listOps.rightPush(value);
return true;
}
@Override
public void clear() {
listOps.trim(key, size() + 1, 0);
listOps.trim(size() + 1, 0);
}
@Override
public boolean remove(Object o) {
Integer result = listOps.remove(key, 0, o);
Integer result = listOps.remove(0, o);
return (result != null && result.intValue() > 0);
}
@Override
public void add(int index, E element) {
if (index == 0) {
listOps.leftPush(key, element);
listOps.leftPush(element);
return;
}
int size = size();
if (index == size()) {
listOps.rightPush(key, element);
listOps.rightPush(element);
return;
}
@@ -121,7 +132,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
Collection<? extends E> reverseC = CollectionUtils.reverse(c);
for (E e : reverseC) {
listOps.leftPush(key, e);
listOps.leftPush(e);
}
return true;
}
@@ -130,7 +141,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
if (index == size()) {
for (E e : c) {
listOps.rightPush(key, e);
listOps.rightPush(e);
}
return true;
}
@@ -147,7 +158,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
return listOps.index(key, index);
return listOps.index(index);
}
@Override
@@ -179,7 +190,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
public E set(int index, E e) {
E object = get(index);
listOps.set(key, index, e);
listOps.set(index, e);
return object;
}
@@ -201,21 +212,21 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
public boolean offer(E e) {
listOps.leftPush(key, e);
listOps.leftPush(e);
return true;
}
@Override
public E peek() {
E element = listOps.index(key, 0);
E element = listOps.index(0);
return (element == null ? null : element);
}
@Override
public E poll() {
E element = listOps.leftPop(key);
E element = listOps.leftPop();
return (element == null ? null : element);
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.redis.util;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.springframework.data.keyvalue.redis.core.BoundHashOperations;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
/**
* Default implementation for {@link RedisMap}.
*
* @author Costin Leau
*/
public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
private final BoundHashOperations<String, K, V> hashOps;
public DefaultRedisMap(String key, RedisOperations<String, ?> operations) {
this.hashOps = operations.forHash(key);
}
public DefaultRedisMap(BoundHashOperations<String, K, V> boundOps) {
this.hashOps = boundOps;
}
@Override
public Integer increment(K key, int delta) {
return hashOps.increment(key, delta);
}
@Override
public boolean putIfAbsent(K key, V value) {
if (!hashOps.hasKey(key)) {
put(key, value);
return true;
}
return false;
}
@Override
public String getKey() {
return hashOps.getKey();
}
@Override
public RedisOperations<String, ?> getOperations() {
return hashOps.getOperations();
}
@Override
public void clear() {
getOperations().delete(getKey());
}
@Override
public boolean containsKey(Object key) {
return hashOps.hasKey(key);
}
@Override
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
throw new UnsupportedOperationException();
}
@Override
public V get(Object key) {
return hashOps.get(key);
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public Set<K> keySet() {
return hashOps.keys();
}
@Override
public V put(K key, V value) {
V oldV = get(key);
hashOps.set(key, value);
return oldV;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
hashOps.multiSet(m);
}
@Override
public V remove(Object key) {
V v = get(key);
hashOps.delete(key);
return v;
}
@Override
public int size() {
return hashOps.length();
}
@Override
public Collection<V> values() {
return hashOps.values();
}
}

View File

@@ -27,7 +27,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
*
* @author Costin Leau
*/
class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
private final BoundZSetOperations<String, E> boundZSetOps;
private double defaultScore = 1;