|
21 | 21 | import com.google.api.core.ApiFuture;
|
22 | 22 | import com.google.api.core.ApiFutures;
|
23 | 23 | import com.google.cloud.firestore.CollectionReference;
|
| 24 | +import com.google.cloud.firestore.DocumentReference; |
24 | 25 | import com.google.cloud.firestore.DocumentSnapshot;
|
25 | 26 | import com.google.cloud.firestore.Firestore;
|
26 | 27 | import com.google.cloud.firestore.Query;
|
27 | 28 | import com.google.cloud.firestore.Query.Direction;
|
| 29 | +import com.google.cloud.firestore.QueryDocumentSnapshot; |
28 | 30 | import com.google.cloud.firestore.QuerySnapshot;
|
29 | 31 | import com.google.cloud.firestore.WriteResult;
|
30 | 32 |
|
| 33 | +import com.google.firestore.v1beta1.Document; |
| 34 | +import com.google.protobuf.Api; |
31 | 35 | import java.util.ArrayList;
|
32 | 36 | import java.util.Arrays;
|
33 | 37 | import java.util.List;
|
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | +import java.util.concurrent.TimeoutException; |
34 | 41 |
|
35 | 42 | /** Snippets to support firestore querying data documentation. */
|
36 | 43 | class QueryDataSnippets {
|
@@ -328,4 +335,52 @@ void createMultipleCursorConditionsQuery() {
|
328 | 335 | .startAt("Springfield", "Missouri");
|
329 | 336 | // [END fs_multiple_cursor_conditions]
|
330 | 337 | }
|
| 338 | + |
| 339 | + /** |
| 340 | + * Create a query using a snapshot as a start point. |
| 341 | + * |
| 342 | + * @return query |
| 343 | + */ |
| 344 | + Query createStartAtSnapshotQueryCursor() |
| 345 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 346 | + // [START fs_document_snapshot_cursor] |
| 347 | + // Fetch the snapshot with an API call, waiting for a maximum of 30 seconds for a result. |
| 348 | + ApiFuture<DocumentSnapshot> future = db.collection("cities").document("SF").get(); |
| 349 | + DocumentSnapshot snapshot = future.get(30, TimeUnit.SECONDS); |
| 350 | + |
| 351 | + // Construct the query |
| 352 | + Query query = db.collection("cities") |
| 353 | + .orderBy("population") |
| 354 | + .startAt(snapshot); |
| 355 | + // [END fs_document_snapshot_cursor] |
| 356 | + return query; |
| 357 | + } |
| 358 | + |
| 359 | + /** |
| 360 | + * Example of a paginated query. |
| 361 | + */ |
| 362 | + List<Query> paginateCursor() throws InterruptedException, ExecutionException, TimeoutException { |
| 363 | + // [START fs_paginate_cursor] |
| 364 | + // Construct query for first 25 cities, ordered by population. |
| 365 | + CollectionReference cities = db.collection("cities"); |
| 366 | + Query firstPage = cities |
| 367 | + .orderBy("population") |
| 368 | + .limit(25); |
| 369 | + |
| 370 | + // Wait for the results of the API call, waiting for a maximum of 30 seconds for a result. |
| 371 | + ApiFuture<QuerySnapshot> future = firstPage.get(); |
| 372 | + List<QueryDocumentSnapshot> docs = future.get(30, TimeUnit.SECONDS).getDocuments(); |
| 373 | + |
| 374 | + // Construct query for the next 25 cities. |
| 375 | + QueryDocumentSnapshot lastDoc = docs.get(docs.size() - 1); |
| 376 | + Query secondPage = cities |
| 377 | + .orderBy("population") |
| 378 | + .startAfter(lastDoc) |
| 379 | + .limit(25); |
| 380 | + |
| 381 | + future = secondPage.get(); |
| 382 | + docs = future.get(30, TimeUnit.SECONDS).getDocuments(); |
| 383 | + // [END fs_paginate_cursor] |
| 384 | + return Arrays.asList(firstPage, secondPage); |
| 385 | + } |
331 | 386 | }
|
0 commit comments