Skip to content

Commit a8462c5

Browse files
committed
DATAREDIS-547 - Polishing.
Return empty list if offset is greater than the available data set. Update supported keywords in reference docs. Original pull request: #216.
1 parent 83ff5ec commit a8462c5

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
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 & 2 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

@@ -217,8 +218,49 @@ public void shouldApplyPageableCorrectlyWhenUsingFindAll() {
217218
repo.save(Arrays.asList(eddard, robb, jon));
218219

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

224266
/**

0 commit comments

Comments
 (0)