| 305 | } |
| 306 | |
| 307 | export class Graph<TSchema extends GraphSchema> implements GraphSource<TSchema> { |
| 308 | #config: GraphConfig<TSchema>; |
| 309 | |
| 310 | #vertexIdentities: WeakMap<StoredVertex, Vertex<TSchema, any>>; |
| 311 | #edgeIdentities: WeakMap<StoredEdge, Edge<TSchema, any>>; |
| 312 | #indexManager: IndexManager<TSchema>; |
| 313 | |
| 314 | public constructor(config: GraphConfig<TSchema>) { |
| 315 | this.#config = config; |
| 316 | this.#vertexIdentities = new WeakMap(); |
| 317 | this.#edgeIdentities = new WeakMap(); |
| 318 | this.#indexManager = new IndexManager(config.schema); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * The index manager for the graph. |
| 323 | * Use this to access indexes for query optimization. |
| 324 | */ |
| 325 | public get indexManager(): IndexManager<TSchema> { |
| 326 | return this.#indexManager; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * The schema of the graph. |
| 331 | */ |
| 332 | public get schema(): TSchema { |
| 333 | return this.#config.schema; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * The storage of the graph. |
| 338 | */ |
| 339 | public get storage(): GraphStorage { |
| 340 | return this.#config.storage; |
| 341 | } |
| 342 | /** |
| 343 | * Generate a new unique identifier for an element. |
| 344 | */ |
| 345 | public generateElementId(label: string): ElementId { |
| 346 | const generateId = this.#config.generateId ?? defaultIdGenerator; |
| 347 | return `${label}:${generateId()}`; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Get an element by its id. |
| 352 | * @param id The id of the element. |
| 353 | * @returns The element. |
| 354 | */ |
| 355 | public getElementById<TLabel extends VertexLabel<TSchema>>( |
| 356 | id: ElementId<TLabel>, |
| 357 | ): Vertex<TSchema, TLabel>; |
| 358 | public getElementById<TLabel extends EdgeLabel<TSchema>>( |
| 359 | id: ElementId<TLabel>, |
| 360 | ): Edge<TSchema, TLabel>; |
| 361 | public getElementById<TLabel extends AnyLabel<TSchema>>(id: ElementId<TLabel>) { |
| 362 | const label = getLabelFromElementId<TLabel>(id); |
| 363 | if (label in this.#config.schema.vertices) { |
| 364 | return this.getVertexById<TLabel>(id); |
nothing calls this directly
no outgoing calls
no test coverage detected