Skip to content

Commit fd030d1

Browse files
authored
[Firestore] Snippets for document snapshots and paginated queries. (GoogleCloudPlatform#1243)
* Add firestore document snapshot and paginate cursor snippets. * Add test annotation. * Fix SnapshotQueryCursor test. * Correct Checkstyle violations. * Add comment clarifing futures wait time. * Update pagination test to check queries. * Fixed test order.
1 parent 6b086f1 commit fd030d1

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

firestore/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<parent>
3333
<groupId>com.google.cloud.samples</groupId>
3434
<artifactId>shared-configuration</artifactId>
35-
<version>1.0.9</version>
35+
<version>1.0.10</version>
3636
</parent>
3737
<properties>
3838
<maven.compiler.target>1.8</maven.compiler.target>
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.google.cloud</groupId>
4747
<artifactId>google-cloud-firestore</artifactId>
48-
<version>0.60.0-beta</version>
48+
<version>0.68.0-beta</version>
4949
</dependency>
5050
<!-- [END fs-maven] -->
5151

firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@
2121
import com.google.api.core.ApiFuture;
2222
import com.google.api.core.ApiFutures;
2323
import com.google.cloud.firestore.CollectionReference;
24+
import com.google.cloud.firestore.DocumentReference;
2425
import com.google.cloud.firestore.DocumentSnapshot;
2526
import com.google.cloud.firestore.Firestore;
2627
import com.google.cloud.firestore.Query;
2728
import com.google.cloud.firestore.Query.Direction;
29+
import com.google.cloud.firestore.QueryDocumentSnapshot;
2830
import com.google.cloud.firestore.QuerySnapshot;
2931
import com.google.cloud.firestore.WriteResult;
3032

33+
import com.google.firestore.v1beta1.Document;
34+
import com.google.protobuf.Api;
3135
import java.util.ArrayList;
3236
import java.util.Arrays;
3337
import java.util.List;
38+
import java.util.concurrent.ExecutionException;
39+
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.TimeoutException;
3441

3542
/** Snippets to support firestore querying data documentation. */
3643
class QueryDataSnippets {
@@ -328,4 +335,52 @@ void createMultipleCursorConditionsQuery() {
328335
.startAt("Springfield", "Missouri");
329336
// [END fs_multiple_cursor_conditions]
330337
}
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+
}
331386
}

firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ public void testMultipleCursorConditions() throws Exception {
197197
assertTrue(Objects.equals(result, expectedResults));
198198
}
199199

200+
@Test
201+
public void testCreateStartAtSnapshotQueryCursor() throws Exception {
202+
Query q = queryDataSnippets.createStartAtSnapshotQueryCursor();
203+
List<String> results = getResults(q);
204+
List<String> expectedResults = Arrays.asList("SF", "LA", "TOK", "BJ");
205+
assertEquals(expectedResults, results);
206+
}
207+
208+
@Test
209+
public void testPaginateCursor() throws Exception {
210+
// Snippet executes it's own query. Failures result in thrown Exceptions
211+
List<Query> pages = queryDataSnippets.paginateCursor();
212+
List<String> firstPage = getResults(pages.get(0));
213+
List<String> secondPage = getResults(pages.get(1));
214+
assertEquals(Arrays.asList("DC", "SF", "LA", "TOK", "BJ"), firstPage);
215+
assertEquals(new ArrayList<String>(), secondPage);
216+
}
217+
200218
private Set<String> getResultsAsSet(Query query) throws Exception {
201219
List<String> docIds = getResults(query);
202220
return new HashSet<>(docIds);

0 commit comments

Comments
 (0)