Skip to content

Commit 6280bd2

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 a93f81f commit 6280bd2

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
@@ -540,6 +540,7 @@ Here's an overview of the keywords supported for Redis and what a method contain
540540
|`And`|`findByLastnameAndFirstname`|`SINTER …:firstname:rand …:lastname:al’thor`
541541
|`Or`|`findByLastnameOrFirstname`|`SUNION …:firstname:rand …:lastname:al’thor`
542542
|`Is,Equals`|`findByFirstname`,`findByFirstnameIs`,`findByFirstnameEquals`|`SINTER …:firstname:rand`
543+
|`Top,First`|`findFirst10ByFirstname`,`findTop5ByFirstname`|
543544
|===============
544545
====
545546

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.Serializable;
1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.Map.Entry;
@@ -357,6 +358,11 @@ public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessExcept
357358

358359
List<byte[]> keys = new ArrayList<byte[]>(ids);
359360

361+
362+
if (keys.isEmpty() || keys.size() < offset) {
363+
return Collections.emptyList();
364+
}
365+
360366
offset = Math.max(0, offset);
361367
if (offset >= 0 && rows > 0) {
362368
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

@@ -202,8 +203,49 @@ public void shouldApplyPageableCorrectlyWhenUsingFindAll() {
202203
repo.save(Arrays.asList(eddard, robb, jon));
203204

204205
Page<Person> firstPage = repo.findAll(new PageRequest(0, 2));
205-
assertThat(firstPage.getContent(), hasSize(2));
206-
assertThat(repo.findAll(firstPage.nextPageable()).getContent(), hasSize(1));
206+
assertThat(firstPage.getContent(), hasSize(2));
207+
assertThat(repo.findAll(firstPage.nextPageable()).getContent(), hasSize(1));
208+
}
209+
210+
/**
211+
* @see DATAREDIS-547
212+
*/
213+
@Test
214+
public void shouldReturnEmptyListWhenPageableOutOfBoundsUsingFindAll() {
215+
216+
Person eddard = new Person("eddard", "stark");
217+
Person robb = new Person("robb", "stark");
218+
Person jon = new Person("jon", "snow");
219+
220+
repo.save(Arrays.asList(eddard, robb, jon));
221+
222+
Page<Person> firstPage = repo.findAll(new PageRequest(100, 2));
223+
assertThat(firstPage.getContent(), hasSize(0));
224+
}
225+
226+
/**
227+
* @see DATAREDIS-547
228+
*/
229+
@Test
230+
public void shouldReturnEmptyListWhenPageableOutOfBoundsUsingQueryMethod() {
231+
232+
Person eddard = new Person("eddard", "stark");
233+
Person robb = new Person("robb", "stark");
234+
Person sansa = new Person("sansa", "stark");
235+
236+
repo.save(Arrays.asList(eddard, robb, sansa));
237+
238+
Page<Person> page1 = repo.findPersonByLastname("stark", new PageRequest(1, 3));
239+
240+
assertThat(page1.getNumberOfElements(), is(0));
241+
assertThat(page1.getContent(), hasSize(0));
242+
assertThat(page1.getTotalElements(), is(3L));
243+
244+
Page<Person> page2 = repo.findPersonByLastname("stark", new PageRequest(2, 3));
245+
246+
assertThat(page2.getNumberOfElements(), is(0));
247+
assertThat(page2.getContent(), hasSize(0));
248+
assertThat(page2.getTotalElements(), is(3L));
207249
}
208250

209251
/**

0 commit comments

Comments
 (0)