Skip to content

Commit

Permalink
cache information about unique constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Nov 24, 2024
1 parent 53ff8a2 commit 19b6340
Showing 1 changed file with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +22,7 @@

public class Neo4jDB implements AttributedGraph<Neo4jNode, Neo4jEdge> {
private final Transaction tx;
private final Map<String, Set<String>> uniqueAttributesPerNodeType = new HashMap<>();

public Neo4jDB(Transaction tx) {
this.tx = tx;
Expand Down Expand Up @@ -50,16 +55,37 @@ private Collection<Neo4jEdge> findEdges(Neo4jNode node, String edgeType, Directi

@Override
public boolean isAttributeUniqueForNodeType(String key, String nodeType) {
Set<String> uniqueAttributeNames = uniqueAttributesPerNodeType.get(nodeType);
if(uniqueAttributeNames == null){
uniqueAttributeNames = findUniqueAttributeNames(nodeType);
uniqueAttributesPerNodeType.put(key, uniqueAttributeNames);
}

return uniqueAttributeNames.contains(key);
}

private Set<String> findUniqueAttributeNames(String nodeType) {
Set<String> 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<String> 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
Expand Down

0 comments on commit 19b6340

Please sign in to comment.