Skip to content

Commit 06a8b4e

Browse files
committed
docs: pagination examples
1 parent 3e02444 commit 06a8b4e

9 files changed

Lines changed: 868 additions & 0 deletions
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* © Copyright IBM Corporation 2025. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package features.pagination;
14+
15+
import java.util.List;
16+
import com.ibm.cloud.cloudant.v1.Cloudant;
17+
import com.ibm.cloud.cloudant.v1.model.DocsResultRow;
18+
import com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions;
19+
import com.ibm.cloud.cloudant.features.pagination.Pager;
20+
import com.ibm.cloud.cloudant.features.pagination.Pagination;
21+
22+
public class AllDocsPagination {
23+
24+
public static void main(String[] args) {
25+
26+
// Initialize service
27+
Cloudant service = Cloudant.newInstance();
28+
29+
// Setup options
30+
PostAllDocsOptions options = new PostAllDocsOptions.Builder()
31+
.db("orders") // example database name
32+
.limit(50) // limit option sets the page size
33+
.startKey("abc") // start from example doc ID abc
34+
.build();
35+
36+
// Create pagination
37+
Pagination<PostAllDocsOptions, DocsResultRow> pagination = Pagination.newPagination(service, options);
38+
// pagination can be reused without side-effects as a factory for iterables, streams or pagers
39+
// options are fixed at pagination creation time
40+
41+
// Option: iterate pages
42+
// Ideal for using an enhanced for loop with each page.
43+
// The Iterable returned from pages() is reusable in that
44+
// calling iterator() returns a new iterator each time.
45+
// The returned iterators, however, are single use.
46+
for (List<DocsResultRow> page : pagination.pages()) {
47+
// Do something with page
48+
}
49+
50+
// Option: stream pages
51+
// Ideal for lazy functional processing of pages and total page limits
52+
pagination.pageStream() // a new stream of the pages
53+
.limit(20) // use Java stream limit to get only the first 20 pages (different from 50 limit used for page size)
54+
.forEach(page -> { // stream operations e.g. terminal forEach
55+
// Do something with page
56+
});
57+
58+
// Option: iterate rows
59+
// Ideal for using an enhanced for loop with each row.
60+
// The Iterable returned from rows() is reusable in that
61+
// calling iterator() returns a new iterator each time.
62+
// The returned iterators, however, are single use.
63+
for (DocsResultRow row : pagination.rows()) {
64+
// Do something with row
65+
}
66+
67+
// Option: stream rows
68+
// Ideal for lazy functional processing of rows and total row limits
69+
pagination.rowStream() // a new stream of the result rows
70+
.limit(1000) // use Java stream limit to cap at 1000 rows (20 page requests of 50 rows each in this example)
71+
.forEach(row -> { // stream operations e.g. terminal forEach
72+
// Do something with row
73+
});
74+
75+
// Option: use pager next page
76+
// For retrieving one page at a time with a method call.
77+
Pager<DocsResultRow> pagePager = pagination.pager();
78+
if (pagePager.hasNext()) {
79+
List<DocsResultRow> page = pagePager.getNext();
80+
// Do something with page
81+
}
82+
83+
// Option: use pager all results
84+
// For retrieving all result rows in a single list
85+
// Note: all result rows may be very large!
86+
// Preferably use streams/iterables instead of getAll for memory efficiency with large result sets.
87+
Pager<DocsResultRow> allPager = pagination.pager();
88+
List<DocsResultRow> allRows = allPager.getAll();
89+
for (DocsResultRow row : allRows) {
90+
// Do something with row
91+
}
92+
93+
}
94+
95+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* © Copyright IBM Corporation 2025. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package features.pagination;
14+
15+
import java.util.List;
16+
import com.ibm.cloud.cloudant.v1.Cloudant;
17+
import com.ibm.cloud.cloudant.v1.model.DocsResultRow;
18+
import com.ibm.cloud.cloudant.v1.model.PostDesignDocsOptions;
19+
import com.ibm.cloud.cloudant.features.pagination.Pager;
20+
import com.ibm.cloud.cloudant.features.pagination.Pagination;
21+
22+
public class DesignDocsPagination {
23+
24+
public static void main(String[] args) {
25+
26+
// Initialize service
27+
Cloudant service = Cloudant.newInstance();
28+
29+
// Setup options
30+
PostDesignDocsOptions options = new PostDesignDocsOptions.Builder()
31+
.db("shoppers") // example database name
32+
.limit(50) // limit option sets the page size
33+
.build();
34+
35+
// Create pagination
36+
Pagination<PostDesignDocsOptions, DocsResultRow> pagination = Pagination.newPagination(service, options);
37+
// pagination can be reused without side-effects as a factory for iterables, streams or pagers
38+
// options are fixed at pagination creation time
39+
40+
// Option: iterate pages
41+
// Ideal for using an enhanced for loop with each page.
42+
// The Iterable returned from pages() is reusable in that
43+
// calling iterator() returns a new iterator each time.
44+
// The returned iterators, however, are single use.
45+
for (List<DocsResultRow> page : pagination.pages()) {
46+
// Do something with page
47+
}
48+
49+
// Option: stream pages
50+
// Ideal for lazy functional processing of pages and total page limits
51+
pagination.pageStream() // a new stream of the pages
52+
.limit(20) // use Java stream limit to get only the first 20 pages (different from 50 limit used for page size)
53+
.forEach(page -> { // stream operations e.g. terminal forEach
54+
// Do something with page
55+
});
56+
57+
// Option: iterate rows
58+
// Ideal for using an enhanced for loop with each row.
59+
// The Iterable returned from rows() is reusable in that
60+
// calling iterator() returns a new iterator each time.
61+
// The returned iterators, however, are single use.
62+
for (DocsResultRow row : pagination.rows()) {
63+
// Do something with row
64+
}
65+
66+
// Option: stream rows
67+
// Ideal for lazy functional processing of rows and total row limits
68+
pagination.rowStream() // a new stream of the rows
69+
.limit(1000) // use Java stream limit to cap at 1000 rows (20 page requests of 50 rows each in this example)
70+
.forEach(row -> { // stream operations e.g. terminal forEach
71+
// Do something with row
72+
});
73+
74+
// Option: use pager next page
75+
// For retrieving one page at a time with a method call.
76+
Pager<DocsResultRow> pagePager = pagination.pager();
77+
if (pagePager.hasNext()) {
78+
List<DocsResultRow> page = pagePager.getNext();
79+
// Do something with page
80+
}
81+
82+
// Option: use pager all results
83+
// For retrieving all result rows in a single list
84+
// Note: all result rows may be very large!
85+
// Preferably use streams/iterables instead of getAll for memory efficiency with large result sets.
86+
Pager<DocsResultRow> allPager = pagination.pager();
87+
List<DocsResultRow> allRows = allPager.getAll();
88+
for (DocsResultRow row : allRows) {
89+
// Do something with row
90+
}
91+
92+
}
93+
94+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* © Copyright IBM Corporation 2025. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package features.pagination;
14+
15+
import java.util.Collections;
16+
import java.util.List;
17+
import com.ibm.cloud.cloudant.v1.Cloudant;
18+
import com.ibm.cloud.cloudant.v1.model.Document;
19+
import com.ibm.cloud.cloudant.v1.model.PostFindOptions;
20+
import com.ibm.cloud.cloudant.features.pagination.Pager;
21+
import com.ibm.cloud.cloudant.features.pagination.Pagination;
22+
23+
public class FindPagination {
24+
25+
public static void main(String[] args) {
26+
27+
// Initialize service
28+
Cloudant service = Cloudant.newInstance();
29+
30+
// Setup options
31+
PostFindOptions options = new PostFindOptions.Builder()
32+
.db("shoppers") // Database name
33+
.limit(50) // limit option sets the page size
34+
.fields(List.of("_id", "type", "name", "email")) // return these fields
35+
.selector(Collections.singletonMap("email_verified", "true")) // select docs with verified emails
36+
.sort(Collections.singletonList(Collections.singletonMap("email", "desc"))) // sort descending by email
37+
.build();
38+
39+
// Create pagination
40+
Pagination<PostFindOptions, Document> pagination = Pagination.newPagination(service, options);
41+
// pagination can be reused without side-effects as a factory for iterables, streams or pagers
42+
// options are fixed at pagination creation time
43+
44+
// Option: iterate pages
45+
// Ideal for using an enhanced for loop with each page.
46+
// The Iterable returned from pages() is reusable in that
47+
// calling iterator() returns a new iterator each time.
48+
// The returned iterators, however, are single use.
49+
for (List<Document> page : pagination.pages()) {
50+
// Do something with page
51+
}
52+
53+
// Option: stream pages
54+
// Ideal for lazy functional processing of pages and total page limits
55+
pagination.pageStream() // a new stream of the pages
56+
.limit(20) // use Java stream limit to get only the first 20 pages (different from 50 limit used for page size)
57+
.forEach(page -> { // stream operations e.g. terminal forEach
58+
// Do something with page
59+
});
60+
61+
// Option: iterate rows
62+
// Ideal for using an enhanced for loop with each row.
63+
// The Iterable returned from rows() is reusable in that
64+
// calling iterator() returns a new iterator each time.
65+
// The returned iterators, however, are single use.
66+
for (Document row : pagination.rows()) {
67+
// Do something with row
68+
}
69+
70+
// Option: stream rows
71+
// Ideal for lazy functional processing of rows and total row limits
72+
pagination.rowStream() // a new stream of the rows
73+
.limit(1000) // use Java stream limit to cap at 1000 rows (20 page requests of 50 rows each in this example)
74+
.forEach(row -> { // stream operations e.g. terminal forEach
75+
// Do something with row
76+
});
77+
78+
// Option: use pager next page
79+
// For retrieving one page at a time with a method call.
80+
Pager<Document> pagePager = pagination.pager();
81+
if (pagePager.hasNext()) {
82+
List<Document> page = pagePager.getNext();
83+
// Do something with page
84+
}
85+
86+
// Option: use pager all results
87+
// For retrieving all result rows in a single list
88+
// Note: all result rows may be very large!
89+
// Preferably use streams/iterables instead of getAll for memory efficiency with large result sets.
90+
Pager<Document> allPager = pagination.pager();
91+
List<Document> allRows = allPager.getAll();
92+
for (Document row : allRows) {
93+
// Do something with row
94+
}
95+
96+
}
97+
98+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* © Copyright IBM Corporation 2025. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package features.pagination;
14+
15+
import java.util.List;
16+
import com.ibm.cloud.cloudant.v1.Cloudant;
17+
import com.ibm.cloud.cloudant.v1.model.DocsResultRow;
18+
import com.ibm.cloud.cloudant.v1.model.PostPartitionAllDocsOptions;
19+
import com.ibm.cloud.cloudant.features.pagination.Pager;
20+
import com.ibm.cloud.cloudant.features.pagination.Pagination;
21+
22+
public class PartitionAllDocsPagination {
23+
24+
public static void main(String[] args) {
25+
26+
// Initialize service
27+
Cloudant service = Cloudant.newInstance();
28+
29+
// Setup options
30+
PostPartitionAllDocsOptions options = new PostPartitionAllDocsOptions.Builder()
31+
.db("events") // example database name
32+
.limit(50) // limit option sets the page size
33+
.partitionKey("ns1HJS13AMkK") // query only this partition
34+
.build();
35+
36+
// Create pagination
37+
Pagination<PostPartitionAllDocsOptions, DocsResultRow> pagination = Pagination.newPagination(service, options);
38+
// pagination can be reused without side-effects as a factory for iterables, streams or pagers
39+
// options are fixed at pagination creation time
40+
41+
// Option: iterate pages
42+
// Ideal for using an enhanced for loop with each page.
43+
// The Iterable returned from pages() is reusable in that
44+
// calling iterator() returns a new iterator each time.
45+
// The returned iterators, however, are single use.
46+
for (List<DocsResultRow> page : pagination.pages()) {
47+
// Do something with page
48+
}
49+
50+
// Option: stream pages
51+
// Ideal for lazy functional processing of pages and total page limits
52+
pagination.pageStream() // a new stream of the pages
53+
.limit(20) // use Java stream limit to get only the first 20 pages (different from 50 limit used for page size)
54+
.forEach(page -> { // stream operations e.g. terminal forEach
55+
// Do something with page
56+
});
57+
58+
// Option: iterate rows
59+
// Ideal for using an enhanced for loop with each row.
60+
// The Iterable returned from rows() is reusable in that
61+
// calling iterator() returns a new iterator each time.
62+
// The returned iterators, however, are single use.
63+
for (DocsResultRow row : pagination.rows()) {
64+
// Do something with row
65+
}
66+
67+
// Option: stream rows
68+
// Ideal for lazy functional processing of rows and total row limits
69+
pagination.rowStream() // a new stream of the rows
70+
.limit(1000) // use Java stream limit to cap at 1000 rows (20 page requests of 50 rows each in this example)
71+
.forEach(row -> { // stream operations e.g. terminal forEach
72+
// Do something with row
73+
});
74+
75+
// Option: use pager next page
76+
// For retrieving one page at a time with a method call.
77+
Pager<DocsResultRow> pagePager = pagination.pager();
78+
if (pagePager.hasNext()) {
79+
List<DocsResultRow> page = pagePager.getNext();
80+
// Do something with page
81+
}
82+
83+
// Option: use pager all results
84+
// For retrieving all result rows in a single list
85+
// Note: all result rows may be very large!
86+
// Preferably use streams/iterables instead of getAll for memory efficiency with large result sets.
87+
Pager<DocsResultRow> allPager = pagination.pager();
88+
List<DocsResultRow> allRows = allPager.getAll();
89+
for (DocsResultRow row : allRows) {
90+
// Do something with row
91+
}
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)