| 129 | } |
| 130 | |
| 131 | export class InMemoryGraphStorage implements GraphStorage { |
| 132 | #vertices: Map<ElementId, StoredVertex>; |
| 133 | #edges: Map<ElementId, StoredEdge>; |
| 134 | #incomingEdges: Map<ElementId, StoredEdge[]>; |
| 135 | #outgoingEdges: Map<ElementId, StoredEdge[]>; |
| 136 | |
| 137 | public constructor(data?: { vertices: StoredVertex[]; edges: StoredEdge[] }) { |
| 138 | const vertices: Map<ElementId, StoredVertex> = new Map(); |
| 139 | const edges: Map<ElementId, StoredEdge> = new Map(); |
| 140 | |
| 141 | this.#vertices = vertices; |
| 142 | this.#edges = edges; |
| 143 | this.#incomingEdges = new Map(); |
| 144 | this.#outgoingEdges = new Map(); |
| 145 | if (data != null) { |
| 146 | for (const vertex of data.vertices) { |
| 147 | this.addVertex(vertex); |
| 148 | } |
| 149 | for (const edge of data.edges) { |
| 150 | this.addEdge(edge); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | protected get vertices(): Map<ElementId, StoredVertex> { |
| 156 | return this.#vertices; |
| 157 | } |
| 158 | |
| 159 | protected get edges(): Map<ElementId, StoredEdge> { |
| 160 | return this.#edges; |
| 161 | } |
| 162 | |
| 163 | protected get incomingEdges(): Map<ElementId, StoredEdge[]> { |
| 164 | return this.#incomingEdges; |
| 165 | } |
| 166 | |
| 167 | protected get outgoingEdges(): Map<ElementId, StoredEdge[]> { |
| 168 | return this.#outgoingEdges; |
| 169 | } |
| 170 | |
| 171 | public getVertexById(id: ElementId): StoredVertex | undefined { |
| 172 | return this.#vertices.get(id); |
| 173 | } |
| 174 | |
| 175 | public *getVertices(labels: string[]): Iterable<StoredVertex> { |
| 176 | // Snapshot keys to prevent infinite loops when vertices are added during iteration. |
| 177 | // Keys are just strings so this is cheap; actual vertex data is still fetched lazily. |
| 178 | const keys = [...this.#vertices.keys()]; |
| 179 | for (const key of keys) { |
| 180 | const vertex = this.#vertices.get(key); |
| 181 | if ( |
| 182 | vertex !== undefined && |
| 183 | (labels.length === 0 || labels.includes(getLabelFromElementId(vertex.id))) |
| 184 | ) { |
| 185 | yield vertex; |
| 186 | } |
| 187 | } |
| 188 | } |
nothing calls this directly
no outgoing calls
no test coverage detected