| | |
| | | "github.com/aisim/kb-cli/internal/vault" |
| | | ) |
| | | |
| | | // rebuildIndex 重建索引:扫描 vault → 构建图 → 写入 SQLite |
| | | func rebuildIndex(store *index.Store, commit string) error { |
| | | // rebuildFull 全量重建索引:扫描 vault → 构建图 → 写入 SQLite |
| | | func rebuildFull(store *index.Store, commit string) error { |
| | | // 清空旧数据 |
| | | if err := store.ClearData(); err != nil { |
| | | return fmt.Errorf("清空数据失败: %w", err) |
| | |
| | | fmt.Fprintf(os.Stderr, "扫描到 %d 个文件\n", len(files)) |
| | | |
| | | // 构建图 |
| | | g := graph.BuildGraph(files) |
| | | g, unresolved := graph.BuildGraph(files) |
| | | |
| | | // 写入节点,并记录 BuildGraph ID -> SQLite ID 的映射 |
| | | idMap := make(map[int64]int64) // BuildGraph ID -> SQLite ID |
| | |
| | | } |
| | | |
| | | // 写入边,将 BuildGraph ID 转换为 SQLite ID |
| | | actualEdgeCount := 0 |
| | | for _, e := range g.Edges { |
| | | fromID, ok := idMap[e.FromNode] |
| | | if !ok { |
| | |
| | | continue |
| | | } |
| | | edge := &graph.Edge{ |
| | | FromNode: fromID, |
| | | ToNode: toID, |
| | | Relation: e.Relation, |
| | | Label: e.Label, |
| | | FromNode: fromID, |
| | | ToNode: toID, |
| | | Relation: e.Relation, |
| | | Label: e.Label, |
| | | Provenance: e.Provenance, |
| | | } |
| | | if err := store.InsertEdge(edge); err != nil { |
| | | return fmt.Errorf("插入边失败: %w", err) |
| | | } |
| | | actualEdgeCount++ |
| | | } |
| | | |
| | | // 写入悬空链接(BuildGraph 内部 ID 映射为 SQLite ID) |
| | | for _, u := range unresolved { |
| | | fromID, ok := idMap[u.FromNode] |
| | | if !ok { |
| | | continue |
| | | } |
| | | if err := store.InsertUnresolved(fromID, u.LinkText, u.NameTail); err != nil { |
| | | return fmt.Errorf("写入悬空链接失败 [%s]: %w", u.LinkText, err) |
| | | } |
| | | } |
| | | |
| | | // 创建并填充 FTS5 索引 |
| | |
| | | return fmt.Errorf("记录构建时间失败: %w", err) |
| | | } |
| | | |
| | | fmt.Fprintf(os.Stderr, "已索引 %d 个节点, %d 条边\n", len(g.Nodes), len(g.Edges)) |
| | | fmt.Fprintf(os.Stderr, "已索引 %d 个节点, %d 条边\n", len(g.Nodes), actualEdgeCount) |
| | | return nil |
| | | } |
| | | |
| | | // syncIndex 增量对账路径(index build 默认、search pre-flight 用) |
| | | func syncIndex(store *index.Store) error { |
| | | res, err := index.Reconcile(store, vaultPath) |
| | | if err != nil { |
| | | return fmt.Errorf("增量同步失败: %w", err) |
| | | } |
| | | fmt.Fprintf(os.Stderr, "增量同步: 新增 %d / 修改 %d / 删除 %d / 未变 %d\n", |
| | | res.Added, res.Modified, res.Deleted, res.Unchanged) |
| | | if commit, err := index.GetGitCommit(vaultPath); err == nil { |
| | | if err := store.SetMeta("git_commit", commit); err != nil { |
| | | return fmt.Errorf("记录 git commit 失败: %w", err) |
| | | } |
| | | } |
| | | if err := store.SetMeta("built_at", time.Now().Format(time.RFC3339)); err != nil { |
| | | return fmt.Errorf("记录构建时间失败: %w", err) |
| | | } |
| | | return nil |
| | | } |