diff --git a/src/main/java/org/springframework/data/redis/core/AbstractOperations.java b/src/main/java/org/springframework/data/redis/core/AbstractOperations.java index 0aa0ebda3..55cd771c7 100644 --- a/src/main/java/org/springframework/data/redis/core/AbstractOperations.java +++ b/src/main/java/org/springframework/data/redis/core/AbstractOperations.java @@ -36,6 +36,7 @@ import org.springframework.util.Assert; * @author Costin Leau * @author Jennifer Hickey * @author Christoph Strobl + * @author David Liu */ abstract class AbstractOperations { @@ -120,6 +121,17 @@ abstract class AbstractOperations { return rawValues; } + byte[][] rawValues(Collection values) { + + byte[][] rawValues = new byte[values.size()][]; + int i = 0; + for (V value : values) { + rawValues[i++] = rawValue(value); + } + + return rawValues; + } + @SuppressWarnings("unchecked") byte[] rawHashKey(HK hashKey) { Assert.notNull(hashKey, "non null hash key required"); diff --git a/src/main/java/org/springframework/data/redis/core/DefaultListOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultListOperations.java index 81e2fd015..4720afd0c 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultListOperations.java @@ -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 extends AbstractOperations implements ListOperations { @@ -245,4 +248,31 @@ class DefaultListOperations extends AbstractOperations implements Li } }, true); } + + @Override + public Long rightPushAll(K key, Collection values) { + + final byte[] rawKey = rawKey(key); + final byte[][] rawValues = rawValues(values); + + return execute(new RedisCallback() { + public Long doInRedis(RedisConnection connection) { + return connection.rPush(rawKey, rawValues); + } + }, true); + + } + + @Override + public Long leftPushAll(K key, Collection values) { + + final byte[] rawKey = rawKey(key); + final byte[][] rawValues = rawValues(values); + + return execute(new RedisCallback() { + public Long doInRedis(RedisConnection connection) { + return connection.lPush(rawKey, rawValues); + } + }, true); + } } diff --git a/src/main/java/org/springframework/data/redis/core/ListOperations.java b/src/main/java/org/springframework/data/redis/core/ListOperations.java index 692c9090f..6d558e419 100644 --- a/src/main/java/org/springframework/data/redis/core/ListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ListOperations.java @@ -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 { @@ -35,6 +38,14 @@ public interface ListOperations { Long leftPushAll(K key, V... values); + /** + * @param key + * @param values + * @return + * @since 1.5 + */ + Long leftPushAll(K key, Collection values); + Long leftPushIfPresent(K key, V value); Long leftPush(K key, V pivot, V value); @@ -43,6 +54,14 @@ public interface ListOperations { Long rightPushAll(K key, V... values); + /** + * @param key + * @param values + * @return + * @since 1.5 + */ + Long rightPushAll(K key, Collection values); + Long rightPushIfPresent(K key, V value); Long rightPush(K key, V pivot, V value); diff --git a/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java index e2a69c867..4c705c337 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java @@ -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 Key test * @param Value test */ @@ -174,4 +173,40 @@ public class DefaultListOperationsTests { 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. 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. asList(v1, v2, v3))); + assertThat(listOps.range(key, 0, -1), isEqual(Arrays.asList(new Object[] { v3, v2, v1 }))); + } }