π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Remove dormant pet recognition storage
laurenspriemd02bbcf50665
diff --git a/mobile/apps/photos/lib/db/ml/base.dart b/mobile/apps/photos/lib/db/ml/base.dartindex 0439b0287d..ed5bdb2607 100644--- a/mobile/apps/photos/lib/db/ml/base.dart+++ b/mobile/apps/photos/lib/db/ml/base.dart@@ -1,18 +1,14 @@import "dart:typed_data";-import "package:photos/db/ml/db_pet_model_mappers.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/vector.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_result.dart";abstract class IMLDataDB<T> {Future<void> bulkInsertFaces(List<Face> faces);- Future<void> bulkInsertPetFaces(List<DBPetFace> petFaces);- Future<void> bulkInsertPetBodies(List<DBPetBody> petBodies);Future<void> updateFaceIdToClusterId(Map<String, String> faceIDToClusterID);Future<Map<int, int>> faceIndexedFileIds({int minimumMlVersion});Future<int> getFaceIndexedFileCount({int minimumMlVersion});@@ -39,8 +35,6 @@ abstract class IMLDataDB<T> {String? clusterID,});Future<List<Face>?> getFacesForGivenFileID(T fileUploadID);- Future<List<DBPetFace>?> getPetFacesForFileID(T fileUploadID);- Future<List<DBPetBody>?> getPetBodiesForFileID(T fileUploadID);Future<Map<int, List<FaceWithoutEmbedding>>>getFileIDsToFacesWithoutEmbedding();Future<Map<String, Iterable<String>>> getClusterToFaceIDs(@@ -123,9 +117,6 @@ abstract class IMLDataDB<T> {List<String>? exceptClusters,});- Future<void> updatePetFaceVectorIds(Map<String, int> petFaceIdToVectorId);- Future<void> updatePetBodyVectorIds(Map<String, int> petBodyIdToVectorId);-Future<List<EmbeddingVector>> getAllClipVectors();Future<Map<int, int>> clipIndexedFileWithVersion();Future<int> getClipIndexedFileCount({int minimumMlVersion});@@ -134,8 +125,6 @@ abstract class IMLDataDB<T> {Future<void> deleteClipIndexes();Future<Map<int, int>> petIndexedFileIds({int minimumMlVersion});- Future<int> getPetIndexedFileCount({int minimumMlVersion});- Future<void> deletePetDataForFiles(List<int> fileIDs);Future<Set<int>> getFullyIndexedFileIds({required bool includePets});@@ -165,14 +154,6 @@ abstract class IMLDataDB<T> {List<double> embedding,);Future<void> removeFaceIdCachedForPersonOrCluster(String personOrClusterID);- Future<void> storePetBodyEmbeddings(- List<DBPetBody> dbPetBodies,- List<PetBodyResult> petBodies,- );- Future<void> storePetFaceEmbeddings(- List<DBPetFace> dbPetFaces,- List<PetFaceResult> petFaces,- );Future<void> putFDStatus(List<FDStatus> fdStatusList);Future<Map<int, PreviewInfo>> getFileIDsVidPreview();Future<Set<int>> getFileIDsWithFDData({DataType? type});@@ -192,21 +173,6 @@ abstract class IMLDataDB<T> {Future<void> insertClipRows(List<ClipEmbedding> embeddings);Future<void> deleteClipRows(List<int> fileIDs);Future<void> deleteAllClipRows();- Future<(List<(String, int?, int)>, List<(String, int?, int)>)>- getPetRowsForFiles(List<int> fileIDs);- Future<void> deletePetRowsForFiles({- required List<int> fileIDs,- required List<String> petFaceIds,- required List<String> petBodyIds,- });- Future<Map<String, int>> getPetFaceVectorIdMap(- Iterable<String> petFaceIds, {- bool createIfMissing,- });- Future<Map<String, int>> getPetBodyVectorIdMap(- Iterable<String> petBodyIds, {- bool createIfMissing,- });Future<int> countClusterSummaries();Future<List<(String, Uint8List)>> getClusterSummaryPage({String? beforeClusterID,diff --git a/mobile/apps/photos/lib/db/ml/dart_db.dart b/mobile/apps/photos/lib/db/ml/dart_db.dartindex c1143870b5..74351909b4 100644--- a/mobile/apps/photos/lib/db/ml/dart_db.dart+++ b/mobile/apps/photos/lib/db/ml/dart_db.dart@@ -11,7 +11,6 @@ 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/db_model_mappers.dart";-import "package:photos/db/ml/db_pet_model_mappers.dart";import "package:photos/db/ml/ml_data_db_orchestration.dart";import 'package:photos/db/ml/schema.dart';import "package:photos/models/ml/clip.dart";@@ -35,20 +34,17 @@ class DartMLDataDBfinal String _databaseName;final ClipVectorDB _clipVectorDB;final ClusterCentroidVectorDB _clusterCentroidVectorDB;- final bool _isLocalGallery;final List<String> _migrationScripts;DartMLDataDB._privateConstructor({String databaseName = "ente.ml.db",ClipVectorDB? clipVectorDB,ClusterCentroidVectorDB? clusterCentroidVectorDB,- bool isLocalGallery = false,List<String>? migrationScripts,}) : _databaseName = databaseName,_clipVectorDB = clipVectorDB ?? ClipVectorDB.instance,_clusterCentroidVectorDB =clusterCentroidVectorDB ?? ClusterCentroidVectorDB.instance,- _isLocalGallery = isLocalGallery,_migrationScripts = migrationScripts ?? _defaultMigrationScripts;static final DartMLDataDB instance = DartMLDataDB._privateConstructor();@@ -57,7 +53,6 @@ class DartMLDataDBdatabaseName: "ente.ml.offline.db",clipVectorDB: ClipVectorDB.localGalleryInstance,clusterCentroidVectorDB: ClusterCentroidVectorDB.localGalleryInstance,- isLocalGallery: true,migrationScripts: _localGalleryMigrationScripts,);@@ -90,9 +85,6 @@ class DartMLDataDBClusterCentroidVectorDB get clusterCentroidVectorDB =>_clusterCentroidVectorDB;- @override- bool get isLocalGallery => _isLocalGallery;-@overrideLogger get logger => _logger;@@ -171,206 +163,6 @@ class DartMLDataDB}}- @override- Future<void> bulkInsertPetFaces(List<DBPetFace> petFaces) async {- final db = await asyncDB;- const batchSize = 500;- final numBatches = (petFaces.length / batchSize).ceil();- for (int i = 0; i < numBatches; i++) {- final start = i * batchSize;- final end = min((i + 1) * batchSize, petFaces.length);- final batch = petFaces.sublist(start, end);-- const String sql =- '''- INSERT INTO $petFacesTable (- $fileIDColumn, $petFaceIDColumn, $faceDetectionColumn, $faceVectorIdColumn, $speciesColumn, $faceScore, $imageHeight, $imageWidth, $mlVersionColumn- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)- ON CONFLICT($fileIDColumn, $petFaceIDColumn) DO UPDATE SET $faceDetectionColumn = excluded.$faceDetectionColumn, $faceVectorIdColumn = excluded.$faceVectorIdColumn, $speciesColumn = excluded.$speciesColumn, $faceScore = excluded.$faceScore, $imageHeight = excluded.$imageHeight, $imageWidth = excluded.$imageWidth, $mlVersionColumn = excluded.$mlVersionColumn- ''';- final parameterSets = batch.map((petFace) {- final map = petFace.toMap();- return [- map[fileIDColumn],- map[petFaceIDColumn],- map[faceDetectionColumn],- map[faceVectorIdColumn],- map[speciesColumn],- map[faceScore],- map[imageHeight],- map[imageWidth],- map[mlVersionColumn],- ];- }).toList();-- await db.executeBatch(sql, parameterSets);- }- }-- @override- Future<void> bulkInsertPetBodies(List<DBPetBody> petBodies) async {- final db = await asyncDB;- const batchSize = 500;- final numBatches = (petBodies.length / batchSize).ceil();- for (int i = 0; i < numBatches; i++) {- final start = i * batchSize;- final end = min((i + 1) * batchSize, petBodies.length);- final batch = petBodies.sublist(start, end);-- const String sql =- '''- INSERT INTO $petBodiesTable (- $fileIDColumn, $petBodyIDColumn, $detectionColumn, $bodyVectorIdColumn, $speciesColumn, $bodyScore, $imageHeight, $imageWidth, $mlVersionColumn- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)- ON CONFLICT($fileIDColumn, $petBodyIDColumn) DO UPDATE SET $detectionColumn = excluded.$detectionColumn, $bodyVectorIdColumn = excluded.$bodyVectorIdColumn, $speciesColumn = excluded.$speciesColumn, $bodyScore = excluded.$bodyScore, $imageHeight = excluded.$imageHeight, $imageWidth = excluded.$imageWidth, $mlVersionColumn = excluded.$mlVersionColumn- ''';- final parameterSets = batch.map((obj) {- final map = obj.toMap();- return [- map[fileIDColumn],- map[petBodyIDColumn],- map[detectionColumn],- map[bodyVectorIdColumn],- map[speciesColumn],- map[bodyScore],- map[imageHeight],- map[imageWidth],- map[mlVersionColumn],- ];- }).toList();-- await db.executeBatch(sql, parameterSets);- }- }-- @override- Future<void> updatePetFaceVectorIds(- Map<String, int> petFaceIdToVectorId,- ) async {- if (petFaceIdToVectorId.isEmpty) return;- final db = await asyncDB;- const batchSize = 500;- final entries = petFaceIdToVectorId.entries.toList();- final numBatches = (entries.length / batchSize).ceil();- for (int i = 0; i < numBatches; i++) {- final start = i * batchSize;- final end = min((i + 1) * batchSize, entries.length);- final batch = entries.sublist(start, end);-- const String sql =- '''- UPDATE $petFacesTable- SET $faceVectorIdColumn = ?- WHERE $petFaceIDColumn = ?- ''';- final parameterSets = batch.map((e) => [e.value, e.key]).toList();- await db.executeBatch(sql, parameterSets);- }- }-- @override- Future<void> updatePetBodyVectorIds(- Map<String, int> petBodyIdToVectorId,- ) async {- if (petBodyIdToVectorId.isEmpty) return;- final db = await asyncDB;- const batchSize = 500;- final entries = petBodyIdToVectorId.entries.toList();- final numBatches = (entries.length / batchSize).ceil();- for (int i = 0; i < numBatches; i++) {- final start = i * batchSize;- final end = min((i + 1) * batchSize, entries.length);- final batch = entries.sublist(start, end);-- const String sql =- '''- UPDATE $petBodiesTable- SET $bodyVectorIdColumn = ?- WHERE $petBodyIDColumn = ?- ''';- final parameterSets = batch.map((e) => [e.value, e.key]).toList();- await db.executeBatch(sql, parameterSets);- }- }-- @override- Future<Map<String, int>> getPetFaceVectorIdMap(- Iterable<String> petFaceIds, {- bool createIfMissing = false,- }) async {- final uniqueIds = petFaceIds.toSet().toList(growable: false);- if (uniqueIds.isEmpty) return {};-- final db = await asyncDB;- if (createIfMissing) {- const insertSql =- '''- INSERT OR IGNORE INTO $petFaceVectorIdMappingTable ($petFaceIDColumn)- VALUES (?)- ''';- final insertParams = <List<Object?>>[];- for (final id in uniqueIds) {- insertParams.add([id]);- }- await db.executeBatch(insertSql, insertParams);- }-- final result = <String, int>{};- const chunkSize = 800;- for (int i = 0; i < uniqueIds.length; i += chunkSize) {- final chunk = uniqueIds.sublist(i, min(i + chunkSize, uniqueIds.length));- final rows = await db.getAll('''- SELECT $petFaceIDColumn, $petFaceVectorIdColumn- FROM $petFaceVectorIdMappingTable- WHERE $petFaceIDColumn IN (${List.filled(chunk.length, '?').join(',')})- ''', chunk);- for (final row in rows) {- result[row[petFaceIDColumn] as String] =- row[petFaceVectorIdColumn] as int;- }- }- return result;- }-- @override- Future<Map<String, int>> getPetBodyVectorIdMap(- Iterable<String> petBodyIds, {- bool createIfMissing = false,- }) async {- final uniqueIds = petBodyIds.toSet().toList(growable: false);- if (uniqueIds.isEmpty) return {};-- final db = await asyncDB;- if (createIfMissing) {- const insertSql =- '''- INSERT OR IGNORE INTO $petBodyVectorIdMappingTable ($petBodyIDColumn)- VALUES (?)- ''';- final insertParams = <List<Object?>>[];- for (final id in uniqueIds) {- insertParams.add([id]);- }- await db.executeBatch(insertSql, insertParams);- }-- final result = <String, int>{};- const chunkSize = 800;- for (int i = 0; i < uniqueIds.length; i += chunkSize) {- final chunk = uniqueIds.sublist(i, min(i + chunkSize, uniqueIds.length));- final rows = await db.getAll('''- SELECT $petBodyIDColumn, $petBodyVectorIdColumn- FROM $petBodyVectorIdMappingTable- WHERE $petBodyIDColumn IN (${List.filled(chunk.length, '?').join(',')})- ''', chunk);- for (final row in rows) {- result[row[petBodyIDColumn] as String] =- row[petBodyVectorIdColumn] as int;- }- }- return result;- }-@overrideFuture<void> updateFaceIdToClusterId(Map<String, String> faceIDToClusterID,@@ -722,40 +514,6 @@ class DartMLDataDBreturn maps.map((e) => mapRowToFace(e)).toList();}- @override- Future<List<DBPetFace>?> getPetFacesForFileID(int fileUploadID) async {- final db = await asyncDB;- const String query =- '''- SELECT * FROM $petFacesTable- WHERE $fileIDColumn = ? AND $speciesColumn != -1- ''';- final List<Map<String, dynamic>> maps = await db.getAll(query, [- fileUploadID,- ]);- if (maps.isEmpty) {- return null;- }- return maps.map((e) => DBPetFace.fromMap(e)).toList();- }-- @override- Future<List<DBPetBody>?> getPetBodiesForFileID(int fileUploadID) async {- final db = await asyncDB;- const String query =- '''- SELECT * FROM $petBodiesTable- WHERE $fileIDColumn = ? AND $speciesColumn != -1- ''';- final List<Map<String, dynamic>> maps = await db.getAll(query, [- fileUploadID,- ]);- if (maps.isEmpty) {- return null;- }- return maps.map((e) => DBPetBody.fromMap(e)).toList();- }-@overrideFuture<Map<int, List<FaceWithoutEmbedding>>>getFileIDsToFacesWithoutEmbedding() async {@@ -1860,19 +1618,6 @@ class DartMLDataDBreturn result;}- @override- Future<int> getPetIndexedFileCount({- int minimumMlVersion = petMlVersion,- }) async {- final db = await asyncDB;- const String query =- 'SELECT COUNT(DISTINCT $fileIDColumn) as count FROM $petFacesTable WHERE $mlVersionColumn >= ?';- final List<Map<String, dynamic>> maps = await db.getAll(query, [- minimumMlVersion,- ]);- return maps.first['count'] as int;- }-@overrideFuture<Set<int>> getFullyIndexedFileIds({required bool includePets}) async {final db = await asyncDB;@@ -1889,79 +1634,6 @@ class DartMLDataDBreturn {for (final map in maps) map[fileIDColumn] as int};}- @override- Future<(List<(String, int?, int)>, List<(String, int?, int)>)>- getPetRowsForFiles(List<int> fileIDs) async {- final db = await asyncDB;- final placeholders = List.filled(fileIDs.length, '?').join(', ');-- final faceRows = await db.getAll(- 'SELECT $petFaceIDColumn, $faceVectorIdColumn, $speciesColumn '- 'FROM $petFacesTable WHERE $fileIDColumn IN ($placeholders)',- fileIDs,- );- final bodyRows = await db.getAll(- 'SELECT $petBodyIDColumn, $bodyVectorIdColumn, $speciesColumn '- 'FROM $petBodiesTable WHERE $fileIDColumn IN ($placeholders)',- fileIDs,- );- return (- [- for (final row in faceRows)- (- row[petFaceIDColumn] as String,- row[faceVectorIdColumn] as int?,- row[speciesColumn] as int,- ),- ],- [- for (final row in bodyRows)- (- row[petBodyIDColumn] as String,- row[bodyVectorIdColumn] as int?,- row[speciesColumn] as int,- ),- ],- );- }-- @override- Future<void> deletePetRowsForFiles({- required List<int> fileIDs,- required List<String> petFaceIds,- required List<String> petBodyIds,- }) async {- final db = await asyncDB;- final placeholders = List.filled(fileIDs.length, '?').join(', ');-- await db.writeTransaction((tx) async {- if (petFaceIds.isNotEmpty) {- final placeholders = List.filled(petFaceIds.length, '?').join(',');- await tx.execute(- 'DELETE FROM $petFaceVectorIdMappingTable '- 'WHERE $petFaceIDColumn IN ($placeholders)',- petFaceIds,- );- }- if (petBodyIds.isNotEmpty) {- final placeholders = List.filled(petBodyIds.length, '?').join(',');- await tx.execute(- 'DELETE FROM $petBodyVectorIdMappingTable '- 'WHERE $petBodyIDColumn IN ($placeholders)',- petBodyIds,- );- }- await tx.execute(- 'DELETE FROM $petFacesTable WHERE $fileIDColumn IN ($placeholders)',- fileIDs,- );- await tx.execute(- 'DELETE FROM $petBodiesTable WHERE $fileIDColumn IN ($placeholders)',- fileIDs,- );- });- }-@overrideFuture<void> insertClipRows(List<ClipEmbedding> embeddings) async {final db = await asyncDB;diff --git a/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dart b/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dartdeleted file mode 100644index 598735429c..0000000000--- a/mobile/apps/photos/lib/db/ml/db_pet_model_mappers.dart+++ /dev/null@@ -1,124 +0,0 @@-import "package:ente_pure_utils/ente_pure_utils.dart";-import "package:photos/db/ml/schema.dart";-import "package:photos/models/ml/ml_versions.dart";--class DBPetFace {- final int fileId;- final String petFaceId;- final String detection;- final int? faceVectorId;- final int species;- final double faceScore;- final int imageHeight;- final int imageWidth;- final int mlVersion;-- DBPetFace({- required this.fileId,- required this.petFaceId,- required this.detection,- required this.faceVectorId,- required this.species,- required this.faceScore,- required this.imageHeight,- required this.imageWidth,- required this.mlVersion,- });-- Map<String, dynamic> toMap() {- return {- fileIDColumn: fileId,- petFaceIDColumn: petFaceId,- faceDetectionColumn: detection,- faceVectorIdColumn: faceVectorId,- speciesColumn: species,- // Use string literals for keys that collide with instance member names.- 'score': faceScore,- 'height': imageHeight,- 'width': imageWidth,- mlVersionColumn: mlVersion,- };- }-- // Sentinel rows mark files with no pet results.- factory DBPetFace.empty(int fileId, {bool error = false}) {- return DBPetFace(- fileId: fileId,- petFaceId: '${fileId}_pet_0_0_0_0',- detection: '{}',- faceVectorId: null,- species: -1,- faceScore: error ? -1.0 : 0.0,- imageHeight: 0,- imageWidth: 0,- mlVersion: petMlVersion,- );- }-- factory DBPetFace.fromMap(Map<String, dynamic> map) {- return DBPetFace(- fileId: map[fileIDColumn] as int,- petFaceId: map[petFaceIDColumn] as String,- detection: map[faceDetectionColumn] as String,- faceVectorId: map[faceVectorIdColumn] as int?,- species: map[speciesColumn] as int,- faceScore: parseIntOrDoubleAsDouble(map['score']) ?? 0.0,- imageHeight: map['height'] as int,- imageWidth: map['width'] as int,- mlVersion: map[mlVersionColumn] as int,- );- }-}--class DBPetBody {- final int fileId;- final String petBodyId;- final String detection;- final int? bodyVectorId;- final int species;- final double score;- final int imageHeight;- final int imageWidth;- final int mlVersion;-- DBPetBody({- required this.fileId,- required this.petBodyId,- required this.detection,- required this.bodyVectorId,- required this.species,- required this.score,- required this.imageHeight,- required this.imageWidth,- required this.mlVersion,- });-- Map<String, dynamic> toMap() {- return {- fileIDColumn: fileId,- petBodyIDColumn: petBodyId,- detectionColumn: detection,- bodyVectorIdColumn: bodyVectorId,- speciesColumn: species,- // Use string literals for keys that collide with instance member names.- 'score': score,- 'height': imageHeight,- 'width': imageWidth,- mlVersionColumn: mlVersion,- };- }-- factory DBPetBody.fromMap(Map<String, dynamic> map) {- return DBPetBody(- fileId: map[fileIDColumn] as int,- petBodyId: map[petBodyIDColumn] as String,- detection: map[detectionColumn] as String,- bodyVectorId: map[bodyVectorIdColumn] as int?,- species: map[speciesColumn] as int,- score: parseIntOrDoubleAsDouble(map['score']) ?? 0.0,- imageHeight: map['height'] as int,- imageWidth: map['width'] as int,- mlVersion: map[mlVersionColumn] as int,- );- }-}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 1a793a59ae..dbb7ce73c3 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@@ -6,8 +6,6 @@ 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/db_pet_model_mappers.dart";-import "package:photos/db/ml/pet_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;@@ -15,7 +13,6 @@ import "package:photos/models/ml/clip.dart";import "package:photos/service_locator.dart";import "package:photos/services/machine_learning/compute_controller.dart";import "package:photos/services/machine_learning/ml_process_lock.dart";-import "package:photos/services/machine_learning/ml_result.dart";import "package:synchronized/synchronized.dart";mixin MLDataDBOrchestration implements IMLDataDB<int> {@@ -33,129 +30,8 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {ClipVectorDB get clipVectorDB;ClusterCentroidVectorDB get clusterCentroidVectorDB;- bool get isLocalGallery;Logger get logger;- @override- Future<void> storePetFaceEmbeddings(- List<DBPetFace> dbPetFaces,- List<PetFaceResult> petFaces,- ) async {- if (dbPetFaces.length != petFaces.length) {- throw StateError(- 'dbPetFaces.length (${dbPetFaces.length}) != petFaces.length (${petFaces.length})',- );- }- try {- final bySpecies = <int, List<(DBPetFace, PetFaceResult)>>{};- for (int i = 0; i < dbPetFaces.length; i++) {- final species = petFaces[i].species;- bySpecies.putIfAbsent(species, () => []);- bySpecies[species]!.add((dbPetFaces[i], petFaces[i]));- }- for (final entry in bySpecies.entries) {- final vdb = PetVectorDB.forModel(- species: entry.key,- isFace: true,- localGallery: isLocalGallery,- );- final petFaceIds = entry.value.map((e) => e.$1.petFaceId).toList();- final idMap = await getPetFaceVectorIdMap(- petFaceIds,- createIfMissing: true,- );- final vectorIds = <int>[];- final embeddings = <Float32List>[];- final insertedPetFaceIds = <String>[];- for (final (dbFace, pfResult) in entry.value) {- final vid = idMap[dbFace.petFaceId];- if (vid == null) continue;- final emb = Float32List.fromList(pfResult.embedding);- if (emb.length != PetVectorDB.faceDimension) {- logger.warning(- "Skipping pet face embedding with wrong dimension ${emb.length}",- );- continue;- }- vectorIds.add(vid);- embeddings.add(emb);- insertedPetFaceIds.add(dbFace.petFaceId);- }- if (vectorIds.isNotEmpty) {- await vdb.bulkInsertEmbeddings(- vectorIds: vectorIds,- embeddings: embeddings,- );- final updateMap = Map.fromIterables(insertedPetFaceIds, vectorIds);- await updatePetFaceVectorIds(updateMap);- }- }- } catch (e, s) {- logger.severe("Failed to store pet face embeddings in vector DB", e, s);- rethrow;- }- }-- @override- Future<void> storePetBodyEmbeddings(- List<DBPetBody> dbPetBodies,- List<PetBodyResult> petBodies,- ) async {- if (dbPetBodies.length != petBodies.length) {- throw StateError(- 'dbPetBodies.length (${dbPetBodies.length}) != petBodies.length (${petBodies.length})',- );- }- try {- final bySpecies = <int, List<(DBPetBody, PetBodyResult)>>{};- for (int i = 0; i < dbPetBodies.length; i++) {- final species = dbPetBodies[i].species;- bySpecies.putIfAbsent(species, () => []);- bySpecies[species]!.add((dbPetBodies[i], petBodies[i]));- }- for (final entry in bySpecies.entries) {- final vdb = PetVectorDB.forModel(- species: entry.key,- isFace: false,- localGallery: isLocalGallery,- );- final bodyIds = entry.value.map((e) => e.$1.petBodyId).toList();- final idMap = await getPetBodyVectorIdMap(- bodyIds,- createIfMissing: true,- );- final vectorIds = <int>[];- final embeddings = <Float32List>[];- final insertedBodyIds = <String>[];- for (final (dbBody, bodyResult) in entry.value) {- final vid = idMap[dbBody.petBodyId];- if (vid == null) continue;- final emb = Float32List.fromList(bodyResult.embedding);- if (emb.length != PetVectorDB.bodyDimension) {- logger.warning(- "Skipping pet body embedding with wrong dimension ${emb.length}",- );- continue;- }- vectorIds.add(vid);- embeddings.add(emb);- insertedBodyIds.add(dbBody.petBodyId);- }- if (vectorIds.isNotEmpty) {- await vdb.bulkInsertEmbeddings(- vectorIds: vectorIds,- embeddings: embeddings,- );- final updateMap = Map.fromIterables(insertedBodyIds, vectorIds);- await updatePetBodyVectorIds(updateMap);- }- }- } catch (e, s) {- logger.severe("Failed to store pet body embeddings in vector DB", e, s);- rethrow;- }- }-@overrideFuture<void> clearTable() =>_runMlOperationExclusive(MlOperation.clearData, _clearTable);@@ -165,12 +41,6 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {await clipVectorDB.deleteIndexFile();await clusterCentroidVectorDB.deleteIndexFile();await clearPetTables();- final petVdbs = isLocalGallery- ? PetVectorDB.allLocalGalleryInstances- : PetVectorDB.allInstances;- for (final vdb in petVdbs) {- await vdb.deleteIndexFile();- }_markClusterSummaryMutated();}@@ -863,63 +733,6 @@ mixin MLDataDBOrchestration implements IMLDataDB<int> {}}- @override- Future<void> deletePetDataForFiles(List<int> fileIDs) async {- if (fileIDs.isEmpty) return;- final (faceRows, bodyRows) = await getPetRowsForFiles(fileIDs);-- final faceVidsBySpecies = <int, List<int>>{};- final faceIdsToRemove = <String>[];- for (final (petFaceId, vid, species) in faceRows) {- faceIdsToRemove.add(petFaceId);- if (vid != null) {- faceVidsBySpecies.putIfAbsent(species, () => []);- faceVidsBySpecies[species]!.add(vid);- }- }-- final bodyVidsBySpecies = <int, List<int>>{};- final bodyIdsToRemove = <String>[];- for (final (petBodyId, vid, species) in bodyRows) {- bodyIdsToRemove.add(petBodyId);- if (vid != null) {- bodyVidsBySpecies.putIfAbsent(species, () => []);- bodyVidsBySpecies[species]!.add(vid);- }- }-- for (final entry in faceVidsBySpecies.entries) {- try {- final vdb = PetVectorDB.forModel(- species: entry.key,- isFace: true,- localGallery: isLocalGallery,- );- await vdb.deleteEmbeddings(entry.value);- } catch (e, s) {- logger.warning("Failed to delete pet face vectors", e, s);- }- }- for (final entry in bodyVidsBySpecies.entries) {- try {- final vdb = PetVectorDB.forModel(- species: entry.key,- isFace: false,- localGallery: isLocalGallery,- );- await vdb.deleteEmbeddings(entry.value);- } catch (e, s) {- logger.warning("Failed to delete pet body vectors", e, s);- }- }-- await deletePetRowsForFiles(- fileIDs: fileIDs,- petFaceIds: faceIdsToRemove,- petBodyIds: bodyIdsToRemove,- );- }-@overrideFuture<void> putClip(List<ClipEmbedding> embeddings) async {if (embeddings.isEmpty) return;diff --git a/mobile/apps/photos/lib/db/ml/pet_vector_db.dart b/mobile/apps/photos/lib/db/ml/pet_vector_db.dartdeleted file mode 100644index cb7c9c42d0..0000000000--- a/mobile/apps/photos/lib/db/ml/pet_vector_db.dart+++ /dev/null@@ -1,285 +0,0 @@-import "dart:io" show File;-import "dart:typed_data" show Float32List;--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/clip_vector_db.dart" show VectorDbStats;-import "package:photos/src/rust/api/usearch_api.dart";-import "package:synchronized/synchronized.dart";--// Each model needs its own vector space; their embeddings are not comparable.-class PetVectorDB {- static final Logger _logger = Logger("PetVectorDB");-- static const int faceDimension = 128;- static const int bodyDimension = 192;-- final String _databaseName;- final BigInt _embeddingDimension;-- static Logger get logger => _logger;-- PetVectorDB._named(this._databaseName, this._embeddingDimension);-- static final dogFace = PetVectorDB._named(- "ente.ml.vectordb.pet.dog_face.usearch",- BigInt.from(faceDimension),- );-- static final catFace = PetVectorDB._named(- "ente.ml.vectordb.pet.cat_face.usearch",- BigInt.from(faceDimension),- );-- static final dogBody = PetVectorDB._named(- "ente.ml.vectordb.pet.dog_body.usearch",- BigInt.from(bodyDimension),- );-- static final catBody = PetVectorDB._named(- "ente.ml.vectordb.pet.cat_body.usearch",- BigInt.from(bodyDimension),- );-- static final localGalleryDogFace = PetVectorDB._named(- "ente.ml.offline.vectordb.pet.dog_face.usearch",- BigInt.from(faceDimension),- );-- static final localGalleryCatFace = PetVectorDB._named(- "ente.ml.offline.vectordb.pet.cat_face.usearch",- BigInt.from(faceDimension),- );-- static final localGalleryDogBody = PetVectorDB._named(- "ente.ml.offline.vectordb.pet.dog_body.usearch",- BigInt.from(bodyDimension),- );-- static final localGalleryCatBody = PetVectorDB._named(- "ente.ml.offline.vectordb.pet.cat_body.usearch",- BigInt.from(bodyDimension),- );-- static final List<PetVectorDB> allInstances = [- dogFace,- catFace,- dogBody,- catBody,- ];-- static final List<PetVectorDB> allLocalGalleryInstances = [- localGalleryDogFace,- localGalleryCatFace,- localGalleryDogBody,- localGalleryCatBody,- ];-- // Species is 0 for dogs and 1 for cats.- static PetVectorDB forModel({- required int species,- required bool isFace,- bool localGallery = false,- }) {- assert(- species == 0 || species == 1,- 'Invalid pet species: $species (expected 0=dog or 1=cat)',- );- if (localGallery) {- if (species == 0) {- return isFace ? localGalleryDogFace : localGalleryDogBody;- } else {- return isFace ? localGalleryCatFace : localGalleryCatBody;- }- }- if (species == 0) {- return isFace ? dogFace : dogBody;- } else {- return isFace ? catFace : catBody;- }- }-- Future<VectorDb>? _vectorDbFuture;- final Lock _writeLock = Lock();-- Future<VectorDb> get _vectorDB async {- _vectorDbFuture ??= _initVectorDB();- return _vectorDbFuture!;- }-- Future<VectorDb> _initVectorDB() async {- final documentsDirectory = await getApplicationDocumentsDirectory();- final String dbPath = join(documentsDirectory.path, _databaseName);- _logger.info("Opening pet vectorDB: DB path $dbPath");- late VectorDb vectorDB;- try {- vectorDB = VectorDb(filePath: dbPath, dimensions: _embeddingDimension);- } catch (e, s) {- _logger.severe("Could not open Pet VectorDB at $dbPath", e, s);- _logger.severe("Deleting the index file and trying again");- await deleteIndexFile();- try {- vectorDB = VectorDb(filePath: dbPath, dimensions: _embeddingDimension);- } catch (e, s) {- _logger.severe("Still can't open Pet VectorDB at $dbPath", e, s);- rethrow;- }- }- final stats = await getIndexStats(vectorDB);- _logger.info("Pet VectorDB opened with stats: ${stats.toString()}");- return vectorDB;- }-- Future<void> _runWriteOperation(- Future<void> Function(VectorDb db) operation,- ) async {- final db = await _vectorDB;- await _writeLock.synchronized(() async {- await operation(db);- });- }-- Future<void> insertEmbedding({- required int vectorId,- required List<double> embedding,- }) async {- try {- await _runWriteOperation((db) async {- await db.addVector(key: BigInt.from(vectorId), vector: embedding);- });- } catch (e, s) {- _logger.severe("Error inserting pet embedding", e, s);- rethrow;- }- }-- Future<void> bulkInsertEmbeddings({- required List<int> vectorIds,- required List<Float32List> embeddings,- }) async {- if (vectorIds.isEmpty || embeddings.isEmpty) {- return;- }- final bigKeys = Uint64List.fromList(vectorIds);- try {- await _runWriteOperation((db) async {- await db.bulkAddVectors(keys: bigKeys, vectors: embeddings);- });- } catch (e, s) {- _logger.severe("Error bulk inserting pet embeddings", e, s);- rethrow;- }- }-- Future<List<Float32List>> getEmbeddings(List<int> vectorIds) async {- final db = await _vectorDB;- try {- final keys = Uint64List.fromList(vectorIds);- return await db.bulkGetVectors(keys: keys);- } catch (e, s) {- _logger.severe("Error getting pet embeddings", e, s);- rethrow;- }- }-- Future<void> deleteEmbeddings(List<int> vectorIds) async {- if (vectorIds.isEmpty) {- return;- }- try {- BigInt deletedCount = BigInt.zero;- await _runWriteOperation((db) async {- deletedCount = await db.bulkRemoveVectors(- keys: Uint64List.fromList(vectorIds),- );- });- _logger.info(- "Deleted $deletedCount pet embeddings, from ${vectorIds.length} keys",- );- } catch (e, s) {- _logger.severe("Error deleting pet embeddings", e, s);- rethrow;- }- }-- Future<(Uint64List, Float32List)> searchClosestVectors(- List<double> query,- int count, {- bool exact = false,- }) async {- final db = await _vectorDB;- try {- return await db.searchVectors(- query: query,- count: BigInt.from(count),- exact: exact,- );- } catch (e, s) {- _logger.severe("Error searching pet vectors", e, s);- rethrow;- }- }-- Future<void> deleteAllEmbeddings() async {- try {- await _runWriteOperation((db) async {- await db.resetIndex();- });- } catch (e, s) {- _logger.severe("Error deleting all pet embeddings", e, s);- rethrow;- }- }-- Future<VectorDbStats> getIndexStats([VectorDb? db]) async {- db ??= await _vectorDB;- try {- final stats = await db.getIndexStats();- return VectorDbStats(- size: stats.$1.toInt(),- capacity: stats.$2.toInt(),- dimensions: stats.$3.toInt(),- fileSize: stats.$4.toInt(),- memoryUsage: stats.$5.toInt(),- expansionAdd: stats.$6.toInt(),- expansionSearch: stats.$7.toInt(),- );- } catch (e, s) {- _logger.severe("Error getting pet index stats", e, s);- rethrow;- }- }-- Future<void> deleteIndex() async {- final db = await _vectorDB;- try {- await _writeLock.synchronized(() async {- await db.deleteIndex();- _vectorDbFuture = null;- });- } catch (e, s) {- _logger.severe("Error deleting pet index", e, s);- rethrow;- }- }-- Future<void> deleteIndexFile() async {- await _writeLock.synchronized(() async {- try {- final documentsDirectory = await getApplicationDocumentsDirectory();- final String dbPath = join(documentsDirectory.path, _databaseName);- _logger.info("Delete pet index file: $dbPath");- final file = File(dbPath);- if (await file.exists()) {- await file.delete();- }- _vectorDbFuture = null;- } catch (e, s) {- _logger.severe("Error deleting pet index file", e, s);- rethrow;- }- });- }-}diff --git a/mobile/apps/photos/lib/db/ml/rust_db.dart b/mobile/apps/photos/lib/db/ml/rust_db.dartindex bda5465e67..c52b0f5a6c 100644--- a/mobile/apps/photos/lib/db/ml/rust_db.dart+++ b/mobile/apps/photos/lib/db/ml/rust_db.dart@@ -8,7 +8,6 @@ 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/cluster_centroid_vector_db.dart";-import "package:photos/db/ml/db_pet_model_mappers.dart";import "package:photos/db/ml/ml_data_db_orchestration.dart";import "package:photos/db/ml/rust_db_model_mappers.dart" as mappers;import "package:photos/models/ml/clip.dart";@@ -27,18 +26,15 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {final String _databaseName;final ClipVectorDB _clipVectorDB;final ClusterCentroidVectorDB _clusterCentroidVectorDB;- final bool _isLocalGallery;RustMLDataDB._privateConstructor({String databaseName = "ente.ml.db",ClipVectorDB? clipVectorDB,ClusterCentroidVectorDB? clusterCentroidVectorDB,- bool isLocalGallery = false,}) : _databaseName = databaseName,_clipVectorDB = clipVectorDB ?? ClipVectorDB.instance,_clusterCentroidVectorDB =- clusterCentroidVectorDB ?? ClusterCentroidVectorDB.instance,- _isLocalGallery = isLocalGallery;+ clusterCentroidVectorDB ?? ClusterCentroidVectorDB.instance;static final RustMLDataDB instance = RustMLDataDB._privateConstructor();static final RustMLDataDB localGalleryInstance =@@ -46,7 +42,6 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {databaseName: "ente.ml.offline.db",clipVectorDB: ClipVectorDB.localGalleryInstance,clusterCentroidVectorDB: ClusterCentroidVectorDB.localGalleryInstance,- isLocalGallery: true,);@override@@ -56,9 +51,6 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {ClusterCentroidVectorDB get clusterCentroidVectorDB =>_clusterCentroidVectorDB;- @override- bool get isLocalGallery => _isLocalGallery;-@overrideLogger get logger => _logger;@@ -91,50 +83,6 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {Future<void> bulkInsertFaces(List<Face> faces) async =>(await _db).bulkInsertFaces(faces: faces.map(mappers.toFaceRow).toList());- @override- Future<void> bulkInsertPetFaces(List<DBPetFace> petFaces) async =>- (await _db).bulkInsertPetFaces(- petFaces: petFaces.map(mappers.toPetFaceRow).toList(),- );-- @override- Future<void> bulkInsertPetBodies(List<DBPetBody> petBodies) async =>- (await _db).bulkInsertPetBodies(- petBodies: petBodies.map(mappers.toPetBodyRow).toList(),- );-- @override- Future<void> updatePetFaceVectorIds(- Map<String, int> petFaceIdToVectorId,- ) async => (await _db).updatePetFaceVectorIds(- petFaceIdToVectorId: petFaceIdToVectorId,- );-- @override- Future<void> updatePetBodyVectorIds(- Map<String, int> petBodyIdToVectorId,- ) async => (await _db).updatePetBodyVectorIds(- petBodyIdToVectorId: petBodyIdToVectorId,- );-- @override- Future<Map<String, int>> getPetFaceVectorIdMap(- Iterable<String> petFaceIds, {- bool createIfMissing = false,- }) async => (await _db).getPetFaceVectorIdMap(- petFaceIds: petFaceIds.toList(),- createIfMissing: createIfMissing,- );-- @override- Future<Map<String, int>> getPetBodyVectorIdMap(- Iterable<String> petBodyIds, {- bool createIfMissing = false,- }) async => (await _db).getPetBodyVectorIdMap(- petBodyIds: petBodyIds.toList(),- createIfMissing: createIfMissing,- );-@overrideFuture<void> updateFaceIdToClusterId(Map<String, String> faceIDToClusterID,@@ -229,20 +177,6 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {return rows.isEmpty ? null : rows.map(mappers.toFace).toList();}- @override- Future<List<DBPetFace>?> getPetFacesForFileID(int fileUploadID) async {- final db = await _db;- final rows = await db.getPetFacesForFileId(fileUploadId: fileUploadID);- return rows.isEmpty ? null : rows.map(mappers.toDBPetFace).toList();- }-- @override- Future<List<DBPetBody>?> getPetBodiesForFileID(int fileUploadID) async {- final db = await _db;- final rows = await db.getPetBodiesForFileId(fileUploadId: fileUploadID);- return rows.isEmpty ? null : rows.map(mappers.toDBPetBody).toList();- }-@overrideFuture<Map<int, List<FaceWithoutEmbedding>>>getFileIDsToFacesWithoutEmbedding() async {@@ -653,12 +587,6 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {int minimumMlVersion = petMlVersion,}) async => (await _db).petIndexedFileIds(minimumMlVersion: minimumMlVersion);- @override- Future<int> getPetIndexedFileCount({- int minimumMlVersion = petMlVersion,- }) async =>- (await _db).getPetIndexedFileCount(minimumMlVersion: minimumMlVersion);-@overrideFuture<Set<int>> getFullyIndexedFileIds({required bool includePets}) async {final db = await _db;@@ -666,36 +594,6 @@ class RustMLDataDB with MLDataDBOrchestration implements IMLDataDB<int> {return fileIDs.inner.toSet();}- @override- Future<(List<(String, int?, int)>, List<(String, int?, int)>)>- getPetRowsForFiles(List<int> fileIDs) async {- final db = await _db;- final rows = await db.getPetRowsForFiles(- fileIds: Int64List.fromList(fileIDs),- );- return (- [- for (final row in rows.faces)- (row.petFaceId, row.faceVectorId, row.species),- ],- [- for (final row in rows.bodies)- (row.petBodyId, row.bodyVectorId, row.species),- ],- );- }-- @override- Future<void> deletePetRowsForFiles({- required List<int> fileIDs,- required List<String> petFaceIds,- required List<String> petBodyIds,- }) async => (await _db).deletePetRowsForFiles(- fileIds: Int64List.fromList(fileIDs),- petFaceIds: petFaceIds,- petBodyIds: petBodyIds,- );-@overrideFuture<void> insertClipRows(List<ClipEmbedding> embeddings) async =>(await _db).insertClipRows(diff --git a/mobile/apps/photos/lib/db/ml/rust_db_model_mappers.dart b/mobile/apps/photos/lib/db/ml/rust_db_model_mappers.dartindex de3ac912c9..da095f1f82 100644--- a/mobile/apps/photos/lib/db/ml/rust_db_model_mappers.dart+++ b/mobile/apps/photos/lib/db/ml/rust_db_model_mappers.dart@@ -1,7 +1,6 @@import "dart:convert";import "dart:typed_data";-import "package:photos/db/ml/db_pet_model_mappers.dart";import "package:photos/models/ml/clip.dart";import "package:photos/models/ml/face/detection.dart";import "package:photos/models/ml/face/face.dart";@@ -65,62 +64,6 @@ FaceDbInfoForClustering toFaceDbInfoForClustering();}-rust.PetFaceRow toPetFaceRow(DBPetFace petFace) {- return rust.PetFaceRow(- fileId: petFace.fileId,- petFaceId: petFace.petFaceId,- detectionJson: petFace.detection,- faceVectorId: petFace.faceVectorId,- species: petFace.species,- faceScore: petFace.faceScore,- imageHeight: petFace.imageHeight,- imageWidth: petFace.imageWidth,- mlVersion: petFace.mlVersion,- );-}--DBPetFace toDBPetFace(rust.PetFaceRow row) {- return DBPetFace(- fileId: row.fileId,- petFaceId: row.petFaceId,- detection: row.detectionJson,- faceVectorId: row.faceVectorId,- species: row.species,- faceScore: row.faceScore,- imageHeight: row.imageHeight,- imageWidth: row.imageWidth,- mlVersion: row.mlVersion,- );-}--rust.PetBodyRow toPetBodyRow(DBPetBody petBody) {- return rust.PetBodyRow(- fileId: petBody.fileId,- petBodyId: petBody.petBodyId,- detectionJson: petBody.detection,- bodyVectorId: petBody.bodyVectorId,- species: petBody.species,- score: petBody.score,- imageHeight: petBody.imageHeight,- imageWidth: petBody.imageWidth,- mlVersion: petBody.mlVersion,- );-}--DBPetBody toDBPetBody(rust.PetBodyRow row) {- return DBPetBody(- fileId: row.fileId,- petBodyId: row.petBodyId,- detection: row.detectionJson,- bodyVectorId: row.bodyVectorId,- species: row.species,- score: row.score,- imageHeight: row.imageHeight,- imageWidth: row.imageWidth,- mlVersion: row.mlVersion,- );-}-rust.ClipEmbedding toClipEmbeddingRow(ClipEmbedding embedding) {return rust.ClipEmbedding(fileId: embedding.fileID,diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dartindex 12211c278b..f1e37b8162 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart@@ -1,5 +1,4 @@import "dart:async";-import "dart:convert" show jsonEncode;import "dart:io" show File, Platform;import "dart:math" show min;import "dart:typed_data" show Uint8List;@@ -11,7 +10,6 @@ import "package:photos/core/event_bus.dart";import "package:photos/db/files_db.dart";import "package:photos/db/ml/base.dart";import "package:photos/db/ml/db.dart";-import "package:photos/db/ml/db_pet_model_mappers.dart";import "package:photos/db/offline_files_db.dart";import "package:photos/events/app_mode_changed_event.dart";import "package:photos/events/compute_control_event.dart";@@ -27,7 +25,6 @@ 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_clustering_service.dart";import "package:photos/services/machine_learning/face_ml/face_clustering/face_db_info_for_clustering.dart";-import "package:photos/services/machine_learning/face_ml/face_detection/detection.dart";import "package:photos/services/machine_learning/face_ml/person/person_service.dart";import "package:photos/services/machine_learning/ml_exceptions.dart";import "package:photos/services/machine_learning/ml_indexing_isolate.dart";@@ -1024,61 +1021,6 @@ class MLService {}}- // Delete stale pet rows first so re-indexing with fewer detections does- // not leave old data behind.- final rustPets = result.petFaces != null || result.petBodies != null;- if (rustPets) {- await mlDataDB.deletePetDataForFiles([result.fileId]);- if (result.petFaces != null && result.petFaces!.isNotEmpty) {- final dbPetFaces = result.petFaces!.map((pf) {- return DBPetFace(- fileId: result.fileId,- petFaceId: pf.petFaceId,- detection: jsonEncode(pf.detection.toJson()),- faceVectorId: null,- species: pf.species,- faceScore: pf.detection.score,- imageHeight: result.decodedImageSize.height,- imageWidth: result.decodedImageSize.width,- mlVersion: petMlVersion,- );- }).toList();- await mlDataDB.bulkInsertPetFaces(dbPetFaces);- await mlDataDB.storePetFaceEmbeddings(dbPetFaces, result.petFaces!);- } else if (instruction.shouldRunPets) {- // No pet faces detected; insert empty marker so the file is- // considered pet-indexed (mirrors Face.empty for human faces).- await mlDataDB.bulkInsertPetFaces([DBPetFace.empty(result.fileId)]);- }-- if (result.petBodies != null && result.petBodies!.isNotEmpty) {- final dbPetBodies = result.petBodies!.map((obj) {- final detectionObj = FaceDetectionRelative(- score: obj.score,- box: [- obj.boxXyxy[0],- obj.boxXyxy[1],- obj.boxXyxy[2],- obj.boxXyxy[3],- ],- allKeypoints: const [],- );- return DBPetBody(- fileId: result.fileId,- petBodyId: obj.petBodyId,- detection: jsonEncode(detectionObj.toJson()),- bodyVectorId: null,- species: obj.cocoClass == 15 ? 1 : 0,- score: obj.score,- imageHeight: result.decodedImageSize.height,- imageWidth: result.decodedImageSize.width,- mlVersion: petMlVersion,- );- }).toList();- await mlDataDB.bulkInsertPetBodies(dbPetBodies);- await mlDataDB.storePetBodyEmbeddings(dbPetBodies, result.petBodies!);- }- }_logger.info("ML result for fileID ${result.fileId} stored remote+local");indexedOrSkipped = true;return actuallyRanML;@@ -1117,13 +1059,6 @@ class MLService {}storedMarkers.add("clip");}- if (instruction.shouldRunPets) {- await mlDataDB.deletePetDataForFiles([instruction.fileKey]);- await mlDataDB.bulkInsertPetFaces([- DBPetFace.empty(instruction.fileKey, error: true),- ]);- storedMarkers.add("pets");- }_logger.info("Stored empty ML result markers for fileID ${instruction.fileKey}: ${storedMarkers.join(', ')}",);@@ -1138,11 +1073,6 @@ class MLService {e,s,);- // Clean up any pet rows that were already committed before the- // failure so the file is not treated as fully indexed.- if (instruction.shouldRunPets) {- await mlDataDB.deletePetDataForFiles([instruction.fileKey]);- }return false;} finally {if (indexedOrSkipped) {diff --git a/mobile/apps/photos/lib/ui/settings/ml/ml_user_dev_screen.dart b/mobile/apps/photos/lib/ui/settings/ml/ml_user_dev_screen.dartindex 0010ef8ea2..e6e6d7accf 100644--- a/mobile/apps/photos/lib/ui/settings/ml/ml_user_dev_screen.dart+++ b/mobile/apps/photos/lib/ui/settings/ml/ml_user_dev_screen.dart@@ -181,7 +181,6 @@ class _MLUserDeveloperOptionsState extends State<MLUserDeveloperOptions> {final Set<int> emptyFileIDs = await mlDataDB.getErroredFileIDs();await mlDataDB.deleteFaceIndexForFiles(emptyFileIDs.toList());await mlDataDB.deleteClipEmbeddings(emptyFileIDs.toList());- await mlDataDB.deletePetDataForFiles(emptyFileIDs.toList());if (!context.mounted) return;showShortToast(context, "Deleted ${emptyFileIDs.length} entries");} catch (e) {diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_details_widget.dart b/mobile/apps/photos/lib/ui/viewer/file/file_details_widget.dartindex 22fa9edfcb..b349b7c483 100644--- a/mobile/apps/photos/lib/ui/viewer/file/file_details_widget.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/file_details_widget.dart@@ -31,7 +31,6 @@ import "package:photos/ui/viewer/file_details/albums_item_widget.dart";import "package:photos/ui/viewer/file_details/creation_time_item_widget.dart";import 'package:photos/ui/viewer/file_details/exif_item_widgets.dart';import "package:photos/ui/viewer/file_details/file_info_faces_item_widget.dart";-import "package:photos/ui/viewer/file_details/file_info_pets_item_widget.dart";import "package:photos/ui/viewer/file_details/file_properties_item_widget.dart";import "package:photos/ui/viewer/file_details/location_tags_widget.dart";import "package:photos/ui/viewer/file_details/preview_properties_item_widget.dart";@@ -200,12 +199,6 @@ class _FileDetailsWidgetState extends State<FileDetailsWidget> {FacesItemWidget(file),const SizedBox(height: Spacing.xxl),]);- if (flagService.petEnabled && localSettings.petRecognitionEnabled) {- fileDetailsTiles.addAll([- PetsItemWidget(file),- const FileDetailsDivider(),- ]);- }}fileDetailsTiles.addAll([diff --git a/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dart b/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dartdeleted file mode 100644index 55ed644c66..0000000000--- a/mobile/apps/photos/lib/ui/viewer/file_details/file_info_pets_item_widget.dart+++ /dev/null@@ -1,242 +0,0 @@-import "dart:convert";-import "dart:typed_data";--import "package:ente_strings/ente_strings.dart";-import "package:ente_ui/components/loading_widget.dart";-import "package:flutter/material.dart";-import "package:logging/logging.dart";-import "package:photos/db/ml/db.dart";-import "package:photos/db/offline_files_db.dart";-import "package:photos/models/file/file.dart";-import "package:photos/models/ml/face/box.dart";-import "package:photos/models/ml/face/detection.dart";-import "package:photos/models/ml/face/face.dart";-import "package:photos/service_locator.dart" show isLocalGalleryMode;-import "package:photos/theme/ente_theme.dart";-import "package:photos/theme/text_style.dart";-import "package:photos/ui/components/buttons/icon_button_widget.dart";-import "package:photos/ui/viewer/people/face_thumbnail_squircle.dart";-import "package:photos/ui/viewer/people/file_face_widget.dart";-import "package:photos/utils/face/face_thumbnail_cache.dart";--final Logger _logger = Logger("PetsItemWidget");--class PetsItemWidget extends StatefulWidget {- final EnteFile file;- const PetsItemWidget(this.file, {super.key});-- @override- State<PetsItemWidget> createState() => _PetsItemWidgetState();-}--class _PetsItemWidgetState extends State<PetsItemWidget> {- bool _isLoading = true;- List<_PetFaceInfo> _petFaces = [];-- @override- void initState() {- super.initState();- _loadPetFaces();- }-- Future<void> _loadPetFaces() async {- try {- final bool isLocalGallery = isLocalGalleryMode;- int? fileKey;- if (isLocalGallery) {- final localId = widget.file.localID;- if (localId == null || localId.isEmpty) {- if (mounted) setState(() => _isLoading = false);- return;- }- fileKey = await OfflineFilesDB.instance.getOrCreateLocalIntId(localId);- } else {- fileKey = widget.file.uploadedFileID;- if (fileKey == null) {- if (mounted) setState(() => _isLoading = false);- return;- }- }-- final mlDataDB = isLocalGallery- ? MLDataDB.localGalleryInstance- : MLDataDB.instance;- final dbPetFaces = await mlDataDB.getPetFacesForFileID(fileKey);- if (dbPetFaces == null || dbPetFaces.isEmpty) {- if (mounted) setState(() => _isLoading = false);- return;- }-- // Stored pet boxes are [xMin, yMin, xMax, yMax], while FaceBox takes- // [x, y, width, height].- final faces = <Face>[];- for (final dbPetFace in dbPetFaces) {- final json = jsonDecode(dbPetFace.detection) as Map<String, dynamic>;- final boxList = (json['box'] as List)- .map((e) => (e as num).toDouble())- .toList();- final detection = Detection(- box: FaceBox(- x: boxList[0],- y: boxList[1],- width: boxList[2] - boxList[0],- height: boxList[3] - boxList[1],- ),- landmarks: const [],- );- faces.add(- Face(- dbPetFace.petFaceId,- dbPetFace.fileId,- const <double>[],- dbPetFace.faceScore,- detection,- 0.0,- ),- );- }-- final faceCrops = await getCachedFaceCrops(- widget.file,- faces,- useTempCache: true,- );-- if (!mounted) return;- if (faceCrops == null) {- setState(() => _isLoading = false);- return;- }-- final petFaceInfos = <_PetFaceInfo>[];- for (final dbPetFace in dbPetFaces) {- final crop = faceCrops[dbPetFace.petFaceId];- if (crop != null) {- petFaceInfos.add(- _PetFaceInfo(- petFaceId: dbPetFace.petFaceId,- species: dbPetFace.species,- faceCrop: crop,- ),- );- }- }-- if (mounted) {- setState(() {- _petFaces = petFaceInfos;- _isLoading = false;- });- }- } catch (e, s) {- _logger.severe("Failed to load pet faces", e, s);- if (mounted) setState(() => _isLoading = false);- }- }-- @override- Widget build(BuildContext context) {- if (!_isLoading && _petFaces.isEmpty) {- return const SizedBox.shrink();- }-- return Row(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- const IconButtonWidget(- icon: Icons.pets,- iconButtonType: IconButtonType.secondary,- ),- const SizedBox(width: 12),- _buildContent(context),- ],- );- }-- Widget _buildContent(BuildContext context) {- if (_isLoading) {- return const Expanded(- child: Padding(- padding: EdgeInsets.only(top: 8, right: 12),- child: Center(- child: EnteLoadingWidget(- padding: 6,- size: 20,- alignment: Alignment.center,- ),- ),- ),- );- }-- final screenWidth = MediaQuery.of(context).size.width;- final thumbnailWidth = screenWidth * 0.16;- final textTheme = getEnteTextTheme(context);-- return Expanded(- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- Text(context.strings.discover_pets, style: textTheme.small),- const SizedBox(height: 10),- Padding(- padding: const EdgeInsets.only(right: 12.0),- child: Wrap(- runSpacing: 8,- spacing: 12,- children: _petFaces- .map(- (info) =>- _buildPetThumbnail(info, thumbnailWidth, textTheme),- )- .toList(),- ),- ),- ],- ),- );- }-- Widget _buildPetThumbnail(- _PetFaceInfo info,- double thumbnailWidth,- EnteTextTheme textTheme,- ) {- final l10n = context.strings;- final speciesLabel = info.species == 0 ? l10n.dog : l10n.cat;-- return SizedBox(- width: thumbnailWidth,- child: Column(- children: [- SizedBox(- height: thumbnailWidth,- width: thumbnailWidth,- child: FaceThumbnailSquircleClip(- child: FileFaceWidget(widget.file, faceCrop: info.faceCrop),- ),- ),- const SizedBox(height: 4),- Text(- speciesLabel,- style: textTheme.mini,- overflow: TextOverflow.ellipsis,- maxLines: 1,- ),- ],- ),- );- }-}--class _PetFaceInfo {- final String petFaceId;- final int species;- final Uint8List faceCrop;-- _PetFaceInfo({- required this.petFaceId,- required this.species,- required this.faceCrop,- });-}diff --git a/rust/bindings/frb/photos/src/api/ml_db_api.rs b/rust/bindings/frb/photos/src/api/ml_db_api.rsindex 04a983a0f7..f94352506e 100644--- a/rust/bindings/frb/photos/src/api/ml_db_api.rs+++ b/rust/bindings/frb/photos/src/api/ml_db_api.rs@@ -4,8 +4,7 @@ use std::sync::OnceLock;use ente_photos::ml_db;pub use ente_photos::ml_db::{ClipEmbedding, ClipRow, ClusterCentroidRow, ClusterSummary, EmbeddingVector,- FaceDbInfoForClustering, FaceRow, FaceWithoutEmbedding, FdStatus, PetBodyRow, PetBodyVectorRow,- PetFaceRow, PetFaceVectorRow, PetRowsForFiles, PreviewInfo,+ FaceDbInfoForClustering, FaceRow, FaceWithoutEmbedding, FdStatus, PreviewInfo,};use ente_photos::ml_store;pub use ente_photos::ml_store::{FillOutcome, FillState, Index};@@ -82,52 +81,6 @@ pub struct _FaceDbInfoForClustering {pub is_sideways: bool,}-#[frb(mirror(PetFaceRow))]-pub struct _PetFaceRow {- pub file_id: i64,- pub pet_face_id: String,- pub detection_json: String,- pub face_vector_id: Option<i64>,- pub species: i64,- pub face_score: f64,- pub image_height: i64,- pub image_width: i64,- pub ml_version: i64,-}--#[frb(mirror(PetBodyRow))]-pub struct _PetBodyRow {- pub file_id: i64,- pub pet_body_id: String,- pub detection_json: String,- pub body_vector_id: Option<i64>,- pub species: i64,- pub score: f64,- pub image_height: i64,- pub image_width: i64,- pub ml_version: i64,-}--#[frb(mirror(PetFaceVectorRow))]-pub struct _PetFaceVectorRow {- pub pet_face_id: String,- pub face_vector_id: Option<i64>,- pub species: i64,-}--#[frb(mirror(PetBodyVectorRow))]-pub struct _PetBodyVectorRow {- pub pet_body_id: String,- pub body_vector_id: Option<i64>,- pub species: i64,-}--#[frb(mirror(PetRowsForFiles))]-pub struct _PetRowsForFiles {- pub faces: Vec<PetFaceVectorRow>,- pub bodies: Vec<PetBodyVectorRow>,-}-#[frb(mirror(ClipEmbedding))]pub struct _ClipEmbedding {pub file_id: i64,@@ -936,48 +889,6 @@ impl MlStore {Ok(self.inner.db().get_clip_rows_page(limit, offset)?)}- pub fn bulk_insert_pet_faces(&self, pet_faces: Vec<PetFaceRow>) -> Result<(), MlDbError> {- Ok(self.inner.db().bulk_insert_pet_faces(&pet_faces)?)- }-- pub fn bulk_insert_pet_bodies(&self, pet_bodies: Vec<PetBodyRow>) -> Result<(), MlDbError> {- Ok(self.inner.db().bulk_insert_pet_bodies(&pet_bodies)?)- }-- pub fn update_pet_face_vector_ids(- &self,- pet_face_id_to_vector_id: HashMap<String, i64>,- ) -> Result<(), MlDbError> {- Ok(self- .inner- .db()- .update_pet_face_vector_ids(&pet_face_id_to_vector_id)?)- }-- pub fn update_pet_body_vector_ids(- &self,- pet_body_id_to_vector_id: HashMap<String, i64>,- ) -> Result<(), MlDbError> {- Ok(self- .inner- .db()- .update_pet_body_vector_ids(&pet_body_id_to_vector_id)?)- }-- pub fn get_pet_faces_for_file_id(- &self,- file_upload_id: i64,- ) -> Result<Vec<PetFaceRow>, MlDbError> {- Ok(self.inner.db().get_pet_faces_for_file_id(file_upload_id)?)- }-- pub fn get_pet_bodies_for_file_id(- &self,- file_upload_id: i64,- ) -> Result<Vec<PetBodyRow>, MlDbError> {- Ok(self.inner.db().get_pet_bodies_for_file_id(file_upload_id)?)- }-pub fn pet_indexed_file_ids(&self,minimum_ml_version: i64,@@ -985,51 +896,6 @@ impl MlStore {Ok(self.inner.db().pet_indexed_file_ids(minimum_ml_version)?)}- pub fn get_pet_indexed_file_count(&self, minimum_ml_version: i64) -> Result<i64, MlDbError> {- Ok(self- .inner- .db()- .get_pet_indexed_file_count(minimum_ml_version)?)- }-- pub fn get_pet_rows_for_files(&self, file_ids: Vec<i64>) -> Result<PetRowsForFiles, MlDbError> {- Ok(self.inner.db().get_pet_rows_for_files(&file_ids)?)- }-- pub fn delete_pet_rows_for_files(- &self,- file_ids: Vec<i64>,- pet_face_ids: Vec<String>,- pet_body_ids: Vec<String>,- ) -> Result<(), MlDbError> {- Ok(self- .inner- .db()- .delete_pet_rows_for_files(&file_ids, &pet_face_ids, &pet_body_ids)?)- }-- pub fn get_pet_face_vector_id_map(- &self,- pet_face_ids: Vec<String>,- create_if_missing: bool,- ) -> Result<HashMap<String, i64>, MlDbError> {- Ok(self- .inner- .db()- .get_pet_face_vector_id_map(&pet_face_ids, create_if_missing)?)- }-- pub fn get_pet_body_vector_id_map(- &self,- pet_body_ids: Vec<String>,- create_if_missing: bool,- ) -> Result<HashMap<String, i64>, MlDbError> {- Ok(self- .inner- .db()- .get_pet_body_vector_id_map(&pet_body_ids, create_if_missing)?)- }-pub fn put_repeated_text_embedding_cache(&self,query: String,diff --git a/rust/crates/photos/src/ml_db/mod.rs b/rust/crates/photos/src/ml_db/mod.rsindex e4bb606aa3..9825fc0a44 100644--- a/rust/crates/photos/src/ml_db/mod.rs+++ b/rust/crates/photos/src/ml_db/mod.rs@@ -16,9 +16,7 @@ pub use queries::clusters::{ClusterCentroidRow, ClusterSummary};pub use queries::faces::{FACE_ML_VERSION, FaceDbInfoForClustering, FaceRow, FaceWithoutEmbedding};pub use queries::filedata::{FdStatus, PreviewInfo};pub use queries::persons::PersonToClusterIdToFaceIds;-pub use queries::pets::{- PET_ML_VERSION, PetBodyRow, PetBodyVectorRow, PetFaceRow, PetFaceVectorRow, PetRowsForFiles,-};+pub use queries::pets::PET_ML_VERSION;#[derive(Debug, thiserror::Error)]pub enum Error {@@ -84,7 +82,7 @@ pub(crate) mod tests {use std::path::Path;use super::queries::clip::tests::full_clip;- use super::queries::{caches, clip, clusters, faces, filedata, persons, pets};+ use super::queries::{caches, clip, clusters, faces, filedata, persons};use super::{Error, MlDb, schema};use crate::db::Connection;use tempfile::TempDir;@@ -185,12 +183,11 @@ pub(crate) mod tests {faces::tests::seed(&db);clusters::tests::seed(&db);persons::tests::seed(&db);- pets::tests::seed(&db);(directory, db)}#[test]- fn clear_non_pet_tables_leaves_pets_and_caches() {+ fn clear_non_pet_tables_leaves_caches() {let (_directory, db) = seeded_all();db.clear_non_pet_tables().unwrap();assert_eq!(db.get_total_face_count().unwrap(), 0);@@ -205,7 +202,6 @@ pub(crate) mod tests {);assert_eq!(db.count_clip_rows().unwrap(), 0);assert!(db.get_file_ids_with_fd_data(None).unwrap().is_empty());- assert_eq!(db.get_pet_indexed_file_count(1).unwrap(), 2);assert_eq!(db.get_face_id_used_for_person_or_cluster("p1").unwrap(),Some("1_0".to_string())@@ -216,41 +212,6 @@ pub(crate) mod tests {);}- #[test]- fn clear_pet_tables_leaves_non_pet_tables() {- let (directory, db) = seeded_all();- let connection = Connection::open(directory.path().join("ente.ml.db")).unwrap();- let row_count = |table: &str| -> i64 {- connection- .query_row(&format!("SELECT COUNT(*) FROM {table}"), (), |row| {- Ok(row.get(0)?)- })- .unwrap()- };- assert_eq!(row_count("pet_face_vector_id_map"), 2);- assert_eq!(row_count("pet_body_vector_id_map"), 1);- db.clear_pet_tables().unwrap();- assert_eq!(row_count("pet_face_vector_id_map"), 0);- assert_eq!(row_count("pet_body_vector_id_map"), 0);- assert_eq!(db.get_pet_indexed_file_count(0).unwrap(), 0);- assert_eq!(- db.get_pet_rows_for_files(&[1, 2, 3]).unwrap().faces.len(),- 0- );- assert_eq!(db.get_total_face_count().unwrap(), 9);- assert_eq!(db.cluster_id_to_face_count().unwrap().len(), 6);- assert_eq!(db.get_person_cluster_ids("p1").unwrap().len(), 2);- assert_eq!(db.count_clip_rows().unwrap(), 4);- assert_eq!(- db.get_file_ids_with_fd_data(None).unwrap(),- HashSet::from([1, 2, 3])- );- assert_eq!(- db.get_face_id_used_for_person_or_cluster("p1").unwrap(),- Some("1_0".to_string())- );- }-#[test]fn ml_connections_keep_existing_settings() {let (_directory, db) = open();diff --git a/rust/crates/photos/src/ml_db/queries/faces.rs b/rust/crates/photos/src/ml_db/queries/faces.rsindex e621d6269d..6ba868350e 100644--- a/rust/crates/photos/src/ml_db/queries/faces.rs+++ b/rust/crates/photos/src/ml_db/queries/faces.rs@@ -606,10 +606,10 @@ pub(in crate::ml_db) mod tests {use std::ops::RangeInclusive;use super::{FaceRow, MlDb};- use crate::ml_db::queries::{clip, clusters, persons, pets};+ use crate::ml_db::Error;+ use crate::ml_db::queries::{clip, clusters, persons};use crate::ml_db::tests::{cases, check, ids, open, sorted, strings};use crate::ml_db::vector_encoding::encode_evector;- use crate::ml_db::{Error, PetFaceRow};use tempfile::TempDir;fn face(file_id: i64, index: i64, score: f64, blur: f64, is_sideways: bool) -> FaceRow {@@ -662,7 +662,6 @@ pub(in crate::ml_db) mod tests {seed(&db);clusters::tests::seed(&db);persons::tests::seed(&db);- pets::tests::seed(&db);(directory, db)}@@ -690,7 +689,6 @@ pub(in crate::ml_db) mod tests {&cases!["errored": ids([5]) => |db| db.get_errored_file_ids(),"fully indexed": ids([1, 2, 3, 4]) => |db| db.get_fully_indexed_file_ids(false),- "fully indexed with pets": ids([1, 2]) => |db| db.get_fully_indexed_file_ids(true),"unclustered": ids([4, 5, 6, 10]) =>|db| db.get_all_file_ids_of_face_ids_not_in_any_cluster(),"clustered": ids([1, 2, 3, 7, 8, 9]) =>@@ -945,21 +943,16 @@ pub(in crate::ml_db) mod tests {good_face(12, 1),]).unwrap();- db.bulk_insert_pet_faces(&[PetFaceRow {- face_score: -1.0,- ..pets::tests::pet_face(13, 0, 0)- }])- .unwrap();- let file_ids: Vec<i64> = (1..=13).chain([99]).collect();+ let file_ids: Vec<i64> = (1..=12).chain([99]).collect();let many: Vec<i64> = (0..=10000).collect();assert_eq!(db.get_file_ids_with_error_results(&[]).unwrap(), ids([]));assert_eq!(db.get_file_ids_with_error_results(&file_ids).unwrap(),- ids([4, 5, 12, 13])+ ids([4, 5, 12]));assert_eq!(db.get_file_ids_with_error_results(&many).unwrap(),- ids([4, 5, 12, 13])+ ids([4, 5, 12]));db.prune_resolved_face_error_results(&[]).unwrap();@@ -970,7 +963,7 @@ pub(in crate::ml_db) mod tests {assert_eq!(db.get_faces_for_given_file_id(11).unwrap().len(), 1);assert_eq!(db.get_file_ids_with_error_results(&file_ids).unwrap(),- ids([4, 5, 12, 13])+ ids([4, 5, 12]));}diff --git a/rust/crates/photos/src/ml_db/queries/pets.rs b/rust/crates/photos/src/ml_db/queries/pets.rsindex fb56a29056..8fcebcd393 100644--- a/rust/crates/photos/src/ml_db/queries/pets.rs+++ b/rust/crates/photos/src/ml_db/queries/pets.rs@@ -1,175 +1,12 @@use std::collections::HashMap;-use super::helpers::{bind_placeholders, pair};-use crate::db::{self, Row, params_from_iter};+use super::helpers::pair;-use super::unique_in_order;use crate::ml_db::{MlDb, Result};pub const PET_ML_VERSION: i64 = 1;-#[derive(Clone, Debug, PartialEq)]-pub struct PetFaceRow {- pub file_id: i64,- pub pet_face_id: String,- pub detection_json: String,- pub face_vector_id: Option<i64>,- pub species: i64,- pub face_score: f64,- pub image_height: i64,- pub image_width: i64,- pub ml_version: i64,-}--#[derive(Clone, Debug, PartialEq)]-pub struct PetBodyRow {- pub file_id: i64,- pub pet_body_id: String,- pub detection_json: String,- pub body_vector_id: Option<i64>,- pub species: i64,- pub score: f64,- pub image_height: i64,- pub image_width: i64,- pub ml_version: i64,-}--#[derive(Clone, Debug, PartialEq, Eq)]-pub struct PetFaceVectorRow {- pub pet_face_id: String,- pub face_vector_id: Option<i64>,- pub species: i64,-}--#[derive(Clone, Debug, PartialEq, Eq)]-pub struct PetBodyVectorRow {- pub pet_body_id: String,- pub body_vector_id: Option<i64>,- pub species: i64,-}--#[derive(Clone, Debug, PartialEq, Eq)]-pub struct PetRowsForFiles {- pub faces: Vec<PetFaceVectorRow>,- pub bodies: Vec<PetBodyVectorRow>,-}--const UPSERT_PET_FACE: &str = r#"- INSERT INTO pet_faces (- file_id, pet_face_id, detection, face_vector_id, species,- score, height, width, ml_version- )- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)- ON CONFLICT (file_id, pet_face_id) DO UPDATE SET- detection = excluded.detection,- face_vector_id = excluded.face_vector_id,- species = excluded.species,- score = excluded.score,- height = excluded.height,- width = excluded.width,- ml_version = excluded.ml_version-"#;-const UPSERT_PET_BODY: &str = r#"- INSERT INTO pet_bodies (- file_id, pet_body_id, detection, body_vector_id, species,- score, height, width, ml_version- )- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)- ON CONFLICT (file_id, pet_body_id) DO UPDATE SET- detection = excluded.detection,- body_vector_id = excluded.body_vector_id,- species = excluded.species,- score = excluded.score,- height = excluded.height,- width = excluded.width,- ml_version = excluded.ml_version-"#;-impl MlDb {- pub fn bulk_insert_pet_faces(&self, pet_faces: &[PetFaceRow]) -> Result<()> {- self.write_batch_atomic(- UPSERT_PET_FACE,- pet_faces.iter().map(|pet_face| {- (- pet_face.file_id,- &pet_face.pet_face_id,- &pet_face.detection_json,- pet_face.face_vector_id,- pet_face.species,- pet_face.face_score,- pet_face.image_height,- pet_face.image_width,- pet_face.ml_version,- )- }),- )- }-- pub fn bulk_insert_pet_bodies(&self, pet_bodies: &[PetBodyRow]) -> Result<()> {- self.write_batch_atomic(- UPSERT_PET_BODY,- pet_bodies.iter().map(|pet_body| {- (- pet_body.file_id,- &pet_body.pet_body_id,- &pet_body.detection_json,- pet_body.body_vector_id,- pet_body.species,- pet_body.score,- pet_body.image_height,- pet_body.image_width,- pet_body.ml_version,- )- }),- )- }-- pub fn update_pet_face_vector_ids(- &self,- pet_face_id_to_vector_id: &HashMap<String, i64>,- ) -> Result<()> {- if pet_face_id_to_vector_id.is_empty() {- return Ok(());- }- self.write_batch_atomic(- "UPDATE pet_faces SET face_vector_id = ? WHERE pet_face_id = ?",- pet_face_id_to_vector_id- .iter()- .map(|(pet_face_id, vector_id)| (vector_id, pet_face_id)),- )- }-- pub fn update_pet_body_vector_ids(- &self,- pet_body_id_to_vector_id: &HashMap<String, i64>,- ) -> Result<()> {- if pet_body_id_to_vector_id.is_empty() {- return Ok(());- }- self.write_batch_atomic(- "UPDATE pet_bodies SET body_vector_id = ? WHERE pet_body_id = ?",- pet_body_id_to_vector_id- .iter()- .map(|(pet_body_id, vector_id)| (vector_id, pet_body_id)),- )- }-- pub fn get_pet_faces_for_file_id(&self, file_upload_id: i64) -> Result<Vec<PetFaceRow>> {- self.read_all(- "SELECT * FROM pet_faces WHERE file_id = ? AND species != -1",- [file_upload_id],- read_pet_face,- )- }-- pub fn get_pet_bodies_for_file_id(&self, file_upload_id: i64) -> Result<Vec<PetBodyRow>> {- self.read_all(- "SELECT * FROM pet_bodies WHERE file_id = ? AND species != -1",- [file_upload_id],- read_pet_body,- )- }-pub fn pet_indexed_file_ids(&self, minimum_ml_version: i64) -> Result<HashMap<i64, i64>> {self.read_all("SELECT DISTINCT file_id, ml_version FROM pet_faces WHERE ml_version >= ?",@@ -177,379 +14,4 @@ impl MlDb {pair,)}-- pub fn get_pet_indexed_file_count(&self, minimum_ml_version: i64) -> Result<i64> {- self.read_value(- "SELECT COUNT(DISTINCT file_id) as count FROM pet_faces WHERE ml_version >= ?",- [minimum_ml_version],- )- }-- pub fn get_pet_rows_for_files(&self, file_ids: &[i64]) -> Result<PetRowsForFiles> {- let placeholders = bind_placeholders(file_ids.len());- let faces_sql = format!(- r#"- SELECT pet_face_id, face_vector_id, species- FROM pet_faces- WHERE file_id IN ({placeholders})- "#- );- let bodies_sql = format!(- r#"- SELECT pet_body_id, body_vector_id, species- FROM pet_bodies- WHERE file_id IN ({placeholders})- "#- );- let faces = self.read_all(&faces_sql, params_from_iter(file_ids), |row| {- Ok(PetFaceVectorRow {- pet_face_id: row.get(0)?,- face_vector_id: row.get(1)?,- species: row.get(2)?,- })- })?;- let bodies = self.read_all(&bodies_sql, params_from_iter(file_ids), |row| {- Ok(PetBodyVectorRow {- pet_body_id: row.get(0)?,- body_vector_id: row.get(1)?,- species: row.get(2)?,- })- })?;- Ok(PetRowsForFiles { faces, bodies })- }-- pub fn delete_pet_rows_for_files(- &self,- file_ids: &[i64],- pet_face_ids: &[String],- pet_body_ids: &[String],- ) -> Result<()> {- let placeholders = bind_placeholders(file_ids.len());- self.db- .write_transaction(|transaction| {- if !pet_face_ids.is_empty() {- transaction.execute(- &format!(- "DELETE FROM pet_face_vector_id_map WHERE pet_face_id IN ({})",- bind_placeholders(pet_face_ids.len())- ),- params_from_iter(pet_face_ids.iter()),- )?;- }- if !pet_body_ids.is_empty() {- transaction.execute(- &format!(- "DELETE FROM pet_body_vector_id_map WHERE pet_body_id IN ({})",- bind_placeholders(pet_body_ids.len())- ),- params_from_iter(pet_body_ids.iter()),- )?;- }- transaction.execute(- &format!("DELETE FROM pet_faces WHERE file_id IN ({placeholders})"),- params_from_iter(file_ids.iter()),- )?;- transaction.execute(- &format!("DELETE FROM pet_bodies WHERE file_id IN ({placeholders})"),- params_from_iter(file_ids.iter()),- )?;- Ok(())- })- .map_err(Into::into)- }-- pub fn get_pet_face_vector_id_map(- &self,- pet_face_ids: &[String],- create_if_missing: bool,- ) -> Result<HashMap<String, i64>> {- self.vector_id_map(- pet_face_ids,- create_if_missing,- "INSERT OR IGNORE INTO pet_face_vector_id_map (pet_face_id) VALUES (?)",- r#"- SELECT pet_face_id, pet_face_vector_id- FROM pet_face_vector_id_map- WHERE pet_face_id IN- "#,- )- }-- pub fn get_pet_body_vector_id_map(- &self,- pet_body_ids: &[String],- create_if_missing: bool,- ) -> Result<HashMap<String, i64>> {- self.vector_id_map(- pet_body_ids,- create_if_missing,- "INSERT OR IGNORE INTO pet_body_vector_id_map (pet_body_id) VALUES (?)",- r#"- SELECT pet_body_id, pet_body_vector_id- FROM pet_body_vector_id_map- WHERE pet_body_id IN- "#,- )- }-- fn vector_id_map(- &self,- ids: &[String],- create_if_missing: bool,- insert_sql: &str,- select_prefix: &str,- ) -> Result<HashMap<String, i64>> {- let unique_ids = unique_in_order(ids);- if unique_ids.is_empty() {- return Ok(HashMap::new());- }- if create_if_missing {- self.write_batch_atomic(insert_sql, unique_ids.iter().map(|id| [id]))?;- }- let select_sql = select_prefix.to_owned() + " ({})";- self.read_chunked_in(&select_sql, &unique_ids, pair)- }-}--fn read_pet_face(row: &Row<'_>) -> db::Result<PetFaceRow> {- Ok(PetFaceRow {- file_id: row.get("file_id")?,- pet_face_id: row.get("pet_face_id")?,- detection_json: row.get("detection")?,- face_vector_id: row.get("face_vector_id")?,- species: row.get("species")?,- face_score: row.get("score")?,- image_height: row.get("height")?,- image_width: row.get("width")?,- ml_version: row.get("ml_version")?,- })-}--fn read_pet_body(row: &Row<'_>) -> db::Result<PetBodyRow> {- Ok(PetBodyRow {- file_id: row.get("file_id")?,- pet_body_id: row.get("pet_body_id")?,- detection_json: row.get("detection")?,- body_vector_id: row.get("body_vector_id")?,- species: row.get("species")?,- score: row.get("score")?,- image_height: row.get("height")?,- image_width: row.get("width")?,- ml_version: row.get("ml_version")?,- })-}--#[cfg(test)]-pub(in crate::ml_db) mod tests {- use std::collections::HashMap;-- use super::{MlDb, PetBodyRow, PetFaceRow};- use crate::ml_db::tests::{cases, check, open, pairs, sorted, strings};- use tempfile::TempDir;-- pub(in crate::ml_db) fn pet_face(file_id: i64, index: i64, species: i64) -> PetFaceRow {- PetFaceRow {- file_id,- pet_face_id: format!("{file_id}_pet_{index}"),- detection_json: "{}".to_string(),- face_vector_id: None,- species,- face_score: 0.7,- image_height: 10,- image_width: 20,- ml_version: 1,- }- }-- fn pet_body(file_id: i64, index: i64, species: i64) -> PetBodyRow {- PetBodyRow {- file_id,- pet_body_id: format!("{file_id}_body_{index}"),- detection_json: "{}".to_string(),- body_vector_id: None,- species,- score: 0.6,- image_height: 10,- image_width: 20,- ml_version: 1,- }- }-- pub(in crate::ml_db) fn seed(db: &MlDb) {- db.bulk_insert_pet_faces(&[- pet_face(1, 0, 0),- pet_face(1, 1, -1),- PetFaceRow {- ml_version: 2,- ..pet_face(2, 0, 1)- },- ])- .unwrap();- db.bulk_insert_pet_bodies(&[pet_body(1, 0, 0), pet_body(3, 0, -1)])- .unwrap();- let face_vector_ids = db- .get_pet_face_vector_id_map(&strings(["1_pet_0", "1_pet_1"]), true)- .unwrap();- db.update_pet_face_vector_ids(&face_vector_ids).unwrap();- let body_vector_ids = db- .get_pet_body_vector_id_map(&strings(["1_body_0"]), true)- .unwrap();- db.update_pet_body_vector_ids(&body_vector_ids).unwrap();- }-- fn seeded() -> (TempDir, MlDb) {- let (directory, db) = open();- seed(&db);- (directory, db)- }-- #[test]- fn seeded_counts() {- let (_directory, db) = seeded();- check(- &db,- &cases![- "pet files": 2 => |db| db.get_pet_indexed_file_count(1),- "pet files v2": 1 => |db| db.get_pet_indexed_file_count(2),- ],- );- }-- #[test]- fn seeded_maps() {- let (_directory, db) = seeded();- check(- &db,- &cases![- "pet face vector ids": pairs([("1_pet_0", 1), ("1_pet_1", 2)]) =>- |db| db.get_pet_face_vector_id_map(&strings(["1_pet_0", "1_pet_1", "2_pet_0"]), false),- "pet body vector ids": pairs([("1_body_0", 1)]) =>- |db| db.get_pet_body_vector_id_map(&strings(["1_body_0"]), false),- ],- );- check(- &db,- &cases![- "pet versions": HashMap::from([(1, 1), (2, 2)]) => |db| db.pet_indexed_file_ids(1),- "pet versions v2": HashMap::from([(2, 2)]) => |db| db.pet_indexed_file_ids(2),- ],- );- }-- #[test]- fn seeded_rows() {- let (_directory, db) = seeded();- let linked_pet_face = PetFaceRow {- face_vector_id: Some(1),- ..pet_face(1, 0, 0)- };- assert_eq!(- db.get_pet_faces_for_file_id(1).unwrap(),- vec![linked_pet_face]- );- let new_pet_face = PetFaceRow {- ml_version: 2,- ..pet_face(2, 0, 1)- };- assert_eq!(db.get_pet_faces_for_file_id(2).unwrap(), vec![new_pet_face]);- assert!(db.get_pet_faces_for_file_id(9).unwrap().is_empty());- let linked_pet_body = PetBodyRow {- body_vector_id: Some(1),- ..pet_body(1, 0, 0)- };- assert_eq!(- db.get_pet_bodies_for_file_id(1).unwrap(),- vec![linked_pet_body]- );- assert!(db.get_pet_bodies_for_file_id(3).unwrap().is_empty());-- let rows = db.get_pet_rows_for_files(&[1, 3]).unwrap();- let faces: Vec<(&str, Option<i64>, i64)> = rows- .faces- .iter()- .map(|row| (row.pet_face_id.as_str(), row.face_vector_id, row.species))- .collect();- assert_eq!(- sorted(faces),- [("1_pet_0", Some(1), 0), ("1_pet_1", Some(2), -1)]- );- let bodies: Vec<(&str, Option<i64>, i64)> = rows- .bodies- .iter()- .map(|row| (row.pet_body_id.as_str(), row.body_vector_id, row.species))- .collect();- assert_eq!(- sorted(bodies),- [("1_body_0", Some(1), 0), ("3_body_0", None, -1)]- );- }-- #[test]- fn pet_upsert_vector_ids_and_deletion() {- let (_directory, db) = seeded();- let updated = PetFaceRow {- face_score: 0.99,- ..pet_face(2, 0, 1)- };- db.bulk_insert_pet_faces(std::slice::from_ref(&updated))- .unwrap();- assert_eq!(db.get_pet_faces_for_file_id(2).unwrap(), vec![updated]);- db.bulk_insert_pet_bodies(&[pet_body(2, 0, 1)]).unwrap();- assert_eq!(- db.get_pet_bodies_for_file_id(2).unwrap(),- vec![pet_body(2, 0, 1)]- );-- let face_vector_ids = db- .get_pet_face_vector_id_map(&strings(["2_pet_0", "1_pet_0", "2_pet_0"]), true)- .unwrap();- assert_eq!(face_vector_ids, pairs([("1_pet_0", 1), ("2_pet_0", 3)]));- assert_eq!(- db.get_pet_body_vector_id_map(&strings(["2_body_0"]), true)- .unwrap(),- pairs([("2_body_0", 2)])- );- db.update_pet_face_vector_ids(&HashMap::new()).unwrap();- db.update_pet_body_vector_ids(&HashMap::new()).unwrap();- db.update_pet_face_vector_ids(&face_vector_ids).unwrap();- assert_eq!(- db.get_pet_faces_for_file_id(2).unwrap()[0].face_vector_id,- Some(3)- );-- db.delete_pet_rows_for_files(- &[1],- &strings(["1_pet_0", "1_pet_1"]),- &strings(["1_body_0"]),- )- .unwrap();- assert!(db.get_pet_faces_for_file_id(1).unwrap().is_empty());- assert!(db.get_pet_bodies_for_file_id(1).unwrap().is_empty());- assert!(- db.get_pet_face_vector_id_map(&strings(["1_pet_0", "1_pet_1"]), false)- .unwrap()- .is_empty()- );- assert!(- db.get_pet_body_vector_id_map(&strings(["1_body_0"]), false)- .unwrap()- .is_empty()- );- db.delete_pet_rows_for_files(&[2], &[], &[]).unwrap();- assert!(db.get_pet_faces_for_file_id(2).unwrap().is_empty());- assert!(db.get_pet_bodies_for_file_id(2).unwrap().is_empty());- assert_eq!(- db.get_pet_face_vector_id_map(&strings(["2_pet_0"]), false)- .unwrap(),- pairs([("2_pet_0", 3)])- );-- db.clear_pet_tables().unwrap();- assert!(- db.get_pet_face_vector_id_map(&strings(["2_pet_0"]), false)- .unwrap()- .is_empty()- );- assert_eq!(db.get_pet_indexed_file_count(0).unwrap(), 0);- }}diff --git a/rust/crates/photos/src/ml_store/index.rs b/rust/crates/photos/src/ml_store/index.rsindex 0d33c2603b..efe4642130 100644--- a/rust/crates/photos/src/ml_store/index.rs+++ b/rust/crates/photos/src/ml_store/index.rs@@ -403,7 +403,6 @@ mod tests {let clip = create_usearch_files(&directory, "ente.ml.vectordb.clip.usearch");let centroid =create_usearch_files(&directory, "ente.ml.vectordb.cluster_centroid.usearch");- let pet = create_usearch_files(&directory, "ente.ml.vectordb.pet.dog_face.usearch");let offline = create_usearch_files(&directory, "ente.ml.offline.vectordb.clip.usearch");assert_eq!(@@ -418,7 +417,6 @@ mod tests {FillOutcome::Completed);assert!(centroid.iter().all(|path| !path.exists()));- assert!(all_exist(&pet));assert!(all_exist(&offline));}