CodeGraph/docs
Section 3.4

Persistence & Serialization

Serializing in-memory graph instances to compact JSON node/edge arrays and deserializing them back into indexed 5-map graphs.

Source: server/src/graph/serialization/graphSerializer.ts

The `PersistedGraph` Format

The 5 in-memory maps contain redundant adjacency indexes for $O(1)$ query speed. When persisting to MongoDB or transmitting over HTTP, CodeGraph flattens the graph into pure node and edge arrays:

server/src/graph/serialization/graphSerializer.ts
1
2
3
4
5
6
7
8
9
10
11
export interface PersistedGraph { nodes: GraphNode[]; edges: GraphEdge[]; } export function serializeGraph(graph: Graph): PersistedGraph { return { nodes: Array.from(graph.nodes.values()), edges: Array.from(graph.edges.values()), }; }

Full Index Reconstruction on Deserialization

deserializeGraph(data) reconstructs the entire 5-map structure atomically:

server/src/graph/serialization/graphSerializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export function deserializeGraph(data: PersistedGraph): Graph { const nodes = new Map(data.nodes.map(node => [node.id, node])); const edges = new Map(data.edges.map(edge => [edge.id, edge])); const nodesByKind = new Map<GraphNodeKind, Set<string>>(); for (const node of data.nodes) { if (!nodesByKind.has(node.kind)) nodesByKind.set(node.kind, new Set()); nodesByKind.get(node.kind)!.add(node.id); } const outgoingEdges = new Map<string, Set<string>>(); const incomingEdges = new Map<string, Set<string>>(); for (const edge of data.edges) { if (!outgoingEdges.has(edge.sourceId)) outgoingEdges.set(edge.sourceId, new Set()); outgoingEdges.get(edge.sourceId)!.add(edge.id); if (!incomingEdges.has(edge.targetId)) incomingEdges.set(edge.targetId, new Set()); incomingEdges.get(edge.targetId)!.add(edge.id); } return { nodes, edges, nodesByKind, outgoingEdges, incomingEdges }; }