From 08e32c71b06c4eab7bd6590a9fd0c33a9b1ecd46 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Wed, 26 Apr 2023 00:05:39 +0000 Subject: [PATCH 1/2] samples: Add example snippets for stale reads --- .../firestore/snippets/QueryDataSnippets.java | 52 ++++++++++++++++++- .../snippets/QueryDataSnippetsIT.java | 8 +++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java b/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java index 8c6d3a220..ad4ac19b4 100644 --- a/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java +++ b/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java @@ -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; @@ -591,7 +595,53 @@ Query filterNotIn() { return query; } - /** Closes the gRPC channels associated with this instance and frees up their resources. */ + /** + * 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. + Query query = db.collection("cities").whereEqualTo("capital", true); + + // run a transaction with the specified transaction options + ApiFuture 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(); } diff --git a/samples/snippets/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java b/samples/snippets/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java index 128a8c54c..50135fdcb 100644 --- a/samples/snippets/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java +++ b/samples/snippets/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java @@ -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; @@ -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(); + assertNotNull("should not be null", doc); + } + private Set getResultsAsSet(Query query) throws Exception { List docIds = getResults(query); return new HashSet<>(docIds); From 18195c99510542d0fdc85178e5e47f16bb968545 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Mon, 8 May 2023 17:42:20 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 8 ++-- .../firestore/snippets/QueryDataSnippets.java | 44 +++++++++---------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5088b6e37..0718ab1e3 100644 --- a/README.md +++ b/README.md @@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies: ```Groovy -implementation platform('com.google.cloud:libraries-bom:26.13.0') +implementation platform('com.google.cloud:libraries-bom:26.14.0') implementation 'com.google.cloud:google-cloud-firestore' ``` If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-firestore:3.9.4' +implementation 'com.google.cloud:google-cloud-firestore:3.10.0' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-firestore" % "3.9.4" +libraryDependencies += "com.google.cloud" % "google-cloud-firestore" % "3.10.0" ``` @@ -222,7 +222,7 @@ Java is a registered trademark of Oracle and/or its affiliates. [kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-firestore/java11.html [stability-image]: https://img.shields.io/badge/stability-stable-green [maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-firestore.svg -[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-firestore/3.9.4 +[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-firestore/3.10.0 [authentication]: https://github.com/googleapis/google-cloud-java#authentication [auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes [predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles diff --git a/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java b/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java index ad4ac19b4..ddab367e9 100644 --- a/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java +++ b/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java @@ -605,43 +605,41 @@ DocumentSnapshot runStaleReads() throws Exception { // 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(); + 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(); + TransactionOptions options = + TransactionOptions.createReadOnlyOptionsBuilder().setReadTime(readTime).build(); // Create a document reference - final DocumentReference documentReference = db - .collection("cities") - .document("SF"); + final DocumentReference documentReference = db.collection("cities").document("SF"); // Create a query against the cities collection. Query query = db.collection("cities").whereEqualTo("capital", true); // run a transaction with the specified transaction options - ApiFuture 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); + ApiFuture 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. - */ + /** Closes the gRPC channels associated with this instance and frees up their resources. */ void close() throws Exception { db.close(); }