Skip to content

Commit f366ffc

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-284 - Add support for zCard to ZSetOperations.
'zCard' has been introduced in addition to 'size' as it feels more natural for people using redis. 'size' in 'ZSetOperations' / 'BoundZsetOperations' now delegate to 'zCard'. Original Pull Request: #55
1 parent d35375e commit f366ffc

File tree

5 files changed

+105
-9
lines changed

5 files changed

+105
-9
lines changed

src/main/java/org/springframework/data/redis/core/BoundZSetOperations.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
* ZSet (or SortedSet) operations bound to a certain key.
2626
*
2727
* @author Costin Leau
28+
* @author Christoph Strobl
2829
*/
2930
public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
3031

@@ -72,7 +73,21 @@ public interface BoundZSetOperations<K, V> extends BoundKeyOperations<K> {
7273

7374
Long count(double min, double max);
7475

76+
/**
77+
* Returns the number of elements of the sorted set.
78+
*
79+
* @return
80+
* @see #zCard()
81+
*/
7582
Long size();
7683

84+
/**
85+
* Returns the number of elements of the sorted set.
86+
*
87+
* @return
88+
* @since 1.3
89+
*/
90+
Long zCard();
91+
7792
Double score(Object o);
7893
}

src/main/java/org/springframework/data/redis/core/DefaultBoundZSetOperations.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
* Default implementation for {@link BoundZSetOperations}.
2727
*
2828
* @author Costin Leau
29+
* @author Christoph Strobl
2930
*/
3031
class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundZSetOperations<K, V> {
3132

@@ -126,8 +127,22 @@ public Long count(double min, double max) {
126127
return ops.count(getKey(), min, max);
127128
}
128129

130+
/*
131+
* (non-Javadoc)
132+
* @see org.springframework.data.redis.core.BoundZSetOperations#size()
133+
*/
134+
@Override
129135
public Long size() {
130-
return ops.size(getKey());
136+
return zCard();
137+
}
138+
139+
/*
140+
* (non-Javadoc)
141+
* @see org.springframework.data.redis.core.BoundZSetOperations#zCard()
142+
*/
143+
@Override
144+
public Long zCard() {
145+
return ops.zCard(getKey());
131146
}
132147

133148
public void unionAndStore(K otherKey, K destKey) {

src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
* Default implementation of {@link ZSetOperations}.
2727
*
2828
* @author Costin Leau
29+
* @author Christoph Strobl
2930
*/
3031
class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZSetOperations<K, V> {
3132

@@ -325,9 +326,23 @@ public Long doInRedis(RedisConnection connection) {
325326
}, true);
326327
}
327328

329+
/*
330+
* (non-Javadoc)
331+
* @see org.springframework.data.redis.core.ZSetOperations#size(java.lang.Object)
332+
*/
333+
@Override
328334
public Long size(K key) {
329-
final byte[] rawKey = rawKey(key);
335+
return zCard(key);
336+
}
330337

338+
/*
339+
* (non-Javadoc)
340+
* @see org.springframework.data.redis.core.ZSetOperations#zCard(java.lang.Object)
341+
*/
342+
@Override
343+
public Long zCard(K key) {
344+
345+
final byte[] rawKey = rawKey(key);
331346
return execute(new RedisCallback<Long>() {
332347

333348
public Long doInRedis(RedisConnection connection) {

src/main/java/org/springframework/data/redis/core/ZSetOperations.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,23 @@ public interface TypedTuple<V> extends Comparable<Double> {
8787

8888
Long count(K key, double min, double max);
8989

90+
/**
91+
* Returns the number of elements of the sorted set stored with given {@code key}.
92+
*
93+
* @see #zCard(Object)
94+
* @param key
95+
* @return
96+
*/
9097
Long size(K key);
9198

99+
/**
100+
* Returns the number of elements of the sorted set stored with given {@code key}.
101+
*
102+
* @param key
103+
* @return
104+
* @since 1.3
105+
*/
106+
Long zCard(K key);
107+
92108
RedisOperations<K, V> getOperations();
93109
}

src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,16 +15,16 @@
1515
*/
1616
package org.springframework.data.redis.core;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertThat;
20-
import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual;
18+
import static org.junit.Assert.*;
19+
import static org.springframework.data.redis.matcher.RedisTestMatchers.*;
2120

2221
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.HashSet;
2524
import java.util.LinkedHashSet;
2625
import java.util.Set;
2726

27+
import org.hamcrest.core.IsEqual;
2828
import org.junit.After;
2929
import org.junit.Before;
3030
import org.junit.Test;
@@ -39,6 +39,7 @@
3939
* Integration test of {@link DefaultZSetOperations}
4040
*
4141
* @author Jennifer Hickey
42+
* @author Christoph Strobl
4243
* @param <K> Key type
4344
* @param <V> Value type
4445
*/
@@ -191,4 +192,38 @@ public void testRemove() {
191192
expected.add(value2);
192193
assertThat(zSetOps.range(key, 0, -1), isEqual(expected));
193194
}
195+
196+
@Test
197+
public void zCardRetrievesDataCorrectly() {
198+
199+
K key = keyFactory.instance();
200+
V value1 = valueFactory.instance();
201+
V value2 = valueFactory.instance();
202+
V value3 = valueFactory.instance();
203+
204+
Set<TypedTuple<V>> values = new HashSet<TypedTuple<V>>();
205+
values.add(new DefaultTypedTuple<V>(value1, 1.7));
206+
values.add(new DefaultTypedTuple<V>(value2, 3.2));
207+
values.add(new DefaultTypedTuple<V>(value3, 0.8));
208+
zSetOps.add(key, values);
209+
210+
assertThat(zSetOps.zCard(key), IsEqual.equalTo(3L));
211+
}
212+
213+
@Test
214+
public void sizeRetrievesDataCorrectly() {
215+
216+
K key = keyFactory.instance();
217+
V value1 = valueFactory.instance();
218+
V value2 = valueFactory.instance();
219+
V value3 = valueFactory.instance();
220+
221+
Set<TypedTuple<V>> values = new HashSet<TypedTuple<V>>();
222+
values.add(new DefaultTypedTuple<V>(value1, 1.7));
223+
values.add(new DefaultTypedTuple<V>(value2, 3.2));
224+
values.add(new DefaultTypedTuple<V>(value3, 0.8));
225+
zSetOps.add(key, values);
226+
227+
assertThat(zSetOps.size(key), IsEqual.equalTo(3L));
228+
}
194229
}

0 commit comments

Comments
 (0)