Skip to content
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

samples: Add example snippets for stale reads #1273

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
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.TransactionOptions;
import com.google.cloud.firestore.WriteResult;
import com.google.protobuf.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -591,6 +595,50 @@ Query filterNotIn() {
return query;
}

/**
* Run stale reads. Demonstrate a document lookup and a query.
*
* @return DocumentSnapshot documentResult
*/
DocumentSnapshot runStaleReads() throws Exception {
// [START firestore_stale_read]
// Create a read time 15 seconds in the past
Instant now = Instant.now();
final int fifteenSeconds = 15;
final Timestamp readTime =
Timestamp.newBuilder()
.setSeconds(now.getEpochSecond() - fifteenSeconds)
.setNanos(now.getNano())
.build();

// Set transaction options. Use read-only and set read time
// Stale reads require a read-only transaction
TransactionOptions options =
TransactionOptions.createReadOnlyOptionsBuilder().setReadTime(readTime).build();

// Create a document reference
final DocumentReference documentReference = db.collection("cities").document("SF");

// Create a query against the cities collection.
jlara310 marked this conversation as resolved.
Show resolved Hide resolved
Query query = db.collection("cities").whereEqualTo("capital", true);

// run a transaction with the specified transaction options
ApiFuture<DocumentSnapshot> futureTransaction =
db.runTransaction(
transaction -> {
// Execute a stale read document lookup
final DocumentSnapshot documentResult = transaction.get(documentReference).get();

// Execute a stale read query
final QuerySnapshot queryResult = transaction.get(query).get();

return documentResult;
},
options);
// [END firestore_stale_read]
return futureTransaction.get();
}

/** Closes the gRPC channels associated with this instance and frees up their resources. */
void close() throws Exception {
db.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.collect.Sets.newHashSet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.example.firestore.BaseIntegrationTest;
Expand Down Expand Up @@ -271,6 +272,13 @@ public void testFilterNotIn() throws Exception {
assertEquals(expected, actual);
}

@Test
public void testSnapshotReads() throws Exception {
// Verify that this runs without error
DocumentSnapshot doc = queryDataSnippets.runStaleReads();
jlara310 marked this conversation as resolved.
Show resolved Hide resolved
assertNotNull("should not be null", doc);
}

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