Skip to content

armgit / ente

Public Imported from GitHub
0

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

Simplify getRepeatedTextEmbeddingCache method

laurenspriem54ad3e4abb99
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dart
index 44a47025fb..4f9fbfa96d 100644
--- a/mobile/apps/photos/lib/db/ml/db.dart
+++ b/mobile/apps/photos/lib/db/ml/db.dart
@@ -1452,25 +1452,31 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
/// WARNING: don't confuse this with [getAllClipVectors]. If you're not sure, use [getAllClipVectors]
Future<List<double>?> getRepeatedTextEmbeddingCache(String query) async {
final db = await asyncDB;
- final threeMonthsAgo = DateTime.now().millisecondsSinceEpoch -
- (90 * 24 * 60 * 60 * 1000); // 90 days in milliseconds
-
final results = await db.getAll(
- 'SELECT $embeddingColumn, $createdAtColumn FROM $textEmbeddingsCacheTable '
- 'WHERE $textQueryColumn = ? AND $mlVersionColumn = ? AND $createdAtColumn > ?',
- [query, clipMlVersion, threeMonthsAgo],
+ 'SELECT $embeddingColumn, $mlVersionColumn, $createdAtColumn '
+ 'FROM $textEmbeddingsCacheTable '
+ 'WHERE $textQueryColumn = ?',
+ [query],
);
- if (results.isNotEmpty) {
- final blob = results.first[embeddingColumn] as Uint8List;
- return Float32List.view(blob.buffer).toList();
- } else {
- // Clean up old/invalid entries if they exist
- await db.execute(
- 'DELETE FROM $textEmbeddingsCacheTable WHERE $textQueryColumn = ?',
- [query],
- );
+ if (results.isEmpty) return null;
+
+ final threeMonthsAgo =
+ DateTime.now().millisecondsSinceEpoch - (90 * 24 * 60 * 60 * 1000);
+
+ // Find first valid entry
+ for (final result in results) {
+ if (result[mlVersionColumn] == clipMlVersion &&
+ result[createdAtColumn] as int > threeMonthsAgo) {
+ return Float32List.view((result[embeddingColumn] as Uint8List).buffer);
+ }
}
+
+ // No valid entry found, clean up
+ await db.execute(
+ 'DELETE FROM $textEmbeddingsCacheTable WHERE $textQueryColumn = ?',
+ [query],
+ );
return null;
}