Skip to content

[Firestore] Snippets for document snapshots and paginated queries. #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions firestore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
</parent>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -45,7 +45,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>0.60.0-beta</version>
<version>0.68.0-beta</version>
</dependency>
<!-- [END fs-maven] -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.Query.Direction;
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.WriteResult;

import com.google.firestore.v1beta1.Document;
import com.google.protobuf.Api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/** Snippets to support firestore querying data documentation. */
class QueryDataSnippets {
Expand Down Expand Up @@ -328,4 +335,52 @@ void createMultipleCursorConditionsQuery() {
.startAt("Springfield", "Missouri");
// [END fs_multiple_cursor_conditions]
}

/**
* Create a query using a snapshot as a start point.
*
* @return query
*/
Query createStartAtSnapshotQueryCursor()
throws InterruptedException, ExecutionException, TimeoutException {
// [START fs_document_snapshot_cursor]
// Fetch the snapshot with an API call, waiting for a maximum of 30 seconds for a result.
ApiFuture<DocumentSnapshot> future = db.collection("cities").document("SF").get();
DocumentSnapshot snapshot = future.get(30, TimeUnit.SECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the magic number represent other than 30 seconds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the amount of time to block for results -- if this limit is exceeded, an exception is thrown.

Copy link
Contributor

@frankyn frankyn Oct 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a variable name instead of a magic number to state the same information?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number isn't magical, it is just a safe amount of time. I don't think a variable would make sense in the context, since the value depends on the users situation and is only used once. I have updated with a comment to better explain what the future is doing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that helps!


// Construct the query
Query query = db.collection("cities")
.orderBy("population")
.startAt(snapshot);
// [END fs_document_snapshot_cursor]
return query;
}

/**
* Example of a paginated query.
*/
List<Query> paginateCursor() throws InterruptedException, ExecutionException, TimeoutException {
// [START fs_paginate_cursor]
// Construct query for first 25 cities, ordered by population.
CollectionReference cities = db.collection("cities");
Query firstPage = cities
.orderBy("population")
.limit(25);

// Wait for the results of the API call, waiting for a maximum of 30 seconds for a result.
ApiFuture<QuerySnapshot> future = firstPage.get();
List<QueryDocumentSnapshot> docs = future.get(30, TimeUnit.SECONDS).getDocuments();

// Construct query for the next 25 cities.
QueryDocumentSnapshot lastDoc = docs.get(docs.size() - 1);
Query secondPage = cities
.orderBy("population")
.startAfter(lastDoc)
.limit(25);

future = secondPage.get();
docs = future.get(30, TimeUnit.SECONDS).getDocuments();
// [END fs_paginate_cursor]
return Arrays.asList(firstPage, secondPage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ public void testMultipleCursorConditions() throws Exception {
assertTrue(Objects.equals(result, expectedResults));
}

@Test
public void testCreateStartAtSnapshotQueryCursor() throws Exception {
Query q = queryDataSnippets.createStartAtSnapshotQueryCursor();
List<String> results = getResults(q);
List<String> expectedResults = Arrays.asList("SF", "LA", "TOK", "BJ");
assertEquals(expectedResults, results);
}

@Test
public void testPaginateCursor() throws Exception {
// Snippet executes it's own query. Failures result in thrown Exceptions
List<Query> pages = queryDataSnippets.paginateCursor();
List<String> firstPage = getResults(pages.get(0));
List<String> secondPage = getResults(pages.get(1));
assertEquals(Arrays.asList("DC", "SF", "LA", "TOK", "BJ"), firstPage);
assertEquals(new ArrayList<String>(), secondPage);
}

private Set<String> getResultsAsSet(Query query) throws Exception {
List<String> docIds = getResults(query);
return new HashSet<>(docIds);
Expand Down