pub struct CodeGraph { /* private fields */ }Expand description
The main code graph database.
CodeGraph provides the primary interface for storing and querying code relationships.
All operations are explicit with no hidden behavior.
Implementations§
Source§impl CodeGraph
impl CodeGraph
Sourcepub fn with_backend(backend: Box<dyn StorageBackend>) -> Result<Self>
pub fn with_backend(backend: Box<dyn StorageBackend>) -> Result<Self>
Open or create a code graph with the given storage backend.
This is the explicit way to create a graph. No automatic scanning or parsing.
§Errors
Returns GraphError::Storage if the backend cannot be initialized.
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Open a persistent code graph at the given path.
Uses RocksDB for production-grade persistent storage.
§Example
use codegraph::CodeGraph;
use std::path::Path;
let graph = CodeGraph::open(Path::new("./my_project.graph")).unwrap();§Errors
Returns GraphError::Storage if the database cannot be opened.
Sourcepub fn in_memory() -> Result<Self>
pub fn in_memory() -> Result<Self>
Create an in-memory code graph for testing.
Warning: All data is lost when the graph is dropped. Only use for testing.
Sourcepub fn add_node(
&mut self,
node_type: NodeType,
properties: PropertyMap,
) -> Result<NodeId>
pub fn add_node( &mut self, node_type: NodeType, properties: PropertyMap, ) -> Result<NodeId>
Add a node to the graph.
§Parameters
node_type: Type of code entityproperties: Metadata for the node
§Returns
The unique ID assigned to the created node.
§Errors
Returns GraphError::Storage if persistence fails.
Sourcepub fn get_node_mut(&mut self, id: NodeId) -> Result<&mut Node>
pub fn get_node_mut(&mut self, id: NodeId) -> Result<&mut Node>
Get a mutable reference to a node by ID.
§Errors
Returns GraphError::NodeNotFound if the node doesn’t exist.
Sourcepub fn update_node_properties(
&mut self,
id: NodeId,
properties: PropertyMap,
) -> Result<()>
pub fn update_node_properties( &mut self, id: NodeId, properties: PropertyMap, ) -> Result<()>
Update properties of an existing node.
Merges new properties with existing ones (overwrites duplicates).
§Errors
Returns error if node not found or persistence fails.
Sourcepub fn delete_node(&mut self, id: NodeId) -> Result<()>
pub fn delete_node(&mut self, id: NodeId) -> Result<()>
Delete a node and all its connected edges.
§Errors
Returns error if node not found or deletion fails.
Sourcepub fn add_edge(
&mut self,
source_id: NodeId,
target_id: NodeId,
edge_type: EdgeType,
properties: PropertyMap,
) -> Result<EdgeId>
pub fn add_edge( &mut self, source_id: NodeId, target_id: NodeId, edge_type: EdgeType, properties: PropertyMap, ) -> Result<EdgeId>
Add an edge to the graph.
§Parameters
source_id: Source node IDtarget_id: Target node IDedge_type: Type of relationshipproperties: Metadata for the edge (e.g., line number)
§Returns
The unique ID assigned to the created edge.
§Errors
Returns error if source or target node doesn’t exist or storage fails.
Sourcepub fn delete_edge(&mut self, id: EdgeId) -> Result<()>
pub fn delete_edge(&mut self, id: EdgeId) -> Result<()>
Sourcepub fn get_edges_between(
&self,
source_id: NodeId,
target_id: NodeId,
) -> Result<Vec<EdgeId>>
pub fn get_edges_between( &self, source_id: NodeId, target_id: NodeId, ) -> Result<Vec<EdgeId>>
Get all edges between two nodes.
Returns all edges from source to target.
Sourcepub fn add_nodes_batch(
&mut self,
nodes: Vec<(NodeType, PropertyMap)>,
) -> Result<Vec<NodeId>>
pub fn add_nodes_batch( &mut self, nodes: Vec<(NodeType, PropertyMap)>, ) -> Result<Vec<NodeId>>
Add multiple nodes in an atomic batch operation.
Either all nodes are added or none are.
§Returns
Vector of assigned node IDs in the same order as input.
Sourcepub fn add_edges_batch(
&mut self,
edges: Vec<(NodeId, NodeId, EdgeType, PropertyMap)>,
) -> Result<Vec<EdgeId>>
pub fn add_edges_batch( &mut self, edges: Vec<(NodeId, NodeId, EdgeType, PropertyMap)>, ) -> Result<Vec<EdgeId>>
Add multiple edges in an atomic batch operation.
Either all edges are added or none are.
§Returns
Vector of assigned edge IDs in the same order as input.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Get the total number of nodes in the graph.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Get the total number of edges in the graph.
Sourcepub fn clear(&mut self) -> Result<()>
pub fn clear(&mut self) -> Result<()>
Clear all nodes and edges from the graph.
This is a destructive operation that cannot be undone.
Sourcepub fn query<'a>(&'a self) -> QueryBuilder<'a>
pub fn query<'a>(&'a self) -> QueryBuilder<'a>
Create a new query builder for this graph.
Returns a QueryBuilder that allows fluent chaining of filters
to find specific nodes in the graph.
§Examples
use codegraph::{CodeGraph, NodeType};
let mut graph = CodeGraph::in_memory()?;
// ... populate graph ...
// Find all public functions
let results = graph.query()
.node_type(NodeType::Function)
.property("visibility", "public")
.execute()?;Sourcepub fn flush(&mut self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
Explicitly flush any buffered writes to disk.
Most operations are durable immediately, but this ensures WAL is synced.
Sourcepub fn bfs(
&self,
start: NodeId,
direction: Direction,
max_depth: Option<usize>,
) -> Result<Vec<NodeId>>
pub fn bfs( &self, start: NodeId, direction: Direction, max_depth: Option<usize>, ) -> Result<Vec<NodeId>>
Perform Breadth-First Search traversal from a starting node.
Returns all reachable nodes within the specified depth limit.
§Parameters
start: Starting node IDdirection: Follow outgoing or incoming edgesmax_depth: Optional maximum depth (None for unlimited)
§Returns
Vec of reachable node IDs (excluding the start node)
Sourcepub fn dfs(
&self,
start: NodeId,
direction: Direction,
max_depth: Option<usize>,
) -> Result<Vec<NodeId>>
pub fn dfs( &self, start: NodeId, direction: Direction, max_depth: Option<usize>, ) -> Result<Vec<NodeId>>
Perform Depth-First Search traversal from a starting node.
Uses an iterative approach to avoid stack overflow.
§Parameters
start: Starting node IDdirection: Follow outgoing or incoming edgesmax_depth: Optional maximum depth (None for unlimited)
§Returns
Vec of reachable node IDs (excluding the start node)
Sourcepub fn find_strongly_connected_components(&self) -> Result<Vec<Vec<NodeId>>>
pub fn find_strongly_connected_components(&self) -> Result<Vec<Vec<NodeId>>>
Find all strongly connected components (SCCs) using Tarjan’s algorithm.
Returns groups of nodes that form circular dependencies.
§Returns
Vec of SCCs, where each SCC is a Vec of node IDs
Sourcepub fn find_all_paths(
&self,
start: NodeId,
end: NodeId,
max_depth: Option<usize>,
) -> Result<Vec<Vec<NodeId>>>
pub fn find_all_paths( &self, start: NodeId, end: NodeId, max_depth: Option<usize>, ) -> Result<Vec<Vec<NodeId>>>
Sourcepub fn export_dot(&self) -> Result<String>
pub fn export_dot(&self) -> Result<String>
Export graph to Graphviz DOT format for visualization.
Warning: Large graphs (>10K nodes) will produce warnings. Graphs over 100K nodes will fail.
Sourcepub fn export_dot_styled(&self, options: DotOptions) -> Result<String>
pub fn export_dot_styled(&self, options: DotOptions) -> Result<String>
Export graph to Graphviz DOT format with custom styling options.
Sourcepub fn export_json(&self) -> Result<String>
pub fn export_json(&self) -> Result<String>
Export graph to D3.js-compatible JSON format.
Warning: Large graphs (>10K nodes) will produce warnings. Graphs over 100K nodes will fail.
Sourcepub fn export_json_filtered(
&self,
node_filter: impl Fn(&Node) -> bool,
include_edges: bool,
) -> Result<String>
pub fn export_json_filtered( &self, node_filter: impl Fn(&Node) -> bool, include_edges: bool, ) -> Result<String>
Export filtered subset of graph to JSON.
Sourcepub fn export_csv_nodes(&self, path: &Path) -> Result<()>
pub fn export_csv_nodes(&self, path: &Path) -> Result<()>
Export nodes to CSV file.
Warning: Large graphs (>10K nodes) will produce warnings. Graphs over 100K nodes will fail.
Sourcepub fn export_csv_edges(&self, path: &Path) -> Result<()>
pub fn export_csv_edges(&self, path: &Path) -> Result<()>
Export edges to CSV file.
Warning: Large graphs (>10K nodes) will produce warnings. Graphs over 100K nodes will fail.
Sourcepub fn export_csv(&self, nodes_path: &Path, edges_path: &Path) -> Result<()>
pub fn export_csv(&self, nodes_path: &Path, edges_path: &Path) -> Result<()>
Export both nodes and edges to separate CSV files (convenience method).
Sourcepub fn export_triples(&self) -> Result<String>
pub fn export_triples(&self) -> Result<String>
Export graph as RDF triples in N-Triples format.
Warning: Large graphs (>10K nodes) will produce warnings. Graphs over 100K nodes will fail.