refactor: rename to GetDB/DocDBUnauthorized/DocDB, add history support, and upgrade ExtractToC (by AI)

This commit is contained in:
AI Engineer 2026-05-15 14:07:01 +08:00
parent 181c1f0be3
commit fda6c2f9f2
3 changed files with 153 additions and 125 deletions

182
doc.go
View File

@ -14,8 +14,8 @@ import (
"apigo.cc/go/tableDB" "apigo.cc/go/tableDB"
) )
// DocDB 实例 // DocDBUnauthorized 实例
type DocDB struct { type DocDBUnauthorized struct {
db *tableDB.TableDBUnauthorized db *tableDB.TableDBUnauthorized
baseDir string baseDir string
onCreatedDoc func(doc *Document) onCreatedDoc func(doc *Document)
@ -23,11 +23,12 @@ type DocDB struct {
onRemoved func(id string) onRemoved func(id string)
} }
// App 实例 (带用户权限) // DocDB 实例 (带用户权限)
type App struct { type DocDB struct {
docDB *DocDB parent *DocDBUnauthorized
userID string userID string
table *tableDB.Table table *tableDB.Table
hist *tableDB.Table
} }
// Document 结构体 // Document 结构体
@ -62,55 +63,70 @@ type ToCNode struct {
Children []ToCNode Children []ToCNode
} }
// New 创建 DocDB 实例 // GetDB 获取 DocDBUnauthorized 实例并初始化表结构
func New(db *tableDB.TableDBUnauthorized, baseDir string) *DocDB { func GetDB(db *tableDB.TableDBUnauthorized, baseDir string) *DocDBUnauthorized {
return &DocDB{ // 使用系统管理员身份初始化
sys := db.Auth(tableDB.SystemUserID)
// 确保系统基础表存在
if raw, err := sys.GetRawDB(); err == nil {
_ = raw.Sync(tableDB.SystemSchema)
}
initTable := func(tableName string) {
res, _ := sys.Table("_Table").Get(tableName)
if res == nil {
_ = sys.SetTable(tableDB.TableSchema{ID: tableName, Name: tableName, Memo: "Document Storage", EnableRLS: true})
fields := []tableDB.FieldSchema{
{Name: "path", Type: "v256", IsIndex: true},
{Name: "title", Type: "v255"},
{Name: "textContent", Type: "t"},
{Name: "filePath", Type: "v256"},
{Name: "thumbnail", Type: "v256"},
{Name: "summary", Type: "t"},
{Name: "metadata", Type: "o"},
{Name: "toc", Type: "o"},
{Name: "tags", Type: "v1024"},
{Name: "language", Type: "v32"},
{Name: "type", Type: "v32"},
{Name: "storeType", Type: "v16"},
{Name: "asFile", Type: "b"},
{Name: "version", Type: "bi"},
{Name: "isSecret", Type: "b"},
{Name: "updateTime", Type: "bi"},
{Name: "updater", Type: "v64"},
{Name: "createTime", Type: "bi"},
{Name: "creator", Type: "v64"},
}
if tableName == "_Doc_History" {
fields = append(fields, tableDB.FieldSchema{Name: "docId", Type: "c10", IsIndex: true})
}
_ = sys.Table(tableName).SetField(fields...)
}
}
initTable("_Doc")
initTable("_Doc_History")
return &DocDBUnauthorized{
db: db, db: db,
baseDir: baseDir, baseDir: baseDir,
} }
} }
// Auth 授权并初始化表结构 // Auth 授权并获取 DocDB
func (d *DocDB) Auth(userID string) *App { func (d *DocDBUnauthorized) Auth(userID string) *DocDB {
appTableDB := d.db.Auth(userID) appTableDB := d.db.Auth(userID)
return &DocDB{
// 检查 _Doc 表是否存在,不存在则初始化 parent: d,
res, _ := appTableDB.Table("_Table").Get("_Doc")
if res == nil {
// 使用高级接口 SetTable 和 SetField
_ = appTableDB.SetTable(tableDB.TableSchema{ID: "_Doc", Name: "_Doc", Memo: "Document Repository"})
_ = appTableDB.Table("_Doc").SetField(
tableDB.FieldSchema{Name: "path", Type: "v256", IsIndex: true},
tableDB.FieldSchema{Name: "title", Type: "v255"},
tableDB.FieldSchema{Name: "textContent", Type: "t"},
tableDB.FieldSchema{Name: "filePath", Type: "v256"},
tableDB.FieldSchema{Name: "thumbnail", Type: "v256"},
tableDB.FieldSchema{Name: "summary", Type: "t"},
tableDB.FieldSchema{Name: "metadata", Type: "o"},
tableDB.FieldSchema{Name: "toc", Type: "o"},
tableDB.FieldSchema{Name: "tags", Type: "v1024"},
tableDB.FieldSchema{Name: "language", Type: "v32"},
tableDB.FieldSchema{Name: "type", Type: "v32"},
tableDB.FieldSchema{Name: "storeType", Type: "v16"},
tableDB.FieldSchema{Name: "asFile", Type: "b"},
tableDB.FieldSchema{Name: "version", Type: "bi"},
tableDB.FieldSchema{Name: "isSecret", Type: "b"},
tableDB.FieldSchema{Name: "updateTime", Type: "bi"},
tableDB.FieldSchema{Name: "updater", Type: "v64"},
tableDB.FieldSchema{Name: "createTime", Type: "bi"},
tableDB.FieldSchema{Name: "creator", Type: "v64"},
)
}
return &App{
docDB: d,
userID: userID, userID: userID,
table: appTableDB.Table("_Doc"), table: appTableDB.Table("_Doc"),
hist: appTableDB.Table("_Doc_History"),
} }
} }
// SetDoc 更新内容 (强制提升版本号) // SetDoc 更新内容 (强制提升版本号,自动保存历史)
func (a *App) SetDoc(docObj *Document) error { func (a *DocDB) SetDoc(docObj *Document) error {
if docObj.ID == "" { if docObj.ID == "" {
docObj.ID = id.MakeID(10) docObj.ID = id.MakeID(10)
} }
@ -121,6 +137,15 @@ func (a *App) SetDoc(docObj *Document) error {
isUpdate := err == nil && existing != nil isUpdate := err == nil && existing != nil
if isUpdate { if isUpdate {
// 保存历史版本
histRecord := make(map[string]any)
for k, v := range existing {
histRecord[k] = v
}
histRecord["docId"] = docObj.ID
delete(histRecord, "id") // 让 tableDB 自动生成历史记录 ID
_ = a.hist.Set(histRecord)
docObj.Version = cast.Uint64(existing["version"]) + 1 docObj.Version = cast.Uint64(existing["version"]) + 1
docObj.CreateTime = cast.Int64(existing["createTime"]) docObj.CreateTime = cast.Int64(existing["createTime"])
docObj.Creator = cast.String(existing["creator"]) docObj.Creator = cast.String(existing["creator"])
@ -153,16 +178,16 @@ func (a *App) SetDoc(docObj *Document) error {
err = a.table.Set(record) err = a.table.Set(record)
if err == nil { if err == nil {
if isUpdate { if isUpdate {
a.docDB.triggerUpdated(docObj) a.parent.triggerUpdated(docObj)
} else { } else {
a.docDB.triggerCreated(docObj) a.parent.triggerCreated(docObj)
} }
} }
return err return err
} }
// SetMeta 更新元数据 (不提升版本号) // SetMeta 更新元数据 (不提升版本号)
func (a *App) SetMeta(id string, meta map[string]any) error { func (a *DocDB) SetMeta(id string, meta map[string]any) error {
existing, err := a.table.Get(id) existing, err := a.table.Get(id)
if err != nil || existing == nil { if err != nil || existing == nil {
return fmt.Errorf("document not found: %s", id) return fmt.Errorf("document not found: %s", id)
@ -178,30 +203,28 @@ func (a *App) SetMeta(id string, meta map[string]any) error {
return a.table.Set(existing) return a.table.Set(existing)
} }
func (a *App) toRecord(docObj *Document) map[string]any { func (a *DocDB) toRecord(docObj *Document) map[string]any {
record := make(map[string]any) record := make(map[string]any)
cast.Convert(&record, docObj) cast.Convert(&record, docObj)
// tableDB 内部约定使用小写 id这里强制转换以避免产生重复字段
if v, ok := record["ID"]; ok { if v, ok := record["ID"]; ok {
record["id"] = v record["id"] = v
delete(record, "ID") delete(record, "ID")
} }
// 移除瞬态字段
delete(record, "binaryContent") delete(record, "binaryContent")
return record return record
} }
func (a *App) saveFile(docObj *Document) error { func (a *DocDB) saveFile(docObj *Document) error {
subDir1 := docObj.ID[0:2] subDir1 := docObj.ID[0:2]
subDir2 := docObj.ID[2:6] subDir2 := docObj.ID[2:6]
subDir3 := docObj.ID[6:10] subDir3 := docObj.ID[6:10]
ext := filepath.Ext(docObj.Path) ext := filepath.Ext(docObj.Path)
fileName := fmt.Sprintf("v%d%s", docObj.Version, ext) fileName := fmt.Sprintf("v%d%s", docObj.Version, ext)
filePath := filepath.Join(a.docDB.baseDir, "doc", subDir1, subDir2, subDir3, fileName) filePath := filepath.Join(a.parent.baseDir, "doc", subDir1, subDir2, subDir3, fileName)
file.EnsureParentDir(filePath) file.EnsureParentDir(filePath)
out, err := os.Create(filePath) out, err := os.Create(filePath)
@ -218,8 +241,8 @@ func (a *App) saveFile(docObj *Document) error {
return nil return nil
} }
// Get 获取文档 // Get 获取文档 (当前版本)
func (a *App) Get(id string) (*Document, error) { func (a *DocDB) Get(id string) (*Document, error) {
res, err := a.table.Get(id) res, err := a.table.Get(id)
if err != nil || res == nil { if err != nil || res == nil {
return nil, err return nil, err
@ -229,46 +252,81 @@ func (a *App) Get(id string) (*Document, error) {
return &doc, nil return &doc, nil
} }
// GetByVersion 按版本获取文档
func (a *DocDB) GetByVersion(id string, version uint64) (*Document, error) {
current, err := a.Get(id)
if err != nil || current == nil {
return nil, err
}
if current.Version == version {
return current, nil
}
res, err := a.hist.List(map[string]any{"docId": id, "version": version})
if err != nil || len(res) == 0 {
return nil, err
}
var doc Document
cast.Convert(&doc, res[0])
doc.ID = id // 确保 ID 是原始文档 ID 而非历史记录 ID
return &doc, nil
}
// Remove 删除文档 // Remove 删除文档
func (a *App) Remove(id string) error { func (a *DocDB) Remove(id string) error {
err := a.table.Remove(id) err := a.table.Remove(id)
if err == nil { if err == nil {
a.docDB.triggerRemoved(id) a.parent.triggerRemoved(id)
} }
return err return err
} }
// 事件订阅 // 事件订阅
func (d *DocDB) OnCreatedDoc(fn func(doc *Document)) { d.onCreatedDoc = fn } func (d *DocDBUnauthorized) OnCreatedDoc(fn func(doc *Document)) { d.onCreatedDoc = fn }
func (d *DocDB) OnUpdatedDoc(fn func(doc *Document)) { d.onUpdatedDoc = fn } func (d *DocDBUnauthorized) OnUpdatedDoc(fn func(doc *Document)) { d.onUpdatedDoc = fn }
func (d *DocDB) OnRemoved(fn func(id string)) { d.onRemoved = fn } func (d *DocDBUnauthorized) OnRemoved(fn func(id string)) { d.onRemoved = fn }
func (d *DocDB) triggerCreated(doc *Document) { func (d *DocDBUnauthorized) triggerCreated(doc *Document) {
if d.onCreatedDoc != nil { if d.onCreatedDoc != nil {
d.onCreatedDoc(doc) d.onCreatedDoc(doc)
} }
} }
func (d *DocDB) triggerUpdated(doc *Document) { func (d *DocDBUnauthorized) triggerUpdated(doc *Document) {
if d.onUpdatedDoc != nil { if d.onUpdatedDoc != nil {
d.onUpdatedDoc(doc) d.onUpdatedDoc(doc)
} }
} }
func (d *DocDB) triggerRemoved(id string) { func (d *DocDBUnauthorized) triggerRemoved(id string) {
if d.onRemoved != nil { if d.onRemoved != nil {
d.onRemoved(id) d.onRemoved(id)
} }
} }
// ExtractToC 保持提取目录逻辑 // ExtractToC 提取 Markdown 目录结构 (带代码块识别)
func ExtractToC(content string) []ToCNode { func ExtractToC(content string) []ToCNode {
lines := strings.Split(content, "\n") lines := strings.Split(content, "\n")
var root []ToCNode var root []ToCNode
stack := []*[]ToCNode{&root} stack := []*[]ToCNode{&root}
inCodeBlock := false // 状态机:是否在代码块内
for _, line := range lines { for _, line := range lines {
trimmed := strings.TrimSpace(line) trimmed := strings.TrimSpace(line)
// 切换代码块状态
if strings.HasPrefix(trimmed, "```") {
inCodeBlock = !inCodeBlock
continue
}
// 如果在代码块内部,直接跳过解析
if inCodeBlock {
continue
}
if !strings.HasPrefix(trimmed, "#") { if !strings.HasPrefix(trimmed, "#") {
continue continue
} }

View File

@ -1,7 +1,6 @@
package docDB package docDB
import ( import (
"bytes"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -20,11 +19,8 @@ func TestDocDB(t *testing.T) {
unauthorizedDB := tableDB.GetDB("sqlite://"+dbFile, logger) unauthorizedDB := tableDB.GetDB("sqlite://"+dbFile, logger)
systemApp := unauthorizedDB.Auth("_system") // 使用新的 GetDB 入口,内部完成系统表初始化与业务表初始化
// Bootstrap system tables via the special :Schema table docDBInst := GetDB(unauthorizedDB, "./test_docs")
_ = systemApp.Table("_Table:Schema").Set(map[string]any{"dsl": ""})
docDBInst := New(unauthorizedDB, "./test_docs")
app := docDBInst.Auth("user1") app := docDBInst.Auth("user1")
var createdCalled, updatedCalled, removedCalled bool var createdCalled, updatedCalled, removedCalled bool
@ -60,11 +56,8 @@ func TestDocDB(t *testing.T) {
if d.Title != "Test Doc" { if d.Title != "Test Doc" {
t.Fatalf("Expected title 'Test Doc', got %s", d.Title) t.Fatalf("Expected title 'Test Doc', got %s", d.Title)
} }
if len(d.ToC) == 0 {
t.Error("ToC should be extracted")
}
// 3. 测试 SetDoc (更新内容, 提升版本) // 3. 测试 SetDoc (更新内容, 提升版本并存入历史)
doc1.TextContent = "# Header 1\nUpdated Content" doc1.TextContent = "# Header 1\nUpdated Content"
err = app.SetDoc(doc1) err = app.SetDoc(doc1)
if err != nil { if err != nil {
@ -77,7 +70,35 @@ func TestDocDB(t *testing.T) {
t.Error("OnUpdatedDoc event not triggered") t.Error("OnUpdatedDoc event not triggered")
} }
// 4. 测试 SetMeta (更新元数据, 不提升版本) // 4. 测试 GetByVersion
hist, err := app.GetByVersion(doc1.ID, 1)
if err != nil || hist == nil {
t.Fatalf("GetByVersion(1) failed: %v", err)
}
if !strings.Contains(hist.TextContent, "Content 1") {
t.Fatalf("Expected 'Content 1' in history version 1, got %s", hist.TextContent)
}
curr, _ := app.GetByVersion(doc1.ID, 2)
if curr.Version != 2 || !strings.Contains(curr.TextContent, "Updated Content") {
t.Fatalf("Expected version 2 content, got version %d", curr.Version)
}
// 5. 测试 ExtractToC 代码块识别
docWithCode := &Document{
Path: "/test/code.md",
TextContent: "# H1\n```markdown\n# This is not a header\n```\n# H2",
Type: "markdown",
}
_ = app.SetDoc(docWithCode)
if len(docWithCode.ToC) != 2 {
t.Fatalf("Expected 2 headers in ToC, got %d", len(docWithCode.ToC))
}
if docWithCode.ToC[1].Title != "H2" {
t.Fatalf("Expected second header to be 'H2', got %s", docWithCode.ToC[1].Title)
}
// 6. 测试 SetMeta (更新元数据, 不提升版本)
err = app.SetMeta(doc1.ID, map[string]any{"title": "New Title"}) err = app.SetMeta(doc1.ID, map[string]any{"title": "New Title"})
if err != nil { if err != nil {
t.Fatalf("SetMeta failed: %v", err) t.Fatalf("SetMeta failed: %v", err)
@ -91,23 +112,7 @@ func TestDocDB(t *testing.T) {
t.Fatalf("Expected version 2 after meta update, got %d", d2.Version) t.Fatalf("Expected version 2 after meta update, got %d", d2.Version)
} }
// 5. 测试 带二进制内容的 SetDoc // 7. 测试 Remove
doc2 := &Document{
Path: "/test/binary.txt",
BinaryContent: bytes.NewReader([]byte("Binary Content")),
}
err = app.SetDoc(doc2)
if err != nil {
t.Fatalf("SetDoc with binary failed: %v", err)
}
if doc2.FilePath == "" {
t.Error("FilePath should be set for binary content")
}
if _, err := os.Stat(doc2.FilePath); os.IsNotExist(err) {
t.Error("File should be written to disk")
}
// 6. 测试 Remove
err = app.Remove(doc1.ID) err = app.Remove(doc1.ID)
if err != nil { if err != nil {
t.Fatalf("Remove failed: %v", err) t.Fatalf("Remove failed: %v", err)
@ -115,11 +120,6 @@ func TestDocDB(t *testing.T) {
if !removedCalled { if !removedCalled {
t.Error("OnRemoved event not triggered") t.Error("OnRemoved event not triggered")
} }
d3, _ := app.Get(doc1.ID)
if d3 != nil {
t.Error("Document should be removed")
}
} }
func BenchmarkExtractToC(b *testing.B) { func BenchmarkExtractToC(b *testing.B) {

30
go.mod
View File

@ -35,33 +35,3 @@ require (
modernc.org/memory v1.11.0 // indirect modernc.org/memory v1.11.0 // indirect
modernc.org/sqlite v1.50.0 // indirect modernc.org/sqlite v1.50.0 // indirect
) )
replace apigo.cc/go/tableDB => ../tableDB
replace apigo.cc/go/cast => ../cast
replace apigo.cc/go/id => ../id
replace apigo.cc/go/file => ../file
replace apigo.cc/go/log => ../log
replace apigo.cc/go/document => ../document
replace apigo.cc/go/db => ../db
replace apigo.cc/go/config => ../config
replace apigo.cc/go/crypto => ../crypto
replace apigo.cc/go/encoding => ../encoding
replace apigo.cc/go/rand => ../rand
replace apigo.cc/go/redis => ../redis
replace apigo.cc/go/safe => ../safe
replace apigo.cc/go/shell => ../shell
replace apigo.cc/go/vision => ../vision