DATAREDIS-288 - Improved support for collection parameters in ListOperations.
Added additional leftPushAll and rightPushAll method overloads that accept a Collection parameter. Previously we only accepted varargs which required users to convert collections to arrays. Based on David Liu's pull request: #100. Original pull request: #114.
This commit is contained in:
committed by
Christoph Strobl
parent
cf46d6e766
commit
d89773387a
@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
|
||||
* @author Costin Leau
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author David Liu
|
||||
*/
|
||||
abstract class AbstractOperations<K, V> {
|
||||
|
||||
@@ -120,6 +121,17 @@ abstract class AbstractOperations<K, V> {
|
||||
return rawValues;
|
||||
}
|
||||
|
||||
byte[][] rawValues(Collection<V> values) {
|
||||
|
||||
byte[][] rawValues = new byte[values.size()][];
|
||||
int i = 0;
|
||||
for (V value : values) {
|
||||
rawValues[i++] = rawValue(value);
|
||||
}
|
||||
|
||||
return rawValues;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<HK> byte[] rawHashKey(HK hashKey) {
|
||||
Assert.notNull(hashKey, "non null hash key required");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 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.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -26,6 +27,8 @@ import org.springframework.util.CollectionUtils;
|
||||
* Default implementation of {@link ListOperations}.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author David Liu
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements ListOperations<K, V> {
|
||||
|
||||
@@ -245,4 +248,31 @@ class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements Li
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long rightPushAll(K key, Collection<V> values) {
|
||||
|
||||
final byte[] rawKey = rawKey(key);
|
||||
final byte[][] rawValues = rawValues(values);
|
||||
|
||||
return execute(new RedisCallback<Long>() {
|
||||
public Long doInRedis(RedisConnection connection) {
|
||||
return connection.rPush(rawKey, rawValues);
|
||||
}
|
||||
}, true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long leftPushAll(K key, Collection<V> values) {
|
||||
|
||||
final byte[] rawKey = rawKey(key);
|
||||
final byte[][] rawValues = rawValues(values);
|
||||
|
||||
return execute(new RedisCallback<Long>() {
|
||||
public Long doInRedis(RedisConnection connection) {
|
||||
return connection.lPush(rawKey, rawValues);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 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.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -22,6 +23,8 @@ import java.util.concurrent.TimeUnit;
|
||||
* Redis list specific operations.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author David Liu
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface ListOperations<K, V> {
|
||||
|
||||
@@ -35,6 +38,14 @@ public interface ListOperations<K, V> {
|
||||
|
||||
Long leftPushAll(K key, V... values);
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param values
|
||||
* @return
|
||||
* @since 1.5
|
||||
*/
|
||||
Long leftPushAll(K key, Collection<V> values);
|
||||
|
||||
Long leftPushIfPresent(K key, V value);
|
||||
|
||||
Long leftPush(K key, V pivot, V value);
|
||||
@@ -43,6 +54,14 @@ public interface ListOperations<K, V> {
|
||||
|
||||
Long rightPushAll(K key, V... values);
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param values
|
||||
* @return
|
||||
* @since 1.5
|
||||
*/
|
||||
Long rightPushAll(K key, Collection<V> values);
|
||||
|
||||
Long rightPushIfPresent(K key, V value);
|
||||
|
||||
Long rightPush(K key, V pivot, V value);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
* Copyright 2013-2014 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.
|
||||
@@ -15,11 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.springframework.data.redis.matcher.RedisTestMatchers.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -39,6 +37,7 @@ import org.springframework.data.redis.connection.RedisConnection;
|
||||
* Integration test of {@link DefaultListOperations}
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Thomas Darimont
|
||||
* @param <K> Key test
|
||||
* @param <V> Value test
|
||||
*/
|
||||
@@ -174,4 +173,40 @@ public class DefaultListOperationsTests<K, V> {
|
||||
assertEquals(Long.valueOf(3), listOps.rightPush(key, v3));
|
||||
assertThat(listOps.range(key, 0, -1), isEqual(Arrays.asList(new Object[] { v1, v2, v3 })));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-288
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
// get rid of varargs warning
|
||||
public void testRightPushAllCollection() {
|
||||
|
||||
K key = keyFactory.instance();
|
||||
|
||||
V v1 = valueFactory.instance();
|
||||
V v2 = valueFactory.instance();
|
||||
V v3 = valueFactory.instance();
|
||||
|
||||
assertEquals(Long.valueOf(3), listOps.rightPushAll(key, Arrays.<V> asList(v1, v2, v3)));
|
||||
assertThat(listOps.range(key, 0, -1), isEqual(Arrays.asList(new Object[] { v1, v2, v3 })));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-288
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("all")
|
||||
// get rid of varargs warning
|
||||
public void testLeftPushAllCollection() {
|
||||
|
||||
K key = keyFactory.instance();
|
||||
|
||||
V v1 = valueFactory.instance();
|
||||
V v2 = valueFactory.instance();
|
||||
V v3 = valueFactory.instance();
|
||||
|
||||
assertEquals(Long.valueOf(3), listOps.leftPushAll(key, Arrays.<V> asList(v1, v2, v3)));
|
||||
assertThat(listOps.range(key, 0, -1), isEqual(Arrays.asList(new Object[] { v3, v2, v1 })));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user