| | |
| | | // 构建图 |
| | | g := graph.BuildGraph(files) |
| | | |
| | | // 写入节点 |
| | | // 写入节点,并记录 BuildGraph ID -> SQLite ID 的映射 |
| | | idMap := make(map[int64]int64) // BuildGraph ID -> SQLite ID |
| | | for _, n := range g.Nodes { |
| | | if _, err := store.InsertNode(n); err != nil { |
| | | sqliteID, err := store.InsertNode(n) |
| | | if err != nil { |
| | | return fmt.Errorf("插入节点失败 [%s]: %w", n.Path, err) |
| | | } |
| | | idMap[n.ID] = sqliteID |
| | | } |
| | | |
| | | // 写入边 |
| | | // 写入边,将 BuildGraph ID 转换为 SQLite ID |
| | | actualEdgeCount := 0 |
| | | for _, e := range g.Edges { |
| | | if err := store.InsertEdge(e); err != nil { |
| | | fromID, ok := idMap[e.FromNode] |
| | | if !ok { |
| | | // 可能是虚拟节点(tag/entity),跳过 |
| | | continue |
| | | } |
| | | toID, ok := idMap[e.ToNode] |
| | | if !ok { |
| | | // 目标节点不存在,跳过 |
| | | continue |
| | | } |
| | | edge := &graph.Edge{ |
| | | FromNode: fromID, |
| | | ToNode: toID, |
| | | Relation: e.Relation, |
| | | Label: e.Label, |
| | | } |
| | | if err := store.InsertEdge(edge); err != nil { |
| | | return fmt.Errorf("插入边失败: %w", err) |
| | | } |
| | | actualEdgeCount++ |
| | | } |
| | | |
| | | // 创建并填充 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 |
| | | } |