feat(nbstore): add indexer sync version (#11324)

This commit is contained in:
EYHN
2025-03-31 16:48:47 +00:00
parent d31e0c0e71
commit bc0f32f20b
5 changed files with 35 additions and 17 deletions

View File

@@ -1,6 +1,8 @@
import { share } from '../../connection'; import { share } from '../../connection';
import type { DocClock } from '../../storage/doc'; import {
import { IndexerSyncStorageBase } from '../../storage/indexer-sync'; type DocIndexedClock,
IndexerSyncStorageBase,
} from '../../storage/indexer-sync';
import { IDBConnection, type IDBConnectionOptions } from './db'; import { IDBConnection, type IDBConnectionOptions } from './db';
export class IndexedDBIndexerSyncStorage extends IndexerSyncStorageBase { export class IndexedDBIndexerSyncStorage extends IndexerSyncStorageBase {
@@ -12,21 +14,26 @@ export class IndexedDBIndexerSyncStorage extends IndexerSyncStorageBase {
super(); super();
} }
async getDocIndexedClock(docId: string): Promise<DocClock | null> { async getDocIndexedClock(docId: string): Promise<DocIndexedClock | null> {
const tx = this.connection.inner.db.transaction('indexerSync', 'readonly'); const tx = this.connection.inner.db.transaction('indexerSync', 'readonly');
const store = tx.store; const store = tx.store;
const result = await store.get(docId); const result = await store.get(docId);
return result return result
? { docId: result.docId, timestamp: result.indexedClock } ? {
docId: result.docId,
timestamp: result.indexedClock,
indexerVersion: result.indexerVersion ?? 0,
}
: null; : null;
} }
async setDocIndexedClock(docClock: DocClock): Promise<void> { async setDocIndexedClock(docClock: DocIndexedClock): Promise<void> {
const tx = this.connection.inner.db.transaction('indexerSync', 'readwrite'); const tx = this.connection.inner.db.transaction('indexerSync', 'readwrite');
const store = tx.store; const store = tx.store;
await store.put({ await store.put({
docId: docClock.docId, docId: docClock.docId,
indexedClock: docClock.timestamp, indexedClock: docClock.timestamp,
indexerVersion: docClock.indexerVersion,
}); });
} }

View File

@@ -38,9 +38,9 @@ Table(PeerClocks)
| str | str | Date | Date | | str | str | Date | Date |
Table(IndexerSync) Table(IndexerSync)
| docId | clock | | docId | indexedClock | indexerVersion |
|-------|-------| |-------|--------------|----------------|
| str | Date | | str | Date | number |
Table(BlobSync) Table(BlobSync)
| peer | key | uploadedAt | | peer | key | uploadedAt |
@@ -134,6 +134,7 @@ export interface DocStorageSchema extends DBSchema {
value: { value: {
docId: string; docId: string;
indexedClock: Date; indexedClock: Date;
indexerVersion?: number;
}; };
}; };
indexerMetadata: { indexerMetadata: {

View File

@@ -1,13 +1,12 @@
import { DummyConnection } from '../../connection'; import { DummyConnection } from '../../connection';
import type { DocClock } from '../doc'; import { type DocIndexedClock, IndexerSyncStorageBase } from '../indexer-sync';
import { IndexerSyncStorageBase } from '../indexer-sync';
export class DummyIndexerSyncStorage extends IndexerSyncStorageBase { export class DummyIndexerSyncStorage extends IndexerSyncStorageBase {
override connection = new DummyConnection(); override connection = new DummyConnection();
override getDocIndexedClock(_docId: string): Promise<DocClock | null> { override getDocIndexedClock(_docId: string): Promise<DocIndexedClock | null> {
return Promise.resolve(null); return Promise.resolve(null);
} }
override setDocIndexedClock(_docClock: DocClock): Promise<void> { override setDocIndexedClock(_docClock: DocIndexedClock): Promise<void> {
return Promise.resolve(); return Promise.resolve();
} }
override clearDocIndexedClock(_docId: string): Promise<void> { override clearDocIndexedClock(_docId: string): Promise<void> {

View File

@@ -2,12 +2,16 @@ import type { Connection } from '../connection';
import type { DocClock } from './doc'; import type { DocClock } from './doc';
import type { Storage } from './storage'; import type { Storage } from './storage';
export interface DocIndexedClock extends DocClock {
indexerVersion: number;
}
export interface IndexerSyncStorage extends Storage { export interface IndexerSyncStorage extends Storage {
readonly storageType: 'indexerSync'; readonly storageType: 'indexerSync';
getDocIndexedClock(docId: string): Promise<DocClock | null>; getDocIndexedClock(docId: string): Promise<DocIndexedClock | null>;
setDocIndexedClock(docClock: DocClock): Promise<void>; setDocIndexedClock(docClock: DocIndexedClock): Promise<void>;
clearDocIndexedClock(docId: string): Promise<void>; clearDocIndexedClock(docId: string): Promise<void>;
} }
@@ -15,7 +19,7 @@ export interface IndexerSyncStorage extends Storage {
export abstract class IndexerSyncStorageBase implements IndexerSyncStorage { export abstract class IndexerSyncStorageBase implements IndexerSyncStorage {
readonly storageType = 'indexerSync'; readonly storageType = 'indexerSync';
abstract connection: Connection<any>; abstract connection: Connection<any>;
abstract getDocIndexedClock(docId: string): Promise<DocClock | null>; abstract getDocIndexedClock(docId: string): Promise<DocIndexedClock | null>;
abstract setDocIndexedClock(docClock: DocClock): Promise<void>; abstract setDocIndexedClock(docClock: DocIndexedClock): Promise<void>;
abstract clearDocIndexedClock(docId: string): Promise<void>; abstract clearDocIndexedClock(docId: string): Promise<void>;
} }

View File

@@ -65,6 +65,10 @@ export interface IndexerSync {
} }
export class IndexerSyncImpl implements IndexerSync { export class IndexerSyncImpl implements IndexerSync {
/**
* increase this number to re-index all docs
*/
readonly INDEXER_VERSION = 1;
private abort: AbortController | null = null; private abort: AbortController | null = null;
private readonly rootDocId = this.doc.spaceId; private readonly rootDocId = this.doc.spaceId;
private readonly status = new IndexerSyncStatus(this.rootDocId); private readonly status = new IndexerSyncStatus(this.rootDocId);
@@ -351,7 +355,9 @@ export class IndexerSyncImpl implements IndexerSync {
await this.indexerSync.getDocIndexedClock(docId); await this.indexerSync.getDocIndexedClock(docId);
if ( if (
docIndexedClock && docIndexedClock &&
docIndexedClock.timestamp.getTime() === docClock.timestamp.getTime() docIndexedClock.timestamp.getTime() ===
docClock.timestamp.getTime() &&
docIndexedClock.indexerVersion === this.INDEXER_VERSION
) { ) {
// doc is already indexed, just skip // doc is already indexed, just skip
continue; continue;
@@ -406,6 +412,7 @@ export class IndexerSyncImpl implements IndexerSync {
await this.indexerSync.setDocIndexedClock({ await this.indexerSync.setDocIndexedClock({
docId, docId,
timestamp: docClock.timestamp, timestamp: docClock.timestamp,
indexerVersion: this.INDEXER_VERSION,
}); });
// #endregion // #endregion
} }