@ThreadSafe public class OffsetPager extends Object
Web UIs and services often provide paginated results with random access, for example: given a page size of 20 elements, fetch page 5. Cassandra does not support this natively (see CASSANDRA-6511), because such queries are inherently linear: the database would have to restart from the beginning every time, and skip unwanted rows until it reaches the desired offset.
However, random pagination is a real need for many applications, and linear performance can be a reasonable trade-off if the cardinality stays low. This class provides a way to emulate this behavior on the client side.
String query = "SELECT ..."; OffsetPager pager = new OffsetPager(20); // Get page 2: start from a fresh result set, throw away rows 1-20, then return rows 21-40 ResultSet rs = session.execute(query); OffsetPager.Page<Row> page2 = pager.getPage(rs, 2); // Get page 5: start from a fresh result set, throw away rows 1-80, then return rows 81-100 rs = session.execute(query); OffsetPager.Page<Row> page5 = pager.getPage(rs, 5);
Statement.setPageSize(int)
and basic.request.page-size
in the configuration.
It happens under the hood, and is completely transparent for offset paging: this class will work
the same no matter how many network roundtrips were needed to fetch the result. You don't need to
set the protocol page size and the logical page size to the same value.Modifier and Type | Class and Description |
---|---|
static interface |
OffsetPager.Page<ElementT>
A page returned as the result of an offset query.
|
Constructor and Description |
---|
OffsetPager(int pageSize)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
<ElementT,IterableT extends AsyncPagingIterable<ElementT,IterableT>> |
getPage(IterableT iterable,
int targetPageNumber)
Extracts a page from an asynchronous result set, by skipping rows until we get to the requested
offset.
|
<ElementT> OffsetPager.Page<ElementT> |
getPage(PagingIterable<ElementT> iterable,
int targetPageNumber)
Extracts a page from a synchronous result set, by skipping rows until we get to the requested
offset.
|
public OffsetPager(int pageSize)
pageSize
- the number of elements per page. Must be greater than or equal to 1.@NonNull public <ElementT> OffsetPager.Page<ElementT> getPage(@NonNull PagingIterable<ElementT> iterable, int targetPageNumber)
iterable
- the iterable to extract the results from: typically a ResultSet
, or a
PagingIterable
returned by the mapper.targetPageNumber
- the page to return (1 for the first page, 2 for the second page, etc).
Must be greater than or equal to 1.IllegalArgumentException
- if the conditions on the arguments are not respected.@NonNull public <ElementT,IterableT extends AsyncPagingIterable<ElementT,IterableT>> CompletionStage<OffsetPager.Page<ElementT>> getPage(@NonNull IterableT iterable, int targetPageNumber)
iterable
- the iterable to extract the results from. Typically an AsyncPagingIterable
, or a MappedAsyncPagingIterable
returned by the mapper.targetPageNumber
- the page to return (1 for the first page, 2 for the second page, etc).
Must be greater than or equal to 1.IllegalArgumentException
- if the conditions on the arguments are not respected.Copyright © 2017–2020. All rights reserved.