Merge pull request #760 from ente-io/batch_collection_insert
This commit is contained in:
commit
41fb304029
2 changed files with 21 additions and 10 deletions
|
@ -156,17 +156,24 @@ class CollectionsDB {
|
|||
];
|
||||
}
|
||||
|
||||
Future<List<dynamic>> insert(List<Collection> collections) async {
|
||||
Future<void> insert(List<Collection> 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,
|
||||
);
|
||||
batchCounter++;
|
||||
}
|
||||
return await batch.commit();
|
||||
await batch.commit(noResult: true);
|
||||
}
|
||||
|
||||
Future<List<Collection>> getAllCollections() async {
|
||||
|
|
|
@ -107,20 +107,15 @@ class CollectionsService {
|
|||
final updatedCollections = <Collection>[];
|
||||
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<File>.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<File>.empty(),
|
||||
source: "syncCollectionDeleted",
|
||||
),
|
||||
);
|
||||
}
|
||||
await _updateDB(updatedCollections);
|
||||
_prefs.setInt(_collectionsSyncTimeKey, maxUpdationTime);
|
||||
watch.logAndReset("till DB insertion");
|
||||
|
@ -1137,6 +1140,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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue