From c400ba62f4df5127376587eb6120357c7b41eb8c Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 2 Jan 2023 14:18:52 +0530 Subject: [PATCH 1/3] Batch insert collection updates --- lib/db/collections_db.dart | 12 +++++++++--- lib/services/collections_service.dart | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/db/collections_db.dart b/lib/db/collections_db.dart index 9fc9c2000..6e7894a61 100644 --- a/lib/db/collections_db.dart +++ b/lib/db/collections_db.dart @@ -156,17 +156,23 @@ class CollectionsDB { ]; } - Future> insert(List collections) async { + Future insert(List collections) async { final db = await instance.database; - final batch = db.batch(); + var batch = db.batch(); + int batchCounter = 0; for (final collection in collections) { + if (batchCounter == 400) { + await batch.commit(noResult: true); + batch = db.batch(); + batchCounter = 0; + } batch.insert( table, _getRowForCollection(collection), conflictAlgorithm: ConflictAlgorithm.replace, ); } - return await batch.commit(); + await batch.commit(noResult: true); } Future> getAllCollections() async { diff --git a/lib/services/collections_service.dart b/lib/services/collections_service.dart index bf384fc33..fa2c99c02 100644 --- a/lib/services/collections_service.dart +++ b/lib/services/collections_service.dart @@ -1137,6 +1137,7 @@ class CollectionsService { try { await _db.insert(collections); } catch (e) { + _logger.severe("Failed to update collections", e); if (attempt < kMaximumWriteAttempts) { return _updateDB(collections, attempt: ++attempt); } else { From 280043bf1b08214aabd724902efe2daecf4267bc Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 2 Jan 2023 14:21:25 +0530 Subject: [PATCH 2/3] Fire single event for deleted collections --- lib/services/collections_service.dart | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/services/collections_service.dart b/lib/services/collections_service.dart index fa2c99c02..24527124e 100644 --- a/lib/services/collections_service.dart +++ b/lib/services/collections_service.dart @@ -107,20 +107,15 @@ class CollectionsService { final updatedCollections = []; int maxUpdationTime = lastCollectionUpdationTime; final ownerID = _config.getUserID(); + bool fireEventForCollectionDeleted = false; for (final collection in fetchedCollections) { if (collection.isDeleted) { await _filesDB.deleteCollection(collection.id); await setCollectionSyncTime(collection.id, null); if (_collectionIDToCollections.containsKey(collection.id)) { - Bus.instance.fire( - LocalPhotosUpdatedEvent( - List.empty(), - source: "syncCollectionDeleted", - ), - ); + fireEventForCollectionDeleted = true; } } - // remove reference for incoming collections when unshared/deleted if (collection.isDeleted && ownerID != collection?.owner?.id) { await _db.deleteCollection(collection.id); @@ -133,6 +128,14 @@ class CollectionsService { ? collection.updationTime : maxUpdationTime; } + if (fireEventForCollectionDeleted) { + Bus.instance.fire( + LocalPhotosUpdatedEvent( + List.empty(), + source: "syncCollectionDeleted", + ), + ); + } await _updateDB(updatedCollections); _prefs.setInt(_collectionsSyncTimeKey, maxUpdationTime); watch.logAndReset("till DB insertion"); From bc14eb4e78252348728c3dc9763611fac8e770fb Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 2 Jan 2023 14:27:55 +0530 Subject: [PATCH 3/3] Fix bug: increment batch counter --- lib/db/collections_db.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/db/collections_db.dart b/lib/db/collections_db.dart index 6e7894a61..f69820246 100644 --- a/lib/db/collections_db.dart +++ b/lib/db/collections_db.dart @@ -171,6 +171,7 @@ class CollectionsDB { _getRowForCollection(collection), conflictAlgorithm: ConflictAlgorithm.replace, ); + batchCounter++; } await batch.commit(noResult: true); }