Skip to content

Commit 1240d8f

Browse files
committed
DATAREDIS-547 - Polishing.
Return empty list if offset is greater than the available data set. Update supported keywords in reference docs.
1 parent 3c11ed6 commit 1240d8f

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/main/asciidoc/reference/redis-repositories.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ Here's an overview of the keywords supported for Redis and what a method contain
579579
|`And`|`findByLastnameAndFirstname`|`SINTER …:firstname:rand …:lastname:al’thor`
580580
|`Or`|`findByLastnameOrFirstname`|`SUNION …:firstname:rand …:lastname:al’thor`
581581
|`Is,Equals`|`findByFirstname`,`findByFirstnameIs`,`findByFirstnameEquals`|`SINTER …:firstname:rand`
582+
|`Top,First`|`findFirst10ByFirstname`,`findTop5ByFirstname`|
582583
|===============
583584
====
584585

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Serializable;
1919
import java.util.ArrayList;
2020
import java.util.Collection;
21+
import java.util.Collections;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
2324
import java.util.Map;
@@ -364,6 +365,11 @@ public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessExcept
364365

365366
List<byte[]> keys = new ArrayList<byte[]>(ids);
366367

368+
369+
if (keys.isEmpty() || keys.size() < offset) {
370+
return Collections.emptyList();
371+
}
372+
367373
offset = Math.max(0, offset);
368374
if (offset >= 0 && rows > 0) {
369375
keys = keys.subList(offset, Math.min(offset + rows, keys.size()));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Map<byte[], Map<byte[], byte[]>> doInRedis(RedisConnection connection) th
102102

103103
final Map<byte[], Map<byte[], byte[]>> rawData = new LinkedHashMap<byte[], Map<byte[], byte[]>>();
104104

105-
if (allKeys.size() == 0 || allKeys.size() < offset) {
105+
if (allKeys.isEmpty() || allKeys.size() < offset) {
106106
return Collections.emptyMap();
107107
}
108108

src/test/java/org/springframework/data/redis/repository/RedisRepositoryIntegrationTestBase.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Base for testing Redis repository support in different configurations.
4848
*
4949
* @author Christoph Strobl
50+
* @author Mark Paluch
5051
*/
5152
public abstract class RedisRepositoryIntegrationTestBase {
5253

@@ -216,9 +217,49 @@ public void shouldApplyPageableCorrectlyWhenUsingFindAll() {
216217

217218
repo.save(Arrays.asList(eddard, robb, jon));
218219

219-
Page<Person> firstPage = repo.findAll(new PageRequest(0, 2));
220-
assertThat(firstPage.getContent(), hasSize(2));
221-
assertThat(repo.findAll(firstPage.nextPageable()).getContent(), hasSize(1));
220+
Page<Person> firstPage = repo.findAll(new PageRequest(100, 2));
221+
assertThat(firstPage.getContent(), hasSize(0));
222+
}
223+
224+
/**
225+
* @see DATAREDIS-547
226+
*/
227+
@Test
228+
public void shouldReturnEmptyListWhenPageableOutOfBoundsUsingFindAll() {
229+
230+
Person eddard = new Person("eddard", "stark");
231+
Person robb = new Person("robb", "stark");
232+
Person jon = new Person("jon", "snow");
233+
234+
repo.save(Arrays.asList(eddard, robb, jon));
235+
236+
Page<Person> firstPage = repo.findAll(new PageRequest(100, 2));
237+
assertThat(firstPage.getContent(), hasSize(0));
238+
}
239+
240+
/**
241+
* @see DATAREDIS-547
242+
*/
243+
@Test
244+
public void shouldReturnEmptyListWhenPageableOutOfBoundsUsingQueryMethod() {
245+
246+
Person eddard = new Person("eddard", "stark");
247+
Person robb = new Person("robb", "stark");
248+
Person sansa = new Person("sansa", "stark");
249+
250+
repo.save(Arrays.asList(eddard, robb, sansa));
251+
252+
Page<Person> page1 = repo.findPersonByLastname("stark", new PageRequest(1, 3));
253+
254+
assertThat(page1.getNumberOfElements(), is(0));
255+
assertThat(page1.getContent(), hasSize(0));
256+
assertThat(page1.getTotalElements(), is(3L));
257+
258+
Page<Person> page2 = repo.findPersonByLastname("stark", new PageRequest(2, 3));
259+
260+
assertThat(page2.getNumberOfElements(), is(0));
261+
assertThat(page2.getContent(), hasSize(0));
262+
assertThat(page2.getTotalElements(), is(3L));
222263
}
223264

224265
/**

0 commit comments

Comments
 (0)