Skip to content

Commit d5e3b1e

Browse files
committed
Add firestore document snapshot and paginate cursor snippets.
1 parent 31b8aa1 commit d5e3b1e

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

firestore/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 54 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,51 @@ 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
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+
void 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+
ApiFuture<QuerySnapshot> firstPage = cities
367+
.orderBy("population")
368+
.limit(25)
369+
.get();
370+
371+
// Wait for results.
372+
List<QueryDocumentSnapshot> docs = firstPage.get(30, TimeUnit.SECONDS).getDocuments();
373+
374+
// Construct query for the next 25 cities.
375+
QueryDocumentSnapshot lastDoc = docs.get(docs.size()-1);
376+
ApiFuture<QuerySnapshot> secondPage = cities
377+
.orderBy("population")
378+
.startAfter(lastDoc)
379+
.limit(25)
380+
.get();
381+
382+
docs = secondPage.get(30, TimeUnit.SECONDS).getDocuments();
383+
// [END fs_paginate_cursor]
384+
}
331385
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ 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");
205+
assertEquals(results, expectedResults);
206+
}
207+
208+
public void testPaginateCursor() throws Exception {
209+
// Snippet executes it's own query. Failures result in thrown Exceptions
210+
queryDataSnippets.paginateCursor();
211+
}
212+
200213
private Set<String> getResultsAsSet(Query query) throws Exception {
201214
List<String> docIds = getResults(query);
202215
return new HashSet<>(docIds);

0 commit comments

Comments
 (0)