Skip to content

Commit

Permalink
feature: findAll by Example paginated reports total elements and numb…
Browse files Browse the repository at this point in the history
…er of pages
  • Loading branch information
bsbodden committed Aug 2, 2024
1 parent f8c143e commit f3fbd0c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,27 @@ public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
SearchStream<S> stream = entityStream.of(example.getProbeType());
var offset = pageable.getOffset() * pageable.getPageSize();
var offset = pageable.getPageNumber() * pageable.getPageSize();
var limit = pageable.getPageSize();
Slice<S> slice = stream.filter(example).loadAll().limit(limit, Math.toIntExact(offset))
.toList(pageable, stream.getEntityClass());

return pageFromSlice(slice);
if (indexer.indexDefinitionExistsFor(metadata.getJavaType())) {
String searchIndex = indexer.getIndexName(metadata.getJavaType());

SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
Query query = new Query(stream.backingQuery());
query.setNoContent();

for (Order order : pageable.getSort()) {
query.setSortBy(order.getProperty(), order.isAscending());
}

SearchResult searchResult = searchOps.search(query);
return pageFromSlice(slice, searchResult.getTotalResults(), pageable.getPageSize());
} else {
return pageFromSlice(slice);
}
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,27 @@ public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
SearchStream<S> stream = entityStream.of(example.getProbeType());
var offset = pageable.getOffset() * pageable.getPageSize();
var offset = pageable.getPageNumber() * pageable.getPageSize();
var limit = pageable.getPageSize();
Slice<S> slice = stream.filter(example).loadAll().limit(limit, Math.toIntExact(offset))
.toList(pageable, stream.getEntityClass());

return pageFromSlice(slice);
if (indexer.indexDefinitionExistsFor(metadata.getJavaType())) {
String searchIndex = indexer.getIndexName(metadata.getJavaType());

SearchOperations<String> searchOps = modulesOperations.opsForSearch(searchIndex);
Query query = new Query(stream.backingQuery());
query.setNoContent();

for (Order order : pageable.getSort()) {
query.setSortBy(order.getProperty(), order.isAscending());
}

SearchResult searchResult = searchOps.search(query);
return pageFromSlice(slice, searchResult.getTotalResults(), pageable.getPageSize());
} else {
return pageFromSlice(slice);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,19 @@ public static String getKey(String keyspace, Object id) {
}

public static <T> Page<T> pageFromSlice(Slice<T> slice) {
return pageFromSlice(slice, Long.MAX_VALUE, slice.getPageable().getPageSize());
}

public static <T> Page<T> pageFromSlice(Slice<T> slice, long totalElements, int pageSize) {
return new Page<>() {
@Override
public int getTotalPages() {
return -1;
return (totalElements == 0 || pageSize == 0) ? 0 : (int) Math.ceil((double) totalElements / (double) pageSize);
}

@Override
public long getTotalElements() {
return -1;
return totalElements;
}

@Override
Expand Down Expand Up @@ -516,22 +520,22 @@ public Sort getSort() {

@Override
public boolean isFirst() {
return slice.isFirst();
return getNumber() == 0;
}

@Override
public boolean isLast() {
return slice.isLast();
return getNumber() + 1 == getTotalPages();
}

@Override
public boolean hasNext() {
return slice.hasNext();
return getNumber() + 1 < getTotalPages();
}

@Override
public boolean hasPrevious() {
return slice.hasPrevious();
return getNumber() > 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,19 @@ void shouldFindAllPagedByExample() {
PersonDoc person = new PersonDoc();
person.setHometown(walt.getHometown());

Page<PersonDoc> result = repository.findAll(Example.of(person), PageRequest.of(0, 2));
Pageable firstPage = PageRequest.of(0, 2);
Page<PersonDoc> result = repository.findAll(Example.of(person), firstPage);
assertThat(result).hasSize(2);
assertThat(result.getTotalElements()).isEqualTo(3);
assertThat(result.getTotalPages()).isEqualTo(2);
assertThat(result.hasNext()).isTrue();

Pageable nextPage = result.nextPageable();
Page<PersonDoc> next = repository.findAll(Example.of(person), nextPage);
assertThat(next).hasSize(1);
assertThat(next.getTotalElements()).isEqualTo(3);
assertThat(next.getTotalPages()).isEqualTo(2);
assertThat(next.hasNext()).isFalse();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,19 @@ void shouldFindAllPagedByExample() {
PersonHash person = new PersonHash();
person.setHometown(walt.getHometown());

Page<PersonHash> result = repository.findAll(Example.of(person), PageRequest.of(0, 2));
Pageable firstPage = PageRequest.of(0, 2);
Page<PersonHash> result = repository.findAll(Example.of(person), firstPage);
assertThat(result).hasSize(2);
assertThat(result.getTotalElements()).isEqualTo(3);
assertThat(result.getTotalPages()).isEqualTo(2);
assertThat(result.hasNext()).isTrue();

Pageable nextPage = result.nextPageable();
Page<PersonHash> next = repository.findAll(Example.of(person), nextPage);
assertThat(next).hasSize(1);
assertThat(next.getTotalElements()).isEqualTo(3);
assertThat(next.getTotalPages()).isEqualTo(2);
assertThat(next.hasNext()).isFalse();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,25 +347,27 @@ void testFlattenCollection() {
@Test
void testPageFromSlice() {
List<String> strings = List.of("Pantufla", "Mondongo", "Latifundio", "Alcachofa");
Slice<String> slice = new SliceImpl<>(strings);
List<String> sliceContent = strings.subList(0, 2);
Slice<String> slice = new SliceImpl<>(sliceContent);

Page<String> page = ObjectUtils.pageFromSlice(slice);
Page<String> page = ObjectUtils.pageFromSlice(slice, strings.size(), 2);

assertThat(page.getContent()).hasSize(4);
assertThat(page.getContent()).hasSize(2);
assertThat(page.getContent().get(0)).isEqualTo("Pantufla");
assertThat(page.getNumber()).isEqualTo(slice.getNumber());
assertThat(page.getSize()).isEqualTo(slice.getSize());
assertThat(page.getNumberOfElements()).isEqualTo(slice.getNumberOfElements());
assertThat(page.getSort()).isEqualTo(slice.getSort());
assertThat(page.hasContent()).isEqualTo(slice.hasContent());
assertThat(page.hasNext()).isEqualTo(slice.hasNext());
assertThat(page.hasPrevious()).isEqualTo(slice.hasPrevious());
assertThat(page.isFirst()).isEqualTo(slice.isFirst());
assertThat(page.isLast()).isEqualTo(slice.isLast());
assertThat(page.hasNext()).isEqualTo(true);
assertThat(page.hasPrevious()).isEqualTo(false);
assertThat(page.isFirst()).isEqualTo(true);
assertThat(page.isLast()).isEqualTo(false);
assertThat(page.nextPageable()).isEqualTo(slice.nextPageable());
assertThat(page.previousPageable()).isEqualTo(slice.previousPageable());
assertThat(page.getTotalPages()).isEqualTo(-1);
assertThat(page.getPageable()).isEqualTo(Pageable.ofSize(4));
assertThat(page.getTotalPages()).isEqualTo(2);
assertThat(page.getPageable()).isEqualTo(Pageable.ofSize(2));
assertThat(page.getTotalElements()).isEqualTo(4);
}

@Test
Expand Down

0 comments on commit f3fbd0c

Please sign in to comment.