From 90c0ad08da222f0f9c9820360dc74a4a21e23db1 Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Sat, 18 May 2024 15:22:41 +0530 Subject: [PATCH] [mob][photos] Remove jank --- mobile/lib/db/files_db.dart | 11 ++++++ .../face_ml/face_ml_service.dart | 34 +++++++++---------- mobile/lib/services/search_service.dart | 15 ++++++++ 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/mobile/lib/db/files_db.dart b/mobile/lib/db/files_db.dart index abce8de6c..f72ecb32a 100644 --- a/mobile/lib/db/files_db.dart +++ b/mobile/lib/db/files_db.dart @@ -695,6 +695,17 @@ class FilesDB { return files; } + Future> getAllFilesFromCollections( + Iterable collectionID, + ) async { + final db = await instance.sqliteAsyncDB; + final String sql = + 'SELECT * FROM $filesTable WHERE $columnCollectionID IN (${collectionID.join(',')})'; + final results = await db.getAll(sql); + final files = convertToFiles(results); + return files; + } + Future> getNewFilesInCollection( int collectionID, int addedTime, diff --git a/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart b/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart index 067c8700c..49333fe7e 100644 --- a/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart +++ b/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart @@ -574,11 +574,14 @@ class FaceMlService { isImageIndexRunning = true; _logger.info('starting image indexing'); + final w = (kDebugMode ? EnteWatch('prepare indexing files') : null) + ?..start(); final Map alreadyIndexedFiles = await FaceMLDataDB.instance.getIndexedFileIds(); - // w?.log('getIndexedFileIds'); + w?.log('getIndexedFileIds'); final List enteFiles = await SearchService.instance.getAllFiles(); + w?.log('getAllFiles'); // Make sure the image conversion isolate is spawned // await ImageMlIsolate.instance.ensureSpawned(); @@ -589,11 +592,10 @@ class FaceMlService { final stopwatch = Stopwatch()..start(); final List filesWithLocalID = []; final List filesWithoutLocalID = []; - final List hiddenFiles = []; - final List allFileIDsToBeIndexed = await getIndexableFileIDs(); + final List hiddenFilesToIndex = []; + w?.log('getIndexableFileIDs'); for (final EnteFile enteFile in enteFiles) { - allFileIDsToBeIndexed.remove(enteFile.uploadedFileID); if (_skipAnalysisEnteFile(enteFile, alreadyIndexedFiles)) { fileSkippedCount++; continue; @@ -604,26 +606,24 @@ class FaceMlService { filesWithLocalID.add(enteFile); } } - if (allFileIDsToBeIndexed.isNotEmpty) { - _logger.info( - 'Found ${allFileIDsToBeIndexed.length} files outside searchable files', - ); - final List uploadFiles = - await FilesDB.instance.getUploadedFiles(allFileIDsToBeIndexed); - for (final EnteFile enteFile in uploadFiles) { - if (_skipAnalysisEnteFile(enteFile, alreadyIndexedFiles)) { - fileSkippedCount++; - continue; - } - hiddenFiles.add(enteFile); + w?.log('sifting through all normal files'); + final List hiddenFiles = + await SearchService.instance.getHiddenFiles(); + w?.log('getHiddenFiles: ${hiddenFiles.length} hidden files'); + for (final EnteFile enteFile in hiddenFiles) { + if (_skipAnalysisEnteFile(enteFile, alreadyIndexedFiles)) { + fileSkippedCount++; + continue; } + hiddenFilesToIndex.add(enteFile); } // list of files where files with localID are first final sortedBylocalID = []; sortedBylocalID.addAll(filesWithLocalID); sortedBylocalID.addAll(filesWithoutLocalID); - sortedBylocalID.addAll(hiddenFiles); + sortedBylocalID.addAll(hiddenFilesToIndex); + w?.log('preparing all files to index'); final List> chunks = sortedBylocalID.chunks(_embeddingFetchLimit); outerLoop: diff --git a/mobile/lib/services/search_service.dart b/mobile/lib/services/search_service.dart index c9c4ddf1d..5e21b0334 100644 --- a/mobile/lib/services/search_service.dart +++ b/mobile/lib/services/search_service.dart @@ -42,6 +42,7 @@ import 'package:tuple/tuple.dart'; class SearchService { Future>? _cachedFilesFuture; + Future>? _cachedHiddenFilesFuture; final _logger = Logger((SearchService).toString()); final _collectionService = CollectionsService.instance; static const _maximumResultsLimit = 20; @@ -54,6 +55,7 @@ class SearchService { Bus.instance.on().listen((event) { // only invalidate, let the load happen on demand _cachedFilesFuture = null; + _cachedHiddenFilesFuture = null; }); } @@ -73,8 +75,21 @@ class SearchService { return _cachedFilesFuture!; } + Future> getHiddenFiles() async { + if (_cachedHiddenFilesFuture != null) { + return _cachedHiddenFilesFuture!; + } + _logger.fine("Reading hidden files from db"); + final hiddenCollections = + CollectionsService.instance.getHiddenCollectionIds(); + _cachedHiddenFilesFuture = + FilesDB.instance.getAllFilesFromCollections(hiddenCollections); + return _cachedHiddenFilesFuture!; + } + void clearCache() { _cachedFilesFuture = null; + _cachedHiddenFilesFuture = null; } // getFilteredCollectionsWithThumbnail removes deleted or archived or