Skip to content

Commit 8920225

Browse files
committed
DocManager 懒加载,初始化加速
1 parent 9ba0716 commit 8920225

File tree

3 files changed

+67
-38
lines changed

3 files changed

+67
-38
lines changed

packages/app/src/docbase/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const getDocBaseConfigFromEnv = (): DocBaseOptions => {
3939
host: MEILI_URL,
4040
apiKey: MEILI_MASTER_KEY
4141
},
42-
initPaths: [INIT_PATH]
42+
initPaths: [INIT_PATH],
43+
initscan: true
4344
}
4445
}
4546

packages/core/src/DocBase.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface DocBaseOptions {
5757
initPlugins?: PluginWithParams<any>[];
5858
/**
5959
* 是否在初始化时扫描初始化知识库目录
60-
* @default true
60+
* @default false
6161
*/
6262
initscan?: boolean;
6363
/**
@@ -217,14 +217,13 @@ export class DocBase {
217217
},
218218
},
219219
],
220-
initscan = true,
220+
initscan = false,
221221
fileOpThrottleMs,
222222
}: DocBaseOptions) => {
223223
console.info("Starting DocBase...");
224224
this.fileOpThrottleMs = fileOpThrottleMs;
225-
// 加载所有插件
226-
// 并行加载所有插件以提高效率
227225
console.info("Loading all plugins...");
226+
// 加载所有插件
228227
await Promise.all(
229228
initPlugins.map((initPlugin) => this.loadPlugin(initPlugin))
230229
);
@@ -248,7 +247,6 @@ export class DocBase {
248247
docLoader: (path) => this.#hyperDocLoader(path),
249248
docSplitter: (text) => this.#docSplitter.func(text),
250249
});
251-
await this.#docManager.init();
252250

253251
// 初始化监视器扫描器
254252
console.info("Initializing watcher and scanner...");

packages/core/src/DocManager.ts

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const chunkDiff = (
6060
* 基于 MeiliSearch 实现的文档管理器
6161
*/
6262
export class DocManager {
63+
#alreadyInit = false
6364
/** MeiliSearch 客户端实例 */
6465
#client: MeiliSearch;
6566

@@ -211,49 +212,72 @@ export class DocManager {
211212
};
212213

213214
/**
214-
* 初始化文档管理器
215+
* 操作前确保初始化完成
215216
*/
216-
init = async () => {
217-
console.info("Initializing DocManager...");
218-
await this.#ensureContainsFilterFeatureOn();
219-
220-
this.#docIndex = await this.#getIndexOrCreate(`${this.#indexPrefix}docs`);
221-
222-
// 获取已有的可筛选属性
223-
const docIndexFilterableAttributes =
224-
await this.#docIndex.getFilterableAttributes();
225-
226-
// 需要设置的可筛选的属性
227-
const docIndexFilterableAttributesNeedCreate = difference(
228-
["path", "chunkHashs"],
229-
docIndexFilterableAttributes
230-
);
231-
232-
// 设置可筛选属性
233-
if (docIndexFilterableAttributesNeedCreate.length > 0) {
234-
console.info(
235-
`Creating filterable attributes for doc index: ${docIndexFilterableAttributesNeedCreate}`
236-
);
237-
await this.#docIndex.updateSettings({
238-
filterableAttributes: [
239-
...docIndexFilterableAttributes,
240-
...docIndexFilterableAttributesNeedCreate,
241-
],
242-
});
217+
#ensureInit = async () => {
218+
if (!this.#alreadyInit) {
219+
await this.#init()
220+
this.#alreadyInit = true
243221
}
222+
}
244223

245-
this.#docChunkIndex = await this.#getIndexOrCreate(
246-
`${this.#indexPrefix}chunks`
247-
);
224+
/**
225+
* 初始化文档管理器
226+
*/
227+
#init = async () => {
228+
await Promise.all([
229+
// 确保 ContainsFilter 功能开启
230+
(async () => {
231+
console.info("Initializing DocManager...");
232+
await this.#ensureContainsFilterFeatureOn();
233+
})(),
234+
// 确保 docIndex 存在
235+
(async () => {
236+
this.#docIndex = await this.#getIndexOrCreate(`${this.#indexPrefix}docs`);
237+
238+
// 获取已有的可筛选属性
239+
const docIndexFilterableAttributes = await this.#docIndex.getFilterableAttributes();
240+
241+
// 需要设置的可筛选的属性
242+
const docIndexFilterableAttributesNeedCreate = difference(
243+
["path", "chunkHashs"],
244+
docIndexFilterableAttributes
245+
);
246+
247+
// 设置可筛选属性
248+
if (docIndexFilterableAttributesNeedCreate.length > 0) {
249+
console.info(
250+
`Creating filterable attributes for doc index: ${docIndexFilterableAttributesNeedCreate}`
251+
);
252+
const task = await this.#docIndex.updateSettings({
253+
filterableAttributes: [
254+
...docIndexFilterableAttributes,
255+
...docIndexFilterableAttributesNeedCreate,
256+
],
257+
});
258+
await this.#docIndex.waitForTask(task.taskUid);
259+
}
260+
})(),
261+
// 确保 docChunkIndex 存在
262+
(async () => {
263+
this.#docChunkIndex = await this.#getIndexOrCreate(
264+
`${this.#indexPrefix}chunks`
265+
);
266+
})()
267+
]);
248268

249269
console.info("DocManager initialized successfully");
250270
};
251271

252272
/** 获取已有 embedders */
253-
getEmbedders = async () => await this.#docChunkIndex.getEmbedders()
273+
getEmbedders = async () => {
274+
await this.#ensureInit()
275+
await this.#docChunkIndex.getEmbedders()
276+
}
254277

255278
/** 重置已有 embedders */
256279
resetEmbedders = async (wait = false) => {
280+
await this.#ensureInit()
257281
const task = await this.#docChunkIndex.resetEmbedders()
258282
if (wait) {
259283
await this.#docChunkIndex.waitForTask(task.taskUid)
@@ -262,6 +286,7 @@ export class DocManager {
262286

263287
/** 增删改 embedders */
264288
updateEmbedders = async (embedders: Embedders, wait = false) => {
289+
await this.#ensureInit()
265290
const task = await this.#docChunkIndex.updateEmbedders(embedders)
266291
if (wait) {
267292
await this.#docChunkIndex.waitForTask(task.taskUid)
@@ -403,6 +428,7 @@ export class DocManager {
403428
};
404429

405430
upsertDoc = async (path: string) => {
431+
await this.#ensureInit()
406432
// 查询该路径有无文档
407433
const [{ mtimeMs }, remoteDocByPath] = await Promise.all([
408434
stat(path),
@@ -509,6 +535,7 @@ export class DocManager {
509535
* @param path - 文档路径
510536
*/
511537
deleteDocByPath = async (path: string) => {
538+
await this.#ensureInit()
512539
console.info(`Deleting document at path: ${path}`);
513540
const doc = await this.#getDocByPathIfExist(path);
514541

@@ -524,6 +551,7 @@ export class DocManager {
524551
* @param path - 目录路径
525552
*/
526553
deleteDocByPathPrefix = async (path: string) => {
554+
await this.#ensureInit()
527555
console.info(`Deleting all documents under path prefix: ${path}`);
528556
const getDocs = async () =>
529557
await this.#docIndex.getDocuments({
@@ -552,6 +580,7 @@ export class DocManager {
552580
* @param hash - 文档 hash
553581
*/
554582
deleteDocByHash = async (hash: string) => {
583+
await this.#ensureInit()
555584
console.info(`Deleting document with hash: ${hash}`);
556585
const doc = await this.#getDocIfExist(hash);
557586

@@ -569,6 +598,7 @@ export class DocManager {
569598
* @returns 返回搜索结果
570599
*/
571600
search = async (query: string, opts?: SearchParams) => {
601+
await this.#ensureInit()
572602
console.debug(`Searching for query: ${query}`);
573603
const result = await this.#docChunkIndex.search(query, opts);
574604
const hits = result.hits;

0 commit comments

Comments
 (0)