Pagination¶
Every listing method (client.instances().list(...), client.spaces().list(...),
client.types().list(...), client.queries().list/run/execute(...)) returns
an org.marmotgraph.sdk.pagination.Page<T>.
Page<Instance> page = client.instances().list(
InstanceListOptions.builder().types("https://openminds.ebrains.eu/core/Person").build());
page.count(); // items on *this* page
page.total(); // total matching items, if returnTotalResults wasn't disabled (Integer, may be null)
page.hasNext(); // whether there's another page to fetch
List<Instance> items = page.items(); // the items on this page, as a plain list
for (Instance person : page) { // iterate just this page
// ...
}
Optional<Page<Instance>> next = page.next(); // fetch the next page
page.allItems().forEach(person -> { // lazily walk every page from here on, as a Stream<Instance>
// ...
});
Markers¶
The API v4 paginates with opaque markers only (there is no offset based
pagination with from anymore). A page which might be followed by more
results carries a nextMarker (page.nextMarker()) - passing it as marker
returns the following page. .next() does this for you. The server only
provides a next marker if the page is full, so the following page can still
be empty - hasNext() is false once a page comes back without a marker.
If you need to continue a pagination later (e.g. across requests of your own
application), keep page.nextMarker() and pass it to the marker option of
the listing method:
Page<Instance> first = client.instances().list(InstanceListOptions.builder().types("...").size(100).build());
String marker = first.nextMarker();
// ... later:
Page<Instance> second = client.instances().list(InstanceListOptions.builder().types("...").size(100).marker(marker).build());
The listing parameters (types, filters, size, ...) have to stay the same between the pages since a marker only points to the last result of the previous page.
Disabling the total count¶
returnTotalResults defaults to true server-side, but computing it has a
cost. If you don't need page.total() (e.g. you're just going to consume
page.allItems() until exhaustion), set it to false on the options
builder:
Page<Instance> page = client.instances().list(
InstanceListOptions.builder().types("...").returnTotalResults(false).build());
Page.hasNext() doesn't depend on the total - it follows the nextMarker.
allItems() does this for you automatically on every page it fetches
beyond the one you already have: once you've committed to walking all the
way to the end, there's no reason to keep paying for a total count on each
request, so it forces returnTotalResults=false regardless of what the
original call asked for. The first page - already fetched before
allItems() is called - keeps whatever total() it came back with; set
returnTotalResults(false) yourself on the initial call if you want to
avoid paying for it there too. A plain page.next() call is left alone
either way, since incremental, caller-driven paging (e.g. a UI pager
showing "page 3 of 10") may still want the total on every page.