Dual-Schema Architecture Implementation Summary

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

Dual-Schema Architecture Implementation Summary

Overview

This implementation provides the complete Dual-Schema Architecture as specified in docs/Dual-Schema Architecture.docx, enabling CognitiveGraph to support both compact (V1) and universal (V2) schemas for maximum flexibility and scalability.

Implemented Components

1. Schema Version Management

2. Schema V2 Data Structures

All V2 structures use 8-byte alignment and 64-bit offsets:

3. Buffer Abstraction Layer

4. V2 Accessors

Zero-allocation ref struct accessors for V2 schema:

5. Configuration

6. CognitiveGraphBuilder Integration ✅ NEW

The builder now fully supports dual-schema as per TDS Section 4.3:

7. CognitiveGraph Integration ✅ NEW

The main class now implements TDS Section 3.1 “Header Resolution Strategy”:

8. Migration & Upgrade ✅ NEW

As specified in TDS Section 5.2:

Backward Compatibility

The implementation maintains 100% backward compatibility with existing V1 graphs:

  1. CognitiveGraphBuffer wrapper class (marked obsolete) delegates to CompactGraphBuffer
  2. All existing V1 accessors (SymbolNode, PackedNode) continue to work unchanged
  3. File format detection via GraphHeaderPreamble automatically identifies V1 vs V2
  4. V2-enabled library can seamlessly read and process V1 files
  5. Builder defaults to V1 for backward compatibility (V2 opt-in via GraphBuilderOptions.Universal())

Key Design Decisions

Default to V1 (Updated)

To maintain backward compatibility, GraphBuilderOptions defaults to SchemaVersion.V1. Users can explicitly choose V2 via GraphBuilderOptions.Universal() when maximum scalability is needed.

Separate Accessor Types

Rather than using a unified accessor with runtime version checks (which would harm performance), the implementation provides separate accessor types (SymbolNode vs SymbolNode64). This ensures optimal performance by avoiding branch misprediction.

Buffer Interface

The IGraphBuffer interface provides a clean abstraction while allowing specialized implementations to optimize for their specific constraints (safe spans for V1, unsafe pointers for V2).

Preamble-First Reading

Following TDS Section 3.1 exactly: CognitiveGraph constructor reads the 6-byte preamble, determines version, then casts/reads the full header (40 bytes for V1, 64 bytes for V2).

Testing

Complete Implementation Status

✅ All Core Components from TDS Implemented:

  1. ✅ Schema V2 structures (GraphHeaderV2, SymbolNodeDataV2, PackedNodeDataV2)
  2. ✅ Buffer abstraction (IGraphBuffer, CompactGraphBuffer, UniversalGraphBuffer)
  3. ✅ V2 accessors (SymbolNode64, PackedNode64)
  4. ✅ Builder configuration (GraphBuilderOptions)
  5. ✅ Builder V2 write support (Section 4.3)
  6. ✅ CognitiveGraph dual-schema detection (Section 3.1)
  7. ✅ Upgrade utility (Section 5.2)

Migration Path

For users wanting to adopt V2:

  1. New graphs: Use new CognitiveGraphBuilder(GraphBuilderOptions.Universal()) to create V2 graphs
  2. Existing V1 graphs: Continue to work without modification
  3. Upgrade V1 → V2: Use CognitiveGraph.Upgrade(inputPath, outputPath) method

File Compatibility