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