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

View File

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

View File

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

View File

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

View File

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