@@ -377,18 +377,39 @@ void add(String path, int readTimeSeconds, int readTimeNanos, MutableDocument do
377
377
}
378
378
379
379
BackfillResult backfill (SQLitePersistence db ) {
380
+ // Just return immediately if there are no pending backfills, as a performance optimization.
381
+ // This just elides a few allocations (e.g. the ArrayList below) that would otherwise
382
+ // needlessly occur.
383
+ if (documentTypeByBackfillKey .isEmpty ()) {
384
+ return BackfillResult .NO_PENDING_BACKFILLS ;
385
+ }
386
+
387
+ ArrayList <Object > sqlBindings = new ArrayList <>();
388
+ String sql = calculateBackfillSql (sqlBindings );
389
+ if (sql != null ) {
390
+ db .execute (sql , sqlBindings .toArray ());
391
+ }
392
+
393
+ return documentTypeByBackfillKey .isEmpty ()
394
+ ? BackfillResult .NO_PENDING_BACKFILLS
395
+ : BackfillResult .HAS_PENDING_BACKFILLS ;
396
+ }
397
+
398
+ @ Nullable
399
+ String calculateBackfillSql (ArrayList <Object > bindings ) {
380
400
StringBuilder caseClauses = new StringBuilder ();
381
401
StringBuilder whereClauses = new StringBuilder ();
382
- ArrayList <Object > bindings = new ArrayList <>();
383
402
384
403
Iterator <BackfillKey > backfillKeys = documentTypeByBackfillKey .keySet ().iterator ();
404
+ boolean backfillsFound = false ;
385
405
while (backfillKeys .hasNext () && bindings .size () < SQLitePersistence .LongQuery .LIMIT ) {
386
406
BackfillKey backfillKey = backfillKeys .next ();
387
407
DocumentType documentType = documentTypeByBackfillKey .remove (backfillKey );
388
408
if (documentType == null ) {
389
409
continue ;
390
410
}
391
411
412
+ backfillsFound = true ;
392
413
bindings .add (backfillKey .path );
393
414
int pathBindingNumber = bindings .size ();
394
415
bindings .add (backfillKey .readTimeSeconds );
@@ -421,19 +442,14 @@ BackfillResult backfill(SQLitePersistence db) {
421
442
.append (')' );
422
443
}
423
444
424
- if (!bindings .isEmpty ()) {
425
- String sql =
426
- "UPDATE remote_documents SET document_type = CASE"
427
- + caseClauses
428
- + " ELSE NULL END WHERE"
429
- + whereClauses ;
430
- android .util .Log .i ("zzyzx" , sql );
431
- db .execute (sql , bindings .toArray ());
445
+ if (!backfillsFound ) {
446
+ return null ;
432
447
}
433
448
434
- return documentTypeByBackfillKey .isEmpty ()
435
- ? BackfillResult .NO_PENDING_BACKFILLS
436
- : BackfillResult .HAS_PENDING_BACKFILLS ;
449
+ return "UPDATE remote_documents SET document_type = CASE"
450
+ + caseClauses
451
+ + " ELSE NULL END WHERE"
452
+ + whereClauses ;
437
453
}
438
454
439
455
private static class BackfillKey {
0 commit comments