refactor(kb): 文件主表 + 文档关联改用雪花ID(弃用按名关联)
把 sundynix_doc 明确为"文件主表",补齐文件基础信息字段; 文档间 [[双链]] 改为以 Doc.ID 关联,查询/渲染一律按文件 ID。 store: - Doc 增 Ext(后缀)/MD5(内容指纹) 字段;ObjectKey 即"存放链接" - DocLink 由 (FromName,ToName) 改为 (FromID,ToID,ToName) · FromID/ToID 关联 Doc.ID;ToName 保留用于悬空链接展示与回填 - SaveDoc 返回新建/更新文件的雪花 ID(供建链) - 新增 GetDocByID(按 ID + owner 取正文,防越权) - ReplaceDocLinks 以 fromID 重建出链,按 [[名称]] 解析目标 ID - 新增 ResolveInboundLinks:目标入库后回填指向它的悬空链接 - ListLinks 只返回已解析(to_id 非空)的 ID→ID 边 - migrateDocLinkToID:旧按名双链表无 from_id 列则重建为按 ID 关联 gateway/handler: - runIngest 计算 ext/md5,SaveDoc 取回 ID 后建链 + 回填悬空 - KbDoc 改为 GET ?id=(按文件 ID 取全文) - KbVault 返回 id+ext;KbLinks 返回 from/to 为 ID desktop: - VaultDoc 增 id/ext;getDoc(docId) 按 ID 取正文 - VaultPanel 选中态/正文/反链/关系图改用 ID,名↔ID 双向映射 渲染;保存笔记后按名定位回其新 ID 验证(gateway+PG+MinIO 实测):vault 带 id+ext;双链 ID→ID 且 A→B 悬空链接在 B 入库后成功回填;按 ID 取大文档(15006字)从 MinIO 完整取回;跨 owner 按 ID 取文档 404(隔离生效)。桌面端 文库 Tab 按 ID 选中/载入/反链渲染正常,无控制台报错。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -84,52 +85,59 @@ func (p *Postgres) DeleteAgent(ctx context.Context, owner, name string) error {
|
||||
return p.db.WithContext(ctx).Where("owner = ? AND name = ?", owner, name).Delete(&Agent{}).Error
|
||||
}
|
||||
|
||||
// Doc 是入库的一份原始文档/笔记(供 Obsidian 式"文库"浏览:列表 + Markdown 阅读 + 双链)。
|
||||
// 表名 sundynix_doc。(owner,kb,name) 唯一;按 owner 隔离。
|
||||
// Doc 是入库的一份文件/笔记 —— 文件主表(供 Obsidian 式"文库"浏览:列表 + Markdown 阅读 + 双链)。
|
||||
// 表名 sundynix_doc。(owner,kb,name) 唯一;按 owner 隔离。文档间关联一律用本表的雪花 ID 关联。
|
||||
type Doc struct {
|
||||
BaseModel
|
||||
Owner string `gorm:"size:64;uniqueIndex:idx_doc_okn"`
|
||||
KB string `gorm:"size:64;uniqueIndex:idx_doc_okn"`
|
||||
Name string `gorm:"size:160;uniqueIndex:idx_doc_okn"`
|
||||
Name string `gorm:"size:160;uniqueIndex:idx_doc_okn"` // 文件名称(不含扩展名 / 笔记名)
|
||||
Ext string `gorm:"size:16"` // 文件后缀(.md/.pdf/.docx…;笔记/文本为空)
|
||||
MD5 string `gorm:"size:32;index"` // 正文内容指纹(去重 / 校验)
|
||||
Size int // 原文字数(rune)
|
||||
Preview string `gorm:"size:600"` // 前若干字预览(列表/反链用,不拉全文)
|
||||
Content string `gorm:"type:text"` // 小文档内联正文;大文档转 MinIO 后置空
|
||||
ObjectKey string `gorm:"size:160"` // 大文档在 MinIO 的对象键(空=内联 Content)
|
||||
ObjectKey string `gorm:"size:160"` // 存放链接:大文档在 MinIO 的对象键(空=内联 Content)
|
||||
}
|
||||
|
||||
func (Doc) TableName() string { return "sundynix_doc" }
|
||||
|
||||
// DocLink 是文档间 [[双链]] 的索引(owner+kb 内 from→to),供反链/笔记关系图按 SQL 查询,
|
||||
// 避免在前端扫全部正文。入库/编辑时按 from 文档重建其出链。
|
||||
// DocLink 是文档间 [[双链]] 的索引(owner+kb 内 from→to),以 Doc.ID 关联,供反链/笔记关系图按 SQL 查询,
|
||||
// 避免在前端扫全部正文。入库/编辑时按 from 文档重建其出链;目标尚未入库时 ToID 为空(悬空,记 ToName 待回填)。
|
||||
type DocLink struct {
|
||||
BaseModel
|
||||
Owner string `gorm:"size:64;index:idx_link_okf"`
|
||||
KB string `gorm:"size:64;index:idx_link_okf"`
|
||||
FromName string `gorm:"size:160;index:idx_link_okf"`
|
||||
ToName string `gorm:"size:160;index"`
|
||||
Owner string `gorm:"size:64;index:idx_link_of"`
|
||||
KB string `gorm:"size:64;index:idx_link_of"`
|
||||
FromID string `gorm:"size:24;index:idx_link_of"` // 源文档 Doc.ID
|
||||
ToID string `gorm:"size:24;index"` // 目标文档 Doc.ID(空=悬空:目标尚未入库)
|
||||
ToName string `gorm:"size:160"` // [[原始名]],供悬空链接展示 / 目标入库后回填 ToID
|
||||
}
|
||||
|
||||
func (DocLink) TableName() string { return "sundynix_doc_link" }
|
||||
|
||||
// docHead 取前 n 个 rune 作预览。
|
||||
func docHead(s string, n int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= n {
|
||||
return s
|
||||
}
|
||||
return string(r[:n])
|
||||
}
|
||||
|
||||
// SaveDoc 写入/更新一份文档(owner+kb+name 唯一,重名覆盖)。
|
||||
// content 为内联正文(大文档转 MinIO 时传空 + objectKey);preview/size 由调用方按全文给出。
|
||||
func (p *Postgres) SaveDoc(ctx context.Context, owner, kb, name, content, objectKey string, size int, preview string) error {
|
||||
// SaveDoc 写入/更新一份文件(owner+kb+name 唯一,重名覆盖),返回该文件的雪花 ID(供关联用)。
|
||||
// content 为内联正文(大文档转 MinIO 时传空 + objectKey);ext/md5/preview/size 由调用方按全文给出。
|
||||
func (p *Postgres) SaveDoc(ctx context.Context, owner, kb, name, ext, md5, content, objectKey string, size int, preview string) (string, error) {
|
||||
if p.db == nil {
|
||||
return nil
|
||||
return "", nil
|
||||
}
|
||||
return p.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "owner"}, {Name: "kb"}, {Name: "name"}},
|
||||
DoUpdates: clause.Assignments(map[string]any{"content": content, "object_key": objectKey, "size": size, "preview": preview, "updated_at": time.Now()}),
|
||||
}).Create(&Doc{Owner: owner, KB: kb, Name: name, Content: content, ObjectKey: objectKey, Size: size, Preview: preview}).Error
|
||||
var d Doc
|
||||
err := p.db.WithContext(ctx).Where("owner = ? AND kb = ? AND name = ?", owner, kb, name).First(&d).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
d = Doc{Owner: owner, KB: kb, Name: name, Ext: ext, MD5: md5, Content: content, ObjectKey: objectKey, Size: size, Preview: preview}
|
||||
if err := p.db.WithContext(ctx).Create(&d).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return d.ID, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
d.Ext, d.MD5, d.Content, d.ObjectKey, d.Size, d.Preview = ext, md5, content, objectKey, size, preview
|
||||
if err := p.db.WithContext(ctx).Save(&d).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return d.ID, nil
|
||||
}
|
||||
|
||||
// ListVault 返回文库列表(仅元数据 + 预览,不含全文),避免一次拉回整库正文。
|
||||
@@ -139,37 +147,46 @@ func (p *Postgres) ListVault(ctx context.Context, owner, kb string) ([]Doc, erro
|
||||
}
|
||||
var rows []Doc
|
||||
err := p.db.WithContext(ctx).
|
||||
Select("id", "name", "size", "preview", "object_key", "updated_at").
|
||||
Select("id", "name", "ext", "size", "preview", "object_key", "updated_at").
|
||||
Where("owner = ? AND kb = ?", owner, kb).Order("updated_at desc").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
// GetDoc 取单篇文档(含全文 Content 与 ObjectKey,供按需阅读)。
|
||||
func (p *Postgres) GetDoc(ctx context.Context, owner, kb, name string) (*Doc, error) {
|
||||
// GetDocByID 按文件 ID 取单篇文档(含全文 Content 与 ObjectKey),owner 作用域防越权。
|
||||
func (p *Postgres) GetDocByID(ctx context.Context, owner, id string) (*Doc, error) {
|
||||
if p.db == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var d Doc
|
||||
if err := p.db.WithContext(ctx).Where("owner = ? AND kb = ? AND name = ?", owner, kb, name).First(&d).Error; err != nil {
|
||||
if err := p.db.WithContext(ctx).Where("owner = ? AND id = ?", owner, id).First(&d).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
// ReplaceDocLinks 重建某文档的出链(先删旧,再插新)—— 入库/编辑时调用。
|
||||
func (p *Postgres) ReplaceDocLinks(ctx context.Context, owner, kb, from string, tos []string) error {
|
||||
// ReplaceDocLinks 以源文件 ID 重建其出链(先删旧,再按 [[名称]] 解析目标 ID 后插新)—— 入库/编辑时调用。
|
||||
// 目标文档尚未入库时 ToID 留空(悬空),待其入库时由 ResolveInboundLinks 回填。
|
||||
func (p *Postgres) ReplaceDocLinks(ctx context.Context, owner, kb, fromID string, toNames []string) error {
|
||||
if p.db == nil {
|
||||
return nil
|
||||
}
|
||||
return p.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("owner = ? AND kb = ? AND from_name = ?", owner, kb, from).Delete(&DocLink{}).Error; err != nil {
|
||||
if err := tx.Where("owner = ? AND kb = ? AND from_id = ?", owner, kb, fromID).Delete(&DocLink{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, to := range tos {
|
||||
if to == "" || to == from {
|
||||
for _, name := range toNames {
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if err := tx.Create(&DocLink{Owner: owner, KB: kb, FromName: from, ToName: to}).Error; err != nil {
|
||||
var t Doc
|
||||
toID := ""
|
||||
if e := tx.Select("id").Where("owner = ? AND kb = ? AND name = ?", owner, kb, name).First(&t).Error; e == nil {
|
||||
toID = t.ID
|
||||
}
|
||||
if toID == fromID { // 自链跳过
|
||||
continue
|
||||
}
|
||||
if err := tx.Create(&DocLink{Owner: owner, KB: kb, FromID: fromID, ToID: toID, ToName: name}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -177,13 +194,23 @@ func (p *Postgres) ReplaceDocLinks(ctx context.Context, owner, kb, from string,
|
||||
})
|
||||
}
|
||||
|
||||
// ListLinks 返回某 kb 的全部 [[双链]](from→to),供反链/笔记关系图按需查询(数据小)。
|
||||
// ResolveInboundLinks 把指向 name 的悬空链接(ToID 空)回填为 id —— 目标文档入库后调用,使反链/关系图即时成形。
|
||||
func (p *Postgres) ResolveInboundLinks(ctx context.Context, owner, kb, name, id string) error {
|
||||
if p.db == nil {
|
||||
return nil
|
||||
}
|
||||
return p.db.WithContext(ctx).Model(&DocLink{}).
|
||||
Where("owner = ? AND kb = ? AND to_name = ? AND (to_id = '' OR to_id IS NULL)", owner, kb, name).
|
||||
Update("to_id", id).Error
|
||||
}
|
||||
|
||||
// ListLinks 返回某 kb 已解析(两端均为本库文件)的 [[双链]](FromID→ToID),供反链/笔记关系图按 ID 渲染。
|
||||
func (p *Postgres) ListLinks(ctx context.Context, owner, kb string) ([]DocLink, error) {
|
||||
if p.db == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var rows []DocLink
|
||||
err := p.db.WithContext(ctx).Where("owner = ? AND kb = ?", owner, kb).Find(&rows).Error
|
||||
err := p.db.WithContext(ctx).Where("owner = ? AND kb = ? AND to_id <> ''", owner, kb).Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user