Skip to content

Commit 16c431a

Browse files
Cole-Greersophiadt
authored andcommitted
merge 'Update getEdgesDirections queries' from aws#16
Replaces the queries to fetch edge directions while building the schema. The old queries worked by testing for the existence of any possible edge, which resulted in a large number of queries for graphs with lots of node and edge labels. The new query instead iterates through all the edge labels, and runs a query to fetch the corresponding from-label to-label pairs.
1 parent c162b62 commit 16c431a

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/NeptuneSchema.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -157,33 +157,23 @@ async function getEdgesNames() {
157157
}
158158

159159

160-
async function checkEdgeDirection(direction) {
161-
let query = `MATCH(from)-[r]->(to)
162-
WHERE $from in labels(from)
163-
AND type(r) = $edge
164-
AND $to in labels(to)
165-
RETURN r as edge LIMIT 1`;
166-
let parameters = `{"from": "${direction.from}", "edge": "${direction.edge.label}", "to": "${direction.to}"}`;
160+
async function findFromAndToLabels(edgeStructure) {
161+
let query = `MATCH (from)-[r]->(to) WHERE type(r) = $edge RETURN DISTINCT labels(from) as fromLabel, labels(to) as toLabel`;
162+
let parameters = `{"edge": "${edgeStructure.label}"}`;
167163
let response = await queryNeptune(query, parameters);
168-
let result = response.results[0];
169-
if (result !== undefined) {
170-
direction.edge.directions.push({from:direction.from, to:direction.to});
171-
consoleOut(' Found edge: ' + yellow(direction.edge.label) + ' direction: ' + yellow(direction.from) + ' -> ' + yellow(direction.to));
164+
for (let result of response.results) {
165+
for (let fromLabel of result.fromLabel) {
166+
for (let toLabel of result.toLabel) {
167+
edgeStructure.directions.push({from:fromLabel, to:toLabel});
168+
consoleOut(' Found edge: ' + yellow(edgeStructure.label) + ' direction: ' + yellow(fromLabel) + ' -> ' + yellow(toLabel));
169+
}
170+
}
172171
}
173172
}
174173

175174

176175
async function getEdgesDirections() {
177-
let possibleDirections = [];
178-
for (const edge of schema.edgeStructures) {
179-
for (const fromNode of schema.nodeStructures) {
180-
for (const toNode of schema.nodeStructures) {
181-
possibleDirections.push({edge:edge, from:fromNode.label, to:toNode.label});
182-
}
183-
}
184-
}
185-
186-
await Promise.all(possibleDirections.map(checkEdgeDirection))
176+
await Promise.all(schema.edgeStructures.map(findFromAndToLabels))
187177
}
188178

189179

0 commit comments

Comments
 (0)