Update logic to deduplicate existing database entries

This commit is contained in:
Vishnu Mohandas 2020-10-29 05:44:48 +05:30
parent 7e5426088f
commit 0afbd87898
2 changed files with 34 additions and 21 deletions

View file

@ -206,17 +206,16 @@ class FilesDB {
return _convertToFiles(results);
}
Future<File> getMatchingFile(String localID, String title,
String deviceFolder, int creationTime, int modificationTime,
Future<List<File>> getMatchingFiles(
String title, String deviceFolder, int creationTime, int modificationTime,
{String alternateTitle}) async {
final db = await instance.database;
final rows = await db.query(
table,
where: '''$columnLocalID=? AND ($columnTitle=? OR $columnTitle=?) AND
where: '''($columnTitle=? OR $columnTitle=?) AND
$columnDeviceFolder=? AND $columnCreationTime=? AND
$columnModificationTime=?''',
whereArgs: [
localID,
title,
alternateTitle,
deviceFolder,
@ -225,9 +224,9 @@ class FilesDB {
],
);
if (rows.isNotEmpty) {
return _getFileFromRow(rows[0]);
return _convertToFiles(rows);
} else {
throw ("No matching file found");
return null;
}
}
@ -255,7 +254,6 @@ class FilesDB {
);
}
// TODO: Remove deleted files on remote
Future<int> markForDeletion(File file) async {
final db = await instance.database;
final values = new Map<String, dynamic>();

View file

@ -242,23 +242,38 @@ class SyncService {
Future _storeDiff(List<File> diff, int collectionID) async {
for (File file in diff) {
try {
final existingFile = await _db.getMatchingFile(file.localID, file.title,
file.deviceFolder, file.creationTime, file.modificationTime,
alternateTitle: getHEICFileNameForJPG(file));
file.localID = existingFile.localID;
if (existingFile.collectionID == null ||
existingFile.collectionID == file.collectionID) {
// Uploaded for the first time || updated within the same collection
file.generatedID = existingFile.generatedID;
final existingFiles = await _db.getMatchingFiles(file.title,
file.deviceFolder, file.creationTime, file.modificationTime,
alternateTitle: getHEICFileNameForJPG(file));
if (existingFiles == null) {
// File uploaded from a different device
file.localID = null;
await _db.insert(file);
} else {
// File exists on device
bool wasUploadedOnAPreviousInstallation =
existingFiles.length == 1 && existingFiles[0].collectionID == null;
file.localID = existingFiles[0]
.localID; // File should ideally have the same localID
if (wasUploadedOnAPreviousInstallation) {
file.generatedID = existingFiles[0].generatedID;
await _db.update(file);
} else {
// If an existing file was added to a collection
await _db.insert(file);
bool wasUpdatedInExistingCollection = false;
for (final existingFile in existingFiles) {
if (file.collectionID == existingFile.collectionID) {
file.generatedID = existingFile.generatedID;
wasUpdatedInExistingCollection = true;
break;
}
}
if (wasUpdatedInExistingCollection) {
await _db.update(file);
} else {
// Added to a new collection
await _db.insert(file);
}
}
} catch (e) {
file.localID = null; // File uploaded from a different device
await _db.insert(file);
}
await _setCollectionSyncTime(collectionID, file.updationTime);
}