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