Search in sources :

Example 1 with PropertyConstraint

use of org.neo4j.kernel.api.constraints.PropertyConstraint in project neo4j by neo4j.

the class GraphDbStructureGuide method showUniqueConstraints.

private void showUniqueConstraints(DbStructureVisitor visitor, ReadOperations read, TokenNameLookup nameLookup) {
    Iterator<ConstraintDescriptor> constraints = read.constraintsGetAll();
    while (constraints.hasNext()) {
        PropertyConstraint constraint = ConstraintBoundary.map(constraints.next());
        String userDescription = constraint.userDescription(nameLookup);
        if (constraint instanceof UniquenessConstraint) {
            visitor.visitUniqueConstraint((UniquenessConstraint) constraint, userDescription);
        } else if (constraint instanceof NodePropertyExistenceConstraint) {
            NodePropertyExistenceConstraint existenceConstraint = (NodePropertyExistenceConstraint) constraint;
            visitor.visitNodePropertyExistenceConstraint(existenceConstraint, userDescription);
        } else if (constraint instanceof RelationshipPropertyExistenceConstraint) {
            RelationshipPropertyExistenceConstraint existenceConstraint = (RelationshipPropertyExistenceConstraint) constraint;
            visitor.visitRelationshipPropertyExistenceConstraint(existenceConstraint, userDescription);
        } else {
            throw new IllegalArgumentException("Unknown constraint type: " + constraint.getClass() + ", " + "constraint: " + constraint);
        }
    }
}
Also used : PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) RelationshipPropertyExistenceConstraint(org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint) NodePropertyExistenceConstraint(org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint)

Example 2 with PropertyConstraint

use of org.neo4j.kernel.api.constraints.PropertyConstraint in project neo4j by neo4j.

the class SchemaProcedure method buildSchemaGraph.

public GraphResult buildSchemaGraph() {
    final Map<String, NodeImpl> nodes = new HashMap<>();
    final Map<String, Set<RelationshipImpl>> relationships = new HashMap<>();
    try (Statement statement = kernelTransaction.acquireStatement()) {
        ReadOperations readOperations = statement.readOperations();
        StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup(readOperations);
        try (Transaction transaction = graphDatabaseAPI.beginTx()) {
            // add all labelsInDatabase
            ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator();
            while (labelsInDatabase.hasNext()) {
                Label label = labelsInDatabase.next();
                Map<String, Object> properties = new HashMap<>();
                Iterator<NewIndexDescriptor> indexDescriptorIterator = readOperations.indexesGetForLabel(readOperations.labelGetForName(label.name()));
                ArrayList<String> indexes = new ArrayList<>();
                while (indexDescriptorIterator.hasNext()) {
                    NewIndexDescriptor index = indexDescriptorIterator.next();
                    String[] propertyNames = PropertyNameUtils.getPropertyKeys(statementTokenNameLookup, index.schema().getPropertyIds());
                    indexes.add(String.join(",", propertyNames));
                }
                properties.put("indexes", indexes);
                Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = readOperations.constraintsGetForLabel(readOperations.labelGetForName(label.name()));
                ArrayList<String> constraints = new ArrayList<>();
                while (nodePropertyConstraintIterator.hasNext()) {
                    PropertyConstraint constraint = ConstraintBoundary.map(nodePropertyConstraintIterator.next());
                    constraints.add(constraint.userDescription(statementTokenNameLookup));
                }
                properties.put("constraints", constraints);
                getOrCreateLabel(label.name(), properties, nodes);
            }
            //add all relationships
            Iterator<RelationshipType> relationshipTypeIterator = graphDatabaseAPI.getAllRelationshipTypesInUse().iterator();
            while (relationshipTypeIterator.hasNext()) {
                RelationshipType relationshipType = relationshipTypeIterator.next();
                String relationshipTypeGetName = relationshipType.name();
                int relId = readOperations.relationshipTypeGetForName(relationshipTypeGetName);
                ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();
                List<NodeImpl> startNodes = new LinkedList<>();
                List<NodeImpl> endNodes = new LinkedList<>();
                while (labelsInUse.hasNext()) {
                    Label labelToken = labelsInUse.next();
                    String labelName = labelToken.name();
                    Map<String, Object> properties = new HashMap<>();
                    NodeImpl node = getOrCreateLabel(labelName, properties, nodes);
                    int labelId = readOperations.labelGetForName(labelName);
                    if (readOperations.countsForRelationship(labelId, relId, ReadOperations.ANY_LABEL) > 0) {
                        startNodes.add(node);
                    }
                    if (readOperations.countsForRelationship(ReadOperations.ANY_LABEL, relId, labelId) > 0) {
                        endNodes.add(node);
                    }
                }
                for (NodeImpl startNode : startNodes) {
                    for (NodeImpl endNode : endNodes) {
                        RelationshipImpl relationship = addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
                    }
                }
            }
            transaction.success();
            return getGraphResult(nodes, relationships);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Label(org.neo4j.graphdb.Label) ArrayList(java.util.ArrayList) RelationshipType(org.neo4j.graphdb.RelationshipType) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) LinkedList(java.util.LinkedList) ReadOperations(org.neo4j.kernel.api.ReadOperations) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)

Aggregations

PropertyConstraint (org.neo4j.kernel.api.constraints.PropertyConstraint)2 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 Label (org.neo4j.graphdb.Label)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 Transaction (org.neo4j.graphdb.Transaction)1 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)1 ReadOperations (org.neo4j.kernel.api.ReadOperations)1 Statement (org.neo4j.kernel.api.Statement)1 StatementTokenNameLookup (org.neo4j.kernel.api.StatementTokenNameLookup)1 NodePropertyExistenceConstraint (org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint)1 RelationshipPropertyExistenceConstraint (org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint)1 UniquenessConstraint (org.neo4j.kernel.api.constraints.UniquenessConstraint)1 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)1