use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class ITSystemTest method setDocumentWithValue.
@Test
public void setDocumentWithValue() throws Exception {
assertEquals(20, randomDoc.getId().length());
randomDoc.set(LocalFirestoreHelper.SINGLE_FIELD_PROTO).get();
DocumentSnapshot documentSnapshot = randomDoc.get().get();
assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class));
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class ITSystemTest method successfulTransactionWithContention.
@Test
public void successfulTransactionWithContention() throws Exception {
final DocumentReference documentReference = addDocument("counter", 1);
final CountDownLatch latch = new CountDownLatch(2);
final AtomicInteger attempts = new AtomicInteger();
// One of these transaction fails and has to be retried since they both acquire locks on the
// same document, which they then modify.
ApiFuture<String> firstTransaction = firestore.runTransaction(transaction -> {
attempts.incrementAndGet();
DocumentSnapshot documentSnapshot = transaction.get(documentReference).get();
latch.countDown();
latch.await();
transaction.update(documentReference, "counter", documentSnapshot.getLong("counter") + 1);
return "foo";
});
ApiFuture<String> secondTransaction = firestore.runTransaction(transaction -> {
attempts.incrementAndGet();
List<DocumentSnapshot> documentSnapshots = transaction.getAll(documentReference).get();
latch.countDown();
latch.await();
transaction.update(documentReference, "counter", documentSnapshots.get(0).getLong("counter") + 1);
return "bar";
});
assertEquals("foo", firstTransaction.get());
assertEquals("bar", secondTransaction.get());
assertEquals(3, attempts.intValue());
assertEquals(3, (long) documentReference.get().get().getLong("counter"));
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class ITSystemTest method mergeDocumentWithServerTimestamp.
@Test
public void mergeDocumentWithServerTimestamp() throws Exception {
Map<String, Object> originalMap = LocalFirestoreHelper.map("a", "b");
Map<String, FieldValue> updateMap = map("c", FieldValue.serverTimestamp());
randomDoc.set(originalMap).get();
randomDoc.set(updateMap, SetOptions.merge()).get();
DocumentSnapshot documentSnapshot = randomDoc.get().get();
assertEquals("b", documentSnapshot.getString("a"));
assertNotNull(documentSnapshot.getDate("c"));
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class ITSystemTest method readOnlyTransaction_successfulRead.
@Test
public void readOnlyTransaction_successfulRead() throws Exception {
DocumentReference documentReference = randomColl.add(SINGLE_FIELD_MAP).get();
Timestamp firstWriteTime = documentReference.set(Collections.singletonMap("counter", 1)).get().getUpdateTime();
documentReference.set(Collections.singletonMap("counter", 2)).get();
final TransactionOptions options = TransactionOptions.createReadOnlyOptionsBuilder().setReadTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(firstWriteTime.getSeconds()).setNanos(firstWriteTime.getNanos())).build();
final ApiFuture<Long> runTransaction = firestore.runTransaction(transaction -> {
final DocumentSnapshot snapshot = transaction.get(documentReference).get(5, TimeUnit.SECONDS);
return snapshot.getLong("counter");
}, options);
assertEquals(1, runTransaction.get(10, TimeUnit.SECONDS).longValue());
DocumentSnapshot documentSnapshot = documentReference.get().get();
assertEquals(2, (long) documentSnapshot.getData().get("counter"));
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class ListenDataSnippets method listenToDocument.
/**
* Listen to a single document, returning data after the first snapshot.
*/
Map<String, Object> listenToDocument() throws Exception {
final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create();
// [START listen_to_document]
// [START firestore_listen_document]
DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirestoreException e) {
if (e != null) {
System.err.println("Listen failed: " + e);
return;
}
if (snapshot != null && snapshot.exists()) {
System.out.println("Current data: " + snapshot.getData());
} else {
System.out.print("Current data: null");
}
// [START_EXCLUDE silent]
if (!future.isDone()) {
future.set(snapshot.getData());
}
// [END_EXCLUDE]
}
});
return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Aggregations