Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Reuse cached files for date search

Neeraj Guptada557d186120
diff --git a/mobile/apps/photos/lib/db/files_db.dart b/mobile/apps/photos/lib/db/files_db.dart
index 3a3a80ab1f..75d50a8244 100644
--- a/mobile/apps/photos/lib/db/files_db.dart
+++ b/mobile/apps/photos/lib/db/files_db.dart
@@ -944,41 +944,6 @@ class FilesDB with SqlDbBase {
return FileLoadResult(files, files.length == limit);
}
- Future<List<EnteFile>> getFilesCreatedWithinDurations(
- List<List<int>> durations,
- Set<int> ignoredCollectionIDs, {
- int? visibility,
- String order = 'ASC',
- bool dedupeUploadID = true,
- }) async {
- if (durations.isEmpty) {
- return <EnteFile>[];
- }
- final db = await instance.sqliteAsyncDB;
- String whereClause = durations
- .map(
- (duration) =>
- "($columnCreationTime >= ${duration[0]} AND $columnCreationTime < ${duration[1]})",
- )
- .join(" OR ");
-
- whereClause = "( $whereClause )";
- if (visibility != null) {
- whereClause += ' AND $columnMMdVisibility = $visibility';
- }
- final query =
- 'SELECT * FROM $filesTable WHERE $whereClause ORDER BY $columnCreationTime $order';
- final results = await db.getAll(query);
- final files = convertToFiles(results);
- return applyDBFilters(
- files,
- DBFilterOptions(
- ignoredCollectionIDs: ignoredCollectionIDs,
- dedupeUploadID: dedupeUploadID,
- ),
- );
- }
-
Future<List<EnteFile>> getFilesPendingForUpload() async {
final db = await instance.sqliteAsyncDB;
final results = await db.getAll(
diff --git a/mobile/apps/photos/lib/services/search_service.dart b/mobile/apps/photos/lib/services/search_service.dart
index a87efa011a..23acb77179 100644
--- a/mobile/apps/photos/lib/services/search_service.dart
+++ b/mobile/apps/photos/lib/services/search_service.dart
@@ -301,6 +301,37 @@ class SearchService {
return _cachedFilesForSearch!;
}
+ Future<List<EnteFile>> _getFilesCreatedWithinDurations(
+ List<List<int>> durations,
+ ) async {
+ if (durations.isEmpty) {
+ return [];
+ }
+
+ final files = await getAllFilesForSearch();
+ final matchedFiles = <EnteFile>[];
+ for (final duration in durations.reversed) {
+ final firstIndex = _firstFileCreatedBefore(files, duration[1]);
+ final endIndex = _firstFileCreatedBefore(files, duration[0]);
+ matchedFiles.addAll(files.getRange(firstIndex, endIndex));
+ }
+ return matchedFiles;
+ }
+
+ int _firstFileCreatedBefore(List<EnteFile> files, int timestamp) {
+ var low = 0;
+ var high = files.length;
+ while (low < high) {
+ final middle = low + ((high - low) >> 1);
+ if (files[middle].creationTime! >= timestamp) {
+ low = middle + 1;
+ } else {
+ high = middle;
+ }
+ }
+ return low;
+ }
+
Future<bool> hasAnyFilesForSearch() async {
if (_cachedFilesFuture != null && _cachedFilesForSearch != null) {
return (await _cachedFilesForSearch!).isNotEmpty;
@@ -549,9 +580,9 @@ class SearchService {
final List<GenericSearchResult> searchResults = [];
for (var yearData in YearsData.instance.yearsData) {
if (yearData.year.startsWith(yearFromQuery)) {
- final List<EnteFile> filesInYear = await _getFilesInYear(
+ final filesInYear = await _getFilesCreatedWithinDurations([
yearData.duration,
- );
+ ]);
if (filesInYear.isNotEmpty) {
searchResults.add(
GenericSearchResult(
@@ -596,12 +627,9 @@ class SearchService {
final List<GenericSearchResult> searchResults = [];
final matchingMonths = _getMatchingMonths(context, query).toList();
for (final month in matchingMonths) {
- final matchedFiles = await FilesDB.instance
- .getFilesCreatedWithinDurations(
- _getDurationsOfMonthInEveryYear(month.monthNumber),
- ignoreCollections(),
- order: 'DESC',
- );
+ final matchedFiles = await _getFilesCreatedWithinDurations(
+ _getDurationsOfMonthInEveryYear(month.monthNumber),
+ );
if (matchedFiles.isNotEmpty) {
searchResults.add(
GenericSearchResult(
@@ -634,15 +662,9 @@ class SearchService {
for (var holiday in holidays) {
if (holiday.name.toLowerCase().contains(query.toLowerCase())) {
- final matchedFiles = await FilesDB.instance
- .getFilesCreatedWithinDurations(
- _getDurationsForCalendarDateInEveryYear(
- holiday.day,
- holiday.month,
- ),
- ignoreCollections(),
- order: 'DESC',
- );
+ final matchedFiles = await _getFilesCreatedWithinDurations(
+ _getDurationsForCalendarDateInEveryYear(holiday.day, holiday.month),
+ );
if (matchedFiles.isNotEmpty) {
searchResults.add(
GenericSearchResult(
@@ -1681,12 +1703,9 @@ class SearchService {
parsedDate.year != null) {
final month = parsedDate.month!;
final year = parsedDate.year!;
- final monthYearFiles = await FilesDB.instance
- .getFilesCreatedWithinDurations(
- [_getDurationForMonthInYear(month, year)],
- ignoreCollections(),
- order: 'DESC',
- );
+ final monthYearFiles = await _getFilesCreatedWithinDurations([
+ _getDurationForMonthInYear(month, year),
+ ]);
if (monthYearFiles.isNotEmpty) {
final monthName = DateParseService.instance.getMonthName(month);
final name = '$monthName $year';
@@ -1710,12 +1729,9 @@ class SearchService {
final int month = parsedDate.month!;
final int? year = parsedDate.year; // nullable for generic dates
- final matchedFiles = await FilesDB.instance
- .getFilesCreatedWithinDurations(
- _getDurationsForCalendarDateInEveryYear(day, month, year: year),
- ignoreCollections(),
- order: 'DESC',
- );
+ final matchedFiles = await _getFilesCreatedWithinDurations(
+ _getDurationsForCalendarDateInEveryYear(day, month, year: year),
+ );
if (matchedFiles.isNotEmpty) {
final monthName = DateParseService.instance.getMonthName(month);
@@ -1939,14 +1955,6 @@ class SearchService {
.toList();
}
- Future<List<EnteFile>> _getFilesInYear(List<int> durationOfYear) async {
- return await FilesDB.instance.getFilesCreatedWithinDurations(
- [durationOfYear],
- ignoreCollections(),
- order: "DESC",
- );
- }
-
List<List<int>> _getDurationsForCalendarDateInEveryYear(
int day,
int month, {