public class Paginator extends java.lang.Object implements java.util.Iterator<ViewResponse>
Paginator
makes it possible to iterate over a
ViewResponse
in pages.
It is possible to iterate over both reduced and non-reduced results, but iterating over non-reduced results is considerably faster, because a more efficient pagination approach can be used.
Usage:
View view = client.getView("design_doc", "view_name");
Query query = new Query();
int docsPerPage = 20;
Paginator paginator = client.paginatedQuery(view, query, docsPerPage);
while(paginator.hasNext()) {
ViewResponse response = paginator.next();
for(ViewRow row : response) {
System.out.println(row.getKey());
}
}
Note that if a custom limit is set on the Query
object when it
gets passed in into the Paginator
, then it is considered as an
absolute limit. The Paginator
will stop iterating once this absolute
limit is reached. If no limit is provided, the Paginator
will move
forward until no more documents are returned by the view.
If you encounter an infinite loop when emitting stringified numbers from
your View, see the forcedKeyType
method for instructions
to remedy this situation.
Constructor and Description |
---|
Paginator(CouchbaseClient client,
View view,
Query query,
int limit)
Create a new Paginator by passing in the needed params.
|
Modifier and Type | Method and Description |
---|---|
void |
forceKeyType(java.lang.Class<?> clazz)
Allows one to override the type of the row key.
|
boolean |
hasNext()
Check if another Page is available.
|
ViewResponse |
next()
Returns the next
ViewResponse . |
void |
remove()
The
remove() method is not supported in this context. |
public Paginator(CouchbaseClient client, View view, Query query, int limit)
client
- the client object to work against.view
- the corresponding view to query.query
- the query object to customize the pages.limit
- the amount of docs to return per page.public final boolean hasNext()
hasNext
in interface java.util.Iterator<ViewResponse>
public final ViewResponse next()
ViewResponse
.next
in interface java.util.Iterator<ViewResponse>
ViewResponse
which represents the next page
or null if there is none (check with hasNext()
first.public void forceKeyType(java.lang.Class<?> clazz)
This should only be used to enforce a different type if absolutely needed, especially if you emit a number as a string from the view. If nothing else is specified, the data will be passed in 1:1 which may lead to infinite loops on stringified numbers. To remedy this situation, forcing to stringify the number again like this helps:
paginatedQuery.forceKeyType(String.class);
Setting it to Integer.class will force a conversion to integer
the other way round. Note that if the class type is not recognized,
the original value will be passed straight through as a String towards
the ComplexKey
class.
This is ignored on reduced and spatial views, because a different strategy (skip) is used there.
clazz
- the enforced key type.public final void remove()
remove()
method is not supported in this context.remove
in interface java.util.Iterator<ViewResponse>
Copyright © 2006-2009 Dustin Sallings, 2009-2012 Couchbase, Inc.