graph

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package graph provides an in-memory code knowledge graph with persistence.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Edge

type Edge struct {
	ID              int      `json:"id"`
	Kind            EdgeKind `json:"kind"`
	SourceQualified string   `json:"source_qualified"`
	TargetQualified string   `json:"target_qualified"`
	FilePath        string   `json:"file_path"`
	Line            int      `json:"line"`
}

Edge represents a relationship between two nodes.

type EdgeKind

type EdgeKind string

EdgeKind represents the type of relationship between nodes.

const (
	EdgeCalls       EdgeKind = "CALLS"
	EdgeImportsFrom EdgeKind = "IMPORTS_FROM"
	EdgeInherits    EdgeKind = "INHERITS"
	EdgeImplements  EdgeKind = "IMPLEMENTS"
	EdgeContains    EdgeKind = "CONTAINS"
	EdgeTestedBy    EdgeKind = "TESTED_BY"
	EdgeDependsOn   EdgeKind = "DEPENDS_ON"
)

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph is a thread-safe in-memory code knowledge graph.

func Load

func Load(path string) (*Graph, error)

Load reads a graph from a JSON file.

func New

func New() *Graph

New creates a new empty Graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(e Edge) int

AddEdge adds an edge to the graph. Returns the edge ID.

func (*Graph) AddNode

func (g *Graph) AddNode(n Node) int

AddNode adds or updates a node in the graph. Returns the node ID.

func (*Graph) AllEdges

func (g *Graph) AllEdges() []*Edge

AllEdges returns all edges in the graph.

func (*Graph) AllFiles

func (g *Graph) AllFiles() []string

AllFiles returns all unique file paths in the graph.

func (*Graph) AllNodes

func (g *Graph) AllNodes() []*Node

AllNodes returns all nodes in the graph.

func (*Graph) GetCallerCount

func (g *Graph) GetCallerCount(qualifiedName string) int

GetCallerCount returns the number of unique callers of a node.

func (*Graph) GetEdgesBySource

func (g *Graph) GetEdgesBySource(qualifiedName string) []*Edge

GetEdgesBySource returns all edges originating from the given qualified name.

func (*Graph) GetEdgesByTarget

func (g *Graph) GetEdgesByTarget(qualifiedName string) []*Edge

GetEdgesByTarget returns all edges targeting the given qualified name.

func (*Graph) GetImpactRadius

func (g *Graph) GetImpactRadius(changedQualifiedNames []string, maxDepth, maxNodes int) []*Node

GetImpactRadius performs BFS from changed files to find affected nodes.

func (*Graph) GetNode

func (g *Graph) GetNode(qualifiedName string) (*Node, bool)

GetNode returns a node by qualified name.

func (*Graph) GetNodesByFile

func (g *Graph) GetNodesByFile(filePath string) []*Node

GetNodesByFile returns all nodes in a given file.

func (*Graph) GetNodesByKind

func (g *Graph) GetNodesByKind(kinds ...NodeKind) []*Node

GetNodesByKind returns all nodes of the given kinds.

func (*Graph) GetStats

func (g *Graph) GetStats() Stats

GetStats returns aggregate statistics about the graph.

func (*Graph) HasTestCoverage

func (g *Graph) HasTestCoverage(qualifiedName string) bool

HasTestCoverage checks if a node has any TESTED_BY edges. TESTED_BY edges: SourceQualified=function, TargetQualified=test_function

func (*Graph) RemoveFileData

func (g *Graph) RemoveFileData(filePath string)

RemoveFileData removes all nodes and edges associated with a file.

func (*Graph) Save

func (g *Graph) Save(path string) error

Save persists the graph to a JSON file.

func (*Graph) SearchNodes

func (g *Graph) SearchNodes(query string, limit int) []*Node

SearchNodes searches for nodes whose name contains the query string.

type Node

type Node struct {
	ID            int      `json:"id"`
	Kind          NodeKind `json:"kind"`
	Name          string   `json:"name"`
	QualifiedName string   `json:"qualified_name"`
	FilePath      string   `json:"file_path"`
	LineStart     int      `json:"line_start"`
	LineEnd       int      `json:"line_end"`
	Language      string   `json:"language"`
	ParentName    string   `json:"parent_name,omitempty"`
	Params        string   `json:"params,omitempty"`
	ReturnType    string   `json:"return_type,omitempty"`
	IsTest        bool     `json:"is_test"`
}

Node represents a code entity in the graph.

type NodeKind

type NodeKind string

NodeKind represents the type of code entity.

const (
	KindFile     NodeKind = "File"
	KindClass    NodeKind = "Class"
	KindFunction NodeKind = "Function"
	KindType     NodeKind = "Type"
	KindTest     NodeKind = "Test"
)

type Stats

type Stats struct {
	TotalNodes  int            `json:"total_nodes"`
	TotalEdges  int            `json:"total_edges"`
	NodesByKind map[string]int `json:"nodes_by_kind"`
	EdgesByKind map[string]int `json:"edges_by_kind"`
	Languages   []string       `json:"languages"`
	FilesCount  int            `json:"files_count"`
}

Stats holds aggregate graph statistics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL