π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] Wire MlStore into the Rust ML DB backend
laurenspriem52fc18b1edef
diff --git a/mobile/apps/photos/lib/db/ml/base.dart b/mobile/apps/photos/lib/db/ml/base.dartindex ed5bdb2607..4e443de836 100644--- a/mobile/apps/photos/lib/db/ml/base.dart+++ b/mobile/apps/photos/lib/db/ml/base.dart@@ -130,10 +130,6 @@ abstract class IMLDataDB<T> {Future<void> checkMigrateFillClipVectorDB({bool force});Future<void> checkMigrateFillClusterCentroidVectorDB({bool force});- Future<Map<String, int>> getClusterCentroidVectorIdMap(- Iterable<String> clusterIDs, {- bool createIfMissing,- });Future<Set<String>> getClustersForMemoryLane(Set<String> assigned);Future<List<String>> getFaceIDsForClusterOrderedByScore(String clusterID, {@@ -158,29 +154,8 @@ abstract class IMLDataDB<T> {Future<Map<int, PreviewInfo>> getFileIDsVidPreview();Future<Set<int>> getFileIDsWithFDData({DataType? type});- Future<void> clearClusterCentroidVectorIdMappings();- Future<void> deleteClusterCentroidVectorIdMapping(String clusterID);Future<int> getClipVectorizableFileCount({int minimumMlVersion});Future<Map<String, String>> getFaceIdToPersonIdForFaces(Iterable<String> faceIDs,);-- Future<void> clearNonPetTables();- Future<void> clearPetTables();- Future<void> resetClusterTables({required bool faces});- Future<void> upsertClusterSummaryRows(Map<String, (Uint8List, int)> summary);- Future<void> deleteClusterSummaryRow(String clusterID);- Future<void> insertClipRows(List<ClipEmbedding> embeddings);- Future<void> deleteClipRows(List<int> fileIDs);- Future<void> deleteAllClipRows();- Future<int> countClusterSummaries();- Future<List<(String, Uint8List)>> getClusterSummaryPage({- String? beforeClusterID,- required int limit,- });- Future<int> countClipRows();- Future<List<(int, Uint8List)>> getClipRowsPage({- required int limit,- required int offset,- });}diff --git a/mobile/apps/photos/lib/db/ml/clip_vector_db.dart b/mobile/apps/photos/lib/db/ml/clip_vector_db.dartindex 8404c685db..c4b07f020e 100644--- a/mobile/apps/photos/lib/db/ml/clip_vector_db.dart+++ b/mobile/apps/photos/lib/db/ml/clip_vector_db.dart@@ -1,10 +1,18 @@+import "package:photos/db/ml/db.dart";+import "package:photos/db/ml/store_clip_vector_db.dart";import "package:photos/db/ml/usearch_clip_vector_db.dart";import "package:photos/services/machine_learning/semantic_search/query_result.dart";abstract class ClipVectorDB {- static ClipVectorDB get instance => UsearchClipVectorDB.instance;- static ClipVectorDB get localGalleryInstance =>- UsearchClipVectorDB.localGalleryInstance;+ static const int embeddingDimensions = 512;+ static const int embeddingBytesLength = embeddingDimensions * 4;++ static ClipVectorDB get instance => MLDataDB.isRustBackend+ ? StoreClipVectorDB.instance+ : UsearchClipVectorDB.instance;+ static ClipVectorDB get localGalleryInstance => MLDataDB.isRustBackend+ ? StoreClipVectorDB.localGalleryInstance+ : UsearchClipVectorDB.localGalleryInstance;Future<bool> isReady();Future<void> warmup();diff --git a/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dart b/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dartindex 628a26fca6..f55058205e 100644--- a/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dart+++ b/mobile/apps/photos/lib/db/ml/cluster_centroid_vector_db.dart@@ -1,3 +1,5 @@+import "package:photos/db/ml/db.dart";+import "package:photos/db/ml/store_cluster_centroid_vector_db.dart";import "package:photos/db/ml/usearch_cluster_centroid_vector_db.dart";typedef ClusterCentroidMatches = ({@@ -6,10 +8,15 @@ typedef ClusterCentroidMatches = ({});abstract class ClusterCentroidVectorDB {- static ClusterCentroidVectorDB get instance =>- UsearchClusterCentroidVectorDB.instance;+ static const int embeddingDimensions = 192;++ static ClusterCentroidVectorDB get instance => MLDataDB.isRustBackend+ ? StoreClusterCentroidVectorDB.instance+ : UsearchClusterCentroidVectorDB.instance;static ClusterCentroidVectorDB get localGalleryInstance =>- UsearchClusterCentroidVectorDB.localGalleryInstance;+ MLDataDB.isRustBackend+ ? StoreClusterCentroidVectorDB.localGalleryInstance+ : UsearchClusterCentroidVectorDB.localGalleryInstance;Future<bool> isReady();Future<void> warmup();diff --git a/mobile/apps/photos/lib/db/ml/dart_db.dart b/mobile/apps/photos/lib/db/ml/dart_db.dartindex 7a4171b24a..924cf81765 100644--- a/mobile/apps/photos/lib/db/ml/dart_db.dart+++ b/mobile/apps/photos/lib/db/ml/dart_db.dart@@ -8,6 +8,7 @@ import 'package:path/path.dart' show join;import 'package:path_provider/path_provider.dart';import "package:photos/db/common/base.dart";import "package:photos/db/ml/base.dart";+import "package:photos/db/ml/clip_vector_db.dart";import "package:photos/db/ml/db_model_mappers.dart";import "package:photos/db/ml/ml_data_db_orchestration.dart";import 'package:photos/db/ml/schema.dart';@@ -1481,7 +1482,7 @@ class DartMLDataDBfinal List<EmbeddingVector> embeddings = [];for (final result in results) {final bytes = result[embeddingColumn] as Uint8List;- if (bytes.lengthInBytes != UsearchClipVectorDB.embeddingBytesLength) {+ if (bytes.lengthInBytes != ClipVectorDB.embeddingBytesLength) {continue;}final embedding = EmbeddingVector(@@ -1595,7 +1596,7 @@ class DartMLDataDB'WHERE $mlVersionColumn >= ? AND LENGTH($embeddingColumn) = ?';final List<Map<String, dynamic>> maps = await db.getAll(query, [minimumMlVersion,- UsearchClipVectorDB.embeddingBytesLength,+ ClipVectorDB.embeddingBytesLength,]);return maps.first['count'] as int;}diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dartindex ed62474c73..434f5ec83a 100644--- a/mobile/apps/photos/lib/db/ml/db.dart+++ b/mobile/apps/photos/lib/db/ml/db.dart@@ -28,4 +28,10 @@ class MLDataDB {"ML DB backend: ${useRust ? "rust" : "dart"} (wantsRust: $preferRust)",);}++ static Future<void> releaseVectorIndexes() async {+ if (_useRust != true) return;+ await RustMLDataDB.instance.releaseVectorIndexes();+ await RustMLDataDB.localGalleryInstance.releaseVectorIndexes();+ }}diff --git a/mobile/apps/photos/lib/db/ml/ml_data_db_orchestration.dart b/mobile/apps/photos/lib/db/ml/ml_data_db_orchestration.dartindex ab2de75c75..f6319f1e0e 100644--- a/mobile/apps/photos/lib/db/ml/ml_data_db_orchestration.dart+++ b/mobile/apps/photos/lib/db/ml/ml_data_db_orchestration.dart@@ -4,11 +4,13 @@ import "dart:typed_data";import "package:logging/logging.dart";import "package:photos/core/event_bus.dart";import "package:photos/db/ml/base.dart";+import "package:photos/db/ml/clip_vector_db.dart";+import "package:photos/db/ml/cluster_centroid_vector_db.dart";+import "package:photos/db/ml/ml_exclusive_operation.dart";import "package:photos/db/ml/usearch_clip_vector_db.dart";import "package:photos/db/ml/usearch_cluster_centroid_vector_db.dart";import "package:photos/events/embedding_updated_event.dart";import "package:photos/generated/protos/ente/common/vector.pb.dart";-import "package:photos/main.dart" show isProcessBg;import "package:photos/models/ml/clip.dart";import "package:photos/service_locator.dart";import "package:photos/services/machine_learning/compute_controller.dart";@@ -16,8 +18,6 @@ import "package:photos/services/machine_learning/ml_process_lock.dart";import "package:synchronized/synchronized.dart";mixin MLDataDBOrchestration implements IMLDataDB<int> {- static const _kMigrationLockWaitDeadline = Duration(minutes: 2);-int _clusterSummaryMutationVersion = 0;Future<void>? _clipVectorDbRecoveryFuture;final Lock _clipVectorRecoveryLock = Lock();@@ -32,9 +32,34 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {UsearchClusterCentroidVectorDB get clusterCentroidVectorDB;Logger get logger;+ Future<void> clearNonPetTables();+ Future<void> clearPetTables();+ Future<void> resetClusterTables({required bool faces});+ Future<void> upsertClusterSummaryRows(Map<String, (Uint8List, int)> summary);+ Future<void> deleteClusterSummaryRow(String clusterID);+ Future<void> insertClipRows(List<ClipEmbedding> embeddings);+ Future<void> deleteClipRows(List<int> fileIDs);+ Future<void> deleteAllClipRows();+ Future<int> countClusterSummaries();+ Future<List<(String, Uint8List)>> getClusterSummaryPage({+ String? beforeClusterID,+ required int limit,+ });+ Future<int> countClipRows();+ Future<List<(int, Uint8List)>> getClipRowsPage({+ required int limit,+ required int offset,+ });+ Future<Map<String, int>> getClusterCentroidVectorIdMap(+ Iterable<String> clusterIDs, {+ bool createIfMissing,+ });+ Future<void> deleteClusterCentroidVectorIdMapping(String clusterID);+ Future<void> clearClusterCentroidVectorIdMappings();+@overrideFuture<void> clearTable() =>- _runMlOperationExclusive(MlOperation.clearData, _clearTable);+ runMlOperationExclusive(MlOperation.clearData, _clearTable);Future<void> _clearTable() async {await clearNonPetTables();@@ -93,8 +118,7 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {);continue;}- if (centroid.length !=- UsearchClusterCentroidVectorDB.embeddingDimensions) {+ if (centroid.length != ClusterCentroidVectorDB.embeddingDimensions) {logger.warning("Unexpected centroid embedding size ${centroid.length} for cluster ${entry.key}, skipping vector update",);@@ -174,10 +198,10 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {if (!force && await clusterCentroidVectorDB.isReady()) {return;}- await _runMlOperationExclusive(+ await runMlOperationExclusive(MlOperation.clusterCentroidVectorMigration,() => _checkMigrateFillClusterCentroidVectorDB(force: force),- waitDeadline: _kMigrationLockWaitDeadline,+ waitDeadline: kMlMigrationLockWaitDeadline,);}@@ -377,8 +401,7 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {);continue;}- if (centroid.length ==- UsearchClusterCentroidVectorDB.embeddingDimensions) {+ if (centroid.length == ClusterCentroidVectorDB.embeddingDimensions) {clusterIDs.add(clusterID);clusterIDToCentroid[clusterID] = centroid;} else {@@ -458,10 +481,10 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {if (!force && await clipVectorDB.isReady()) {return;}- await _runMlOperationExclusive(+ await runMlOperationExclusive(MlOperation.clipVectorMigration,() => _checkMigrateFillClipVectorDB(force: force),- waitDeadline: _kMigrationLockWaitDeadline,+ waitDeadline: kMlMigrationLockWaitDeadline,);}@@ -524,7 +547,7 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {final List<Float32List> embeddings = [];for (final (fileID, embeddingBytes) in results) {final embedding = Float32List.view(embeddingBytes.buffer);- if (embedding.length == UsearchClipVectorDB.embeddingDimensions) {+ if (embedding.length == ClipVectorDB.embeddingDimensions) {fileIDs.add(fileID);embeddings.add(Float32List.view(embeddingBytes.buffer));} else if (embedding.isEmpty) {@@ -596,26 +619,6 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {});}- Future<void> _runMlOperationExclusive(- MlOperation operation,- Future<void> Function() body, {- Duration? waitDeadline,- }) async {- final attempt = await MlProcessLock.instance.tryRunExclusive(- operation,- body,- background: isProcessBg,- waitForAvailability: true,- waitDeadline: waitDeadline,- );- if (attempt != MlLockAttempt.ran) {- throw StateError(- "${operation.name} could not acquire the ML process lock "- "(${attempt.name})",- );- }- }-Future<void> _withClipVectorWriteRecovery({required String operation,required Future<void> Function() writeOperation,@@ -655,8 +658,7 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {}bool _isVectorizableClipEmbedding(ClipEmbedding embedding) {- return embedding.embedding.length ==- UsearchClipVectorDB.embeddingDimensions;+ return embedding.embedding.length == ClipVectorDB.embeddingDimensions;}List<ClipEmbedding> _vectorizableClipEmbeddings(@@ -677,7 +679,7 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {for (final (fileID, length) in skippedNonEmpty) {logger.warning(- "Skipping ClipVectorDB write for fileID $fileID because embedding length $length does not match expected ${UsearchClipVectorDB.embeddingDimensions}",+ "Skipping ClipVectorDB write for fileID $fileID because embedding length $length does not match expected ${ClipVectorDB.embeddingDimensions}",);}diff --git a/mobile/apps/photos/lib/db/ml/ml_exclusive_operation.dart b/mobile/apps/photos/lib/db/ml/ml_exclusive_operation.dartnew file mode 100644index 0000000000..774764407e--- /dev/null+++ b/mobile/apps/photos/lib/db/ml/ml_exclusive_operation.dart@@ -0,0 +1,24 @@+import "package:photos/main.dart" show isProcessBg;+import "package:photos/services/machine_learning/ml_process_lock.dart";++const kMlMigrationLockWaitDeadline = Duration(minutes: 2);++Future<void> runMlOperationExclusive(+ MlOperation operation,+ Future<void> Function() body, {+ Duration? waitDeadline,+}) async {+ final attempt = await MlProcessLock.instance.tryRunExclusive(+ operation,+ body,+ background: isProcessBg,+ waitForAvailability: true,+ waitDeadline: waitDeadline,+ );+ if (attempt != MlLockAttempt.ran) {+ throw StateError(+ "${operation.name} could not acquire the ML process lock "+ "(${attempt.name})",+ );+ }+}diff --git a/mobile/apps/photos/lib/db/ml/rust_db.dart b/mobile/apps/photos/lib/db/ml/rust_db.dartindex 368e84e142..af82f9f521 100644--- a/mobile/apps/photos/lib/db/ml/rust_db.dart+++ b/mobile/apps/photos/lib/db/ml/rust_db.dart@@ -4,60 +4,43 @@ import "package:flutter_rust_bridge/flutter_rust_bridge.dart" show Int64List;import "package:logging/logging.dart";import "package:path/path.dart" show join;import "package:path_provider/path_provider.dart";+import "package:photos/core/event_bus.dart";import "package:photos/db/common/base.dart";import "package:photos/db/ml/base.dart";-import "package:photos/db/ml/ml_data_db_orchestration.dart";+import "package:photos/db/ml/clip_vector_db.dart";+import "package:photos/db/ml/ml_exclusive_operation.dart";import "package:photos/db/ml/rust_db_model_mappers.dart" as mappers;-import "package:photos/db/ml/usearch_clip_vector_db.dart";-import "package:photos/db/ml/usearch_cluster_centroid_vector_db.dart";+import "package:photos/events/embedding_updated_event.dart";import "package:photos/models/ml/clip.dart";import "package:photos/models/ml/face/face.dart";import "package:photos/models/ml/face/face_with_embedding.dart";import "package:photos/models/ml/ml_versions.dart";import "package:photos/models/ml/vector.dart";+import "package:photos/service_locator.dart";import "package:photos/services/filedata/model/file_data.dart";import "package:photos/services/machine_learning/face_ml/face_clustering/face_db_info_for_clustering.dart";+import "package:photos/services/machine_learning/ml_process_lock.dart";import "package:photos/src/rust/api/ml_db_api.dart" as rust;import "package:photos/utils/ml_util.dart";-class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {+class RustMLDataDB implements IMLDataDB<int> {static final Logger _logger = Logger("MLDataDB");+ static const _clipFillBlocker = "clip_vector_db_migration_in_progress";+ static const _clusterCentroidFillBlocker =+ "cluster_centroid_vector_db_migration_in_progress";final String _databaseName;- final UsearchClipVectorDB _clipVectorDB;- final UsearchClusterCentroidVectorDB _clusterCentroidVectorDB;-- RustMLDataDB._privateConstructor({- String databaseName = "ente.ml.db",- UsearchClipVectorDB? clipVectorDB,- UsearchClusterCentroidVectorDB? clusterCentroidVectorDB,- }) : _databaseName = databaseName,- _clipVectorDB = clipVectorDB ?? UsearchClipVectorDB.instance,- _clusterCentroidVectorDB =- clusterCentroidVectorDB ?? UsearchClusterCentroidVectorDB.instance;++ RustMLDataDB._privateConstructor({String databaseName = "ente.ml.db"})+ : _databaseName = databaseName;static final RustMLDataDB instance = RustMLDataDB._privateConstructor();static final RustMLDataDB localGalleryInstance =- RustMLDataDB._privateConstructor(- databaseName: "ente.ml.offline.db",- clipVectorDB: UsearchClipVectorDB.localGalleryInstance,- clusterCentroidVectorDB:- UsearchClusterCentroidVectorDB.localGalleryInstance,- );-- @override- UsearchClipVectorDB get clipVectorDB => _clipVectorDB;-- @override- UsearchClusterCentroidVectorDB get clusterCentroidVectorDB =>- _clusterCentroidVectorDB;-- @override- Logger get logger => _logger;+ RustMLDataDB._privateConstructor(databaseName: "ente.ml.offline.db");Future<rust.MlStore>? _dbFuture;- Future<rust.MlStore> get _db async {+ Future<rust.MlStore> get store async {final future = _dbFuture ??= _openDatabase();try {return await future;@@ -80,67 +63,135 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {}}+ Future<rust.FillState> fillState(rust.Index index) async =>+ (await store).fillState(index: index);++ Future<void> releaseVectorIndexes() async {+ if (_dbFuture == null) return;+ try {+ await (await store).release();+ } catch (e, s) {+ _logger.warning("Failed to release vector indexes", e, s);+ }+ }++ @override+ Future<void> clearTable() => runMlOperationExclusive(+ MlOperation.clearData,+ () async => (await store).clearAll(),+ );+@override- Future<void> bulkInsertFaces(List<Face> faces) async =>- (await _db).bulkInsertFaces(faces: faces.map(mappers.toFaceRow).toList());+ Future<void> checkMigrateFillClipVectorDB({bool force = false}) =>+ _checkMigrateFillIndex(+ rust.Index.clip,+ force: force,+ operation: MlOperation.clipVectorMigration,+ blocker: _clipFillBlocker,+ fill: (db) => db.fillClipIndex(force: force),+ );++ @override+ Future<void> checkMigrateFillClusterCentroidVectorDB({bool force = false}) =>+ _checkMigrateFillIndex(+ rust.Index.clusterCentroid,+ force: force,+ operation: MlOperation.clusterCentroidVectorMigration,+ blocker: _clusterCentroidFillBlocker,+ fill: (db) => db.fillClusterCentroidIndex(force: force),+ );++ Future<void> _checkMigrateFillIndex(+ rust.Index index, {+ required bool force,+ required MlOperation operation,+ required String blocker,+ required Future<rust.FillReport> Function(rust.MlStore db) fill,+ }) async {+ if (!force && await fillState(index) == rust.FillState.filled) {+ return;+ }+ await runMlOperationExclusive(+ operation,+ () => _fillIndex(index, blocker: blocker, fill: fill),+ waitDeadline: kMlMigrationLockWaitDeadline,+ );+ }++ Future<void> _fillIndex(+ rust.Index index, {+ required String blocker,+ required Future<rust.FillReport> Function(rust.MlStore db) fill,+ }) async {+ final stopwatch = Stopwatch()..start();+ try {+ computeController.blockCompute(blocker: blocker);+ final report = await fill(await store);+ _logger.info(+ "${index.name} index fill ${report.outcome.name} in ${stopwatch.elapsedMilliseconds} ms: rows=${report.rows}, indexed=${report.indexed}, skipped=${report.skipped}, resumed=${report.resumed}",+ );+ } finally {+ stopwatch.stop();+ computeController.unblockCompute(blocker: blocker);+ }+ }++ @override+ Future<void> bulkInsertFaces(List<Face> faces) async => (await store)+ .bulkInsertFaces(faces: faces.map(mappers.toFaceRow).toList());@overrideFuture<void> updateFaceIdToClusterId(Map<String, String> faceIDToClusterID,- ) async =>- (await _db).updateFaceIdToClusterId(faceIdToClusterId: faceIDToClusterID);+ ) async => (await store).updateFaceIdToClusterId(+ faceIdToClusterId: faceIDToClusterID,+ );@overrideFuture<Map<int, int>> faceIndexedFileIds({int minimumMlVersion = faceMlVersion,}) async =>- (await _db).faceIndexedFileIds(minimumMlVersion: minimumMlVersion);+ (await store).faceIndexedFileIds(minimumMlVersion: minimumMlVersion);@overrideFuture<int> getFaceIndexedFileCount({int minimumMlVersion = faceMlVersion,}) async =>- (await _db).getFaceIndexedFileCount(minimumMlVersion: minimumMlVersion);+ (await store).getFaceIndexedFileCount(minimumMlVersion: minimumMlVersion);@overrideFuture<Map<String, int>> clusterIdToFaceCount() async =>- (await _db).clusterIdToFaceCount();+ (await store).clusterIdToFaceCount();@overrideFuture<Set<String>> getBadFaceSingletonClusterIDs() async =>- (await _db).getBadFaceSingletonClusterIds();+ (await store).getBadFaceSingletonClusterIds();@overrideFuture<Set<String>> getClustersWithThreeOrMoreNotPersonFeedback() async =>- (await _db).getClustersWithThreeOrMoreNotPersonFeedback();+ (await store).getClustersWithThreeOrMoreNotPersonFeedback();@overrideFuture<Set<String>> getPersonIgnoredClusters(String personID) async =>- (await _db).getPersonIgnoredClusters(personId: personID);+ (await store).getPersonIgnoredClusters(personId: personID);@overrideFuture<Map<String, Set<String>>> getPersonToRejectedSuggestions() async =>- (await _db).getPersonToRejectedSuggestions();+ (await store).getPersonToRejectedSuggestions();@overrideFuture<Set<String>> getPersonClusterIDs(String personID) async =>- (await _db).getPersonClusterIds(personId: personID);+ (await store).getPersonClusterIds(personId: personID);@overrideFuture<Set<String>> getPersonsClusterIDs(List<String> personID) async =>- (await _db).getPersonsClusterIds(personIds: personID);-- @override- Future<void> clearNonPetTables() async => (await _db).clearNonPetTables();-- @override- Future<void> clearPetTables() async => (await _db).clearPetTables();+ (await store).getPersonsClusterIds(personIds: personID);@overrideFuture<Iterable<Uint8List>> getFaceEmbeddingsForCluster(String clusterID, {int? limit,- }) async => (await _db).getFaceEmbeddingsForCluster(+ }) async => (await store).getFaceEmbeddingsForCluster(clusterId: clusterID,limit: limit,);@@ -149,7 +200,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<Map<String, Iterable<Uint8List>>> getFaceEmbeddingsForClusters(Iterable<String> clusterIDs, {int? limit,- }) async => (await _db).getFaceEmbeddingsForClusters(+ }) async => (await store).getFaceEmbeddingsForClusters(clusterIds: clusterIDs.toList(),limit: limit,);@@ -161,7 +212,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {String? avatarFaceId,String? clusterID,}) async {- final db = await _db;+ final db = await store;final row = await db.getCoverFaceForPerson(recentFileId: recentFileID,personId: personID,@@ -173,7 +224,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<List<Face>?> getFacesForGivenFileID(int fileUploadID) async {- final db = await _db;+ final db = await store;final rows = await db.getFacesForGivenFileId(fileUploadId: fileUploadID);return rows.isEmpty ? null : rows.map(mappers.toFace).toList();}@@ -181,7 +232,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<Map<int, List<FaceWithoutEmbedding>>>getFileIDsToFacesWithoutEmbedding() async {- final db = await _db;+ final db = await store;final rows = await db.getFileIdsToFacesWithoutEmbedding();return rows.map((fileID, faces) =>@@ -192,25 +243,25 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<Map<String, Iterable<String>>> getClusterToFaceIDs(Set<String> clusterIDs,- ) async => (await _db).getClusterToFaceIds(clusterIds: clusterIDs);+ ) async => (await store).getClusterToFaceIds(clusterIds: clusterIDs);@overrideFuture<String?> getClusterIDForFaceID(String faceID) async =>- (await _db).getClusterIdForFaceId(faceId: faceID);+ (await store).getClusterIdForFaceId(faceId: faceID);@overrideFuture<Map<String, Iterable<String>>> getAllClusterIdToFaceIDs() async =>- (await _db).getAllClusterIdToFaceIds();+ (await store).getAllClusterIdToFaceIds();@overrideFuture<Iterable<String>> getFaceIDsForCluster(String clusterID) async =>- (await _db).getFaceIdsForCluster(clusterId: clusterID);+ (await store).getFaceIdsForCluster(clusterId: clusterID);@overrideFuture<List<String>> getFaceIDsForClusterOrderedByScore(String clusterID, {int limit = 10,- }) async => (await _db).getFaceIdsForClusterOrderedByScore(+ }) async => (await store).getFaceIdsForClusterOrderedByScore(clusterId: clusterID,limit: limit,);@@ -218,63 +269,65 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<Map<String, Map<String, Set<String>>>>getPersonToClusterIdToFaceIds() async =>- (await _db).getPersonToClusterIdToFaceIds();+ (await store).getPersonToClusterIdToFaceIds();@overrideFuture<Map<String, Set<String>>> getPersonToClusterIDs() async =>- (await _db).getPersonToClusterIds();+ (await store).getPersonToClusterIds();@overrideFuture<Map<String, String>> getFaceIdToPersonIdForFaces(Iterable<String> faceIDs,- ) async => (await _db).getFaceIdToPersonIdForFaces(faceIds: faceIDs.toList());+ ) async =>+ (await store).getFaceIdToPersonIdForFaces(faceIds: faceIDs.toList());@overrideFuture<Map<String, Set<String>>> getClusterIdToFaceIdsForPerson(String personID,- ) async => (await _db).getClusterIdToFaceIdsForPerson(personId: personID);+ ) async => (await store).getClusterIdToFaceIdsForPerson(personId: personID);@overrideFuture<Set<String>> getFaceIDsForPerson(String personID) async =>- (await _db).getFaceIdsForPerson(personId: personID);+ (await store).getFaceIdsForPerson(personId: personID);@overrideFuture<List<String>> getFaceIDsForPersonOrderedByScore(String personID, {int limit = 10,- }) async => (await _db).getFaceIdsForPersonOrderedByScore(+ }) async => (await store).getFaceIdsForPersonOrderedByScore(personId: personID,limit: limit,);@overrideFuture<Iterable<double>> getBlurValuesForCluster(String clusterID) async =>- (await _db).getBlurValuesForCluster(clusterId: clusterID);+ (await store).getBlurValuesForCluster(clusterId: clusterID);@overrideFuture<Map<String, String?>> getFaceIdsToClusterIds(Iterable<String> faceIds,- ) async => (await _db).getFaceIdsToClusterIds(faceIds: faceIds.toList());+ ) async => (await store).getFaceIdsToClusterIds(faceIds: faceIds.toList());@overrideFuture<Map<int, Set<String>>> getFileIdToClusterIds() async =>- (await _db).getFileIdToClusterIds();+ (await store).getFileIdToClusterIds();@overrideFuture<void> forceUpdateClusterIds(Map<String, String> faceIDToClusterID,) async =>- (await _db).forceUpdateClusterIds(faceIdToClusterId: faceIDToClusterID);+ (await store).forceUpdateClusterIds(faceIdToClusterId: faceIDToClusterID);@overrideFuture<void> removeFaceIdToClusterId(Map<String, String> faceIDToClusterID,- ) async =>- (await _db).removeFaceIdToClusterId(faceIdToClusterId: faceIDToClusterID);+ ) async => (await store).removeFaceIdToClusterId(+ faceIdToClusterId: faceIDToClusterID,+ );@overrideFuture<void> removePerson(String personID) async =>- (await _db).removePerson(personId: personID);+ (await store).removePerson(personId: personID);@overrideFuture<List<FaceDbInfoForClustering>> getFaceInfoForClustering({@@ -282,7 +335,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {int offset = 0,int batchSize = 10000,}) async {- final db = await _db;+ final db = await store;final rows = await db.getFaceInfoForClustering(maxFaces: maxFaces,offset: offset,@@ -295,7 +348,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<Map<String, Uint8List>> getFaceEmbeddingMapForFaces(Iterable<String> faceIDs,) async {- final db = await _db;+ final db = await store;final rows = await db.getFaceEmbeddingRowsForFaces(faceIds: faceIDs.toList(),);@@ -303,27 +356,28 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {}@override- Future<int> getTotalFaceCount() async => (await _db).getTotalFaceCount();+ Future<int> getTotalFaceCount() async => (await store).getTotalFaceCount();@override- Future<int> getErroredFaceCount() async => (await _db).getErroredFaceCount();+ Future<int> getErroredFaceCount() async =>+ (await store).getErroredFaceCount();@overrideFuture<Set<int>> getErroredFileIDs() async {- final db = await _db;+ final db = await store;final fileIDs = await db.getErroredFileIds();return fileIDs.inner.toSet();}@overrideFuture<void> pruneResolvedFaceErrorResults(List<int> fileIDs) async =>- (await _db).pruneResolvedFaceErrorResults(+ (await store).pruneResolvedFaceErrorResults(fileIds: Int64List.fromList(fileIDs),);@overrideFuture<Set<int>> getFileIDsWithErrorResults(List<int> fileIDs) async {- final db = await _db;+ final db = await store;final result = await db.getFileIdsWithErrorResults(fileIds: Int64List.fromList(fileIDs),);@@ -331,18 +385,18 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {}@override- Future<void> deleteFaceIndexForFiles(List<int> fileIDs) async =>- (await _db).deleteFaceIndexForFiles(fileIds: Int64List.fromList(fileIDs));+ Future<void> deleteFaceIndexForFiles(List<int> fileIDs) async => (await store)+ .deleteFaceIndexForFiles(fileIds: Int64List.fromList(fileIDs));@overrideFuture<void> deleteUnclusteredFaceIndexForFiles(List<int> fileIDs) async =>- (await _db).deleteUnclusteredFaceIndexForFiles(+ (await store).deleteUnclusteredFaceIndexForFiles(fileIds: Int64List.fromList(fileIDs),);@overrideFuture<int> getClusteredOrFacelessFileCount() async =>- (await _db).getClusteredOrFacelessFileCount();+ (await store).getClusteredOrFacelessFileCount();@overrideFuture<double> getClusteredToIndexableFilesRatio() async {@@ -354,13 +408,13 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<int> getUnclusteredFaceCount() async =>- (await _db).getUnclusteredFaceCount();+ (await store).getUnclusteredFaceCount();@overrideFuture<void> assignClusterToPerson({required String personID,required String clusterID,- }) async => (await _db).assignClusterToPerson(+ }) async => (await store).assignClusterToPerson(personId: personID,clusterId: clusterID,);@@ -368,7 +422,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<void> bulkAssignClusterToPersonID(Map<String, String> clusterToPersonID,- ) async => (await _db).bulkAssignClusterToPersonId(+ ) async => (await store).bulkAssignClusterToPersonId(clusterToPersonId: clusterToPersonID,);@@ -376,7 +430,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<void> captureNotPersonFeedback({required String personID,required String clusterID,- }) async => (await _db).captureNotPersonFeedback(+ }) async => (await store).captureNotPersonFeedback(personId: personID,clusterId: clusterID,);@@ -384,7 +438,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<void> bulkCaptureNotPersonFeedback(Map<String, String> clusterToPersonID,- ) async => (await _db).bulkCaptureNotPersonFeedback(+ ) async => (await store).bulkCaptureNotPersonFeedback(clusterToPersonId: clusterToPersonID,);@@ -392,7 +446,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<void> removeNotPersonFeedback({required String personID,required String clusterID,- }) async => (await _db).removeNotPersonFeedback(+ }) async => (await store).removeNotPersonFeedback(personId: personID,clusterId: clusterID,);@@ -401,7 +455,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<void> removeClusterToPerson({required String personID,required String clusterID,- }) async => (await _db).removeClusterToPerson(+ }) async => (await store).removeClusterToPerson(personId: personID,clusterId: clusterID,);@@ -409,50 +463,57 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<Map<int, Set<String>>> getFileIdToClusterIDSet(String personID,- ) async => (await _db).getFileIdToClusterIdSet(personId: personID);+ ) async => (await store).getFileIdToClusterIdSet(personId: personID);@overrideFuture<Map<int, Set<String>>> getFileIdToClusterIDSetForCluster(Set<String> clusterIDs,) async =>- (await _db).getFileIdToClusterIdSetForCluster(clusterIds: clusterIDs);-- @override- Future<Map<String, int>> getClusterCentroidVectorIdMap(- Iterable<String> clusterIDs, {- bool createIfMissing = false,- }) async => (await _db).getClusterCentroidVectorIdMap(- clusterIds: clusterIDs.toList(),- createIfMissing: createIfMissing,- );+ (await store).getFileIdToClusterIdSetForCluster(clusterIds: clusterIDs);@override- Future<void> deleteClusterCentroidVectorIdMapping(String clusterID) async =>- (await _db).deleteClusterCentroidVectorIdMapping(clusterId: clusterID);-- @override- Future<void> clearClusterCentroidVectorIdMappings() async =>- (await _db).clearClusterCentroidVectorIdMappings();-- @override- Future<void> upsertClusterSummaryRows(+ Future<void> clusterSummaryUpdate(Map<String, (Uint8List, int)> summary,- ) async => (await _db).upsertClusterSummaryRows(- summary: summary.map(- (clusterID, value) =>- MapEntry(clusterID, mappers.toClusterSummaryRow(value)),- ),- );+ ) async {+ if (summary.isEmpty) return;+ try {+ await (await store).clusterSummaryUpdate(+ summary: summary.map(+ (clusterID, value) =>+ MapEntry(clusterID, mappers.toClusterSummaryRow(value)),+ ),+ );+ } on rust.MlDbError_Index catch (e, s) {+ _logClusterCentroidIndexWriteFailure("clusterSummaryUpdate", e, s);+ }+ }@override- Future<void> deleteClusterSummaryRow(String clusterID) async =>- (await _db).deleteClusterSummaryRow(clusterId: clusterID);+ Future<void> deleteClusterSummary(String clusterID) async {+ try {+ await (await store).deleteClusterSummary(clusterId: clusterID);+ } on rust.MlDbError_Index catch (e, s) {+ _logClusterCentroidIndexWriteFailure("deleteClusterSummary", e, s);+ }+ }++ void _logClusterCentroidIndexWriteFailure(+ String operation,+ rust.MlDbError_Index error,+ StackTrace stackTrace,+ ) {+ _logger.severe(+ "ClusterCentroidVectorDB write failed during `$operation`. The index is marked stale and refills on the next fill check.",+ error,+ stackTrace,+ );+ }@overrideFuture<Map<String, (Uint8List, int)>> getAllClusterSummary([int? minClusterSize,]) async {- final db = await _db;+ final db = await store;final rows = await db.getAllClusterSummary(minClusterSize: minClusterSize);return rows.map((clusterID, summary) =>@@ -464,7 +525,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<Map<String, (Uint8List, int)>> getClusterToClusterSummary(Iterable<String> clusterIDs,) async {- final db = await _db;+ final db = await store;final rows = await db.getClusterToClusterSummary(clusterIds: clusterIDs.toList(),);@@ -476,16 +537,21 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<Map<String, String>> getClusterIDToPersonID() async =>- (await _db).getClusterIdToPersonId();+ (await store).getClusterIdToPersonId();@override- Future<void> resetClusterTables({required bool faces}) async =>- (await _db).resetClusterTables(faces: faces);+ Future<void> dropClustersAndPersonTable({bool faces = false}) async {+ try {+ await (await store).dropClustersAndPersonTable(faces: faces);+ } catch (e, s) {+ _logger.severe("Error dropping clusters and person table", e, s);+ }+ }@overrideFuture<void> dropFacesFeedbackTables() async {try {- final db = await _db;+ final db = await store;await db.dropFacesFeedbackTables();} catch (e) {_logger.severe('Error dropping feedback tables', e);@@ -494,21 +560,21 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<List<int>> getFileIDsOfPersonID(String personID) async {- final db = await _db;+ final db = await store;final fileIDs = await db.getFileIdsOfPersonId(personId: personID);return fileIDs.inner.toList();}@overrideFuture<List<int>> getFileIDsOfClusterID(String clusterID) async {- final db = await _db;+ final db = await store;final fileIDs = await db.getFileIdsOfClusterId(clusterId: clusterID);return fileIDs.inner.toList();}@overrideFuture<Set<int>> getAllFileIDsOfFaceIDsNotInAnyCluster() async {- final db = await _db;+ final db = await store;final fileIDs = await db.getAllFileIdsOfFaceIdsNotInAnyCluster();return fileIDs.inner.toSet();}@@ -517,7 +583,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<Set<int>> getAllFilesAssociatedWithAllClusters({List<String>? exceptClusters,}) async {- final db = await _db;+ final db = await store;final fileIDs = await db.getAllFilesAssociatedWithAllClusters(exceptClusters: exceptClusters,);@@ -526,107 +592,85 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<List<EmbeddingVector>> getAllClipVectors() async {- final db = await _db;+ final db = await store;final rows = await db.getAllClipVectors();return rows.where(- (row) =>- row.embedding.length == UsearchClipVectorDB.embeddingDimensions,+ (row) => row.embedding.length == ClipVectorDB.embeddingDimensions,).map(mappers.toEmbeddingVector).toList();}- @override- Future<int> countClusterSummaries() async =>- (await _db).countClusterSummaries();-- @override- Future<List<(String, Uint8List)>> getClusterSummaryPage({- String? beforeClusterID,- required int limit,- }) async {- final db = await _db;- final rows = await db.getClusterSummaryPage(- beforeClusterId: beforeClusterID,- limit: limit,- );- return [for (final row in rows) (row.clusterId, row.avg)];- }-- @override- Future<int> countClipRows() async => (await _db).countClipRows();-- @override- Future<List<(int, Uint8List)>> getClipRowsPage({- required int limit,- required int offset,- }) async {- final db = await _db;- final rows = await db.getClipRowsPage(limit: limit, offset: offset);- return [for (final row in rows) (row.fileId, row.embedding)];- }-@overrideFuture<Map<int, int>> clipIndexedFileWithVersion() async =>- (await _db).clipIndexedFileWithVersion();+ (await store).clipIndexedFileWithVersion();@overrideFuture<int> getClipIndexedFileCount({int minimumMlVersion = clipMlVersion,}) async =>- (await _db).getClipIndexedFileCount(minimumMlVersion: minimumMlVersion);+ (await store).getClipIndexedFileCount(minimumMlVersion: minimumMlVersion);@overrideFuture<int> getClipVectorizableFileCount({int minimumMlVersion = clipMlVersion,- }) async => (await _db).getClipVectorizableFileCount(+ }) async => (await store).getClipVectorizableFileCount(minimumMlVersion: minimumMlVersion,);@overrideFuture<Map<int, int>> petIndexedFileIds({int minimumMlVersion = petMlVersion,- }) async => (await _db).petIndexedFileIds(minimumMlVersion: minimumMlVersion);+ }) async =>+ (await store).petIndexedFileIds(minimumMlVersion: minimumMlVersion);@overrideFuture<Set<int>> getFullyIndexedFileIds({required bool includePets}) async {- final db = await _db;+ final db = await store;final fileIDs = await db.getFullyIndexedFileIds(includePets: includePets);return fileIDs.inner.toSet();}@override- Future<void> insertClipRows(List<ClipEmbedding> embeddings) async =>- (await _db).insertClipRows(- embeddings: embeddings.map(mappers.toClipEmbeddingRow).toList(),- );+ Future<void> putClip(List<ClipEmbedding> embeddings) async {+ if (embeddings.isEmpty) return;+ await (await store).putClip(+ embeddings: embeddings.map(mappers.toClipEmbeddingRow).toList(),+ );+ Bus.instance.fire(EmbeddingUpdatedEvent());+ }++ @override+ Future<void> deleteClipEmbeddings(List<int> fileIDs) async {+ await (await store).deleteClip(fileIds: Int64List.fromList(fileIDs));+ Bus.instance.fire(EmbeddingUpdatedEvent());+ }++ @override+ Future<void> deleteClipIndexes() async {+ await (await store).deleteAllClip();+ Bus.instance.fire(EmbeddingUpdatedEvent());+ }@overrideFuture<void> putRepeatedTextEmbeddingCache(String query,List<double> embedding,- ) async => (await _db).putRepeatedTextEmbeddingCache(+ ) async => (await store).putRepeatedTextEmbeddingCache(query: query,embedding: embedding,);@overrideFuture<List<double>?> getRepeatedTextEmbeddingCache(String query) async =>- (await _db).getRepeatedTextEmbeddingCache(query: query);-- @override- Future<void> deleteClipRows(List<int> fileIDs) async =>- (await _db).deleteClipRows(fileIds: Int64List.fromList(fileIDs));-- @override- Future<void> deleteAllClipRows() async => (await _db).deleteAllClipRows();+ (await store).getRepeatedTextEmbeddingCache(query: query);@overrideFuture<void> putFaceIdCachedForPersonOrCluster(String personOrClusterId,String faceID,- ) async => (await _db).putFaceIdCachedForPersonOrCluster(+ ) async => (await store).putFaceIdCachedForPersonOrCluster(personOrClusterId: personOrClusterId,faceId: faceID,);@@ -634,30 +678,30 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<String?> getFaceIdUsedForPersonOrCluster(String personOrClusterId,- ) async => (await _db).getFaceIdUsedForPersonOrCluster(+ ) async => (await store).getFaceIdUsedForPersonOrCluster(personOrClusterId: personOrClusterId,);@overrideFuture<void> removeFaceIdCachedForPersonOrCluster(String personOrClusterID,- ) async => (await _db).removeFaceIdCachedForPersonOrCluster(+ ) async => (await store).removeFaceIdCachedForPersonOrCluster(personOrClusterId: personOrClusterID,);@overrideFuture<Set<String>> getClustersForMemoryLane(Set<String> assigned) async =>- (await _db).getClustersForMemoryLane(assigned: assigned);+ (await store).getClustersForMemoryLane(assigned: assigned);@overrideFuture<void> putFDStatus(List<FDStatus> fdStatusList) async =>- (await _db).putFdStatus(+ (await store).putFdStatus(fdStatusList: fdStatusList.map(mappers.toFdStatusRow).toList(),);@overrideFuture<Map<int, PreviewInfo>> getFileIDsVidPreview() async {- final db = await _db;+ final db = await store;final rows = await db.getFileIdsVidPreview();return rows.map((fileID, info) => MapEntry(fileID, mappers.toPreviewInfo(info)),@@ -666,7 +710,7 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {@overrideFuture<Set<int>> getFileIDsWithFDData({DataType? type}) async {- final db = await _db;+ final db = await store;final fileIDs = await db.getFileIdsWithFdData(dataType: type?.toJson());return fileIDs.inner.toSet();}diff --git a/mobile/apps/photos/lib/db/ml/store_clip_vector_db.dart b/mobile/apps/photos/lib/db/ml/store_clip_vector_db.dartnew file mode 100644index 0000000000..d612370a96--- /dev/null+++ b/mobile/apps/photos/lib/db/ml/store_clip_vector_db.dart@@ -0,0 +1,105 @@+import "package:collection/collection.dart";+import "package:logging/logging.dart";+import "package:photos/db/ml/clip_vector_db.dart";+import "package:photos/db/ml/rust_db.dart";+import "package:photos/services/machine_learning/semantic_search/query_result.dart";+import "package:photos/src/rust/api/ml_db_api.dart" as rust;++class StoreClipVectorDB implements ClipVectorDB {+ static final Logger _logger = Logger("StoreClipVectorDB");+ static const int _bulkSearchBatchSize = 500;++ final RustMLDataDB _mlDataDB;++ StoreClipVectorDB._(this._mlDataDB);++ static final instance = StoreClipVectorDB._(RustMLDataDB.instance);+ static final localGalleryInstance = StoreClipVectorDB._(+ RustMLDataDB.localGalleryInstance,+ );++ @override+ Future<bool> isReady() async =>+ await _mlDataDB.fillState(rust.Index.clip) == rust.FillState.filled;++ @override+ Future<void> warmup() async {+ final stopwatch = Stopwatch()..start();+ final stats = await (await _mlDataDB.store).stats(index: rust.Index.clip);+ _logger.info(+ "Clip vector index warmed up with ${stats.liveCount} vectors in ${stopwatch.elapsedMilliseconds} ms",+ );+ }++ @override+ Future<List<QueryResult>> searchSimilaritiesWithinThreshold(+ List<double> query,+ double minimumSimilarity,+ ) async {+ if (!await isReady()) {+ throw StateError(+ "ClipVectorDB index is not filled, cannot run similarity search",+ );+ }+ final matches = await (await _mlDataDB.store).search(+ index: rust.Index.clip,+ query: query,+ limit: null,+ maxDistance: 1.0 - minimumSimilarity,+ exact: true,+ allowedKeys: null,+ );+ return [+ for (final match in matches)+ QueryResult(int.parse(match.key), 1.0 - match.distance),+ ];+ }++ @override+ Future<Map<int, List<(int, double)>>> bulkSearchNearestForFiles(+ List<int> fileIDs, {+ required int count,+ required double maxDistance,+ required bool exact,+ }) async {+ if (fileIDs.isEmpty) return {};+ final store = await _mlDataDB.store;+ final nearest = <int, List<(int, double)>>{};+ for (final batch in fileIDs.slices(_bulkSearchBatchSize)) {+ nearest.addAll(+ await _bulkSearchStoredBatch(+ store,+ batch,+ count: count,+ maxDistance: maxDistance,+ exact: exact,+ ),+ );+ }+ return nearest;+ }++ Future<Map<int, List<(int, double)>>> _bulkSearchStoredBatch(+ rust.MlStore store,+ List<int> fileIDs, {+ required int count,+ required double maxDistance,+ required bool exact,+ }) async {+ final results = await store.bulkSearchStored(+ index: rust.Index.clip,+ keys: [for (final fileID in fileIDs) fileID.toString()],+ count: count,+ maxDistance: maxDistance,+ exact: exact,+ restrictToInput: false,+ );+ return {+ for (final result in results)+ int.parse(result.key): [+ for (final match in result.matches)+ (int.parse(match.key), match.distance),+ ],+ };+ }+}diff --git a/mobile/apps/photos/lib/db/ml/store_cluster_centroid_vector_db.dart b/mobile/apps/photos/lib/db/ml/store_cluster_centroid_vector_db.dartnew file mode 100644index 0000000000..c98e089ae9--- /dev/null+++ b/mobile/apps/photos/lib/db/ml/store_cluster_centroid_vector_db.dart@@ -0,0 +1,108 @@+import "dart:typed_data" show Float32List;++import "package:logging/logging.dart";+import "package:photos/db/ml/cluster_centroid_vector_db.dart";+import "package:photos/db/ml/rust_db.dart";+import "package:photos/src/rust/api/ml_db_api.dart" as rust;++class StoreClusterCentroidVectorDB implements ClusterCentroidVectorDB {+ static final Logger _logger = Logger("StoreClusterCentroidVectorDB");++ final RustMLDataDB _mlDataDB;++ StoreClusterCentroidVectorDB._(this._mlDataDB);++ static final instance = StoreClusterCentroidVectorDB._(RustMLDataDB.instance);+ static final localGalleryInstance = StoreClusterCentroidVectorDB._(+ RustMLDataDB.localGalleryInstance,+ );++ @override+ Future<bool> isReady() async =>+ await _mlDataDB.fillState(rust.Index.clusterCentroid) ==+ rust.FillState.filled;++ @override+ Future<void> warmup() async {+ final stopwatch = Stopwatch()..start();+ final stats = await (await _mlDataDB.store).stats(+ index: rust.Index.clusterCentroid,+ );+ _logger.info(+ "Cluster centroid vector index warmed up with ${stats.liveCount} vectors in ${stopwatch.elapsedMilliseconds} ms",+ );+ }++ Future<void> _ensureReady() async {+ if (!await isReady()) {+ throw StateError(+ "Cluster centroid vector index is not filled, cannot run search",+ );+ }+ }++ @override+ Future<List<List<(String, double)>>> bulkSearchNearestAllowed(+ List<List<double>> queries,+ Set<String> allowedClusterIDs, {+ required double maxDistance,+ int count = 10,+ }) async {+ await _ensureReady();+ if (count <= 0 || queries.isEmpty || allowedClusterIDs.isEmpty) {+ return List.generate(queries.length, (_) => const <(String, double)>[]);+ }+ final matchesPerQuery = await (await _mlDataDB.store).bulkSearch(+ index: rust.Index.clusterCentroid,+ queries: [+ for (final query in queries)+ query is Float32List ? query : Float32List.fromList(query),+ ],+ limit: count,+ maxDistance: maxDistance,+ exact: false,+ allowedKeys: allowedClusterIDs.toList(),+ );+ return [+ for (var i = 0; i < queries.length; i++)+ i < matchesPerQuery.length+ ? [+ for (final match in matchesPerQuery[i])+ (match.key, match.distance),+ ]+ : const <(String, double)>[],+ ];+ }++ @override+ Future<Map<String, ClusterCentroidMatches>> bulkSearchNearestForClusters(+ Set<String> queryClusterIDs, {+ required Set<String> candidateClusterIDs,+ required int count,+ double? maxDistance,+ }) async {+ await _ensureReady();+ if (count <= 0 || queryClusterIDs.isEmpty) {+ return const {};+ }+ final results = await (await _mlDataDB.store).bulkSearchStored(+ index: rust.Index.clusterCentroid,+ keys: queryClusterIDs.toList(),+ count: count,+ maxDistance: maxDistance,+ exact: false,+ restrictToInput: false,+ );+ return {+ for (final result in results)+ result.key: (+ matches: [+ for (final match in result.matches)+ if (candidateClusterIDs.contains(match.key))+ (match.key, match.distance),+ ],+ reachedCount: result.matches.length >= count,+ ),+ };+ }+}diff --git a/mobile/apps/photos/lib/db/ml/usearch_clip_vector_db.dart b/mobile/apps/photos/lib/db/ml/usearch_clip_vector_db.dartindex 017360806b..f945c97283 100644--- a/mobile/apps/photos/lib/db/ml/usearch_clip_vector_db.dart+++ b/mobile/apps/photos/lib/db/ml/usearch_clip_vector_db.dart@@ -14,13 +14,13 @@ import "package:synchronized/synchronized.dart";class UsearchClipVectorDB implements ClipVectorDB {static final Logger _logger = Logger("UsearchClipVectorDB");- static const int embeddingDimensions = 512;- static const int embeddingBytesLength = embeddingDimensions * 4;final String _databaseName;final String _migrationKey;- static final BigInt _embeddingDimension = BigInt.from(embeddingDimensions);+ static final BigInt _embeddingDimension = BigInt.from(+ ClipVectorDB.embeddingDimensions,+ );static Logger get logger => _logger;diff --git a/mobile/apps/photos/lib/db/ml/usearch_cluster_centroid_vector_db.dart b/mobile/apps/photos/lib/db/ml/usearch_cluster_centroid_vector_db.dartindex 1ba2a58bda..93b3cc2874 100644--- a/mobile/apps/photos/lib/db/ml/usearch_cluster_centroid_vector_db.dart+++ b/mobile/apps/photos/lib/db/ml/usearch_cluster_centroid_vector_db.dart@@ -5,9 +5,8 @@ import "package:flutter_rust_bridge/flutter_rust_bridge.dart" show Uint64List;import "package:logging/logging.dart";import "package:path/path.dart";import "package:path_provider/path_provider.dart";-import "package:photos/db/ml/base.dart";import "package:photos/db/ml/cluster_centroid_vector_db.dart";-import "package:photos/db/ml/db.dart";+import "package:photos/db/ml/dart_db.dart";import "package:photos/src/rust/api/usearch_api.dart";import "package:shared_preferences/shared_preferences.dart";import "package:synchronized/synchronized.dart";@@ -19,8 +18,9 @@ class UsearchClusterCentroidVectorDB implements ClusterCentroidVectorDB {final String _migrationKey;final bool _localGallery;- static const int embeddingDimensions = 192;- static final BigInt _embeddingDimension = BigInt.from(embeddingDimensions);+ static final BigInt _embeddingDimension = BigInt.from(+ ClusterCentroidVectorDB.embeddingDimensions,+ );static Logger get logger => _logger;@@ -44,8 +44,8 @@ class UsearchClusterCentroidVectorDB implements ClusterCentroidVectorDB {factory UsearchClusterCentroidVectorDB() => instance;- IMLDataDB<int> get _mlDataDB =>- _localGallery ? MLDataDB.localGalleryInstance : MLDataDB.instance;+ DartMLDataDB get _mlDataDB =>+ _localGallery ? DartMLDataDB.localGalleryInstance : DartMLDataDB.instance;Future<VectorDb>? _vectorDbFuture;Future<void>? _warmupFuture;diff --git a/mobile/apps/photos/lib/services/app_lifecycle_service.dart b/mobile/apps/photos/lib/services/app_lifecycle_service.dartindex 7b6e129924..0503b4813e 100644--- a/mobile/apps/photos/lib/services/app_lifecycle_service.dart+++ b/mobile/apps/photos/lib/services/app_lifecycle_service.dart@@ -1,5 +1,8 @@+import "dart:async";+import 'package:logging/logging.dart';import 'package:media_extension/media_extension_action_types.dart';+import "package:photos/db/ml/db.dart";import "package:shared_preferences/shared_preferences.dart";class AppLifecycleService {@@ -43,6 +46,7 @@ class AppLifecycleService {_logger.info("App already in background, skipping open time update");}isForeground = false;+ unawaited(MLDataDB.releaseVectorIndexes());}int getLastAppOpenTime() {diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dartindex 9d1c658628..c449a9725e 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart@@ -5,6 +5,7 @@ import "package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart"show Uint64List;import "package:logging/logging.dart";import "package:photos/core/errors.dart";+import "package:photos/db/ml/db.dart";import "package:photos/models/ml/vector.dart";import "package:photos/services/machine_learning/ml_constants.dart";import "package:photos/services/machine_learning/ml_model_assets.dart";@@ -58,10 +59,13 @@ class MLComputer extends SuperIsolate {required bool exact,}) async {try {- final result = await runInIsolate(- IsolateOperation.bulkVectorSearchWithKeys,- {"fileIDs": fileIDs, "maxDistance": maxDistance, "exact": exact},- );+ final result =+ await runInIsolate(IsolateOperation.bulkVectorSearchWithKeys, {+ "fileIDs": fileIDs,+ "maxDistance": maxDistance,+ "exact": exact,+ "rustMlDb": MLDataDB.isRustBackend,+ });return result;} catch (e, s) {_logger.severe("Could not run bulk vector search in MLComputer", e, s);diff --git a/mobile/apps/photos/lib/ui/settings/debug/ml_debug_settings_page.dart b/mobile/apps/photos/lib/ui/settings/debug/ml_debug_settings_page.dartindex 8c441ecde5..ce89f15e72 100644--- a/mobile/apps/photos/lib/ui/settings/debug/ml_debug_settings_page.dart+++ b/mobile/apps/photos/lib/ui/settings/debug/ml_debug_settings_page.dart@@ -8,6 +8,7 @@ import "package:photos/core/event_bus.dart";import "package:photos/db/ml/clip_vector_db.dart";import "package:photos/db/ml/cluster_centroid_vector_db.dart";import "package:photos/db/ml/db.dart";+import "package:photos/db/ml/rust_db.dart";import "package:photos/db/ml/usearch_clip_vector_db.dart";import "package:photos/events/people_changed_event.dart";import "package:photos/models/ml/face/person.dart";@@ -20,6 +21,7 @@ import "package:photos/services/machine_learning/ml_process_lock.dart";import "package:photos/services/machine_learning/ml_run_control.dart";import "package:photos/services/machine_learning/ml_service.dart";import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";+import "package:photos/src/rust/api/ml_db_api.dart" as rust;import "package:photos/theme/ente_theme.dart";import "package:photos/ui/components/menu_item_widget/menu_item_widget_new.dart";import "package:photos/ui/components/settings/settings_grouped_card.dart";@@ -29,6 +31,8 @@ import "package:photos/ui/settings/debug/memories_debug_page.dart";import "package:photos/utils/dialog_util.dart";import "package:photos/utils/ml_util.dart";+typedef _VectorIndexStatus = ({String label, bool ready});+class MLDebugSettingsPage extends StatefulWidget {const MLDebugSettingsPage({super.key});@@ -319,7 +323,9 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {}Widget _buildVectorDbMigrationCard(BuildContext context) {- return FutureBuilder<({bool clipDone, bool clusterCentroidDone})>(+ return FutureBuilder<+ ({_VectorIndexStatus clip, _VectorIndexStatus clusterCentroid})+ >(future: _getVectorDbMigrationStatus(),builder: (context, snapshot) {final status = snapshot.data;@@ -333,7 +339,7 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {),trailingWidget: _buildMigrationStatusWidget(context,- isDone: status?.clipDone,+ status: status?.clip,hasError: snapshot.hasError,),),@@ -345,7 +351,7 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {),trailingWidget: _buildMigrationStatusWidget(context,- isDone: status?.clusterCentroidDone,+ status: status?.clusterCentroid,hasError: snapshot.hasError,),),@@ -371,7 +377,7 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {Widget _buildMigrationStatusWidget(BuildContext context, {- required bool? isDone,+ required _VectorIndexStatus? status,required bool hasError,}) {final colorScheme = getEnteColorScheme(context);@@ -382,22 +388,32 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {if (hasError) {label = "Error";color = colorScheme.warning500;- } else if (isDone == null) {+ } else if (status == null) {label = "Loading";color = colorScheme.textMuted;- } else if (isDone) {- label = "Done";- color = colorScheme.greenBase;} else {- label = "Pending";- color = colorScheme.warning500;+ label = status.label;+ color = status.ready ? colorScheme.greenBase : colorScheme.warning500;}return Text(label, style: textTheme.miniBold.copyWith(color: color));}- Future<({bool clipDone, bool clusterCentroidDone})>+ Future<({_VectorIndexStatus clip, _VectorIndexStatus clusterCentroid})>_getVectorDbMigrationStatus() async {+ if (MLDataDB.isRustBackend) {+ final rustDataDB = isLocalGalleryMode+ ? RustMLDataDB.localGalleryInstance+ : RustMLDataDB.instance;+ final fillStates = await Future.wait<rust.FillState>([+ rustDataDB.fillState(rust.Index.clip),+ rustDataDB.fillState(rust.Index.clusterCentroid),+ ]);+ return (+ clip: _fillStateStatus(fillStates[0]),+ clusterCentroid: _fillStateStatus(fillStates[1]),+ );+ }final clipVectorDB = isLocalGalleryMode? ClipVectorDB.localGalleryInstance: ClipVectorDB.instance;@@ -409,11 +425,23 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {clusterCentroidVectorDB.isReady(),]);return (- clipDone: migrationStatus[0],- clusterCentroidDone: migrationStatus[1],+ clip: _migrationStatus(migrationStatus[0]),+ clusterCentroid: _migrationStatus(migrationStatus[1]),);}+ static _VectorIndexStatus _migrationStatus(bool isDone) =>+ (label: isDone ? "Done" : "Pending", ready: isDone);++ static _VectorIndexStatus _fillStateStatus(rust.FillState state) => (+ label: switch (state) {+ rust.FillState.filled => "Filled",+ rust.FillState.filling => "Filling",+ rust.FillState.stale => "Stale",+ },+ ready: state == rust.FillState.filled,+ );+Widget _buildThresholdsCard(BuildContext context) {return SettingsGroupedCard(children: [@@ -633,14 +661,18 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {onTap: () async => _onResetAllLocalClip(context),),MenuItemWidgetNew(- title: "Reset USearch index",+ title: MLDataDB.isRustBackend+ ? "Rebuild clip vector index"+ : "Reset USearch index",leadingIconWidget: _buildIconWidget(context,HugeIcons.strokeRoundedAiSearch,),trailingIcon: Icons.chevron_right_outlined,trailingIconIsMuted: true,- onTap: () async => _onResetUsearchIndex(context),+ onTap: () async => MLDataDB.isRustBackend+ ? _onRebuildClipVectorIndex(context)+ : _onResetUsearchIndex(context),),],);@@ -1053,4 +1085,31 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {},);}++ Future<void> _onRebuildClipVectorIndex(BuildContext context) async {+ await showChoiceDialog(+ context,+ title: "Are you sure?",+ body:+ "This will rebuild the clip vector index from the database. Compute is paused while the rebuild runs.",+ firstButtonLabel: "Yes, confirm",+ firstButtonOnTap: () async {+ try {+ final db = isLocalGalleryMode+ ? MLDataDB.localGalleryInstance+ : MLDataDB.instance;+ await db.checkMigrateFillClipVectorDB(force: true);+ if (!context.mounted) return;+ showShortToast(context, "Done");+ if (mounted) {+ setState(() {});+ }+ } catch (e, s) {+ logger.warning('rebuild clip vector index failed ', e, s);+ if (!context.mounted) return;+ await showGenericErrorDialog(context: context, error: e);+ }+ },+ );+ }}diff --git a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dartindex f3ffe36d8f..8aa7a96963 100644--- a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart+++ b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart@@ -4,6 +4,7 @@ import "package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart"show Int64List;import "package:ml_linalg/linalg.dart";import "package:photos/db/ml/clip_vector_db.dart";+import "package:photos/db/ml/db.dart";import "package:photos/db/ml/usearch_clip_vector_db.dart";import "package:photos/models/ml/face/box.dart";import "package:photos/models/ml/vector.dart";@@ -65,16 +66,21 @@ Future<dynamic> isolateFunction(switch (function) {case IsolateOperation.bulkVectorSearchWithKeys:await _ensureRustLoaded();+ MLDataDB.initialize(preferRust: args["rustMlDb"] as bool);final fileIDs = args["fileIDs"] as List<int>;final maxDistance = args["maxDistance"] as double;final exact = args["exact"] as bool;- return ClipVectorDB.instance.bulkSearchNearestForFiles(- fileIDs,- count: 100,- maxDistance: maxDistance,- exact: exact,- );+ try {+ return await ClipVectorDB.instance.bulkSearchNearestForFiles(+ fileIDs,+ count: 100,+ maxDistance: maxDistance,+ exact: exact,+ );+ } finally {+ await MLDataDB.releaseVectorIndexes();+ }case IsolateOperation.bulkVectorSearch:await _ensureRustLoaded();diff --git a/rust/bindings/frb/photos/src/api/ml_db_api.rs b/rust/bindings/frb/photos/src/api/ml_db_api.rsindex f94352506e..6be3943bec 100644--- a/rust/bindings/frb/photos/src/api/ml_db_api.rs+++ b/rust/bindings/frb/photos/src/api/ml_db_api.rs@@ -3,8 +3,8 @@ use std::sync::OnceLock;use ente_photos::ml_db;pub use ente_photos::ml_db::{- ClipEmbedding, ClipRow, ClusterCentroidRow, ClusterSummary, EmbeddingVector,- FaceDbInfoForClustering, FaceRow, FaceWithoutEmbedding, FdStatus, PreviewInfo,+ ClipEmbedding, ClusterSummary, EmbeddingVector, FaceDbInfoForClustering, FaceRow,+ FaceWithoutEmbedding, FdStatus, PreviewInfo,};use ente_photos::ml_store;pub use ente_photos::ml_store::{FillOutcome, FillState, Index};@@ -24,6 +24,7 @@ pub fn decide_ml_db_backend(prefer_rust: bool) -> bool {#[frb]pub enum MlDbError {Downgrade { message: String },+ Index { message: String },Other { message: String },}@@ -41,6 +42,9 @@ impl From<ml_store::Error> for MlDbError {fn from(error: ml_store::Error) -> Self {match error {ml_store::Error::Database(error) => Self::from(error),+ ml_store::Error::Index(_) => Self::Index {+ message: ente_core::error::chain(&error),+ },other => Self::Other {message: ente_core::error::chain(&other),},@@ -94,24 +98,12 @@ pub struct _EmbeddingVector {pub embedding: Vec<f32>,}-#[frb(mirror(ClipRow))]-pub struct _ClipRow {- pub file_id: i64,- pub embedding: Vec<u8>,-}-#[frb(mirror(ClusterSummary))]pub struct _ClusterSummary {pub avg: Vec<u8>,pub count: i64,}-#[frb(mirror(ClusterCentroidRow))]-pub struct _ClusterCentroidRow {- pub cluster_id: String,- pub avg: Vec<u8>,-}-#[frb(mirror(FdStatus))]pub struct _FdStatus {pub file_id: i64,@@ -309,14 +301,6 @@ impl MlStore {Ok(to_api_stats(self.inner.stats(index)?))}- pub fn clear_non_pet_tables(&self) -> Result<(), MlDbError> {- Ok(self.inner.db().clear_non_pet_tables()?)- }-- pub fn clear_pet_tables(&self) -> Result<(), MlDbError> {- Ok(self.inner.db().clear_pet_tables()?)- }-pub fn bulk_insert_faces(&self, faces: Vec<FaceRow>) -> Result<(), MlDbError> {Ok(self.inner.db().bulk_insert_faces(&faces)?)}@@ -597,45 +581,6 @@ impl MlStore {Ok(self.inner.db().get_file_ids_of_cluster_id(&cluster_id)?)}- pub fn get_cluster_centroid_vector_id_map(- &self,- cluster_ids: Vec<String>,- create_if_missing: bool,- ) -> Result<HashMap<String, i64>, MlDbError> {- Ok(self- .inner- .db()- .get_cluster_centroid_vector_id_map(&cluster_ids, create_if_missing)?)- }-- pub fn delete_cluster_centroid_vector_id_mapping(- &self,- cluster_id: String,- ) -> Result<(), MlDbError> {- Ok(self- .inner- .db()- .delete_cluster_centroid_vector_id_mapping(&cluster_id)?)- }-- pub fn clear_cluster_centroid_vector_id_mappings(&self) -> Result<(), MlDbError> {- Ok(self- .inner- .db()- .clear_cluster_centroid_vector_id_mappings()?)- }-- pub fn upsert_cluster_summary_rows(- &self,- summary: HashMap<String, ClusterSummary>,- ) -> Result<(), MlDbError> {- Ok(self.inner.db().upsert_cluster_summary_rows(&summary)?)- }-- pub fn delete_cluster_summary_row(&self, cluster_id: String) -> Result<(), MlDbError> {- Ok(self.inner.db().delete_cluster_summary_row(&cluster_id)?)- }-pub fn get_all_cluster_summary(&self,min_cluster_size: Option<i64>,@@ -653,25 +598,6 @@ impl MlStore {.get_cluster_to_cluster_summary(&cluster_ids)?)}- pub fn count_cluster_summaries(&self) -> Result<i64, MlDbError> {- Ok(self.inner.db().count_cluster_summaries()?)- }-- pub fn get_cluster_summary_page(- &self,- before_cluster_id: Option<String>,- limit: i64,- ) -> Result<Vec<ClusterCentroidRow>, MlDbError> {- Ok(self- .inner- .db()- .get_cluster_summary_page(before_cluster_id.as_deref(), limit)?)- }-- pub fn reset_cluster_tables(&self, faces: bool) -> Result<(), MlDbError> {- Ok(self.inner.db().reset_cluster_tables(faces)?)- }-pub fn get_clusters_for_memory_lane(&self,assigned: HashSet<String>,@@ -869,26 +795,6 @@ impl MlStore {.get_clip_vectorizable_file_count(minimum_ml_version)?)}- pub fn insert_clip_rows(&self, embeddings: Vec<ClipEmbedding>) -> Result<(), MlDbError> {- Ok(self.inner.db().insert_clip_rows(&embeddings)?)- }-- pub fn delete_clip_rows(&self, file_ids: Vec<i64>) -> Result<(), MlDbError> {- Ok(self.inner.db().delete_clip_rows(&file_ids)?)- }-- pub fn delete_all_clip_rows(&self) -> Result<(), MlDbError> {- Ok(self.inner.db().delete_all_clip_rows()?)- }-- pub fn count_clip_rows(&self) -> Result<i64, MlDbError> {- Ok(self.inner.db().count_clip_rows()?)- }-- pub fn get_clip_rows_page(&self, limit: i64, offset: i64) -> Result<Vec<ClipRow>, MlDbError> {- Ok(self.inner.db().get_clip_rows_page(limit, offset)?)- }-pub fn pet_indexed_file_ids(&self,minimum_ml_version: i64,@@ -968,6 +874,8 @@ impl MlStore {#[cfg(test)]mod tests {+ use ente_vecdb::VecDbError;+use super::{FillOutcome, FillReport, MlDbError, ml_db, ml_store};#[test]@@ -1005,6 +913,15 @@ mod tests {));}+ #[test]+ fn store_index_errors_map_to_the_index_variant() {+ let error = MlDbError::from(ml_store::Error::Index(VecDbError::Closed));+ assert!(matches!(+ error,+ MlDbError::Index { message } if message == "vector db was deleted"+ ));+ }+#[test]fn other_store_errors_map_to_the_other_variant() {let error = MlDbError::from(ml_store::Error::InvalidArgument("bad path".into()));