11import { 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' ;
38import {
49 Entity ,
510 IndexedDBIndexStorage ,
@@ -21,12 +26,18 @@ export function isEmptyUpdate(binary: Uint8Array) {
2126}
2227
2328const logger = new DebugLogger ( 'crawler' ) ;
29+ const WORKSPACE_DOCS_INDEXER_VERSION_KEY = 'docs-indexer-version' ;
2430
2531interface IndexerJobPayload {
2632 storageDocId : string ;
2733}
2834
2935export 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 }
0 commit comments