import "dart:io"; import "package:isar/isar.dart"; import 'package:path_provider/path_provider.dart'; import "package:photos/core/event_bus.dart"; import "package:photos/events/embedding_updated_event.dart"; import "package:photos/models/embedding.dart"; class EmbeddingsDB { late final Isar _isar; EmbeddingsDB._privateConstructor(); static final EmbeddingsDB instance = EmbeddingsDB._privateConstructor(); Future init() async { final dir = await getApplicationDocumentsDirectory(); _isar = await Isar.open( [EmbeddingSchema], directory: dir.path, ); await _clearDeprecatedStore(dir); } Future clearTable() async { await _isar.writeTxn(() => _isar.clear()); } Future> getAll(Model model) async { return _isar.embeddings.filter().modelEqualTo(model).findAll(); } Future put(Embedding embedding) { return _isar.writeTxn(() async { await _isar.embeddings.putByIndex(Embedding.index, embedding); Bus.instance.fire(EmbeddingUpdatedEvent()); }); } Future putMany(List embeddings) { return _isar.writeTxn(() async { await _isar.embeddings.putAllByIndex(Embedding.index, embeddings); Bus.instance.fire(EmbeddingUpdatedEvent()); }); } Future> getUnsyncedEmbeddings() async { return await _isar.embeddings.filter().updationTimeEqualTo(null).findAll(); } Future deleteAllForModel(Model model) async { await _isar.writeTxn(() async { final embeddings = await _isar.embeddings.filter().modelEqualTo(model).findAll(); await _isar.embeddings.deleteAll(embeddings.map((e) => e.id).toList()); Bus.instance.fire(EmbeddingUpdatedEvent()); }); } Future _clearDeprecatedStore(Directory dir) async { final deprecatedStore = Directory(dir.path + "/object-box-store"); if (await deprecatedStore.exists()) { await deprecatedStore.delete(recursive: true); } } }