Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Clear embeddings for files that no longer exist locally (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnukvmd authored Feb 20, 2024
2 parents ceb4a00 + e8b0212 commit 712fb85
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/db/embeddings_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ class EmbeddingsDB {
return await _isar.embeddings.filter().updationTimeEqualTo(null).findAll();
}

Future<void> deleteEmbeddings(List<int> fileIDs) async {
await _isar.writeTxn(() async {
final embeddings = <Embedding>[];
for (final fileID in fileIDs) {
embeddings.addAll(
await _isar.embeddings.filter().fileIDEqualTo(fileID).findAll(),
);
}
await _isar.embeddings.deleteAll(embeddings.map((e) => e.id).toList());
Bus.instance.fire(EmbeddingUpdatedEvent());
});
}

Future<void> deleteAllForModel(Model model) async {
await _isar.writeTxn(() async {
final embeddings =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ class EmbeddingStore {
final fileMap = await FilesDB.instance
.getFilesFromIDs(pendingItems.map((e) => e.fileID).toList());
_logger.info("Pushing ${pendingItems.length} embeddings");
final deletedEntries = <int>[];
for (final item in pendingItems) {
try {
await _pushEmbedding(fileMap[item.fileID]!, item);
final file = fileMap[item.fileID];
if (file != null) {
await _pushEmbedding(file, item);
} else {
deletedEntries.add(item.fileID);
}
} catch (e, s) {
_logger.severe(e, s);
}
}
if (deletedEntries.isNotEmpty) {
await EmbeddingsDB.instance.deleteEmbeddings(deletedEntries);
}
}

Future<void> storeEmbedding(EnteFile file, Embedding embedding) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,23 @@ class SemanticSearchService {

final ignoredCollections =
CollectionsService.instance.getHiddenCollectionIds();
final deletedEntries = <int>[];
for (final result in queryResults) {
final file = filesMap[result.id];
if (file != null && !ignoredCollections.contains(file.collectionID)) {
results.add(filesMap[result.id]!);
results.add(file);
}
if (file == null) {
deletedEntries.add(result.id);
}
}

_logger.info(results.length.toString() + " results");

if (deletedEntries.isNotEmpty) {
unawaited(EmbeddingsDB.instance.deleteEmbeddings(deletedEntries));
}

return results;
}

Expand Down

0 comments on commit 712fb85

Please sign in to comment.