From 19b6340ae495feace7645923060f9c4b85145a90 Mon Sep 17 00:00:00 2001 From: danthe1st Date: Sun, 24 Nov 2024 10:17:25 +0100 Subject: [PATCH] cache information about unique constraints --- .../danthe1st/arebac/neo4j/graph/Neo4jDB.java | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java index 5f34bce..e9a898f 100644 --- a/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java +++ b/arebac-neo4j/src/main/java/io/github/danthe1st/arebac/neo4j/graph/Neo4jDB.java @@ -2,7 +2,11 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; import io.github.danthe1st.arebac.data.commongraph.attributed.AttributeValue; import io.github.danthe1st.arebac.data.commongraph.attributed.AttributedGraph; @@ -18,6 +22,7 @@ public class Neo4jDB implements AttributedGraph { private final Transaction tx; + private final Map> uniqueAttributesPerNodeType = new HashMap<>(); public Neo4jDB(Transaction tx) { this.tx = tx; @@ -50,16 +55,37 @@ private Collection findEdges(Neo4jNode node, String edgeType, Directi @Override public boolean isAttributeUniqueForNodeType(String key, String nodeType) { + Set uniqueAttributeNames = uniqueAttributesPerNodeType.get(nodeType); + if(uniqueAttributeNames == null){ + uniqueAttributeNames = findUniqueAttributeNames(nodeType); + uniqueAttributesPerNodeType.put(key, uniqueAttributeNames); + } + + return uniqueAttributeNames.contains(key); + } + + private Set findUniqueAttributeNames(String nodeType) { + Set uniqueNodeTypes = new HashSet<>(); for(ConstraintDefinition constraint : tx.schema().getConstraints(Label.label(nodeType))){ if(constraint.isConstraintType(ConstraintType.UNIQUENESS)){ - for(String constraintKey : constraint.getPropertyKeys()){ - if(constraintKey.equals(key)){ - return true; - } - } + checkConstraint(constraint, uniqueNodeTypes); + } + } + return Set.copyOf(uniqueNodeTypes); + } + + private void checkConstraint(ConstraintDefinition constraint, Set uniqueNodeTypes) { + String attributeName = null; + for(String constraintKey : constraint.getPropertyKeys()){ + if(attributeName != null){ + // we only want unique constraints with a single attribute + return; } + attributeName = constraintKey; + } + if(attributeName != null){ + uniqueNodeTypes.add(attributeName); } - return false; } @Override