π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Optimize only-new backup filtering with single DB query
Prateek Sunala7c8851a8de4
diff --git a/mobile/apps/photos/lib/db/files_db.dart b/mobile/apps/photos/lib/db/files_db.dartindex 270c2c8c4f..c362b1f454 100644--- a/mobile/apps/photos/lib/db/files_db.dart+++ b/mobile/apps/photos/lib/db/files_db.dart@@ -1331,36 +1331,19 @@ class FilesDB with SqlDbBase {return convertToFiles(results);}- Future<Set<String>> getLocalIDsNewerThan(- Set<String> localIDs,+ Future<Set<String>> getAllLocalIDsNewerThan(int creationTimeThreshold,) async {- if (localIDs.isEmpty) {- return {};- }final db = await instance.sqliteAsyncDB;- final result = <String>{};- const batchSize = 800;- final idsList = localIDs.toList();- for (int start = 0; start < idsList.length; start += batchSize) {- final end = start + batchSize > idsList.length- ? idsList.length- : start + batchSize;- final batch = idsList.sublist(start, end);- final placeholders = List.filled(batch.length, '?').join(',');- final rows = await db.getAll(- '''- SELECT DISTINCT $columnLocalID- FROM $filesTable- WHERE $columnLocalID IN ($placeholders) AND $columnCreationTime >= ?- ''',- [...batch, creationTimeThreshold],- );- for (final row in rows) {- result.add(row[columnLocalID] as String);- }- }- return result;+ final rows = await db.getAll(+ '''+ SELECT DISTINCT $columnLocalID+ FROM $filesTable+ WHERE $columnCreationTime >= ? AND $columnLocalID IS NOT NULL+ ''',+ [creationTimeThreshold],+ );+ return rows.map((row) => row[columnLocalID] as String).toSet();}Future<void> deleteUnSyncedLocalFiles(List<String> localIDs) async {diff --git a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart b/mobile/apps/photos/lib/services/sync/remote_sync_service.dartindex b27358f1a2..b86af29fe7 100644--- a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart@@ -364,11 +364,22 @@ class RemoteSyncService {deviceCollections.sort((a, b) => a.count.compareTo(b.count));final Map<String, Set<String>> pathIdToLocalIDs =await _db.getDevicePathIDToLocalIDMap();++ // Fetch all newer local IDs once if only-new backup is enabled+ Set<String>? newerLocalIDs;+ if (flagService.enableOnlyBackupFuturePhotos) {+ final int? onlyNewSince = localSettings.onlyNewSinceEpoch;+ if (onlyNewSince != null) {+ // Single DB query for all newer files+ newerLocalIDs = await _db.getAllLocalIDsNewerThan(onlyNewSince);+ _logger.info("Found ${newerLocalIDs.length} newer files");+ }+ }+bool moreFilesMarkedForBackup = false;for (final deviceCollection in deviceCollections) {- Set<String> localIDsToSync = {- ...(pathIdToLocalIDs[deviceCollection.id] ?? {}),- };+ final Set<String> localIDsToSync =+ pathIdToLocalIDs[deviceCollection.id]?.toSet() ?? {};if (deviceCollection.uploadStrategy == UploadStrategy.ifMissing) {final Set<String> alreadyClaimedLocalIDs =await _db.getLocalIDsMarkedForOrAlreadyUploaded(ownerID);@@ -382,8 +393,9 @@ class RemoteSyncService {}}- if (flagService.enableOnlyBackupFuturePhotos) {- localIDsToSync = await _filterLocalIDsBasedOnOnlyNew(localIDsToSync);+ // Filter by only-new using pre-fetched set+ if (newerLocalIDs != null) {+ localIDsToSync.retainAll(newerLocalIDs);}if (localIDsToSync.isEmpty) {@@ -621,18 +633,6 @@ class RemoteSyncService {}).toList();}- Future<Set<String>> _filterLocalIDsBasedOnOnlyNew(- Set<String> localIDs,- ) async {- final int? onlyNewSince = localSettings.onlyNewSinceEpoch;- if (!flagService.enableOnlyBackupFuturePhotos ||- onlyNewSince == null ||- localIDs.isEmpty) {- return localIDs;- }- return _db.getLocalIDsNewerThan(localIDs, onlyNewSince);- }-Future<bool> _uploadFiles(List<EnteFile> filesToBeUploaded) async {final int ownerID = _config.getUserID()!;final updatedFileIDs = await _db.getUploadedFileIDsToBeUpdated(ownerID);