[mob][photos] Migrate to sqlite_async (1)

This commit is contained in:
ashilkn 2024-05-13 15:39:12 +05:30
parent 50613b8c57
commit 5bd845d32b

View file

@ -778,15 +778,13 @@ class FilesDB {
// Files which user added to a collection manually but they are not // Files which user added to a collection manually but they are not
// uploaded yet or files belonging to a collection which is marked for backup // uploaded yet or files belonging to a collection which is marked for backup
Future<List<EnteFile>> getFilesPendingForUpload() async { Future<List<EnteFile>> getFilesPendingForUpload() async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final results = await db.query( final results = await db.getAll(
filesTable, 'SELECT * FROM $filesTable WHERE ($columnUploadedFileID IS NULL OR '
where: '$columnUploadedFileID IS -1) AND $columnCollectionID IS NOT NULL AND '
'($columnUploadedFileID IS NULL OR $columnUploadedFileID IS -1) AND ' '$columnCollectionID IS NOT -1 AND $columnLocalID IS NOT NULL AND '
'$columnCollectionID IS NOT NULL AND $columnCollectionID IS NOT -1 AND ' '$columnLocalID IS NOT -1 GROUP BY $columnLocalID '
'$columnLocalID IS NOT NULL AND $columnLocalID IS NOT -1', 'ORDER BY $columnCreationTime DESC',
orderBy: '$columnCreationTime DESC',
groupBy: columnLocalID,
); );
final files = convertToFiles(results); final files = convertToFiles(results);
// future-safe filter just to ensure that the query doesn't end up returning files // future-safe filter just to ensure that the query doesn't end up returning files
@ -801,30 +799,22 @@ class FilesDB {
} }
Future<List<EnteFile>> getUnUploadedLocalFiles() async { Future<List<EnteFile>> getUnUploadedLocalFiles() async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final results = await db.query( final results = await db.getAll(
filesTable, 'SELECT * FROM $filesTable WHERE ($columnUploadedFileID IS NULL OR '
where: '$columnUploadedFileID IS -1) AND $columnLocalID IS NOT NULL '
'($columnUploadedFileID IS NULL OR $columnUploadedFileID IS -1) AND $columnLocalID IS NOT NULL', 'GROUP BY $columnLocalID ORDER BY $columnCreationTime DESC',
orderBy: '$columnCreationTime DESC',
groupBy: columnLocalID,
); );
return convertToFiles(results); return convertToFiles(results);
} }
Future<List<int>> getUploadedFileIDsToBeUpdated(int ownerID) async { Future<List<int>> getUploadedFileIDsToBeUpdated(int ownerID) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final rows = await db.query( final rows = await db.getAll(
filesTable, 'SELECT DISTINCT $columnUploadedFileID FROM $filesTable WHERE '
columns: [columnUploadedFileID], '($columnLocalID IS NOT NULL AND $columnOwnerID = ? AND '
where: '($columnLocalID IS NOT NULL AND $columnOwnerID = ? AND ' '($columnUploadedFileID IS NOT NULL AND $columnUploadedFileID IS NOT -1) '
'($columnUploadedFileID ' 'AND $columnUpdationTime IS NULL) ORDER BY $columnCreationTime DESC ');
'IS NOT '
'NULL AND $columnUploadedFileID IS NOT -1) AND $columnUpdationTime IS NULL)',
whereArgs: [ownerID],
orderBy: '$columnCreationTime DESC',
distinct: true,
);
final uploadedFileIDs = <int>[]; final uploadedFileIDs = <int>[];
for (final row in rows) { for (final row in rows) {
uploadedFileIDs.add(row[columnUploadedFileID] as int); uploadedFileIDs.add(row[columnUploadedFileID] as int);
@ -836,15 +826,11 @@ class FilesDB {
int uploadedFileID, int uploadedFileID,
int userID, int userID,
) async { ) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final results = await db.query( final results = await db.getAll(
filesTable, 'SELECT * FROM $filesTable WHERE $columnLocalID IS NOT NULL AND '
where: '$columnLocalID IS NOT NULL AND $columnOwnerID = ? AND ' '$columnOwnerID = ? AND $columnUploadedFileID = ?',
'$columnUploadedFileID = ?', [userID, uploadedFileID],
whereArgs: [
userID,
uploadedFileID,
],
); );
if (results.isEmpty) { if (results.isEmpty) {
return <EnteFile>[]; return <EnteFile>[];
@ -853,14 +839,12 @@ class FilesDB {
} }
Future<Set<String>> getExistingLocalFileIDs(int ownerID) async { Future<Set<String>> getExistingLocalFileIDs(int ownerID) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final rows = await db.query( final rows = await db.getAll(
filesTable, 'SELECT DISTINCT $columnLocalID FROM $filesTable '
columns: [columnLocalID], 'WHERE $columnLocalID IS NOT NULL AND ($columnOwnerID IS NULL OR '
distinct: true,
where: '$columnLocalID IS NOT NULL AND ($columnOwnerID IS NULL OR '
'$columnOwnerID = ?)', '$columnOwnerID = ?)',
whereArgs: [ownerID], [ownerID],
); );
final result = <String>{}; final result = <String>{};
for (final row in rows) { for (final row in rows) {
@ -870,16 +854,13 @@ class FilesDB {
} }
Future<Set<String>> getLocalIDsMarkedForOrAlreadyUploaded(int ownerID) async { Future<Set<String>> getLocalIDsMarkedForOrAlreadyUploaded(int ownerID) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final rows = await db.query( final rows = await db.getAll(
filesTable, 'SELECT DISTINCT $columnLocalID FROM $filesTable '
columns: [columnLocalID], 'WHERE $columnLocalID IS NOT NULL AND ($columnCollectionID IS NOT NULL '
distinct: true, 'AND $columnCollectionID != -1) AND ($columnOwnerID = ? OR '
where: '$columnLocalID IS NOT NULL AND ($columnCollectionID IS NOT NULL '
'AND '
'$columnCollectionID != -1) AND ($columnOwnerID = ? OR '
'$columnOwnerID IS NULL)', '$columnOwnerID IS NULL)',
whereArgs: [ownerID], [ownerID],
); );
final result = <String>{}; final result = <String>{};
for (final row in rows) { for (final row in rows) {
@ -889,12 +870,11 @@ class FilesDB {
} }
Future<Set<String>> getLocalFileIDsForCollection(int collectionID) async { Future<Set<String>> getLocalFileIDsForCollection(int collectionID) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final rows = await db.query( final rows = await db.getAll(
filesTable, 'SELECT $columnLocalID FROM $filesTable '
columns: [columnLocalID], 'WHERE $columnLocalID IS NOT NULL AND $columnCollectionID = ?',
where: '$columnLocalID IS NOT NULL AND $columnCollectionID = ?', [collectionID],
whereArgs: [collectionID],
); );
final result = <String>{}; final result = <String>{};
for (final row in rows) { for (final row in rows) {
@ -905,17 +885,17 @@ class FilesDB {
// Sets the collectionID for the files with given LocalIDs if the // Sets the collectionID for the files with given LocalIDs if the
// corresponding file entries are not already mapped to some other collection // corresponding file entries are not already mapped to some other collection
Future<int> setCollectionIDForUnMappedLocalFiles( Future<void> setCollectionIDForUnMappedLocalFiles(
int collectionID, int collectionID,
Set<String> localIDs, Set<String> localIDs,
) async { ) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
String inParam = ""; String inParam = "";
for (final localID in localIDs) { for (final localID in localIDs) {
inParam += "'" + localID + "',"; inParam += "'" + localID + "',";
} }
inParam = inParam.substring(0, inParam.length - 1); inParam = inParam.substring(0, inParam.length - 1);
return await db.rawUpdate( await db.execute(
''' '''
UPDATE $filesTable UPDATE $filesTable
SET $columnCollectionID = $collectionID SET $columnCollectionID = $collectionID
@ -925,7 +905,7 @@ class FilesDB {
); );
} }
Future<int> markFilesForReUpload( Future<void> markFilesForReUpload(
int ownerID, int ownerID,
String localID, String localID,
String? title, String? title,
@ -934,22 +914,32 @@ class FilesDB {
int modificationTime, int modificationTime,
FileType fileType, FileType fileType,
) async { ) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
return await db.update(
filesTable, await db.execute(
{ '''
columnTitle: title, UPDATE $filesTable
columnLatitude: location?.latitude, SET $columnTitle = ?,
columnLongitude: location?.longitude, $columnLatitude = ?,
columnCreationTime: creationTime, $columnLongitude = ?,
columnModificationTime: modificationTime, $columnCreationTime = ?,
// #hack reset updation time to null for re-upload $columnModificationTime = ?,
columnUpdationTime: null, $columnUpdationTime = NULL,
columnFileType: getInt(fileType), $columnFileType = ?
}, WHERE $columnLocalID = ? AND ($columnOwnerID = ? OR $columnOwnerID IS NULL);
where: ''',
'$columnLocalID = ? AND ($columnOwnerID = ? OR $columnOwnerID IS NULL)', [
whereArgs: [localID, ownerID], title,
location?.latitude,
location?.longitude,
localID,
creationTime,
modificationTime,
null,
getInt(fileType),
localID,
ownerID,
],
); );
} }
@ -964,12 +954,12 @@ class FilesDB {
required String title, required String title,
required String deviceFolder, required String deviceFolder,
}) async { }) async {
final db = await instance.database; final db = await instance.sqliteAsyncDB;
// on iOS, match using localID and fileType. title can either match or // on iOS, match using localID and fileType. title can either match or
// might be null based on how the file was imported // might be null based on how the file was imported
String whereClause = ''' ($columnOwnerID = ? OR $columnOwnerID IS NULL) AND String query = '''SELECT * FROM $filesTable WHERE ($columnOwnerID = ?
$columnLocalID = ? AND $columnFileType = ? AND OR $columnOwnerID IS NULL) AND $columnLocalID = ?
($columnTitle=? OR $columnTitle IS NULL) '''; AND $columnFileType = ? AND ($columnTitle=? OR $columnTitle IS NULL) ''';
List<Object> whereArgs = [ List<Object> whereArgs = [
ownerID, ownerID,
localID, localID,
@ -977,9 +967,9 @@ class FilesDB {
title, title,
]; ];
if (Platform.isAndroid) { if (Platform.isAndroid) {
whereClause = ''' ($columnOwnerID = ? OR $columnOwnerID IS NULL) AND query = '''SELECT * FROM $filesTable WHERE ($columnOwnerID = ? OR
$columnLocalID = ? AND $columnFileType = ? AND $columnTitle=? AND $columnDeviceFolder= ? $columnOwnerID IS NULL) AND $columnLocalID = ? AND $columnFileType = ?
'''; AND $columnTitle=? AND $columnDeviceFolder= ? ''';
whereArgs = [ whereArgs = [
ownerID, ownerID,
localID, localID,
@ -989,10 +979,9 @@ class FilesDB {
]; ];
} }
final rows = await db.query( final rows = await db.getAll(
filesTable, query,
where: whereClause, whereArgs,
whereArgs: whereArgs,
); );
return convertToFiles(rows); return convertToFiles(rows);
@ -1030,14 +1019,12 @@ class FilesDB {
if (fileType == FileType.livePhoto && hashData.zipHash != null) { if (fileType == FileType.livePhoto && hashData.zipHash != null) {
inParam += ",'${hashData.zipHash}'"; inParam += ",'${hashData.zipHash}'";
} }
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final rows = await db.query( final rows = await db.getAll(
filesTable, 'SELECT * FROM $filesTable WHERE $columnUploadedFileID != NULL OR '
where: '($columnUploadedFileID != NULL OR $columnUploadedFileID != -1) ' '$columnUploadedFileID != -1) AND $columnOwnerID = ? AND '
'AND $columnOwnerID = ? AND $columnFileType =' '$columnFileType = ? AND $columnHash IN ($inParam)',
' ? ' [
'AND $columnHash IN ($inParam)',
whereArgs: [
ownerID, ownerID,
getInt(fileType), getInt(fileType),
], ],
@ -1371,10 +1358,9 @@ class FilesDB {
inParam += "'" + id.toString() + "',"; inParam += "'" + id.toString() + "',";
} }
inParam = inParam.substring(0, inParam.length - 1); inParam = inParam.substring(0, inParam.length - 1);
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final results = await db.query( final results = await db.getAll(
filesTable, 'SELECT * FROM $filesTable WHERE $columnUploadedFileID IN ($inParam)',
where: '$columnUploadedFileID IN ($inParam)',
); );
final files = convertToFiles(results); final files = convertToFiles(results);
for (final file in files) { for (final file in files) {
@ -1393,10 +1379,9 @@ class FilesDB {
inParam += "'" + id.toString() + "',"; inParam += "'" + id.toString() + "',";
} }
inParam = inParam.substring(0, inParam.length - 1); inParam = inParam.substring(0, inParam.length - 1);
final db = await instance.database; final db = await instance.sqliteAsyncDB;
final results = await db.query( final results = await db.getAll(
filesTable, 'SELECT * FROM $filesTable WHERE $columnGeneratedID IN ($inParam)',
where: '$columnGeneratedID IN ($inParam)',
); );
final files = convertToFiles(results); final files = convertToFiles(results);
for (final file in files) { for (final file in files) {