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;
}
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);
}
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();
}
Aggregations