Files
knowledge-governance/internal/librarysnapshot/rows.go
T
姚章浩 281699a431
build / build (push) Successful in 1m21s
feat(library): 生产整库真实读写/校验/恢复与本机真实服务验收
为什么:生产目标此前是"无条件失败"的占位,编排再完整也不能落地;本轮把它做成真实实现,
并在本机 WeKnora 测试栈的真实 PG/ES 上跑通导入、撤销、预备份与故障恢复。

- ProductionLibraryTarget:Current 读行数+身份摘要;Apply 按阶段真实写入(database/wiki
  同事务先删本库再插、resources 传对象、index 搬运原始文档与向量);Verify 比对身份摘要;
  Restore 按预备份快照回放。
- 撤销真正生效:索引阶段写入后 PruneScope 删除快照里没有的文档。
- ProductionBackupStore:导出前独立完整预备份,导出前后行数不一致即失败。
- worker 用注入配置装配目标与预备份;缺项把变量名写进阶段并失败,不再有占位对象。
- capacity 安全加法防溢出;保留策略组合语义明确(各维度都超限才候选)。
- 新增本机真实服务集成测试(一次性 kg_library_test_* 库 + 随机 ES 索引),
  覆盖导入/校验/撤销/预备份/故障注入/恢复。
2026-10-28 15:54:01 +08:00

103 lines
2.9 KiB
Go

package librarysnapshot
import (
"encoding/json"
"errors"
"io"
"sort"
)
// IndexDocument 是归档索引部件里的一条原始文档(含向量)。
// 生产同步直接搬运它,不重新解析、不重新 Embedding。
type IndexDocument struct {
ID string `json:"id"`
Source json.RawMessage `json:"source"`
}
// ReadRowIDs 读出行数组里每行的 id 并排序,用于"身份集合"比对。
func ReadRowIDs(r io.Reader) ([]string, error) {
decoder := json.NewDecoder(r)
token, err := decoder.Token()
if err != nil || token != json.Delim('[') {
return nil, errors.New("snapshot component must be a JSON row array")
}
ids := make([]string, 0, 64)
for decoder.More() {
var row struct {
ID string `json:"id"`
}
if err := decoder.Decode(&row); err != nil {
return nil, errors.New("invalid archived row")
}
if row.ID == "" {
return nil, errors.New("archived row without id")
}
ids = append(ids, row.ID)
}
sort.Strings(ids)
return ids, nil
}
// ReadIndexDocuments 读取索引部件。归档部件有两种历史形状:直接是文档数组,
// 或 [{id, source}] 包装;两种都必须支持,否则旧归档无法同步。
func ReadIndexDocuments(r io.Reader) ([]IndexDocument, error) {
decoder := json.NewDecoder(r)
token, err := decoder.Token()
if err != nil || token != json.Delim('[') {
return nil, errors.New("index component must be a JSON array")
}
documents := make([]IndexDocument, 0, 64)
for decoder.More() {
var row map[string]json.RawMessage
if err := decoder.Decode(&row); err != nil {
return nil, errors.New("invalid archived index row")
}
// 历史归档有两种键风格:导出结构体的 "ID"/"Source",以及小写 "id"/"source"。
wrapped, hasSource := row["source"]
if !hasSource {
wrapped, hasSource = row["Source"]
}
if hasSource {
idRaw, ok := row["id"]
if !ok {
idRaw = row["ID"]
}
var id string
_ = json.Unmarshal(idRaw, &id)
documents = append(documents, IndexDocument{ID: id, Source: wrapped})
continue
}
id := documentIdentity(row)
if id == "" {
return nil, errors.New("archived index row has no stable id")
}
raw, err := json.Marshal(row)
if err != nil {
return nil, err
}
documents = append(documents, IndexDocument{ID: id, Source: raw})
}
if len(documents) == 0 {
return documents, nil
}
for _, document := range documents {
if document.ID == "" || len(document.Source) == 0 {
return nil, errors.New("archived index document incomplete")
}
}
sort.Slice(documents, func(i, j int) bool { return documents[i].ID < documents[j].ID })
return documents, nil
}
func documentIdentity(row map[string]json.RawMessage) string {
for _, key := range []string{"chunk_id", "source_id", "id"} {
if raw, ok := row[key]; ok {
var value string
if json.Unmarshal(raw, &value) == nil && value != "" {
return value
}
}
}
return ""
}