diff --git a/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java b/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java index 24fbe2a2c..521177e1d 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundZSetOperations.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. @@ -25,6 +25,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple; * ZSet (or SortedSet) operations bound to a certain key. * * @author Costin Leau + * @author Christoph Strobl */ public interface BoundZSetOperations extends BoundKeyOperations { @@ -72,7 +73,21 @@ public interface BoundZSetOperations extends BoundKeyOperations { Long count(double min, double max); + /** + * Returns the number of elements of the sorted set. + * + * @return + * @see #zCard() + */ Long size(); + /** + * Returns the number of elements of the sorted set. + * + * @return + * @since 1.3 + */ + Long zCard(); + Double score(Object o); } diff --git a/src/main/java/org/springframework/data/redis/core/DefaultBoundZSetOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultBoundZSetOperations.java index 417db58c6..bb7aa4d56 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultBoundZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultBoundZSetOperations.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. @@ -26,6 +26,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple; * Default implementation for {@link BoundZSetOperations}. * * @author Costin Leau + * @author Christoph Strobl */ class DefaultBoundZSetOperations extends DefaultBoundKeyOperations implements BoundZSetOperations { @@ -126,8 +127,22 @@ class DefaultBoundZSetOperations extends DefaultBoundKeyOperations impl return ops.count(getKey(), min, max); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.BoundZSetOperations#size() + */ + @Override public Long size() { - return ops.size(getKey()); + return zCard(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.BoundZSetOperations#zCard() + */ + @Override + public Long zCard() { + return ops.zCard(getKey()); } public void unionAndStore(K otherKey, K destKey) { diff --git a/src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.java index 65518df65..3d4b25cd4 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.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. @@ -26,6 +26,7 @@ import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; * Default implementation of {@link ZSetOperations}. * * @author Costin Leau + * @author Christoph Strobl */ class DefaultZSetOperations extends AbstractOperations implements ZSetOperations { @@ -325,9 +326,23 @@ class DefaultZSetOperations extends AbstractOperations implements ZS }, true); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ZSetOperations#size(java.lang.Object) + */ + @Override public Long size(K key) { - final byte[] rawKey = rawKey(key); + return zCard(key); + } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.core.ZSetOperations#zCard(java.lang.Object) + */ + @Override + public Long zCard(K key) { + + final byte[] rawKey = rawKey(key); return execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) { diff --git a/src/main/java/org/springframework/data/redis/core/ZSetOperations.java b/src/main/java/org/springframework/data/redis/core/ZSetOperations.java index e8871d0dd..7db1ae9de 100644 --- a/src/main/java/org/springframework/data/redis/core/ZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ZSetOperations.java @@ -87,7 +87,23 @@ public interface ZSetOperations { Long count(K key, double min, double max); + /** + * Returns the number of elements of the sorted set stored with given {@code key}. + * + * @see #zCard(Object) + * @param key + * @return + */ Long size(K key); + /** + * Returns the number of elements of the sorted set stored with given {@code key}. + * + * @param key + * @return + * @since 1.3 + */ + Long zCard(K key); + RedisOperations getOperations(); } diff --git a/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java index bf83a4eb0..ad2c47509 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.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,9 +15,8 @@ */ package org.springframework.data.redis.core; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual; +import static org.junit.Assert.*; +import static org.springframework.data.redis.matcher.RedisTestMatchers.*; import java.util.Collection; import java.util.Collections; @@ -25,6 +24,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; +import org.hamcrest.core.IsEqual; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,6 +39,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple; * Integration test of {@link DefaultZSetOperations} * * @author Jennifer Hickey + * @author Christoph Strobl * @param Key type * @param Value type */ @@ -191,4 +192,38 @@ public class DefaultZSetOperationsTests { expected.add(value2); assertThat(zSetOps.range(key, 0, -1), isEqual(expected)); } + + @Test + public void zCardRetrievesDataCorrectly() { + + K key = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + V value3 = valueFactory.instance(); + + Set> values = new HashSet>(); + values.add(new DefaultTypedTuple(value1, 1.7)); + values.add(new DefaultTypedTuple(value2, 3.2)); + values.add(new DefaultTypedTuple(value3, 0.8)); + zSetOps.add(key, values); + + assertThat(zSetOps.zCard(key), IsEqual.equalTo(3L)); + } + + @Test + public void sizeRetrievesDataCorrectly() { + + K key = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + V value3 = valueFactory.instance(); + + Set> values = new HashSet>(); + values.add(new DefaultTypedTuple(value1, 1.7)); + values.add(new DefaultTypedTuple(value2, 3.2)); + values.add(new DefaultTypedTuple(value3, 0.8)); + zSetOps.add(key, values); + + assertThat(zSetOps.size(key), IsEqual.equalTo(3L)); + } }