Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathid448676b8f43
diff --git a/web/apps/photos/src/services/face/indexer.ts b/web/apps/photos/src/services/face/indexer.ts
index 5930e60c1e..829638c755 100644
--- a/web/apps/photos/src/services/face/indexer.ts
+++ b/web/apps/photos/src/services/face/indexer.ts
@@ -3,6 +3,7 @@ import { ComlinkWorker } from "@/next/worker/comlink-worker";
import { wait } from "@/utils/promise";
import { type Remote } from "comlink";
import mlIDbStorage, { ML_SEARCH_CONFIG_NAME } from "services/face/db-old";
+import { getLocalFiles } from "services/fileService";
import machineLearningService, {
defaultMLVersion,
} from "services/machineLearning/machineLearningService";
@@ -10,7 +11,7 @@ import mlWorkManager from "services/machineLearning/mlWorkManager";
import type { EnteFile } from "types/file";
import { isInternalUserForML } from "utils/user";
import { indexableAndIndexedCounts, markIndexingFailed } from "./db";
-import type { IndexStatus } from "./db-old";
+import type { IndexStatus, MinimalPersistedFileData } from "./db-old";
import { indexFaces } from "./f-index";
import { FaceIndexerWorker } from "./indexer.worker";
@@ -242,3 +243,70 @@ export const isFaceIndexingEnabled = async () => {
export const setIsFaceIndexingEnabled = async (enabled: boolean) => {
return mlIDbStorage.putConfig(ML_SEARCH_CONFIG_NAME, { enabled });
};
+
+export const syncLocalFiles = async (userID: number) => {
+ const startTime = Date.now();
+ const localFilesMap = await getLocalFilesMap(userID);
+
+ const db = await mlIDbStorage.db;
+ const tx = db.transaction("files", "readwrite");
+ const mlFileIdsArr = await mlIDbStorage.getAllFileIdsForUpdate(tx);
+ const mlFileIds = new Set<number>();
+ mlFileIdsArr.forEach((mlFileId) => mlFileIds.add(mlFileId));
+
+ const newFileIds: Array<number> = [];
+ for (const localFileId of localFilesMap.keys()) {
+ if (!mlFileIds.has(localFileId)) {
+ newFileIds.push(localFileId);
+ }
+ }
+
+ let updated = false;
+ if (newFileIds.length > 0) {
+ log.info("newFiles: ", newFileIds.length);
+ const newFiles = newFileIds.map(
+ (fileId) =>
+ ({
+ fileId,
+ mlVersion: 0,
+ errorCount: 0,
+ }) as MinimalPersistedFileData,
+ );
+ await mlIDbStorage.putAllFiles(newFiles, tx);
+ updated = true;
+ }
+
+ const removedFileIds: Array<number> = [];
+ for (const mlFileId of mlFileIds) {
+ if (!localFilesMap.has(mlFileId)) {
+ removedFileIds.push(mlFileId);
+ }
+ }
+
+ if (removedFileIds.length > 0) {
+ log.info("removedFiles: ", removedFileIds.length);
+ await mlIDbStorage.removeAllFiles(removedFileIds, tx);
+ updated = true;
+ }
+
+ await tx.done;
+
+ if (updated) {
+ // TODO: should do in same transaction
+ await mlIDbStorage.incrementIndexVersion("files");
+ }
+
+ log.info("syncLocalFiles", Date.now() - startTime, "ms");
+
+ return localFilesMap;
+};
+
+const getLocalFilesMap = async (userID: number) => {
+ const localFiles = await getLocalFiles();
+
+ const personalFiles = localFiles.filter((f) => f.ownerID === userID);
+ const localFilesMap = new Map<number, EnteFile>();
+ personalFiles.forEach((f) => localFilesMap.set(f.id, f));
+
+ return localFilesMap;
+};
diff --git a/web/apps/photos/src/services/machineLearning/machineLearningService.ts b/web/apps/photos/src/services/machineLearning/machineLearningService.ts
index f28c3f76c4..4aa2235db4 100644
--- a/web/apps/photos/src/services/machineLearning/machineLearningService.ts
+++ b/web/apps/photos/src/services/machineLearning/machineLearningService.ts
@@ -4,6 +4,7 @@ import PQueue from "p-queue";
import mlIDbStorage, {
type MinimalPersistedFileData,
} from "services/face/db-old";
+import { syncLocalFiles } from "services/face/indexer";
import { FaceIndexerWorker } from "services/face/indexer.worker";
import { getLocalFiles } from "services/fileService";
import { EnteFile } from "types/file";
@@ -65,7 +66,8 @@ class MachineLearningService {
const syncContext = await this.getSyncContext(token, userID, userAgent);
- await this.syncLocalFiles(syncContext);
+ const localFiles = await syncLocalFiles(userID);
+ syncContext.localFilesMap = localFiles;
await this.getOutOfSyncFiles(syncContext);
@@ -102,53 +104,6 @@ class MachineLearningService {
return syncContext.localFilesMap;
}
- private async syncLocalFiles(syncContext: MLSyncContext) {
- const startTime = Date.now();
- const localFilesMap = await this.getLocalFilesMap(syncContext);
-
- const db = await mlIDbStorage.db;
- const tx = db.transaction("files", "readwrite");
- const mlFileIdsArr = await mlIDbStorage.getAllFileIdsForUpdate(tx);
- const mlFileIds = new Set<number>();
- mlFileIdsArr.forEach((mlFileId) => mlFileIds.add(mlFileId));
-
- const newFileIds: Array<number> = [];
- for (const localFileId of localFilesMap.keys()) {
- if (!mlFileIds.has(localFileId)) {
- newFileIds.push(localFileId);
- }
- }
-
- let updated = false;
- if (newFileIds.length > 0) {
- log.info("newFiles: ", newFileIds.length);
- const newFiles = newFileIds.map((fileId) => this.newMlData(fileId));
- await mlIDbStorage.putAllFiles(newFiles, tx);
- updated = true;
- }
-
- const removedFileIds: Array<number> = [];
- for (const mlFileId of mlFileIds) {
- if (!localFilesMap.has(mlFileId)) {
- removedFileIds.push(mlFileId);
- }
- }
-
- if (removedFileIds.length > 0) {
- log.info("removedFiles: ", removedFileIds.length);
- await mlIDbStorage.removeAllFiles(removedFileIds, tx);
- updated = true;
- }
-
- await tx.done;
-
- if (updated) {
- // TODO: should do in same transaction
- await mlIDbStorage.incrementIndexVersion("files");
- }
-
- log.info("syncLocalFiles", Date.now() - startTime, "ms");
- }
private async getOutOfSyncFiles(syncContext: MLSyncContext) {
const startTime = Date.now();