Skip to content

Commit fb64bc7

Browse files
committed
feat(core): indexer upgrade (#8062)
1 parent 51f3566 commit fb64bc7

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

packages/frontend/core/src/modules/docs-search/entities/docs-indexer.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { DebugLogger } from '@affine/debug';
2-
import type { Job, JobQueue, WorkspaceService } from '@toeverything/infra';
2+
import type {
3+
Job,
4+
JobQueue,
5+
WorkspaceLocalState,
6+
WorkspaceService,
7+
} from '@toeverything/infra';
38
import {
49
Entity,
510
IndexedDBIndexStorage,
@@ -21,12 +26,18 @@ export function isEmptyUpdate(binary: Uint8Array) {
2126
}
2227

2328
const logger = new DebugLogger('crawler');
29+
const WORKSPACE_DOCS_INDEXER_VERSION_KEY = 'docs-indexer-version';
2430

2531
interface IndexerJobPayload {
2632
storageDocId: string;
2733
}
2834

2935
export class DocsIndexer extends Entity {
36+
/**
37+
* increase this number to re-index all docs
38+
*/
39+
static INDEXER_VERSION = 1;
40+
3041
private readonly jobQueue: JobQueue<IndexerJobPayload> =
3142
new IndexedDBJobQueue<IndexerJobPayload>(
3243
'jq:' + this.workspaceService.workspace.id
@@ -66,7 +77,10 @@ export class DocsIndexer extends Entity {
6677
{}
6778
);
6879

69-
constructor(private readonly workspaceService: WorkspaceService) {
80+
constructor(
81+
private readonly workspaceService: WorkspaceService,
82+
private readonly workspaceLocalState: WorkspaceLocalState
83+
) {
7084
super();
7185
}
7286

@@ -96,6 +110,16 @@ export class DocsIndexer extends Entity {
96110
return;
97111
}
98112

113+
const dbVersion = this.getVersion();
114+
115+
if (dbVersion > DocsIndexer.INDEXER_VERSION) {
116+
// stop if db version is higher then self
117+
this.runner.stop();
118+
throw new Error('Indexer is outdated');
119+
}
120+
121+
const isUpgrade = dbVersion < DocsIndexer.INDEXER_VERSION;
122+
99123
// jobs should have the same storage docId, so we just pick the first one
100124
const storageDocId = jobs[0].payload.storageDocId;
101125

@@ -111,7 +135,6 @@ export class DocsIndexer extends Entity {
111135
await this.workspaceEngine.doc.storage.loadDocFromLocal(
112136
this.workspaceId
113137
);
114-
115138
if (!rootDocBuffer) {
116139
return;
117140
}
@@ -134,6 +157,7 @@ export class DocsIndexer extends Entity {
134157
type: 'rootDoc',
135158
allIndexedDocs,
136159
rootDocBuffer,
160+
reindexAll: isUpgrade,
137161
});
138162
} else {
139163
const rootDocBuffer =
@@ -226,6 +250,10 @@ export class DocsIndexer extends Entity {
226250
);
227251
}
228252

253+
if (isUpgrade) {
254+
this.setVersion();
255+
}
256+
229257
const duration = performance.now() - startTime;
230258
logger.debug(
231259
'Finish crawling job for storageDocId:' +
@@ -238,6 +266,7 @@ export class DocsIndexer extends Entity {
238266

239267
startCrawling() {
240268
this.runner.start();
269+
241270
this.jobQueue
242271
.enqueue([
243272
{
@@ -257,6 +286,27 @@ export class DocsIndexer extends Entity {
257286
return this.worker;
258287
}
259288

289+
getVersion() {
290+
const version = this.workspaceLocalState.get<number>(
291+
WORKSPACE_DOCS_INDEXER_VERSION_KEY
292+
);
293+
if (typeof version !== 'number') {
294+
return -1;
295+
} else {
296+
return version;
297+
}
298+
}
299+
300+
setVersion(version = DocsIndexer.INDEXER_VERSION) {
301+
if (this.getVersion() >= version) {
302+
return;
303+
}
304+
return this.workspaceLocalState.set(
305+
WORKSPACE_DOCS_INDEXER_VERSION_KEY,
306+
version
307+
);
308+
}
309+
260310
override dispose(): void {
261311
this.runner.stop();
262312
}

packages/frontend/core/src/modules/docs-search/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { DocsSearchService } from './services/docs-search';
22

33
import {
44
type Framework,
5+
WorkspaceLocalState,
56
WorkspaceScope,
67
WorkspaceService,
78
} from '@toeverything/infra';
@@ -13,5 +14,5 @@ export function configureDocsSearchModule(framework: Framework) {
1314
framework
1415
.scope(WorkspaceScope)
1516
.service(DocsSearchService, [WorkspaceService])
16-
.entity(DocsIndexer, [WorkspaceService]);
17+
.entity(DocsIndexer, [WorkspaceService, WorkspaceLocalState]);
1718
}

packages/frontend/core/src/modules/docs-search/worker/in-worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ async function crawlingDocData({
285285
function crawlingRootDocData({
286286
allIndexedDocs,
287287
rootDocBuffer,
288+
reindexAll,
288289
}: WorkerInput & {
289290
type: 'rootDoc';
290291
}): WorkerOutput {
@@ -317,7 +318,9 @@ function crawlingRootDocData({
317318
}
318319

319320
const needDelete = difference(allIndexedDocs, availableDocs);
320-
const needAdd = difference(availableDocs, allIndexedDocs);
321+
const needAdd = reindexAll
322+
? availableDocs
323+
: difference(availableDocs, allIndexedDocs);
321324

322325
return {
323326
reindexDoc: [...needAdd, ...needDelete].map(docId => ({

packages/frontend/core/src/modules/docs-search/worker/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type WorkerInput =
3131
type: 'rootDoc';
3232
rootDocBuffer: Uint8Array;
3333
allIndexedDocs: string[];
34+
reindexAll?: boolean;
3435
}
3536
| {
3637
type: 'doc';

0 commit comments

Comments
 (0)