Search in sources :

Example 31 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class ManageDataSnippets method runSimpleTransaction.

// [END firestore_data_delete_collection]
// [END fs_delete_collection]
/**
 * Run a simple transaction to perform a field value increment.
 *
 * @return transaction future
 */
ApiFuture<Void> runSimpleTransaction() throws Exception {
    // [START fs_run_simple_transaction]
    // [START firestore_transaction_document_update]
    // Initialize doc
    final DocumentReference docRef = db.collection("cities").document("SF");
    City city = new City("SF");
    city.setCountry("USA");
    city.setPopulation(860000L);
    docRef.set(city).get();
    // run an asynchronous transaction
    ApiFuture<Void> futureTransaction = db.runTransaction(transaction -> {
        // retrieve document and increment population field
        DocumentSnapshot snapshot = transaction.get(docRef).get();
        long oldPopulation = snapshot.getLong("population");
        transaction.update(docRef, "population", oldPopulation + 1);
        return null;
    });
    // [END fs_run_simple_transaction]
    return futureTransaction;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 32 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class ManageDataSnippets method returnInfoFromTransaction.

/**
 * Return information from a conditional transaction.
 *
 * @param population : set initial population.
 */
String returnInfoFromTransaction(long population) throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put("population", population);
    // Block until transaction is complete is using transaction.get()
    db.collection("cities").document("SF").set(map).get();
    // [START fs_return_info_transaction]
    // [START firestore_transaction_document_update_conditional]
    final DocumentReference docRef = db.collection("cities").document("SF");
    ApiFuture<String> futureTransaction = db.runTransaction(transaction -> {
        DocumentSnapshot snapshot = transaction.get(docRef).get();
        Long newPopulation = snapshot.getLong("population") + 1;
        // conditionally update based on current population
        if (newPopulation <= 1000000L) {
            transaction.update(docRef, "population", newPopulation);
            return "Population increased to " + newPopulation;
        } else {
            throw new Exception("Sorry! Population is too big.");
        }
    });
    // Print information retrieved from transaction
    System.out.println(futureTransaction.get());
    // [END fs_return_info_transaction]
    return futureTransaction.get();
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) HashMap(java.util.HashMap) DocumentReference(com.google.cloud.firestore.DocumentReference) ExecutionException(java.util.concurrent.ExecutionException)

Example 33 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class BaseIntegrationTest method deleteAllDocuments.

protected static void deleteAllDocuments(Firestore db) throws Exception {
    ApiFuture<QuerySnapshot> future = db.collection("cities").get();
    QuerySnapshot querySnapshot = future.get();
    for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
        // block on delete operation
        db.collection("cities").document(doc.getId()).delete().get();
    }
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 34 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class QuickstartIT method validate.

private void validate(DocumentReference docRef, Map<String, Object> data) throws Exception {
    DocumentSnapshot documentSnapshot = docRef.get().get();
    assertTrue(Objects.equals(documentSnapshot.getData(), data));
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot)

Example 35 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class ManageDataSnippetsIT method testUpdateDocumentIncrementSuccessful.

@Test
public void testUpdateDocumentIncrementSuccessful() throws Exception {
    manageDataSnippets.updateDocumentIncrement();
    CollectionReference collection = db.collection("cities");
    DocumentReference documentReference = collection.document("DC");
    final DocumentSnapshot data = documentReference.get().get();
    assertTrue(data.contains("population"));
    assertEquals((Long) 150L, data.getLong("population"));
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

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