Search in sources :

Example 1 with Transaction

use of com.google.cloud.firestore.Transaction in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method runSimpleTransaction.

// [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]
    // 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> transaction = db.runTransaction(new Transaction.Function<Void>() {

        @Override
        public Void updateCallback(Transaction transaction) throws Exception {
            // 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 transaction;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Transaction(com.google.cloud.firestore.Transaction) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 2 with Transaction

use of com.google.cloud.firestore.Transaction in project jade-data-repo by DataBiosphere.

the class FireStoreDirectoryDao method enumerateDirectory.

// -- private methods --
List<FireStoreDirectoryEntry> enumerateDirectory(Firestore firestore, String collectionId, String dirPath) {
    ApiFuture<List<FireStoreDirectoryEntry>> transaction = firestore.runTransaction(xn -> {
        Query query = firestore.collection(collectionId).whereEqualTo("path", dirPath);
        ApiFuture<QuerySnapshot> querySnapshot = xn.get(query);
        List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
        List<FireStoreDirectoryEntry> entryList = documents.stream().map(document -> document.toObject(FireStoreDirectoryEntry.class)).collect(Collectors.toList());
        return entryList;
    });
    return fireStoreUtils.transactionGet("enumerateDirectory", transaction);
}
Also used : Firestore(com.google.cloud.firestore.Firestore) FileSystemAbortTransactionException(bio.terra.service.filedata.exception.FileSystemAbortTransactionException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionReference(com.google.cloud.firestore.CollectionReference) StringUtils(org.apache.commons.lang3.StringUtils) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) Logger(org.slf4j.Logger) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) Collection(java.util.Collection) UUID(java.util.UUID) NotImplementedException(bio.terra.common.exception.NotImplementedException) Transaction(com.google.cloud.firestore.Transaction) Instant(java.time.Instant) AbortedException(com.google.api.gax.rpc.AbortedException) Collectors(java.util.stream.Collectors) ApiFuture(com.google.api.core.ApiFuture) ExecutionException(java.util.concurrent.ExecutionException) Component(org.springframework.stereotype.Component) List(java.util.List) Query(com.google.cloud.firestore.Query) DocumentReference(com.google.cloud.firestore.DocumentReference) Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) List(java.util.List) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 3 with Transaction

use of com.google.cloud.firestore.Transaction in project java-docs-samples by GoogleCloudPlatform.

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]
    final DocumentReference docRef = db.collection("cities").document("SF");
    ApiFuture<String> transaction = db.runTransaction(new Transaction.Function<String>() {

        @Override
        public String updateCallback(Transaction transaction) throws Exception {
            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(transaction.get());
    // [END fs_return_info_transaction]
    return transaction.get();
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Transaction(com.google.cloud.firestore.Transaction) HashMap(java.util.HashMap) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentReference (com.google.cloud.firestore.DocumentReference)3 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)3 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)3 Transaction (com.google.cloud.firestore.Transaction)3 NotImplementedException (bio.terra.common.exception.NotImplementedException)1 FileSystemAbortTransactionException (bio.terra.service.filedata.exception.FileSystemAbortTransactionException)1 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)1 City (com.example.firestore.snippets.model.City)1 ApiFuture (com.google.api.core.ApiFuture)1 AbortedException (com.google.api.gax.rpc.AbortedException)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Firestore (com.google.cloud.firestore.Firestore)1 Query (com.google.cloud.firestore.Query)1 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)1 WriteResult (com.google.cloud.firestore.WriteResult)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1