* 获取指定插件的所有文档 key(供内部调用)
(pluginName: string)
| 539 | * 获取指定插件的所有文档 key(供内部调用) |
| 540 | */ |
| 541 | private async _getPluginDocKeys(pluginName: string): Promise<{ |
| 542 | success: boolean |
| 543 | data?: Array<{ key: string; type: 'document' | 'attachment' }> |
| 544 | error?: string |
| 545 | }> { |
| 546 | try { |
| 547 | const target = this.resolvePluginDataTarget(pluginName) |
| 548 | if (!target) { |
| 549 | return { success: false, error: '插件标识无效' } |
| 550 | } |
| 551 | const prefix = target.prefix |
| 552 | |
| 553 | // 使用 Set 去重(避免重复添加) |
| 554 | const keySet = new Set<string>() |
| 555 | const keyTypeMap = new Map<string, 'document' | 'attachment'>() |
| 556 | |
| 557 | // 1. 从主数据库获取文档 key |
| 558 | const allDocs = lmdbInstance.allDocs(prefix) |
| 559 | for (const doc of allDocs) { |
| 560 | const key = doc._id.substring(prefix.length) |
| 561 | keySet.add(key) |
| 562 | keyTypeMap.set(key, 'document') |
| 563 | } |
| 564 | |
| 565 | // 2. 从附件数据库获取附件 key |
| 566 | const attachmentDb = lmdbInstance.getAttachmentDb() |
| 567 | const attachmentPrefix = `attachment-ext:${prefix}` |
| 568 | |
| 569 | for (const { key } of attachmentDb.getRange({ |
| 570 | start: attachmentPrefix, |
| 571 | end: this.getNextPrefix(attachmentPrefix) |
| 572 | })) { |
| 573 | if (key.startsWith(attachmentPrefix)) { |
| 574 | const attachmentKey = key.substring(attachmentPrefix.length) |
| 575 | keySet.add(attachmentKey) |
| 576 | keyTypeMap.set(attachmentKey, 'attachment') |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // 3. 转换为数组 |
| 581 | const keys = Array.from(keySet).map((key) => ({ |
| 582 | key, |
| 583 | type: keyTypeMap.get(key) || 'document' |
| 584 | })) |
| 585 | |
| 586 | return { success: true, data: keys } |
| 587 | } catch (error: unknown) { |
| 588 | console.error('[Database] 获取插件文档 key 失败:', error) |
| 589 | return { success: false, error: error instanceof Error ? error.message : String(error) } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * 获取指定插件的文档或附件内容(供内部调用) |
no test coverage detected