Skip to content

Commit 16904c6

Browse files
committed
firestore/testutil/IntegrationTestUtil.java: use a WriteBatch to create documents, for efficiency
1 parent cfc409c commit 16904c6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/IntegrationTestUtil.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.firebase.firestore.ListenerRegistration;
3737
import com.google.firebase.firestore.MetadataChanges;
3838
import com.google.firebase.firestore.QuerySnapshot;
39+
import com.google.firebase.firestore.WriteBatch;
3940
import com.google.firebase.firestore.auth.User;
4041
import com.google.firebase.firestore.core.DatabaseInfo;
4142
import com.google.firebase.firestore.model.DatabaseId;
@@ -347,8 +348,27 @@ public static CollectionReference testCollectionWithDocs(Map<String, Map<String,
347348

348349
public static void writeAllDocs(
349350
CollectionReference collection, Map<String, Map<String, Object>> docs) {
351+
WriteBatch writeBatch = null;
352+
int writeBatchSize = 0;
353+
350354
for (Map.Entry<String, Map<String, Object>> doc : docs.entrySet()) {
351-
waitFor(collection.document(doc.getKey()).set(doc.getValue()));
355+
if (writeBatch == null) {
356+
writeBatch = collection.getFirestore().batch();
357+
}
358+
359+
writeBatch.set(collection.document(doc.getKey()), doc.getValue());
360+
writeBatchSize++;
361+
362+
// Write batches are capped at 500 writes. Use 400 just to be safe.
363+
if (writeBatchSize == 400) {
364+
waitFor(writeBatch.commit());
365+
writeBatch = null;
366+
writeBatchSize = 0;
367+
}
368+
}
369+
370+
if (writeBatch != null) {
371+
waitFor(writeBatch.commit());
352372
}
353373
}
354374

0 commit comments

Comments
 (0)