Merge branch 'perf_minor' into migratie_collections
This commit is contained in:
commit
b98e9c8aec
|
@ -188,6 +188,24 @@ class CollectionsDB {
|
||||||
return collections;
|
return collections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getActiveCollectionIDsAndUpdationTime returns map of collectionID to
|
||||||
|
// updationTime for non-deleted collections
|
||||||
|
Future<Map<int, int>> getActiveIDsAndRemoteUpdateTime() async {
|
||||||
|
final db = await instance.database;
|
||||||
|
final rows = await db.query(
|
||||||
|
table,
|
||||||
|
where: '($columnIsDeleted = ? OR $columnIsDeleted IS NULL)',
|
||||||
|
whereArgs: [_sqlBoolFalse],
|
||||||
|
columns: [columnID, columnUpdationTime],
|
||||||
|
);
|
||||||
|
final collectionIDsAndUpdationTime = <int, int>{};
|
||||||
|
for (final row in rows) {
|
||||||
|
collectionIDsAndUpdationTime[row[columnID] as int] =
|
||||||
|
int.parse(row[columnUpdationTime] as String);
|
||||||
|
}
|
||||||
|
return collectionIDsAndUpdationTime;
|
||||||
|
}
|
||||||
|
|
||||||
Future<int> deleteCollection(int collectionID) async {
|
Future<int> deleteCollection(int collectionID) async {
|
||||||
final db = await instance.database;
|
final db = await instance.database;
|
||||||
return db.delete(
|
return db.delete(
|
||||||
|
|
|
@ -167,15 +167,18 @@ class CollectionsService {
|
||||||
_cachedKeys.clear();
|
_cachedKeys.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Collection>> getCollectionsToBeSynced() async {
|
Future<Map<int, int>> getCollectionIDsToBeSynced() async {
|
||||||
final collections = await _db.getAllCollections();
|
final idsToRemoveUpdateTimeMap =
|
||||||
final updatedCollections = <Collection>[];
|
await _db.getActiveIDsAndRemoteUpdateTime();
|
||||||
for (final c in collections) {
|
final result = <int, int>{};
|
||||||
if (c.updationTime > getCollectionSyncTime(c.id) && !c.isDeleted) {
|
for (final MapEntry<int, int> e in idsToRemoveUpdateTimeMap.entries) {
|
||||||
updatedCollections.add(c);
|
final int cid = e.key;
|
||||||
|
final int remoteUpdateTime = e.value;
|
||||||
|
if (remoteUpdateTime > getCollectionSyncTime(cid)) {
|
||||||
|
result[cid] = remoteUpdateTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return updatedCollections;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<int> getArchivedCollections() {
|
Set<int> getArchivedCollections() {
|
||||||
|
|
|
@ -181,14 +181,19 @@ class RemoteSyncService {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _syncUpdatedCollections() async {
|
Future<void> _syncUpdatedCollections() async {
|
||||||
final updatedCollections =
|
final idsToRemoteUpdationTimeMap =
|
||||||
await _collectionsService.getCollectionsToBeSynced();
|
await _collectionsService.getCollectionIDsToBeSynced();
|
||||||
for (final c in updatedCollections) {
|
for (final cid in idsToRemoteUpdationTimeMap.keys) {
|
||||||
await _syncCollectionDiff(
|
await _syncCollectionDiff(
|
||||||
c.id,
|
cid,
|
||||||
_collectionsService.getCollectionSyncTime(c.id),
|
_collectionsService.getCollectionSyncTime(cid),
|
||||||
);
|
);
|
||||||
await _collectionsService.setCollectionSyncTime(c.id, c.updationTime);
|
// update syncTime for the collection in sharedPrefs. Note: the
|
||||||
|
// syncTime can change on remote but we might not get a diff for the
|
||||||
|
// collection if there are not changes in the file, but the collection
|
||||||
|
// metadata (name, archive status, sharing etc) has changed.
|
||||||
|
final remoteUpdateTime = idsToRemoteUpdationTimeMap[cid];
|
||||||
|
await _collectionsService.setCollectionSyncTime(cid, remoteUpdateTime);
|
||||||
}
|
}
|
||||||
_logger.info("All updated collections synced");
|
_logger.info("All updated collections synced");
|
||||||
}
|
}
|
||||||
|
@ -216,24 +221,7 @@ class RemoteSyncService {
|
||||||
final diff =
|
final diff =
|
||||||
await _diffFetcher.getEncryptedFilesDiff(collectionID, sinceTime);
|
await _diffFetcher.getEncryptedFilesDiff(collectionID, sinceTime);
|
||||||
if (diff.deletedFiles.isNotEmpty) {
|
if (diff.deletedFiles.isNotEmpty) {
|
||||||
final fileIDs = diff.deletedFiles.map((f) => f.uploadedFileID!).toList();
|
await _syncCollectionDiffDelete(diff, collectionID);
|
||||||
final deletedFiles = (await _db.getFilesFromIDs(fileIDs)).values.toList();
|
|
||||||
await _db.deleteFilesFromCollection(collectionID, fileIDs);
|
|
||||||
Bus.instance.fire(
|
|
||||||
CollectionUpdatedEvent(
|
|
||||||
collectionID,
|
|
||||||
deletedFiles,
|
|
||||||
"syncDeleteFromRemote",
|
|
||||||
type: EventType.deletedFromRemote,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
Bus.instance.fire(
|
|
||||||
LocalPhotosUpdatedEvent(
|
|
||||||
deletedFiles,
|
|
||||||
type: EventType.deletedFromRemote,
|
|
||||||
source: "syncDeleteFromRemote",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (diff.updatedFiles.isNotEmpty) {
|
if (diff.updatedFiles.isNotEmpty) {
|
||||||
await _storeDiff(diff.updatedFiles, collectionID);
|
await _storeDiff(diff.updatedFiles, collectionID);
|
||||||
|
@ -274,6 +262,32 @@ class RemoteSyncService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _syncCollectionDiffDelete(Diff diff, int collectionID) async {
|
||||||
|
final fileIDs = diff.deletedFiles.map((f) => f.uploadedFileID!).toList();
|
||||||
|
final localDeleteCount =
|
||||||
|
await _db.deleteFilesFromCollection(collectionID, fileIDs);
|
||||||
|
if (localDeleteCount > 0) {
|
||||||
|
final collectionFiles =
|
||||||
|
(await _db.getFilesFromIDs(fileIDs)).values.toList();
|
||||||
|
collectionFiles.removeWhere((f) => f.collectionID != collectionID);
|
||||||
|
Bus.instance.fire(
|
||||||
|
CollectionUpdatedEvent(
|
||||||
|
collectionID,
|
||||||
|
collectionFiles,
|
||||||
|
"syncDeleteFromRemote",
|
||||||
|
type: EventType.deletedFromRemote,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Bus.instance.fire(
|
||||||
|
LocalPhotosUpdatedEvent(
|
||||||
|
collectionFiles,
|
||||||
|
type: EventType.deletedFromRemote,
|
||||||
|
source: "syncDeleteFromRemote",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> syncDeviceCollectionFilesForUpload() async {
|
Future<void> syncDeviceCollectionFilesForUpload() async {
|
||||||
_logger.info("Syncing device collections to be uploaded");
|
_logger.info("Syncing device collections to be uploaded");
|
||||||
final int ownerID = _config.getUserID()!;
|
final int ownerID = _config.getUserID()!;
|
||||||
|
|
Loading…
Reference in a new issue