Skip to content

armgit / ente

Public Imported from GitHub
0

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

Extra safeguards around migration and offline mode

laurenspriem7cf0a5b56b9a
diff --git a/mobile/apps/photos/lib/db/ml/clip_vector_db.dart b/mobile/apps/photos/lib/db/ml/clip_vector_db.dart
index 478b6ff86a..433b046ddd 100644
--- a/mobile/apps/photos/lib/db/ml/clip_vector_db.dart
+++ b/mobile/apps/photos/lib/db/ml/clip_vector_db.dart
@@ -47,6 +47,13 @@ class ClipVectorDB {
final documentsDirectory = await getApplicationDocumentsDirectory();
final String dbPath = join(documentsDirectory.path, _databaseName);
_logger.info("Opening vectorDB access: DB path " + dbPath);
+ final indexFile = File(dbPath);
+ if (!await indexFile.exists() && await checkIfMigrationDone()) {
+ _logger.severe(
+ "VectorDB file is missing while migration is marked done. Invalidating migration state.",
+ );
+ await invalidateMigrationState();
+ }
late VectorDb vectorDB;
try {
vectorDB = VectorDb(
@@ -56,7 +63,7 @@ class ClipVectorDB {
} catch (e, s) {
_logger.severe("Could not open VectorDB at path $dbPath", e, s);
_logger.severe("Deleting the index file and trying again");
- await deleteIndexFile();
+ await deleteIndexFile(undoMigration: true);
try {
vectorDB = VectorDb(
filePath: dbPath,
@@ -96,6 +103,13 @@ class ClipVectorDB {
_migrationDone = true;
}
+ Future<void> invalidateMigrationState() async {
+ _logger.info("Invalidating ClipVectorDB migration state");
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.setBool(_migrationKey, false);
+ _migrationDone = false;
+ }
+
Future<void> insertEmbedding({
required int fileID,
required List<double> embedding,
@@ -156,6 +170,7 @@ class ClipVectorDB {
}
Future<void> deleteAllEmbeddings() async {
+ await invalidateMigrationState();
final db = await _vectorDB;
try {
await db.resetIndex();
@@ -259,6 +274,11 @@ class ClipVectorDB {
double minimumSimilarity,
) async {
final db = await _vectorDB;
+ if (!await checkIfMigrationDone()) {
+ throw StateError(
+ "ClipVectorDB migration is not done, cannot run approximate search",
+ );
+ }
try {
final result = await db.approxSearchVectorsWithinSimilarity(
query: query,
@@ -365,6 +385,7 @@ class ClipVectorDB {
}
Future<void> deleteIndex() async {
+ await invalidateMigrationState();
final db = await _vectorDB;
try {
await db.deleteIndex();
@@ -388,12 +409,12 @@ class ClipVectorDB {
_logger.info("Deleted index file on disk");
_vectorDbFuture = null;
_warmupFuture = null;
- if (undoMigration) {
- final prefs = await SharedPreferences.getInstance();
- await prefs.setBool(_migrationKey, false);
- _migrationDone = false;
- _logger.info("Undid migration flag");
+ if (!undoMigration) {
+ _logger.info(
+ "Index file was deleted without explicit undo flag; invalidating migration state regardless.",
+ );
}
+ await invalidateMigrationState();
} catch (e, s) {
_logger.severe("Error deleting index file on disk", e, s);
rethrow;
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dart
index 237aecf51b..a786518e7a 100644
--- a/mobile/apps/photos/lib/db/ml/db.dart
+++ b/mobile/apps/photos/lib/db/ml/db.dart
@@ -278,7 +278,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
await db.execute(deleteClipEmbeddingsTable);
await db.execute(deleteFileDataTable);
if (await _clipVectorDB.checkIfMigrationDone()) {
- await _clipVectorDB.deleteIndexFile();
+ await _clipVectorDB.deleteIndexFile(undoMigration: true);
}
}
@@ -1291,6 +1291,7 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
final totalCount = countResult.first['total'] as int;
if (totalCount == 0) {
_logger.info("No clip embeddings to migrate");
+ await _clipVectorDB.deleteAllEmbeddings();
await _clipVectorDB.setMigrationDone();
return;
}
diff --git a/mobile/apps/photos/lib/ui/settings/debug/ml_debug_section_widget.dart b/mobile/apps/photos/lib/ui/settings/debug/ml_debug_section_widget.dart
index 26db287665..003f7aadbb 100644
--- a/mobile/apps/photos/lib/ui/settings/debug/ml_debug_section_widget.dart
+++ b/mobile/apps/photos/lib/ui/settings/debug/ml_debug_section_widget.dart
@@ -114,7 +114,10 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {
trailingIconIsMuted: true,
onTap: () async {
try {
- await ClipVectorDB.instance.deleteIndexFile(undoMigration: true);
+ final clipVectorDB = isOfflineMode
+ ? ClipVectorDB.offlineInstance
+ : ClipVectorDB.instance;
+ await clipVectorDB.deleteIndexFile(undoMigration: true);
await SimilarImagesService.instance.clearCache();
showShortToast(context, 'Deleted vectorDB index');
} catch (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.dart
index 3ec9cdc3c2..369ab0cb75 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
@@ -771,7 +771,10 @@ class _MLDebugSettingsPageState extends State<MLDebugSettingsPage> {
firstButtonLabel: "Yes, confirm",
firstButtonOnTap: () async {
try {
- await ClipVectorDB.instance.deleteIndexFile(undoMigration: true);
+ final vectorDB = isOfflineMode
+ ? ClipVectorDB.offlineInstance
+ : ClipVectorDB.instance;
+ await vectorDB.deleteIndexFile(undoMigration: true);
showShortToast(context, "Done");
} catch (e, s) {
logger.warning('reset usearch index failed ', e, s);