From 4e88e490908ceb4ce313f9fb8bf5b317d5cf2e9f Mon Sep 17 00:00:00 2001 From: Amar07Singh Date: Thu, 25 Jun 2026 19:40:41 +0530 Subject: [PATCH] perf: parallelize Neo4j index creation in ensureKnowledgeIndexes, ensureConceptGraphIndexes, ensureFlatFolderIndexes - Replace sequential for...of loops with Promise.allSettled() - All CREATE ... IF NOT EXISTS statements are idempotent and safe to parallelize - Re-throw on actual failures after checking all settled results --- packages/neo4j/src/conceptGraphIndexes.ts | 26 ++++++++++++++--------- packages/neo4j/src/flatFolderIndexes.ts | 26 ++++++++++++++--------- packages/neo4j/src/indexes.ts | 26 ++++++++++++++--------- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/packages/neo4j/src/conceptGraphIndexes.ts b/packages/neo4j/src/conceptGraphIndexes.ts index e3ff522..120d4d0 100644 --- a/packages/neo4j/src/conceptGraphIndexes.ts +++ b/packages/neo4j/src/conceptGraphIndexes.ts @@ -22,16 +22,22 @@ const FULLTEXT_INDEXES = [ ]; export async function ensureConceptGraphIndexes(): Promise { - for (const cypher of [...CONSTRAINTS, ...FULLTEXT_INDEXES]) { - try { - await _runCypher(cypher); - } catch (cause: unknown) { - const msg = cause instanceof Error ? cause.message : String(cause); - if (msg.includes("already exists") || msg.includes("EquivalentSchemaRuleAlreadyExists")) { - process.stderr.write(`[neo4j] concept-graph schema already present, skipping: ${cypher.slice(0, 60)}…\n`); - continue; - } - throw cause; + const results = await Promise.allSettled( + [...CONSTRAINTS, ...FULLTEXT_INDEXES].map((cypher) => + _runCypher(cypher).catch((cause: unknown) => { + const msg = cause instanceof Error ? cause.message : String(cause); + if (msg.includes("already exists") || msg.includes("EquivalentSchemaRuleAlreadyExists")) { + process.stderr.write(`[neo4j] concept-graph schema already present, skipping: ${cypher.slice(0, 60)}…\n`); + return; + } + throw cause; + }), + ), + ); + + for (const result of results) { + if (result.status === "rejected") { + throw result.reason; } } } diff --git a/packages/neo4j/src/flatFolderIndexes.ts b/packages/neo4j/src/flatFolderIndexes.ts index 1eef2ba..47deea5 100644 --- a/packages/neo4j/src/flatFolderIndexes.ts +++ b/packages/neo4j/src/flatFolderIndexes.ts @@ -23,16 +23,22 @@ const FULLTEXT_INDEXES = [ ]; export async function ensureFlatFolderIndexes(): Promise { - for (const cypher of [...CONSTRAINTS, ...FULLTEXT_INDEXES]) { - try { - await _runCypher(cypher); - } catch (cause: unknown) { - const msg = cause instanceof Error ? cause.message : String(cause); - if (msg.includes("already exists") || msg.includes("EquivalentSchemaRuleAlreadyExists")) { - process.stderr.write(`[neo4j] flat-folder schema already present, skipping: ${cypher.slice(0, 60)}…\n`); - continue; - } - throw cause; + const results = await Promise.allSettled( + [...CONSTRAINTS, ...FULLTEXT_INDEXES].map((cypher) => + _runCypher(cypher).catch((cause: unknown) => { + const msg = cause instanceof Error ? cause.message : String(cause); + if (msg.includes("already exists") || msg.includes("EquivalentSchemaRuleAlreadyExists")) { + process.stderr.write(`[neo4j] flat-folder schema already present, skipping: ${cypher.slice(0, 60)}…\n`); + return; + } + throw cause; + }), + ), + ); + + for (const result of results) { + if (result.status === "rejected") { + throw result.reason; } } } diff --git a/packages/neo4j/src/indexes.ts b/packages/neo4j/src/indexes.ts index 735ee58..c588e4d 100644 --- a/packages/neo4j/src/indexes.ts +++ b/packages/neo4j/src/indexes.ts @@ -18,16 +18,22 @@ const FULLTEXT_INDEXES = [ ]; export async function ensureKnowledgeIndexes(): Promise { - for (const cypher of [...CONSTRAINTS, ...FULLTEXT_INDEXES]) { - try { - await _runCypher(cypher); - } catch (cause: unknown) { - const msg = cause instanceof Error ? cause.message : String(cause); - if (msg.includes("already exists") || msg.includes("EquivalentSchemaRuleAlreadyExists")) { - process.stderr.write(`[neo4j] schema already present, skipping: ${cypher.slice(0, 60)}…\n`); - continue; - } - throw cause; + const results = await Promise.allSettled( + [...CONSTRAINTS, ...FULLTEXT_INDEXES].map((cypher) => + _runCypher(cypher).catch((cause: unknown) => { + const msg = cause instanceof Error ? cause.message : String(cause); + if (msg.includes("already exists") || msg.includes("EquivalentSchemaRuleAlreadyExists")) { + process.stderr.write(`[neo4j] schema already present, skipping: ${cypher.slice(0, 60)}…\n`); + return; + } + throw cause; + }), + ), + ); + + for (const result of results) { + if (result.status === "rejected") { + throw result.reason; } } }