Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATACASS-56 - Cassandra paging support. #114

Closed
wants to merge 4 commits into from
Closed

Conversation

mp911de
Copy link
Member

@mp911de mp911de commented Sep 5, 2017

We now support forward-only paging with Cassandra through the Template API and repositories. Results in Cassandra are paged by navigating forward-only through pages described by a binary paging state encapsulated by CassandraPageRequest and accessible via the returned Slice. Spring Data Page's do not fit to Cassandra's paging concept because Cassandra paging is not based on limit/offset.

Page requests are applicable to a Query and as a parameter of query methods.

Query query = Query.empty().pageRequest(CassandraPageRequest.first(10));
Slice<User> slice = template.slice(query, User.class);

do {

// consume slice

if (slice.hasNext()) {
slice = template.select(query, slice.nextPageable(), User.class);
} else {
break;
}
} while (!slice.getContent().isEmpty());

assertThat(ids).hasSize(100);
assertThat(iterations).isEqualTo(10);

interface UserRepository implements Repository<User, String> {

Slice<User> findAllByName(String name, Pageable pageRequest);
}

Related ticket: DATACASS-56.

We now allow query option mutation through QueryOptions.mutate() returning an initialized builder. The mutation builder is initialized with the state of the QueryOptions object and allows further customization without changing the previous state of the immutable QueryOptions object.

QueryOptions queryOptions = …;

QueryOptions mutated = queryOptions.mutate().readTimeout(Duration.ofSeconds(5)).build();
We now support forward-only paging with Cassandra through the Template API and repositories. Results in Cassandra are paged by navigating forward-only through pages described by a binary paging state encapsulated by CassandraPageRequest and accessible via the returned Slice. Spring Data Page's do not fit to Cassandra's paging concept because Cassandra paging is not based on limit/offset.

Page requests are applicable to a Query and as parameter of query methods.

Query query = Query.empty().pageRequest(CassandraPageRequest.first(10));
Slice<User> slice = template.slice(query, User.class);

do {

    // consume slice

    if (slice.hasNext()) {
        slice = template.select(query, slice.nextPageable(), User.class);
    } else {
        break;
    }
} while (!slice.getContent().isEmpty());

assertThat(ids).hasSize(100);
assertThat(iterations).isEqualTo(10);

interface UserRepository implements Repository<User, String> {

    Slice<User> findAllByName(String name, Pageable pageRequest);
}
Align override comments.
@jxblum jxblum self-assigned this Sep 12, 2017
@jxblum jxblum added this to the Kay GA milestone Sep 12, 2017
@jxblum
Copy link
Contributor

jxblum commented Sep 12, 2017

Reviewed, polished and merged to master for Spring Data Kay GA.

@jxblum jxblum closed this Sep 12, 2017
@mp911de mp911de deleted the issue/DATACASS-56 branch September 12, 2017 09:14
@lukasz-gosiewski
Copy link

lukasz-gosiewski commented Jun 14, 2018

@mp911de
Hello,
Could you please explain why above code checks if slice content is not empty in every loop?
I'm now implementing something similar and when I'm fetching results from DB with pageSize set to a total amount of results in DB I'm getting two pages instead of one, and one of them is empty. Is this proper behavior?

@mp911de
Copy link
Member Author

mp911de commented Jun 14, 2018

Stores can return empty results but it's possible that the paging is not finished yet.

In general, please file a ticket or ask questions on StackOverflow as closed pull requests are not a good place for discussions.

@mustafin
Copy link

mustafin commented Aug 15, 2018

@mp911de How to make it work with reactive repo? Not working with Flux<T>, return all results.
Mono<Slice<T>> yields IncorrectResultSizeDataAccessException

@mp911de
Copy link
Member Author

mp911de commented Aug 15, 2018

Please upgrade to at least 2.1RC1, see DATACASS-529.

@mgezkaya
Copy link

Hi all I have written a code block to use pagination with user action here is my code I hope it helps you well.
https://gist.github.com/mgezkaya/f90e19e8e86e35cabcf22bd97325cf3a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants