From 1ec941b0224d6e4459a1785e2a74862884baf50f Mon Sep 17 00:00:00 2001 From: ashilkn Date: Tue, 9 Apr 2024 16:12:37 +0530 Subject: [PATCH] Migrate getAllPendingOrUploadedFiles from sqflite to sqlite_async --- mobile/lib/db/files_db.dart | 55 ++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/mobile/lib/db/files_db.dart b/mobile/lib/db/files_db.dart index 5ffd710e5..dd941a979 100644 --- a/mobile/lib/db/files_db.dart +++ b/mobile/lib/db/files_db.dart @@ -573,31 +573,43 @@ class FilesDB { bool applyOwnerCheck = false, }) async { final stopWatch = EnteWatch('getAllPendingOrUploadedFiles')..start(); - late String whereQuery; - late List? whereArgs; + final order = (asc ?? false ? 'ASC' : 'DESC'); + + late String query; + late List? args; if (applyOwnerCheck) { - whereQuery = '$columnCreationTime >= ? AND $columnCreationTime <= ? ' + query = + 'SELECT * FROM $filesTable WHERE $columnCreationTime >= ? AND $columnCreationTime <= ? ' 'AND ($columnOwnerID IS NULL OR $columnOwnerID = ?) ' 'AND ($columnCollectionID IS NOT NULL AND $columnCollectionID IS NOT -1)' - ' AND $columnMMdVisibility = ?'; - whereArgs = [startTime, endTime, ownerID, visibility]; + ' AND $columnMMdVisibility = ? ORDER BY $columnCreationTime $order, $columnModificationTime $order'; + + args = [startTime, endTime, ownerID, visibility]; } else { - whereQuery = - '$columnCreationTime >= ? AND $columnCreationTime <= ? AND ($columnCollectionID IS NOT NULL AND $columnCollectionID IS NOT -1)' - ' AND $columnMMdVisibility = ?'; - whereArgs = [startTime, endTime, visibility]; + query = + 'SELECT * FROM $filesTable WHERE $columnCreationTime >= ? AND $columnCreationTime <= ? ' + 'AND ($columnCollectionID IS NOT NULL AND $columnCollectionID IS NOT -1)' + ' AND $columnMMdVisibility = ? ORDER BY $columnCreationTime $order, $columnModificationTime $order'; + args = [startTime, endTime, visibility]; } - final db = await instance.database; - final order = (asc ?? false ? 'ASC' : 'DESC'); - final results = await db.query( - filesTable, - where: whereQuery, - whereArgs: whereArgs, - orderBy: - '$columnCreationTime ' + order + ', $columnModificationTime ' + order, - limit: limit, + if (limit != null) { + query += ' LIMIT ?'; + args.add(limit); + } + + _logger.info( + "--------------------------- getAllPendingOrUploadedFilesSqliteAsync with limit: $limit", ); + final stopwatch = Stopwatch()..start(); + + final db = await instance.sqliteAsyncDB; + final results = await db.getAll(query, args); + _logger.info( + "------------------------getAllPendingOrUploadedFilesSqliteAsync query took ${stopwatch.elapsedMilliseconds} ms", + ); + stopwatch.stop(); + _logger.info("message"); stopWatch.log('queryDone'); final files = convertToFiles(results); stopWatch.log('convertDone'); @@ -1600,8 +1612,9 @@ class FilesDB { Set collectionsToIgnore, { bool dedupeByUploadId = true, }) async { + final stopwatch = Stopwatch()..start(); + _logger.info("-------------------- getAllFilesFromDB"); final db = await instance.sqliteAsyncDB; - final result = await db.getAll( 'SELECT * FROM $filesTable ORDER BY $columnCreationTime DESC', ); @@ -1615,6 +1628,10 @@ class FilesDB { dedupeUploadID: dedupeByUploadId, ), ); + _logger.info( + "---------------------------- getAllFilesFromDB took ${stopwatch.elapsedMilliseconds} ms", + ); + stopwatch.stop(); return deduplicatedFiles; }