MCPcopy Create free account
hub / github.com/codemix/graph / traverse

Method traverse

packages/graph/src/Steps.ts:6380–6547  ·  view source on GitHub ↗
(
    source: GraphSource<any>,
    input: Iterable<TraversalPath<any, any, any>>,
    context?: QueryContext,
  )

Source from the content-addressed store, hash-verified

6378 }
6379
6380 public *traverse(
6381 source: GraphSource<any>,
6382 input: Iterable<TraversalPath<any, any, any>>,
6383 context?: QueryContext,
6384 ): IterableIterator<TraversalPath<any, any, any>> {
6385 const graph = source as any;
6386 if (typeof graph.addVertex !== "function") {
6387 throw new Error("MERGE requires a Graph instance");
6388 }
6389
6390 const { pattern, onCreate, onMatch } = this.config;
6391
6392 for (const path of input) {
6393 this.traversed++;
6394
6395 let currentPath = path;
6396 let created = false;
6397
6398 if (pattern.type === "node") {
6399 // Try to find existing vertex matching labels + properties
6400 let found: Vertex<any, any> | undefined;
6401
6402 const label = pattern.labels[0] || "Node";
6403 const resolvedProperties = this.resolveProperties(pattern.properties, context, path);
6404
6405 // First, try to find via unique index (much faster for large datasets)
6406 const indexManager = source.indexManager;
6407 if (indexManager) {
6408 const vertices =
6409 pattern.labels.length > 0
6410 ? graph.storage.getVertices(pattern.labels)
6411 : graph.storage.getVertices([]);
6412 const existingId = indexManager.findByUniqueProperties(
6413 label,
6414 resolvedProperties,
6415 vertices,
6416 );
6417 if (existingId) {
6418 const existingVertex = graph.getVertexById(existingId);
6419 if (
6420 existingVertex &&
6421 this.matchesProperties(existingVertex, pattern.properties, context)
6422 ) {
6423 found = existingVertex;
6424 }
6425 }
6426 }
6427
6428 // If not found via unique index, fall back to linear scan
6429 if (!found) {
6430 const vertices =
6431 pattern.labels.length > 0 ? graph.getVertices(...pattern.labels) : graph.getVertices();
6432
6433 for (const vertex of vertices) {
6434 if (this.matchesProperties(vertex, pattern.properties, context)) {
6435 found = vertex;
6436 break;
6437 }

Callers

nothing calls this directly

Calls 12

matchesPropertiesMethod · 0.95
resolveValueMethod · 0.95
withMethod · 0.80
getVerticesMethod · 0.65
getVertexByIdMethod · 0.65
addVertexMethod · 0.65
getMethod · 0.65
getEdgesMethod · 0.65
getEdgeByIdMethod · 0.65
getOutgoingEdgesMethod · 0.65
addEdgeMethod · 0.65

Tested by

no test coverage detected