Skip to content

Commit f8e01a2

Browse files
DATAKV-102 - Add possibility to iterate over available keys.
Adapter now exposes keys(Serializable keyspace) which allows to retrieve all keys contained in that specific region. Fixed false test names (scan vs. entries (see DATAKV-99)) along the way.
1 parent 6cea815 commit f8e01a2

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,21 @@ public interface KeyValueAdapter extends DisposableBean {
7474
Iterable<?> getAllOf(Serializable keyspace);
7575

7676
/**
77-
* Returns a {@link KeyValueIterator} that iterates over all entries.
77+
* Returns a {@link KeyValueIterator} that iterates over all entries.
7878
*
79-
* @param keyspace
79+
* @param keyspace must not be {@literal null}.
8080
* @return
8181
*/
8282
KeyValueIterator<? extends Serializable, ?> entries(Serializable keyspace);
8383

84+
/**
85+
* Returns all keys available in {@literal keyspace}.
86+
*
87+
* @param keyspace must not be {@literal null}.
88+
* @return empty {@link Iterable} if no keys available or keyspace unknown.
89+
*/
90+
Iterable<? extends Serializable> keys(Serializable keyspace);
91+
8492
/**
8593
* Remove all objects of given type.
8694
*

src/main/java/org/springframework/data/map/MapKeyValueAdapter.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public boolean contains(Serializable id, Serializable keyspace) {
104104
return get(id, keyspace) != null;
105105
}
106106

107-
108107
/* (non-Javadoc)
109108
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#count(java.io.Serializable)
110109
*/
@@ -153,6 +152,15 @@ public Collection<?> getAllOf(Serializable keyspace) {
153152
return new ForwardingKeyValueIterator<Serializable, Object>(getKeySpaceMap(keyspace).entrySet().iterator());
154153
}
155154

155+
/*
156+
* (non-Javadoc)
157+
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#keys(java.io.Serializable)
158+
*/
159+
@Override
160+
public Iterable<Serializable> keys(Serializable keyspace) {
161+
return getKeySpaceMap(keyspace).keySet();
162+
}
163+
156164
/*
157165
* (non-Javadoc)
158166
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#deleteAllOf(java.io.Serializable)

src/test/java/org/springframework/data/map/MapKeyValueAdapterUnitTests.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.map;
1717

18+
import static org.hamcrest.collection.IsEmptyIterable.*;
1819
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
1920
import static org.hamcrest.core.Is.*;
2021
import static org.hamcrest.core.IsEqual.*;
@@ -24,6 +25,8 @@
2425

2526
import java.io.Serializable;
2627

28+
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
29+
import org.hamcrest.collection.IsIterableWithSize;
2730
import org.junit.Before;
2831
import org.junit.Test;
2932
import org.springframework.data.keyvalue.core.KeyValueIterator;
@@ -193,7 +196,7 @@ public void deleteShouldReturnDeletedObject() {
193196
* @see DATAKV-99
194197
*/
195198
@Test
196-
public void scanShouldIterateOverAvailableEntries() {
199+
public void entriesShouldIterateOverAvailableEntries() {
197200

198201
adapter.put("1", object1, COLLECTION_1);
199202
adapter.put("2", object2, COLLECTION_1);
@@ -209,15 +212,15 @@ public void scanShouldIterateOverAvailableEntries() {
209212
* @see DATAKV-99
210213
*/
211214
@Test
212-
public void scanShouldReturnEmptyIteratorWhenNoElementsAvailable() {
215+
public void entriesShouldReturnEmptyIteratorWhenNoElementsAvailable() {
213216
assertThat(adapter.entries(COLLECTION_1).hasNext(), is(false));
214217
}
215218

216219
/**
217220
* @see DATAKV-99
218221
*/
219222
@Test
220-
public void scanDoesNotMixResultsFromMultipleKeyspaces() {
223+
public void entriesShouldNotMixResultsFromMultipleKeyspaces() {
221224

222225
adapter.put("1", object1, COLLECTION_1);
223226
adapter.put("2", object2, COLLECTION_2);
@@ -228,6 +231,40 @@ public void scanDoesNotMixResultsFromMultipleKeyspaces() {
228231
assertThat(iterator.hasNext(), is(false));
229232
}
230233

234+
/**
235+
* @see DATAKV-102
236+
*/
237+
@Test
238+
public void keysShouldReturnAvailableKeys() {
239+
240+
adapter.put("1", object1, COLLECTION_1);
241+
adapter.put("2", object2, COLLECTION_1);
242+
243+
Iterable<Serializable> iterable = adapter.keys(COLLECTION_1);
244+
245+
assertThat(iterable, IsIterableContainingInAnyOrder.<Serializable> containsInAnyOrder("1", "2"));
246+
}
247+
248+
/**
249+
* @see DATAKV-102
250+
*/
251+
@Test
252+
public void keyShouldReturnEmptyIterableWhenNoElementsAvailable() {
253+
assertThat(adapter.keys(COLLECTION_1), emptyIterable());
254+
}
255+
256+
/**
257+
* @see DATAKV-102
258+
*/
259+
@Test
260+
public void keysShouldNotMixResultsFromMultipleKeyspaces() {
261+
262+
adapter.put("1", object1, COLLECTION_1);
263+
adapter.put("2", object2, COLLECTION_2);
264+
265+
assertThat(adapter.keys(COLLECTION_1), IsIterableWithSize.<Serializable> iterableWithSize(1));
266+
}
267+
231268
static class SimpleObject {
232269

233270
protected String stringValue;

0 commit comments

Comments
 (0)