codegraph

package
v0.0.0-...-6320ad3 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FirstClassMetadata = map[string]bool{
		"fake":           true,
		"nameID":         true,
		"return":         true,
		"repo":           true,
		"path":           true,
		"language":       true,
		"is_constructor": true,
	}
)

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	Nodes     []*ast.Node
	Relations []RelationSpec
}

type CodeGraph

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

func NewCodeGraph

func NewCodeGraph(uri, username, password string, config *config.Config, logger *zap.Logger) (*CodeGraph, error)

func (*CodeGraph) BatchCreateRelations

func (cg *CodeGraph) BatchCreateRelations(ctx context.Context, relations []RelationSpec) error

BatchCreateRelations creates multiple relationships in a single database transaction This is much faster than individual CreateRelation calls for bulk operations

func (*CodeGraph) BatchUpdateNodeMetaData

func (cg *CodeGraph) BatchUpdateNodeMetaData(ctx context.Context, updates map[ast.NodeID]map[string]any) error

BatchUpdateNodeMetaData updates metadata for multiple nodes in a single transaction This is more efficient than calling UpdateNodeMetaData multiple times

func (*CodeGraph) BatchWriteNodes

func (cg *CodeGraph) BatchWriteNodes(ctx context.Context, nodes []*ast.Node) error

BatchWriteNodes writes multiple nodes in a single database transaction using UNWIND This is much faster than individual writeNode calls for bulk operations

func (*CodeGraph) CleanRepository

func (cg *CodeGraph) CleanRepository(ctx context.Context, repoName string) error

CleanRepository deletes all nodes and relationships for a specific repository from Neo4j. This includes all FileScopes and their descendant nodes (functions, classes, variables, etc.)

func (*CodeGraph) CleanupFileBuffers

func (cg *CodeGraph) CleanupFileBuffers(ctx context.Context, fileID int32) error

CleanupFileBuffers flushes and removes buffers for a file after processing completes This frees memory and ensures data is written to database

func (*CodeGraph) Close

func (cg *CodeGraph) Close(ctx context.Context) error

func (*CodeGraph) CreateAliasRelation

func (cg *CodeGraph) CreateAliasRelation(ctx context.Context, aliasNodeID, originalNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateAnnotationRelation

func (cg *CodeGraph) CreateAnnotationRelation(ctx context.Context, parentNodeID, annotationNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateBlock

func (cg *CodeGraph) CreateBlock(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateBodyRelation

func (cg *CodeGraph) CreateBodyRelation(ctx context.Context, parentNodeID, bodyNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateCallsFunctionRelation

func (cg *CodeGraph) CreateCallsFunctionRelation(ctx context.Context, callerNodeID, calleeNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateCallsRelation

func (cg *CodeGraph) CreateCallsRelation(ctx context.Context, callerNodeID, calleeNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateClass

func (cg *CodeGraph) CreateClass(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateConditional

func (cg *CodeGraph) CreateConditional(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateConditionalRelation

func (cg *CodeGraph) CreateConditionalRelation(ctx context.Context, condNodeID,
	branchNodeID ast.NodeID, position int, conditionID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateContainsRelation

func (cg *CodeGraph) CreateContainsRelation(ctx context.Context, parentNodeID, childNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateDataFlowRelation

func (cg *CodeGraph) CreateDataFlowRelation(ctx context.Context, sourceNodeID, targetNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateExpression

func (cg *CodeGraph) CreateExpression(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateField

func (cg *CodeGraph) CreateField(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateFileScope

func (cg *CodeGraph) CreateFileScope(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateFromRelation

func (cg *CodeGraph) CreateFromRelation(ctx context.Context, fromNodeID, toNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateFunction

func (cg *CodeGraph) CreateFunction(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateFunctionArgRelation

func (cg *CodeGraph) CreateFunctionArgRelation(ctx context.Context, functionNodeID, argNodeID ast.NodeID,
	position int, fileID int32) error

func (*CodeGraph) CreateFunctionCall

func (cg *CodeGraph) CreateFunctionCall(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateFunctionCallArgRelation

func (cg *CodeGraph) CreateFunctionCallArgRelation(ctx context.Context, callNodeID, argNodeID ast.NodeID,
	position int, fileID int32) error

func (*CodeGraph) CreateHasFieldRelation

func (cg *CodeGraph) CreateHasFieldRelation(ctx context.Context, parentNodeID, childNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateImport

func (cg *CodeGraph) CreateImport(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateImportsRelation

func (cg *CodeGraph) CreateImportsRelation(ctx context.Context, importerNodeID, importedNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateInheritsRelation

func (cg *CodeGraph) CreateInheritsRelation(ctx context.Context, parentNodeID, childNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateLoop

func (cg *CodeGraph) CreateLoop(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateModuleScope

func (cg *CodeGraph) CreateModuleScope(ctx context.Context, node *ast.Node) error

func (*CodeGraph) CreateRelation

func (cg *CodeGraph) CreateRelation(ctx context.Context, parentNodeID, childNodeID ast.NodeID,
	relationLabel string, metaData map[string]any, fileID int32) error

func (*CodeGraph) CreateRelationReal

func (cg *CodeGraph) CreateRelationReal(ctx context.Context, parentNodeID, childNodeID ast.NodeID,
	relationLabel string, metaData map[string]any, fileID int32) error

func (*CodeGraph) CreateReturnsRelation

func (cg *CodeGraph) CreateReturnsRelation(ctx context.Context, functionNodeID, returnNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateUsesVariableRelation

func (cg *CodeGraph) CreateUsesVariableRelation(ctx context.Context, userNodeID, variableNodeID ast.NodeID, fileID int32) error

func (*CodeGraph) CreateVariable

func (cg *CodeGraph) CreateVariable(ctx context.Context, node *ast.Node) error

func (*CodeGraph) DumpToFile

func (cg *CodeGraph) DumpToFile(ctx context.Context, filePath string, repoNames []string) error

DumpToFile dumps the code graph for the specified repositories to a file. FileScopes are output in alphabetical order by their path. For each FileScope, all nodes and relations within that file are dumped.

func (*CodeGraph) ExecuteRead

func (cg *CodeGraph) ExecuteRead(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)

ExecuteRead executes a read-only Cypher query and returns the raw records. This is exposed for use by higher-level query APIs (e.g., codeapi package).

func (*CodeGraph) ExecuteReadSingle

func (cg *CodeGraph) ExecuteReadSingle(ctx context.Context, query string, params map[string]any) (map[string]any, error)

ExecuteReadSingle executes a read-only Cypher query expecting a single record. Returns error if no records found.

func (*CodeGraph) ExecuteWrite

func (cg *CodeGraph) ExecuteWrite(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)

ExecuteWrite executes a write Cypher query and returns the raw records. This is exposed for use by higher-level query APIs (e.g., codeapi package).

func (*CodeGraph) FindAllClassesInFile

func (cg *CodeGraph) FindAllClassesInFile(ctx context.Context, fileID int32) ([]*ast.Node, error)

FindAllClassesInFile returns all classes in a file.

func (*CodeGraph) FindAllClassesWithInheritance

func (cg *CodeGraph) FindAllClassesWithInheritance(ctx context.Context, repoName string) ([]*ast.Node, error)

FindAllClassesWithInheritance returns all classes in a repository that have extends or implements metadata.

func (*CodeGraph) FindClassByName

func (cg *CodeGraph) FindClassByName(ctx context.Context, filePath string, className string) (*ast.Node, error)

FindClassByName finds a class by name within a file path

func (*CodeGraph) FindClassInModule

func (cg *CodeGraph) FindClassInModule(ctx context.Context, name string, moduleName string) ([]*ast.Node, error)

func (*CodeGraph) FindClassesByNameInRepo

func (cg *CodeGraph) FindClassesByNameInRepo(ctx context.Context, name string, repoName string) ([]*ast.Node, error)

FindClassesByNameInRepo finds all classes with the given name in a repository. This is useful for inheritance resolution where a class may extend another class in a different file.

func (*CodeGraph) FindConstructorCallsInFile

func (cg *CodeGraph) FindConstructorCallsInFile(ctx context.Context, fileID int32) ([]*ast.Node, error)

FindConstructorCallsInFile returns all constructor calls (new expressions) in a file. These are FunctionCall nodes with is_constructor=true metadata.

func (*CodeGraph) FindFileByPath

func (cg *CodeGraph) FindFileByPath(ctx context.Context, repoName string, filePath string) (*ast.Node, error)

FindFileByPath finds a file node by its path in a repository

func (*CodeGraph) FindFileScopes

func (cg *CodeGraph) FindFileScopes(ctx context.Context, repoName, filePath string) ([]*ast.Node, error)

func (*CodeGraph) FindFunctionByName

func (cg *CodeGraph) FindFunctionByName(ctx context.Context, filePath string, functionName string) (*ast.Node, error)

FindFunctionByName finds a function by name within a file path

func (*CodeGraph) FindFunctionCalls

func (cg *CodeGraph) FindFunctionCalls(ctx context.Context, fileID ast.NodeID) (map[ast.NodeID][]*ast.Node, error)

func (*CodeGraph) FindFunctionsByFileID

func (cg *CodeGraph) FindFunctionsByFileID(ctx context.Context, fileID int32) ([]*ast.Node, error)

FindFunctionsByFileID returns all Function nodes in a file identified by file ID

func (*CodeGraph) FindFunctionsByFilePath

func (cg *CodeGraph) FindFunctionsByFilePath(ctx context.Context, repoName string, filePath string) ([]*ast.Node, error)

FindFunctionsByFilePath returns all Function nodes in a file identified by repo name and file path

func (*CodeGraph) FindFunctionsByName

func (cg *CodeGraph) FindFunctionsByName(ctx context.Context, fileID int, name string) ([]*ast.Node, error)

func (*CodeGraph) FindNodesByNameAndTypeInFile

func (cg *CodeGraph) FindNodesByNameAndTypeInFile(ctx context.Context, name string, nodeType ast.NodeType, fileID int32) ([]*ast.Node, error)

func (*CodeGraph) Flush

func (cg *CodeGraph) Flush(ctx context.Context, fileID *int32) error

Flush writes buffered nodes and relations to the database If fileID is provided, only flushes buffers for that file If fileID is nil, flushes all buffers IMPORTANT: Nodes are flushed BEFORE relations to ensure they exist in the database

func (*CodeGraph) FlushNodes

func (cg *CodeGraph) FlushNodes(ctx context.Context, fileID *int32) error

FlushNodes writes buffered nodes to the database If fileID is provided, only flushes nodes for that file If fileID is nil, flushes all buffered nodes

func (*CodeGraph) FlushRelations

func (cg *CodeGraph) FlushRelations(ctx context.Context, fileID *int32) error

FlushRelations writes buffered relations to the database If fileID is provided, only flushes relations for that file If fileID is nil, flushes all buffered relations

func (*CodeGraph) GetChildNodes

func (cg *CodeGraph) GetChildNodes(ctx context.Context, parentID ast.NodeID, relationLabel string, childType ast.NodeType) ([]*ast.Node, error)

GetChildNodes returns all child nodes of a given parent via a relationship

func (*CodeGraph) GetClassMethods

func (cg *CodeGraph) GetClassMethods(ctx context.Context, classID ast.NodeID) ([]*ast.Node, error)

GetClassMethods returns all method nodes for a class

func (*CodeGraph) GetConstructorsOfClass

func (cg *CodeGraph) GetConstructorsOfClass(ctx context.Context, classID ast.NodeID) ([]*ast.Node, error)

GetConstructorsOfClass returns all constructors of a class. These are Function nodes with is_constructor=true contained by the class.

func (*CodeGraph) GetContainingClass

func (cg *CodeGraph) GetContainingClass(ctx context.Context, methodID ast.NodeID) (*ast.Node, error)

GetContainingClass returns the class that contains a method

func (*CodeGraph) GetFieldOwnerClass

func (cg *CodeGraph) GetFieldOwnerClass(ctx context.Context, fieldID ast.NodeID) (*ast.Node, error)

GetFieldOwnerClass returns the class that owns a field

func (*CodeGraph) GetFieldsAccessedByMethod

func (cg *CodeGraph) GetFieldsAccessedByMethod(ctx context.Context, methodID ast.NodeID) ([]*ast.Node, error)

GetFieldsAccessedByMethod returns all Field nodes contained within a method (directly or nested)

func (*CodeGraph) GetFieldsAccessedViaThis

func (cg *CodeGraph) GetFieldsAccessedViaThis(ctx context.Context, methodID ast.NodeID) ([]*ast.Node, error)

GetFieldsAccessedViaThis returns fields accessed through the "this" receiver in a method Pattern: (method)-[:CONTAINS*]->(thisVar)-[:THIS]->(class), (thisVar)-[:HAS_FIELD*]->(field)

func (*CodeGraph) GetFieldsOfClass

func (cg *CodeGraph) GetFieldsOfClass(ctx context.Context, classID ast.NodeID) ([]*ast.Node, error)

GetFieldsOfClass returns all fields contained by a class

func (*CodeGraph) GetFieldsWrittenByMethod

func (cg *CodeGraph) GetFieldsWrittenByMethod(ctx context.Context, methodID ast.NodeID) ([]*ast.Node, error)

GetFieldsWrittenByMethod returns all fields that are written to within a method (fields that are targets of DATA_FLOW relationships)

func (*CodeGraph) GetFilePath

func (cg *CodeGraph) GetFilePath(ctx context.Context, fileID int32) string

func (*CodeGraph) GetIncomingRelations

func (cg *CodeGraph) GetIncomingRelations(ctx context.Context, toNodeID ast.NodeID, relationLabel string) ([]RelationInfo, error)

GetIncomingRelations returns all incoming relationships to a node

func (*CodeGraph) GetMethodsOfClass

func (cg *CodeGraph) GetMethodsOfClass(ctx context.Context, classID ast.NodeID) ([]*ast.Node, error)

GetMethodsOfClass returns all methods (functions) contained by a class

func (*CodeGraph) GetModuleName

func (cg *CodeGraph) GetModuleName(ctx context.Context, fileId int32) (string, error)

func (*CodeGraph) GetNodeByID

func (cg *CodeGraph) GetNodeByID(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

GetNodeByID returns a node by its ID

func (*CodeGraph) GetNodesByName

func (cg *CodeGraph) GetNodesByName(ctx context.Context, name string, nodeType ast.NodeType) ([]*ast.Node, error)

GetNodesByName returns all nodes with a given name and type

func (*CodeGraph) GetNodesByType

func (cg *CodeGraph) GetNodesByType(ctx context.Context, nodeType ast.NodeType) ([]*ast.Node, error)

GetNodesByType returns all nodes of a given type

func (*CodeGraph) GetNodesByTypeAndFileID

func (cg *CodeGraph) GetNodesByTypeAndFileID(ctx context.Context, nodeType ast.NodeType, fileID int32) ([]*ast.Node, error)

GetNodesByTypeAndFileID returns all nodes of a given type in a specific file

func (*CodeGraph) GetOutgoingRelations

func (cg *CodeGraph) GetOutgoingRelations(ctx context.Context, fromNodeID ast.NodeID, relationLabel string) ([]RelationInfo, error)

GetOutgoingRelations returns all outgoing relationships from a node

func (*CodeGraph) GetThisClassForMethod

func (cg *CodeGraph) GetThisClassForMethod(ctx context.Context, methodID ast.NodeID) (*ast.Node, error)

GetThisClassForMethod returns the class that the method's receiver (this) points to

func (*CodeGraph) InitializeFileBuffers

func (cg *CodeGraph) InitializeFileBuffers(fileID int32)

InitializeFileBuffers initializes buffers for a file before processing starts This reduces lock contention during writeNode/CreateRelation calls

func (*CodeGraph) IsFieldWrittenInMethod

func (cg *CodeGraph) IsFieldWrittenInMethod(ctx context.Context, methodID ast.NodeID, fieldID ast.NodeID) (bool, error)

IsFieldWrittenInMethod checks if a field has an incoming DATA_FLOW relationship within the scope of a method, indicating the field is being written to

func (*CodeGraph) MarkThis

func (t *CodeGraph) MarkThis(ctx context.Context, fileID int32, thisNodeId ast.NodeID, classNodeId ast.NodeID)

func (*CodeGraph) ReadBlock

func (cg *CodeGraph) ReadBlock(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadClass

func (cg *CodeGraph) ReadClass(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadConditional

func (cg *CodeGraph) ReadConditional(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadExpression

func (cg *CodeGraph) ReadExpression(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadField

func (cg *CodeGraph) ReadField(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadFileScope

func (cg *CodeGraph) ReadFileScope(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadFunction

func (cg *CodeGraph) ReadFunction(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadFunctionCall

func (cg *CodeGraph) ReadFunctionCall(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadImport

func (cg *CodeGraph) ReadImport(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadModuleScope

func (cg *CodeGraph) ReadModuleScope(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) ReadVariable

func (cg *CodeGraph) ReadVariable(ctx context.Context, nodeID ast.NodeID) (*ast.Node, error)

func (*CodeGraph) UpdateFakeClasses

func (cg *CodeGraph) UpdateFakeClasses(ctx context.Context, fileID int32) error

func (*CodeGraph) UpdateNodeMetaData

func (cg *CodeGraph) UpdateNodeMetaData(ctx context.Context, nodeID ast.NodeID, fileID int32, metadata map[string]any) error

UpdateNodeMetaData updates the metadata of an existing node Works in both batch and non-batch write modes In batch mode: updates the buffered node if it exists, otherwise performs immediate update In non-batch mode: performs immediate update to database

type GraphDatabase

type GraphDatabase interface {
	// ExecuteRead executes a read-only Cypher query and returns the raw records
	ExecuteRead(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)

	// ExecuteWrite executes a write Cypher query and returns the raw records
	ExecuteWrite(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)

	// ExecuteReadSingle executes a read-only Cypher query expecting a single record
	ExecuteReadSingle(ctx context.Context, query string, params map[string]any) (map[string]any, error)

	// ExecuteWriteSingle executes a write Cypher query expecting a single record
	ExecuteWriteSingle(ctx context.Context, query string, params map[string]any) (map[string]any, error)

	// Close closes the database connection
	Close(ctx context.Context) error

	// VerifyConnectivity checks if the database connection is working
	VerifyConnectivity(ctx context.Context) error
}

GraphDatabase represents a generic graph database interface for executing Cypher queries

type GraphNode

type GraphNode interface {
	GetProperties() map[string]any
}

GraphNode represents a node returned from the graph database

func WrapNeo4jNode

func WrapNeo4jNode(node neo4j.Node) GraphNode

WrapNeo4jNode wraps a Neo4j node in our GraphNode interface

type Neo4jDatabase

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

Neo4jDatabase implements the GraphDatabase interface using Neo4j

func NewNeo4jDatabase

func NewNeo4jDatabase(uri, username, password string, logger *zap.Logger) (*Neo4jDatabase, error)

NewNeo4jDatabase creates a new Neo4j database instance

func (*Neo4jDatabase) Close

func (db *Neo4jDatabase) Close(ctx context.Context) error

Close closes the database connection

func (*Neo4jDatabase) ExecuteRead

func (db *Neo4jDatabase) ExecuteRead(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)

ExecuteRead executes a read-only Cypher query and returns the raw records

func (*Neo4jDatabase) ExecuteReadSingle

func (db *Neo4jDatabase) ExecuteReadSingle(ctx context.Context, query string, params map[string]any) (map[string]any, error)

ExecuteReadSingle executes a read-only Cypher query expecting a single record

func (*Neo4jDatabase) ExecuteWrite

func (db *Neo4jDatabase) ExecuteWrite(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)

ExecuteWrite executes a write Cypher query and returns the raw records

func (*Neo4jDatabase) ExecuteWriteSingle

func (db *Neo4jDatabase) ExecuteWriteSingle(ctx context.Context, query string, params map[string]any) (map[string]any, error)

ExecuteWriteSingle executes a write Cypher query expecting a single record

func (*Neo4jDatabase) VerifyConnectivity

func (db *Neo4jDatabase) VerifyConnectivity(ctx context.Context) error

VerifyConnectivity checks if the database connection is working

type Neo4jNode

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

Neo4jNode wraps a Neo4j node to implement the GraphNode interface

func (*Neo4jNode) GetProperties

func (n *Neo4jNode) GetProperties() map[string]any

GetProperties returns the node properties

type RelationInfo

type RelationInfo struct {
	FromNodeID ast.NodeID
	ToNodeID   ast.NodeID
	Label      string
}

RelationInfo represents a relationship between nodes

type RelationSpec

type RelationSpec struct {
	ParentID ast.NodeID
	ChildID  ast.NodeID
	Label    string
	Metadata map[string]any
	FileID   int32 // File ID for buffer management (can be from parent or child node)
}

RelationSpec specifies a relationship to be created

Jump to

Keyboard shortcuts

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