Search in sources :

Example 56 with DocumentSnapshot

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

the class RetrieveDataSnippetsIT method testRetrieveAllDocuments.

@Test
public void testRetrieveAllDocuments() throws Exception {
    List<QueryDocumentSnapshot> docs = retrieveDataSnippets.getAllDocuments();
    assertEquals(docs.size(), 5);
    Set<String> docIds = new HashSet<>();
    for (DocumentSnapshot doc : docs) {
        docIds.add(doc.getId());
    }
    assertTrue(docIds.contains("SF") && docIds.contains("LA") && docIds.contains("DC") && docIds.contains("TOK") && docIds.contains("BJ"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) HashSet(java.util.HashSet) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 57 with DocumentSnapshot

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

the class QueryDataSnippets method createAQueryAlternate.

/**
 * Creates a sample query.
 *
 * @return query
 */
Query createAQueryAlternate() throws Exception {
    // [START fs_create_query_country]
    // Create a reference to the cities collection
    CollectionReference cities = db.collection("cities");
    // Create a query against the collection.
    Query query = cities.whereEqualTo("state", "CA");
    // retrieve  query results asynchronously using query.get()
    ApiFuture<QuerySnapshot> querySnapshot = query.get();
    for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
        System.out.println(document.getId());
    }
    // [END fs_create_query_country]
    return query;
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 58 with DocumentSnapshot

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

the class RetrieveDataSnippets method getQueryResults.

/**
 * Return multiple documents from a collection based on a query.
 *
 * @return list of documents of capital cities.
 */
public List<QueryDocumentSnapshot> getQueryResults() throws Exception {
    // [START fs_get_multiple_docs]
    // asynchronously retrieve multiple documents
    ApiFuture<QuerySnapshot> future = db.collection("cities").whereEqualTo("capital", true).get();
    // future.get() blocks on response
    List<QueryDocumentSnapshot> documents = future.get().getDocuments();
    for (DocumentSnapshot document : documents) {
        System.out.println(document.getId() + " => " + document.toObject(City.class));
    }
    // [END fs_get_multiple_docs]
    return documents;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 59 with DocumentSnapshot

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

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 60 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot 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

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