API Reference

High-Performance Zero-Copy Cognitive Graph for Advanced Code Analysis

API Reference

Overview

The CognitiveGraph library provides a high-performance, zero-copy graph structure for advanced code analysis. The API is designed around three core concepts:

  1. Building Graphs - Creating graph structures with CognitiveGraphBuilder
  2. Reading Graphs - Accessing graph data with zero-copy CognitiveGraph
  3. Navigation - Traversing nodes and edges through accessor objects

Core Classes

CognitiveGraph

The main entry point for reading and navigating cognitive graphs.

public sealed class CognitiveGraph : IDisposable

Key Methods

Usage Example

using var graph = new CognitiveGraph(buffer);
var rootNode = graph.GetRootNode();
var sourceText = graph.GetSourceText();

CognitiveGraphBuilder

Builder class for creating cognitive graphs with zero-copy architecture.

public sealed class CognitiveGraphBuilder : IDisposable

Key Methods

Usage Example

using var builder = new CognitiveGraphBuilder();
var nodeOffset = builder.WriteSymbolNode(
    symbolId: 1,
    nodeType: 200,
    sourceStart: 0,
    sourceLength: 13,
    properties: properties
);
var buffer = builder.Build(nodeOffset, sourceCode);

Accessor Classes

SymbolNode

Represents a symbol in the Abstract Syntax Tree with zero-copy access.

public readonly ref struct SymbolNode

Properties

Key Methods

Usage Example

if (rootNode.TryGetProperty("Operator", out var op))
{
    Console.WriteLine($"Operator: {op.AsString()}");
}

if (rootNode.IsAmbiguous)
{
    var interpretations = rootNode.GetPackedNodes();
    foreach (var interpretation in interpretations)
    {
        // Process each possible parse tree
    }
}

PackedNode

Represents a specific parse interpretation for ambiguous syntax.

public readonly ref struct PackedNode

Properties

Key Methods

Property

Type-safe property value accessor supporting multiple data types.

public readonly ref struct Property

Properties

Key Methods

Usage Example

if (node.TryGetProperty("LineNumber", out var lineProp))
{
    if (lineProp.TryAsInt32(out int lineNum))
    {
        Console.WriteLine($"Line: {lineNum}");
    }
}

CpgEdge

Represents control flow and data flow relationships between nodes.

public readonly ref struct CpgEdge

Properties

Edge Types

Enumerations

PropertyValueType

Supported property value types:

CpgEdgeType

Types of relationships in the Code Property Graph:

Memory Management

Zero-Copy Architecture

The library uses ReadOnlySpan<T> and ref struct types to provide direct memory access without allocations:

Disposal Pattern

// Builder disposal
using var builder = new CognitiveGraphBuilder();
// Automatically disposed when out of scope

// Graph disposal
using var graph = new CognitiveGraph(buffer);
// Automatically disposed when out of scope

// Memory-mapped file usage
using var mmf = MemoryMappedFile.CreateFromFile("graph.bin");
using var accessor = mmf.CreateViewAccessor();
// Resources automatically cleaned up

Error Handling

Common Exceptions

Best Practices

// Always use try-get methods for optional data
if (node.TryGetProperty("OptionalProp", out var prop))
{
    // Property exists, safe to use
}

// Check type before casting
if (prop.Type == PropertyValueType.String)
{
    string value = prop.AsString();
}

// Use using statements for proper disposal
using var graph = new CognitiveGraph(buffer);
// Graph automatically disposed at end of scope

Threading and Concurrency

Thread Safety

Example: Concurrent Reading

var graph = new CognitiveGraph(buffer);

// Multiple threads can safely read from the same graph
Parallel.ForEach(nodeOffsets, offset =>
{
    var node = graph.GetNode(offset);
    // Process node safely in parallel
});

Performance Characteristics

Time Complexity

Space Complexity

Integration Examples

Basic Usage Pattern

// 1. Build a graph
using var builder = new CognitiveGraphBuilder();
var rootOffset = builder.WriteSymbolNode(/* parameters */);
var buffer = builder.Build(rootOffset, sourceCode);

// 2. Read and navigate
using var graph = new CognitiveGraph(buffer);
var root = graph.GetRootNode();

// 3. Extract information
if (root.TryGetProperty("NodeType", out var nodeType))
{
    ProcessNodeType(nodeType.AsString());
}

// 4. Navigate structure
foreach (var child in root.GetChildNodes())
{
    ProcessChild(child);
}

Advanced: Memory-Mapped Files

// For very large graphs that don't fit in memory
using var mmf = MemoryMappedFile.CreateFromFile("huge-graph.bin");
using var accessor = mmf.CreateViewAccessor();

unsafe
{
    byte* ptr = (byte*)accessor.SafeMemoryMappedViewHandle.DangerousGetHandle();
    var span = new ReadOnlySpan<byte>(ptr, (int)accessor.Capacity);
    var buffer = new CognitiveGraphBuffer(span);
    
    using var graph = new CognitiveGraph(buffer);
    // Process without loading entire file into memory
}

Version Compatibility

For more examples and advanced usage patterns, see the Examples directory in the source code.