Skip to content

armgit / ente

Public Imported from GitHub
0

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

bye

Manav Rathi21567d546e7c
diff --git a/web/apps/photos/src/services/face/db-old.ts b/web/apps/photos/src/services/face/db-old.ts
deleted file mode 100644
index b66c72d811..0000000000
--- a/web/apps/photos/src/services/face/db-old.ts
+++ /dev/null
@@ -1,391 +0,0 @@
-import { haveWindow } from "@/next/env";
-import log from "@/next/log";
-import {
- DBSchema,
- IDBPDatabase,
- IDBPTransaction,
- StoreNames,
- deleteDB,
- openDB,
-} from "idb";
-import isElectron from "is-electron";
-import type { Person } from "services/face/people";
-import type { MlFileData } from "services/face/types-old";
-import { MAX_ML_SYNC_ERROR_COUNT } from "services/machineLearning/machineLearningService";
-
-export interface IndexStatus {
- outOfSyncFilesExists: boolean;
- nSyncedFiles: number;
- nTotalFiles: number;
- localFilesSynced: boolean;
- peopleIndexSynced: boolean;
-}
-
-/**
- * TODO(MR): Transient type with an intersection of values that both existing
- * and new types during the migration will have. Eventually we'll store the the
- * server ML data shape here exactly.
- */
-export interface MinimalPersistedFileData {
- fileID: number;
- mlVersion: number;
- errorCount: number;
- faceEmbedding: { faces: { faceID: string }[] };
-}
-
-interface Config {}
-
-export const ML_SEARCH_CONFIG_NAME = "ml-search";
-
-const MLDATA_DB_NAME = "mldata";
-interface MLDb extends DBSchema {
- files: {
- key: number;
- value: MinimalPersistedFileData;
- indexes: { mlVersion: [number, number] };
- };
- people: {
- key: number;
- value: Person;
- };
- // Unused, we only retain this is the schema so that we can delete it during
- // migration.
- things: {
- key: number;
- value: unknown;
- };
- versions: {
- key: string;
- value: number;
- };
- library: {
- key: string;
- value: unknown;
- };
- configs: {
- key: string;
- value: Config;
- };
-}
-
-class MLIDbStorage {
- public _db: Promise<IDBPDatabase<MLDb>>;
-
- constructor() {
- if (!haveWindow() || !isElectron()) {
- return;
- }
-
- this.db;
- }
-
- private openDB(): Promise<IDBPDatabase<MLDb>> {
- return openDB<MLDb>(MLDATA_DB_NAME, 4, {
- terminated: async () => {
- log.error("ML Indexed DB terminated");
- this._db = undefined;
- // TODO: remove if there is chance of this going into recursion in some case
- await this.db;
- },
- blocked() {
- // TODO: make sure we dont allow multiple tabs of app
- log.error("ML Indexed DB blocked");
- },
- blocking() {
- // TODO: make sure we dont allow multiple tabs of app
- log.error("ML Indexed DB blocking");
- },
- async upgrade(db, oldVersion, newVersion, tx) {
- let wasMLSearchEnabled = false;
- try {
- const searchConfig: unknown = await tx
- .objectStore("configs")
- .get(ML_SEARCH_CONFIG_NAME);
- if (
- searchConfig &&
- typeof searchConfig == "object" &&
- "enabled" in searchConfig &&
- typeof searchConfig.enabled == "boolean"
- ) {
- wasMLSearchEnabled = searchConfig.enabled;
- }
- } catch (e) {
- // The configs store might not exist (e.g. during logout).
- // Ignore.
- }
- log.info(
- `Previous ML database v${oldVersion} had ML search ${wasMLSearchEnabled ? "enabled" : "disabled"}`,
- );
-
- if (oldVersion < 1) {
- const filesStore = db.createObjectStore("files", {
- // keyPath: "fileId", TODO(MR): Changing this, since
- // we're going to be deleting this DB before this PR is
- // merged.
- keyPath: "fileID",
- });
- filesStore.createIndex("mlVersion", [
- "mlVersion",
- "errorCount",
- ]);
-
- db.createObjectStore("people", {
- keyPath: "id",
- });
-
- db.createObjectStore("things", {
- keyPath: "id",
- });
-
- db.createObjectStore("versions");
-
- db.createObjectStore("library");
- }
- if (oldVersion < 2) {
- // TODO: update configs if version is updated in defaults
- db.createObjectStore("configs");
-
- /*
- await tx
- .objectStore("configs")
- .add(
- DEFAULT_ML_SYNC_JOB_CONFIG,
- "ml-sync-job",
- );
-
- await tx
- .objectStore("configs")
- .add(DEFAULT_ML_SYNC_CONFIG, ML_SYNC_CONFIG_NAME);
- */
- }
- if (oldVersion < 3) {
- const DEFAULT_ML_SEARCH_CONFIG = {
- enabled: false,
- };
-
- await tx
- .objectStore("configs")
- .add(DEFAULT_ML_SEARCH_CONFIG, ML_SEARCH_CONFIG_NAME);
- }
- /*
- This'll go in version 5. Note that version 4 was never released,
- but it was in main for a while, so we'll just skip it to avoid
- breaking the upgrade path for people who ran the mainline.
- */
- if (oldVersion < 4) {
- /*
- try {
- await tx
- .objectStore("configs")
- .delete(ML_SEARCH_CONFIG_NAME);
-
- await tx
- .objectStore("configs")
- .delete(""ml-sync"");
-
- await tx
- .objectStore("configs")
- .delete("ml-sync-job");
-
- await tx
- .objectStore("configs")
- .add(
- { enabled: wasMLSearchEnabled },
- ML_SEARCH_CONFIG_NAME,
- );
-
- db.deleteObjectStore("library");
- db.deleteObjectStore("things");
- } catch {
- // TODO: ignore for now as we finalize the new version
- // the shipped implementation should have a more
- // deterministic migration.
- }
- */
- }
- log.info(
- `ML DB upgraded from version ${oldVersion} to version ${newVersion}`,
- );
- },
- });
- }
-
- public get db(): Promise<IDBPDatabase<MLDb>> {
- if (!this._db) {
- this._db = this.openDB();
- log.info("Opening Ml DB");
- }
-
- return this._db;
- }
-
- public async clearMLDB() {
- const db = await this.db;
- db.close();
- await deleteDB(MLDATA_DB_NAME);
- log.info("Cleared Ml DB");
- this._db = undefined;
- await this.db;
- }
-
- public async getAllFileIdsForUpdate(
- tx: IDBPTransaction<MLDb, ["files"], "readwrite">,
- ) {
- return tx.store.getAllKeys();
- }
-
- public async getFileIds(
- count: number,
- limitMlVersion: number,
- maxErrorCount: number,
- ) {
- const db = await this.db;
- const tx = db.transaction("files", "readonly");
- const index = tx.store.index("mlVersion");
- let cursor = await index.openKeyCursor(
- IDBKeyRange.upperBound([limitMlVersion], true),
- );
-
- const fileIds: number[] = [];
- while (cursor && fileIds.length < count) {
- if (
- cursor.key[0] < limitMlVersion &&
- cursor.key[1] <= maxErrorCount
- ) {
- fileIds.push(cursor.primaryKey);
- }
- cursor = await cursor.continue();
- }
- await tx.done;
-
- return fileIds;
- }
-
- public async getFile(fileId: number): Promise<MinimalPersistedFileData> {
- const db = await this.db;
- return db.get("files", fileId);
- }
-
- public async putFile(mlFile: MlFileData) {
- const db = await this.db;
- return db.put("files", mlFile);
- }
-
- public async upsertFileInTx(
- fileId: number,
- upsert: (mlFile: MinimalPersistedFileData) => MinimalPersistedFileData,
- ) {
- const db = await this.db;
- const tx = db.transaction("files", "readwrite");
- const existing = await tx.store.get(fileId);
- const updated = upsert(existing);
- await tx.store.put(updated);
- await tx.done;
-
- return updated;
- }
-
- public async putAllFiles(
- mlFiles: MinimalPersistedFileData[],
- tx: IDBPTransaction<MLDb, ["files"], "readwrite">,
- ) {
- await Promise.all(mlFiles.map((mlFile) => tx.store.put(mlFile)));
- }
-
- public async removeAllFiles(
- fileIds: Array<number>,
- tx: IDBPTransaction<MLDb, ["files"], "readwrite">,
- ) {
- await Promise.all(fileIds.map((fileId) => tx.store.delete(fileId)));
- }
-
- public async getPerson(id: number) {
- const db = await this.db;
- return db.get("people", id);
- }
-
- public async getAllPeople() {
- const db = await this.db;
- return db.getAll("people");
- }
-
- public async incrementIndexVersion(index: StoreNames<MLDb>) {
- if (index === "versions") {
- throw new Error("versions store can not be versioned");
- }
- const db = await this.db;
- const tx = db.transaction(["versions", index], "readwrite");
- let version = await tx.objectStore("versions").get(index);
- version = (version || 0) + 1;
- tx.objectStore("versions").put(version, index);
- await tx.done;
-
- return version;
- }
-
- public async getConfig<T extends Config>(name: string, def: T) {
- const db = await this.db;
- const tx = db.transaction("configs", "readwrite");
- let config = (await tx.store.get(name)) as T;
- if (!config) {
- config = def;
- await tx.store.put(def, name);
- }
- await tx.done;
-
- return config;
- }
-
- public async putConfig(name: string, data: Config) {
- const db = await this.db;
- return db.put("configs", data, name);
- }
-
- public async getIndexStatus(latestMlVersion: number): Promise<IndexStatus> {
- const db = await this.db;
- const tx = db.transaction(["files", "versions"], "readonly");
- const mlVersionIdx = tx.objectStore("files").index("mlVersion");
-
- let outOfSyncCursor = await mlVersionIdx.openKeyCursor(
- IDBKeyRange.upperBound([latestMlVersion], true),
- );
- let outOfSyncFilesExists = false;
- while (outOfSyncCursor && !outOfSyncFilesExists) {
- if (
- outOfSyncCursor.key[0] < latestMlVersion &&
- outOfSyncCursor.key[1] <= MAX_ML_SYNC_ERROR_COUNT
- ) {
- outOfSyncFilesExists = true;
- }
- outOfSyncCursor = await outOfSyncCursor.continue();
- }
-
- const nSyncedFiles = await mlVersionIdx.count(
- IDBKeyRange.lowerBound([latestMlVersion]),
- );
- const nTotalFiles = await mlVersionIdx.count();
-
- const filesIndexVersion = await tx.objectStore("versions").get("files");
- const peopleIndexVersion = await tx
- .objectStore("versions")
- .get("people");
- const filesIndexVersionExists =
- filesIndexVersion !== null && filesIndexVersion !== undefined;
- const peopleIndexVersionExists =
- peopleIndexVersion !== null && peopleIndexVersion !== undefined;
-
- await tx.done;
-
- return {
- outOfSyncFilesExists,
- nSyncedFiles,
- nTotalFiles,
- localFilesSynced: filesIndexVersionExists,
- peopleIndexSynced:
- peopleIndexVersionExists &&
- peopleIndexVersion === filesIndexVersion,
- };
- }
-}
-
-export default new MLIDbStorage();
diff --git a/web/apps/photos/src/services/face/db.ts b/web/apps/photos/src/services/face/db.ts
index afc9581382..ab03b726f7 100644
--- a/web/apps/photos/src/services/face/db.ts
+++ b/web/apps/photos/src/services/face/db.ts
@@ -82,6 +82,8 @@ interface FileStatus {
let _faceDB: ReturnType<typeof openFaceDB> | undefined;
const openFaceDB = async () => {
+ deleteLegacyDB();
+
const db = await openDB<FaceDBSchema>("face", 1, {
upgrade(db, oldVersion, newVersion) {
log.info(`Upgrading face DB ${oldVersion} => ${newVersion}`);
@@ -112,6 +114,13 @@ const openFaceDB = async () => {
return db;
};
+const deleteLegacyDB = () => {
+ // Delete the legacy face DB.
+ // This code was added June 2024 (v1.7.1-rc) and can be removed once clients
+ // have migrated over.
+ void deleteDB("mldata");
+};
+
/**
* @returns a lazily created, cached connection to the face DB.
*/
@@ -138,6 +147,7 @@ export const closeFaceDBConnectionsIfNeeded = async () => {
* Meant to be called during logout.
*/
export const clearFaceData = async () => {
+ deleteLegacyDB();
await closeFaceDBConnectionsIfNeeded();
return deleteDB("face", {
blocked() {
diff --git a/web/apps/photos/src/services/face/types-old.ts b/web/apps/photos/src/services/face/types-old.ts
deleted file mode 100644
index d2bf61a4f8..0000000000
--- a/web/apps/photos/src/services/face/types-old.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import type { FaceIndex } from "./types";
-
-export type MlFileData = FaceIndex & {
- mlVersion: number;
- errorCount: number;
-};
diff --git a/web/apps/photos/src/services/machineLearning/machineLearningService.ts b/web/apps/photos/src/services/machineLearning/machineLearningService.ts
index 6612e3c443..196f3d46e8 100644
--- a/web/apps/photos/src/services/machineLearning/machineLearningService.ts
+++ b/web/apps/photos/src/services/machineLearning/machineLearningService.ts
@@ -9,8 +9,6 @@ export const defaultMLVersion = 1;
const batchSize = 200;
-export const MAX_ML_SYNC_ERROR_COUNT = 1;
-
class MLSyncContext {
public token: string;
public userID: number;
diff --git a/web/apps/photos/src/services/machineLearning/mlWorkManager.ts b/web/apps/photos/src/services/machineLearning/mlWorkManager.ts
index 2d58efc32c..9502c5a75d 100644
--- a/web/apps/photos/src/services/machineLearning/mlWorkManager.ts
+++ b/web/apps/photos/src/services/machineLearning/mlWorkManager.ts
@@ -8,7 +8,6 @@ import { getToken, getUserID } from "@ente/shared/storage/localStorage/helpers";
import debounce from "debounce";
import PQueue from "p-queue";
import { createFaceComlinkWorker } from "services/face";
-import mlIDbStorage from "services/face/db-old";
import type { DedicatedMLWorker } from "services/face/face.worker";
import { EnteFile } from "types/file";
@@ -167,7 +166,6 @@ class MLWorkManager {
this.stopSyncJob();
this.mlSyncJob = undefined;
await this.terminateLiveSyncWorker();
- await mlIDbStorage.clearMLDB();
}
private async fileUploadedHandler(arg: {