Search in sources :

Example 51 with CollectionReference

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

the class FireStoreUtils method scanCollectionObjects.

/**
 * This code is a bit ugly, but here is why...
 * (from https://cloud.google.com/firestore/docs/solutions/delete-collections)
 * <ul>
 * <li>There is no operation that atomically deletes a collection.</li>
 * <li>Deleting a document does not delete the documents in its subcollections.</li>
 * <li>If your documents have dynamic subcollections, (we don't do this!)
 * it can be hard to know what data to delete for a given path.</li>
 * <li>Deleting a collection of more than 500 documents requires multiple batched
 * write operations or hundreds of single deletes.</li>
 * </ul>
 * <p>
 * Our objects are small, so I think we can use the maximum batch size without
 * concern for using too much memory.
 */
void scanCollectionObjects(Firestore firestore, String collectionId, int batchSize, Consumer<QueryDocumentSnapshot> func) {
    CollectionReference datasetCollection = firestore.collection(collectionId);
    try {
        int batchCount = 0;
        int visited;
        do {
            visited = 0;
            ApiFuture<QuerySnapshot> future = datasetCollection.limit(batchSize).get();
            List<QueryDocumentSnapshot> documents = future.get().getDocuments();
            batchCount++;
            logger.info("Visiting batch " + batchCount + " of ~" + batchSize + " documents");
            for (QueryDocumentSnapshot document : documents) {
                func.accept(document);
                visited++;
            }
        } while (visited >= batchSize);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new FileSystemExecutionException("scanning collection - execution interrupted", ex);
    } catch (ExecutionException ex) {
        throw new FileSystemExecutionException("scanning collection - execution exception", ex);
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 52 with CollectionReference

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

the class ITSystemTest method testRecursiveDeleteDoesNotAffectOtherCollections.

@Test
public void testRecursiveDeleteDoesNotAffectOtherCollections() throws Exception {
    setupRecursiveDeleteTest();
    // Add another nested collection that shouldn't be deleted.
    CollectionReference collectionB = firestore.collection("doggos");
    collectionB.document("doggo").set(map("name", "goodboi")).get();
    firestore.recursiveDelete(collectionB).get();
    assertEquals(6, countCollectionChildren(randomColl));
    assertEquals(0, countCollectionChildren(collectionB));
}
Also used : CollectionReference(com.google.cloud.firestore.CollectionReference) Test(org.junit.Test)

Example 53 with CollectionReference

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

the class ITSystemTest method listCollections.

@Test
public void listCollections() throws Exception {
    // We test with 21 collections since 20 collections are by default returned in a single paged
    // response.
    String[] collections = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21" };
    // Sort in alphabetical (non-numeric) order.
    Arrays.sort(collections);
    WriteBatch batch = firestore.batch();
    for (String collection : collections) {
        batch.create(randomDoc.collection(collection).document("doc"), SINGLE_FIELD_OBJECT);
    }
    batch.commit().get();
    Iterable<CollectionReference> collectionRefs = randomDoc.listCollections();
    int count = 0;
    for (CollectionReference collectionRef : collectionRefs) {
        assertEquals(collections[count++], collectionRef.getId());
    }
    assertEquals(collections.length, count);
}
Also used : WriteBatch(com.google.cloud.firestore.WriteBatch) CollectionReference(com.google.cloud.firestore.CollectionReference) Test(org.junit.Test)

Example 54 with CollectionReference

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

the class ITSystemTest method testEnforcesTimeouts.

@Test
public void testEnforcesTimeouts() {
    FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().setRetrySettings(RetrySettings.newBuilder().setMaxRpcTimeout(Duration.ofMillis(1)).setTotalTimeout(Duration.ofMillis(1)).setInitialRpcTimeout(Duration.ofMillis(1)).build()).build();
    firestore = firestoreOptions.getService();
    CollectionReference collection = firestore.collection("timeout");
    // RunQuery
    assertThrows(ExecutionException.class, () -> collection.get().get());
    // CommitRequest
    assertThrows(ExecutionException.class, () -> collection.add(map()).get());
    // BulkCommit
    assertThrows(ExecutionException.class, () -> {
        BulkWriter bulkWriter = firestore.bulkWriter();
        ApiFuture<WriteResult> op = bulkWriter.set(collection.document(), map());
        bulkWriter.close();
        op.get();
    });
    // BatchGetDocuments
    assertThrows(ExecutionException.class, () -> collection.document().get().get());
    // ListDocuments
    assertThrows(FirestoreException.class, () -> collection.listDocuments().iterator().hasNext());
    // ListCollections
    assertThrows(FirestoreException.class, () -> collection.document().listCollections().iterator().hasNext());
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) BulkWriter(com.google.cloud.firestore.BulkWriter) CollectionReference(com.google.cloud.firestore.CollectionReference) FirestoreOptions(com.google.cloud.firestore.FirestoreOptions) Test(org.junit.Test)

Example 55 with CollectionReference

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

the class QueryDataSnippets method createChainedQuery.

/**
 * Creates chained where clauses.
 *
 * <p>Note : equality and inequality clauses over multiple fields cannot be chained.
 *
 * @return query
 */
Query createChainedQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_chained_query]
    // [START firestore_query_filter_compound_multi_eq]
    Query chainedQuery1 = cities.whereEqualTo("state", "CO").whereEqualTo("name", "Denver");
    // [END fs_chained_query]
    return chainedQuery1;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Aggregations

CollectionReference (com.google.cloud.firestore.CollectionReference)72 Query (com.google.cloud.firestore.Query)48 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)16 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)12 DocumentReference (com.google.cloud.firestore.DocumentReference)11 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)11 ArrayList (java.util.ArrayList)11 ExecutionException (java.util.concurrent.ExecutionException)9 Test (org.junit.Test)9 WriteResult (com.google.cloud.firestore.WriteResult)7 DatasetDataProject (bio.terra.service.dataset.DatasetDataProject)6 ApiFuture (com.google.api.core.ApiFuture)6 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)5 HashMap (java.util.HashMap)5 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)4 City (com.example.firestore.snippets.model.City)4 Firestore (com.google.cloud.firestore.Firestore)4 FileSystemCorruptException (bio.terra.service.filedata.exception.FileSystemCorruptException)2 TranslateMessage (com.getstarted.background.objects.TranslateMessage)2 FirestoreOptions (com.google.cloud.firestore.FirestoreOptions)2