Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): journal index #8552

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/common/infra/src/modules/db/entities/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class WorkspaceDBTable<
.docState$(this.props.storageDocId)
.map(docState => docState.loading);

ydocId = this.props.storageDocId;

create = this.table.create.bind(this.table);
update = this.table.update.bind(this.table);
get = this.table.get.bind(this.table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
WorkspaceService,
} from '@toeverything/infra';
import {
Document,
Entity,
IndexedDBIndexStorage,
IndexedDBJobQueue,
Expand Down Expand Up @@ -79,7 +80,8 @@ export class DocsIndexer extends Entity {

constructor(
private readonly workspaceService: WorkspaceService,
private readonly workspaceLocalState: WorkspaceLocalState
private readonly workspaceLocalState: WorkspaceLocalState,
private readonly workspaceDBService: WorkspaceDBService
) {
super();
}
Expand Down Expand Up @@ -135,7 +137,11 @@ export class DocsIndexer extends Entity {
await this.workspaceEngine.doc.storage.loadDocFromLocal(
this.workspaceId
);
if (!rootDocBuffer) {
const propertyDBDocBuffer =
await this.workspaceEngine.doc.storage.loadDocFromLocal(
this.workspaceDBService.db.docProperties.ydocId
);
if (!rootDocBuffer || !propertyDBDocBuffer) {
return;
}

Expand All @@ -145,18 +151,20 @@ export class DocsIndexer extends Entity {
type: 'all',
},
{
fields: ['journal'],
pagination: {
limit: Number.MAX_SAFE_INTEGER,
skip: 0,
},
}
)
).nodes.map(n => n.id);
).nodes.map(n => Document.from(n.id, n.fields));

workerOutput = await worker.run({
type: 'rootDoc',
allIndexedDocs,
rootDocBuffer,
propertyDBDocBuffer,
reindexAll: isUpgrade,
});
} else {
Expand Down
30 changes: 22 additions & 8 deletions packages/frontend/core/src/modules/docs-search/worker/in-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function crawlingDocData({
} else {
const ydoc = new YDoc();
let docTitle = '';
let summaryLenNeeded = 1000;
let summaryLenNeeded = 500;
let summary = '';
const blockDocuments: Document<BlockIndexSchema>[] = [];

Expand Down Expand Up @@ -363,16 +363,24 @@ async function crawlingDocData({
}
}

function crawlingRootDocData({
async function crawlingRootDocData({
allIndexedDocs,
rootDocBuffer,
reindexAll,
propertyDBDocBuffer,
}: WorkerInput & {
type: 'rootDoc';
}): WorkerOutput {
const ydoc = new YDoc();
}): Promise<WorkerOutput> {
const rootDocBufferHash = toHexString(await digest(rootDocBuffer));

applyUpdate(ydoc, rootDocBuffer);
let ydoc;
if (cachedRootDoc && cachedRootDoc.hash === rootDocBufferHash) {
ydoc = cachedRootDoc.doc;
} else {
ydoc = new YDoc();
applyUpdate(ydoc, rootDocBuffer);
cachedRootDoc = { doc: ydoc, hash: rootDocBufferHash };
}

const docs = ydoc.getMap('meta').get('pages') as
| YArray<YMap<any>>
Expand All @@ -398,10 +406,16 @@ function crawlingRootDocData({
}
}

const needDelete = difference(allIndexedDocs, availableDocs);
const needDelete = difference(
allIndexedDocs.map(doc => doc.id),
availableDocs
);
const needAdd = reindexAll
? availableDocs
: difference(availableDocs, allIndexedDocs);
: difference(
availableDocs,
allIndexedDocs.map(doc => doc.id)
);

return {
reindexDoc: [...needAdd, ...needDelete].map(docId => ({
Expand All @@ -422,7 +436,7 @@ globalThis.onmessage = async (event: MessageEvent<WorkerIngoingMessage>) => {
try {
let data;
if (input.type === 'rootDoc') {
data = crawlingRootDocData(input);
data = await crawlingRootDocData(input);
} else {
data = await crawlingDocData(input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export type WorkerInput =
| {
type: 'rootDoc';
rootDocBuffer: Uint8Array;
allIndexedDocs: string[];
propertyDBDocBuffer: Uint8Array;
allIndexedDocs: Document<DocIndexSchema>[];
reindexAll?: boolean;
}
| {
Expand Down
Loading