Skip to content

Commit eb1e714

Browse files
authored
samples: Add example snippet for snapshot reads (#809)
* Add sample snippet for snapshot reads * Fix linting error * Change readtime to 15 seconds ago. Incorporate feedback.
1 parent 141d68b commit eb1e714

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,4 +1145,31 @@ public void testInQuerySorted() {
11451145
// [END datastore_in_query_sorted]
11461146
assertValidQueryRealBackend(query);
11471147
}
1148+
1149+
@Test
1150+
public void testStaleReads() {
1151+
setUpQueryTestsRealBackend();
1152+
Datastore datastoreClient = datastoreRealBackend;
1153+
// [START datastore_stale_read]
1154+
Key taskKey =
1155+
datastoreClient
1156+
.newKeyFactory()
1157+
.setKind("Task")
1158+
.addAncestors(PathElement.of("TaskList", "default"))
1159+
.newKey("someTask");
1160+
1161+
Timestamp fifteenSecondsAgo =
1162+
Timestamp.ofTimeSecondsAndNanos(Timestamp.now().getSeconds() - 15L, 0);
1163+
// Create a readOption with read time fifteenSecondsAgo
1164+
ReadOption readOption = ReadOption.readTime(fifteenSecondsAgo);
1165+
// Use the readOption to Fetch entity
1166+
Entity entity = datastoreClient.get(taskKey, readOption);
1167+
1168+
// Use the readOption to Query kind Task
1169+
Query<Entity> query = Query.newEntityQueryBuilder().setKind("Task").setLimit(10).build();
1170+
QueryResults<Entity> results = datastoreClient.run(query, readOption);
1171+
Entity result = results.next();
1172+
// [END datastore_stale_read]
1173+
assertValidQueryRealBackend(query);
1174+
}
11481175
}

0 commit comments

Comments
 (0)