From cf3b75702192ceb0baf922015ef25639958eef6e Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 19 Aug 2024 14:26:55 +0530 Subject: [PATCH] Most recent file IDs first --- web/packages/new/photos/services/ml/db.ts | 17 +++++++++++++---- web/packages/new/photos/services/ml/worker.ts | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/web/packages/new/photos/services/ml/db.ts b/web/packages/new/photos/services/ml/db.ts index 7252483845..91f0dd21fa 100644 --- a/web/packages/new/photos/services/ml/db.ts +++ b/web/packages/new/photos/services/ml/db.ts @@ -394,14 +394,23 @@ export const indexableAndIndexedCounts = async () => { * universe, we filter out fileIDs the files corresponding to which have already * been indexed, or which should be ignored. * - * @param count Limit the result to up to {@link count} items. + * @param count Limit the result to up to {@link count} items. If there are more + * than {@link count} items present, the files with the higher file IDs (which + * can be taken as a approximate for their creation order) are preferred. */ -export const indexableFileIDs = async (count?: number) => { +export const indexableFileIDs = async (count: number) => { const db = await mlDB(); const tx = db.transaction("file-status", "readonly"); - return tx.store + let cursor = await tx.store .index("status") - .getAllKeys(IDBKeyRange.only("indexable"), count); + .openKeyCursor(IDBKeyRange.only("indexable"), "prev"); + const result: number[] = []; + while (cursor && count > 0) { + result.push(cursor.primaryKey); + cursor = await cursor.continue(); + count -= 1; + } + return result; }; /** diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts index 79520cc076..27dd602aef 100644 --- a/web/packages/new/photos/services/ml/worker.ts +++ b/web/packages/new/photos/services/ml/worker.ts @@ -339,6 +339,8 @@ const indexNextBatch = async ( * about. Then return the next {@link count} files that still need to be * indexed. * + * When returning from amongst pending files, prefer the most recent ones first. + * * For specifics of what a "sync" entails, see {@link updateAssumingLocalFiles}. * * @param userID Sync only files owned by a {@link userID} with the face DB.