Skip to content

Commit df9bf43

Browse files
committed
SQLiteRemoteDocumentCache.java: factor out sql statement string building
1 parent 96fe2e7 commit df9bf43

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,39 @@ void add(String path, int readTimeSeconds, int readTimeNanos, MutableDocument do
377377
}
378378

379379
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) {
380400
StringBuilder caseClauses = new StringBuilder();
381401
StringBuilder whereClauses = new StringBuilder();
382-
ArrayList<Object> bindings = new ArrayList<>();
383402

384403
Iterator<BackfillKey> backfillKeys = documentTypeByBackfillKey.keySet().iterator();
404+
boolean backfillsFound = false;
385405
while (backfillKeys.hasNext() && bindings.size() < SQLitePersistence.LongQuery.LIMIT) {
386406
BackfillKey backfillKey = backfillKeys.next();
387407
DocumentType documentType = documentTypeByBackfillKey.remove(backfillKey);
388408
if (documentType == null) {
389409
continue;
390410
}
391411

412+
backfillsFound = true;
392413
bindings.add(backfillKey.path);
393414
int pathBindingNumber = bindings.size();
394415
bindings.add(backfillKey.readTimeSeconds);
@@ -421,19 +442,14 @@ BackfillResult backfill(SQLitePersistence db) {
421442
.append(')');
422443
}
423444

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;
432447
}
433448

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;
437453
}
438454

439455
private static class BackfillKey {

0 commit comments

Comments
 (0)