Section 3.4Source:
server/src/graph/serialization/graphSerializer.tsPersistence & 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.tsThe `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
1234567891011export 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
1234567891011121314151617181920212223export 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 }; }