MCPcopy
hub / github.com/ZToolsCenter/ZTools / _clearPluginData

Method _clearPluginData

src/main/api/shared/database.ts:638–717  ·  view source on GitHub ↗

* 清空指定插件的所有数据(供内部调用)

(
    pluginName: string
  )

Source from the content-addressed store, hash-verified

636 * 清空指定插件的所有数据(供内部调用)
637 */
638 private async _clearPluginData(
639 pluginName: string
640 ): Promise<{ success: boolean; deletedCount?: number; error?: string }> {
641 try {
642 const target = this.resolvePluginDataTarget(pluginName)
643 if (!target) {
644 return { success: false, error: '插件标识无效' }
645 }
646 if (target.isHostData) {
647 return { success: false, error: '主程序数据不支持通过该接口清空' }
648 }
649
650 const prefix = target.prefix
651 const allDocs = lmdbInstance.allDocs(prefix)
652
653 let deletedCount = 0
654
655 // 1. 删除主数据库和元数据库的文档
656 for (const doc of allDocs) {
657 const result = lmdbInstance.remove(doc._id)
658 if (result.ok) {
659 deletedCount++
660 }
661 }
662
663 // 2. 清理可能残留的元数据(主数据已删除但 meta 还在的情况)
664 const metaDb = lmdbInstance.getMetaDb()
665 const metaKeysToDelete: string[] = []
666 for (const { key } of metaDb.getRange({
667 start: prefix,
668 end: this.getNextPrefix(prefix)
669 })) {
670 if (key.startsWith(prefix)) {
671 metaKeysToDelete.push(key)
672 }
673 }
674
675 for (const key of metaKeysToDelete) {
676 metaDb.removeSync(key)
677 }
678
679 // 3. 删除附件数据库的附件和元数据
680 const attachmentDb = lmdbInstance.getAttachmentDb()
681 const attachmentPrefix = `attachment:${prefix}`
682 const metadataPrefix = `attachment-ext:${prefix}`
683
684 const attachmentKeysToDelete: string[] = []
685 for (const { key } of attachmentDb.getRange({
686 start: attachmentPrefix,
687 end: this.getNextPrefix(attachmentPrefix)
688 })) {
689 if (key.startsWith(attachmentPrefix)) {
690 attachmentKeysToDelete.push(key)
691 }
692 }
693
694 const metadataKeysToDelete: string[] = []
695 for (const { key } of attachmentDb.getRange({

Callers 2

setupIPCMethod · 0.95
clearPluginDataMethod · 0.95

Calls 8

getNextPrefixMethod · 0.95
getMetaDbMethod · 0.80
getRangeMethod · 0.80
removeSyncMethod · 0.80
getAttachmentDbMethod · 0.80
allDocsMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected