Search in sources :

Example 86 with DocumentSnapshot

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));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) SingleField(com.google.cloud.firestore.LocalFirestoreHelper.SingleField) Test(org.junit.Test)

Example 87 with DocumentSnapshot

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"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 88 with DocumentSnapshot

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"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FieldValue(com.google.cloud.firestore.FieldValue) Test(org.junit.Test)

Example 89 with DocumentSnapshot

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"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) TransactionOptions(com.google.cloud.firestore.TransactionOptions) Timestamp(com.google.cloud.Timestamp) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 90 with DocumentSnapshot

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);
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FirestoreException(com.google.cloud.firestore.FirestoreException) Map(java.util.Map) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)103 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)73 Test (org.junit.Test)52 DocumentReference (com.google.cloud.firestore.DocumentReference)49 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)23 ExecutionException (java.util.concurrent.ExecutionException)13 CollectionReference (com.google.cloud.firestore.CollectionReference)12 Query (com.google.cloud.firestore.Query)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)10 SingleField (com.google.cloud.firestore.LocalFirestoreHelper.SingleField)9 WriteResult (com.google.cloud.firestore.WriteResult)9 ArrayList (java.util.ArrayList)8 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)6 HashMap (java.util.HashMap)6 BulkWriter (com.google.cloud.firestore.BulkWriter)5 City (com.example.firestore.snippets.model.City)4 Timestamp (com.google.cloud.Timestamp)4 FirestoreBundleTest.verifyNamedQuery (com.google.cloud.firestore.FirestoreBundleTest.verifyNamedQuery)4 AllSupportedTypes (com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes)4 WriteBatch (com.google.cloud.firestore.WriteBatch)4