Skip to content

armgit / ente

Public Imported from GitHub
0

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

memories decoding optimization

laurenspriem36928e4f3954
diff --git a/mobile/lib/models/base_location.dart b/mobile/lib/models/base_location.dart
index 7bdbb7eaec..d98152dad3 100644
--- a/mobile/lib/models/base_location.dart
+++ b/mobile/lib/models/base_location.dart
@@ -1,19 +1,18 @@
import "dart:convert";
-import "package:photos/models/file/file.dart";
import "package:photos/models/location/location.dart";
const baseRadius = 0.6;
class BaseLocation {
- final List<EnteFile> files;
+ final List<int> fileIDs;
int? firstCreationTime;
int? lastCreationTime;
final Location location;
final bool isCurrentBase;
BaseLocation(
- this.files,
+ this.fileIDs,
this.location,
this.isCurrentBase, {
this.firstCreationTime,
@@ -22,12 +21,9 @@ class BaseLocation {
static List<BaseLocation> decodeJsonToList(
String jsonString,
- Map<int, EnteFile> filesMap,
) {
final jsonList = jsonDecode(jsonString) as List;
- return jsonList
- .map((json) => BaseLocation.fromJson(json, filesMap))
- .toList();
+ return jsonList.map((json) => BaseLocation.fromJson(json)).toList();
}
static String encodeListToJson(List<BaseLocation> baseLocations) {
@@ -38,13 +34,9 @@ class BaseLocation {
static BaseLocation fromJson(
Map<String, dynamic> json,
- Map<int, EnteFile> filesMap,
) {
return BaseLocation(
- (json['fileIDs'] as List)
- .where((uploadID) => filesMap[uploadID] != null)
- .map((uploadID) => filesMap[uploadID]!)
- .toList(),
+ (json['fileIDs'] as List).cast<int>(),
Location(
latitude: json['location']['latitude'],
longitude: json['location']['longitude'],
@@ -57,10 +49,7 @@ class BaseLocation {
Map<String, dynamic> toJson() {
return {
- 'fileIDs': files
- .where((file) => file.uploadedFileID != null)
- .map((file) => file.uploadedFileID!)
- .toList(),
+ 'fileIDs': fileIDs,
'location': {
'latitude': location.latitude!,
'longitude': location.longitude!,
@@ -71,32 +60,15 @@ class BaseLocation {
};
}
- int averageCreationTime() {
- if (firstCreationTime != null && lastCreationTime != null) {
- return (firstCreationTime! + lastCreationTime!) ~/ 2;
- }
- final List<int> creationTimes = files
- .where((file) => file.creationTime != null)
- .map((file) => file.creationTime!)
- .toList();
- if (creationTimes.length < 2) {
- return creationTimes.isEmpty ? 0 : creationTimes.first;
- }
- creationTimes.sort();
- firstCreationTime ??= creationTimes.first;
- lastCreationTime ??= creationTimes.last;
- return (firstCreationTime! + lastCreationTime!) ~/ 2;
- }
-
BaseLocation copyWith({
- List<EnteFile>? files,
+ List<int>? files,
int? firstCreationTime,
int? lastCreationTime,
Location? location,
bool? isCurrentBase,
}) {
return BaseLocation(
- files ?? this.files,
+ files ?? fileIDs,
location ?? this.location,
isCurrentBase ?? this.isCurrentBase,
firstCreationTime: firstCreationTime ?? this.firstCreationTime,
diff --git a/mobile/lib/models/memories/memories_cache.dart b/mobile/lib/models/memories/memories_cache.dart
index 8257224fe7..949107901d 100644
--- a/mobile/lib/models/memories/memories_cache.dart
+++ b/mobile/lib/models/memories/memories_cache.dart
@@ -2,7 +2,6 @@ import "dart:convert";
import "package:photos/models/base/id.dart";
import "package:photos/models/base_location.dart";
-import "package:photos/models/file/file.dart";
import "package:photos/models/location/location.dart";
import "package:photos/models/memories/clip_memory.dart";
import "package:photos/models/memories/people_memory.dart";
@@ -39,7 +38,6 @@ class MemoriesCache {
factory MemoriesCache.fromJson(
Map<String, dynamic> json,
- Map<int, EnteFile> filesMap,
) {
return MemoriesCache(
toShowMemories: ToShowMemory.decodeJsonToList(json['toShowMemories']),
@@ -47,10 +45,7 @@ class MemoriesCache {
clipShownLogs: ClipShownLog.decodeJsonToList(json['clipShownLogs']),
tripsShownLogs: TripsShownLog.decodeJsonToList(json['tripsShownLogs']),
baseLocations: json['baseLocations'] != null
- ? BaseLocation.decodeJsonToList(
- json['baseLocations'],
- filesMap,
- )
+ ? BaseLocation.decodeJsonToList(json['baseLocations'])
: [],
);
}
@@ -71,9 +66,8 @@ class MemoriesCache {
static MemoriesCache decodeFromJsonString(
String jsonString,
- Map<int, EnteFile> filesMap,
) {
- return MemoriesCache.fromJson(jsonDecode(jsonString), filesMap);
+ return MemoriesCache.fromJson(jsonDecode(jsonString));
}
}
diff --git a/mobile/lib/services/memories_cache_service.dart b/mobile/lib/services/memories_cache_service.dart
index f3d6eb9b59..4eed3fbba6 100644
--- a/mobile/lib/services/memories_cache_service.dart
+++ b/mobile/lib/services/memories_cache_service.dart
@@ -6,6 +6,7 @@ import "package:flutter/material.dart" show BuildContext;
import "package:logging/logging.dart";
import "package:path_provider/path_provider.dart";
import "package:photos/core/event_bus.dart";
+import "package:photos/db/files_db.dart";
import "package:photos/db/memories_db.dart";
import "package:photos/events/files_updated_event.dart";
import "package:photos/events/memories_changed_event.dart";
@@ -205,20 +206,10 @@ class MemoriesCacheService {
_logger.info("No memories cache found");
return null;
}
- final allFiles = Set<EnteFile>.from(
- await SearchService.instance.getAllFilesForSearch(),
- );
- final allFileIdsToFile = <int, EnteFile>{};
- for (final file in allFiles) {
- if (file.uploadedFileID != null) {
- allFileIdsToFile[file.uploadedFileID!] = file;
- }
- }
try {
final bytes = await file.readAsBytes();
final jsonString = String.fromCharCodes(bytes);
- final cache =
- MemoriesCache.decodeFromJsonString(jsonString, allFileIdsToFile);
+ final cache = MemoriesCache.decodeFromJsonString(jsonString);
_logger.info("Reading memories cache result from disk done");
return cache;
} catch (e, s) {
@@ -232,25 +223,32 @@ class MemoriesCacheService {
try {
_logger.info('Processing disk cache memories to smart memories');
final List<SmartMemory> memories = [];
- final allFiles = Set<EnteFile>.from(
- await SearchService.instance.getAllFilesForSearch(),
+ final seenTimes = await _memoriesDB.getSeenTimes();
+ final minimalFileIDs = <int>{};
+ for (final ToShowMemory memory in cache.toShowMemories) {
+ if (memory.shouldShowNow()) {
+ minimalFileIDs.addAll(memory.fileUploadedIDs);
+ }
+ }
+ final minimalFiles = await FilesDB.instance.getFilesFromIDs(
+ minimalFileIDs.toList(),
+ collectionsToIgnore: SearchService.instance.ignoreCollections(),
);
- final allFileIdsToFile = <int, EnteFile>{};
- for (final file in allFiles) {
+ final minimalFileIdsToFile = <int, EnteFile>{};
+ for (final file in minimalFiles) {
if (file.uploadedFileID != null) {
- allFileIdsToFile[file.uploadedFileID!] = file;
+ minimalFileIdsToFile[file.uploadedFileID!] = file;
}
}
- final seenTimes = await _memoriesDB.getSeenTimes();
for (final ToShowMemory memory in cache.toShowMemories) {
if (memory.shouldShowNow()) {
final smartMemory = SmartMemory(
memory.fileUploadedIDs
- .where((fileID) => allFileIdsToFile.containsKey(fileID))
+ .where((fileID) => minimalFileIdsToFile.containsKey(fileID))
.map(
(fileID) =>
- Memory.fromFile(allFileIdsToFile[fileID]!, seenTimes),
+ Memory.fromFile(minimalFileIdsToFile[fileID]!, seenTimes),
)
.toList(),
memory.type,
diff --git a/mobile/lib/services/smart_memories_service.dart b/mobile/lib/services/smart_memories_service.dart
index b17fbbe4a4..7e5f94ba23 100644
--- a/mobile/lib/services/smart_memories_service.dart
+++ b/mobile/lib/services/smart_memories_service.dart
@@ -272,6 +272,7 @@ class SmartMemoriesService {
// Trip memories
final (tripMemories, bases) = await _getTripsResults(
allFiles,
+ allFileIdsToFile,
now,
oldCache.tripsShownLogs,
surfaceAll: debugSurfaceAll,
@@ -851,6 +852,7 @@ class SmartMemoriesService {
static Future<(List<TripMemory>, List<BaseLocation>)> _getTripsResults(
Iterable<EnteFile> allFiles,
+ Map<int, EnteFile> allFileIdsToFile,
DateTime currentTime,
List<TripsShownLog> shownTrips, {
bool surfaceAll = false,
@@ -956,7 +958,13 @@ class SmartMemoriesService {
const Duration(days: 90),
),
);
- baseLocations.add(BaseLocation(files, location, isCurrent));
+ baseLocations.add(
+ BaseLocation(
+ files.map((file) => file.uploadedFileID!).toList(),
+ location,
+ isCurrent,
+ ),
+ );
}
// Identify trip locations
@@ -1108,8 +1116,11 @@ class SmartMemoriesService {
for (final baseLocation in baseLocations) {
String name =
"Base (${baseLocation.isCurrentBase ? 'current' : 'old'})";
+ final files = baseLocation.fileIDs
+ .map((fileID) => allFileIdsToFile[fileID]!)
+ .toList();
final String? locationName = _tryFindLocationName(
- Memory.fromFiles(baseLocation.files, seenTimes),
+ Memory.fromFiles(files, seenTimes),
cities,
base: true,
);
@@ -1119,7 +1130,7 @@ class SmartMemoriesService {
}
memoryResults.add(
TripMemory(
- Memory.fromFiles(baseLocation.files, seenTimes),
+ Memory.fromFiles(files, seenTimes),
nowInMicroseconds,
windowEnd,
baseLocation.location,