Search in sources :

Example 16 with ConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.

the class ConstraintEnforcingEntityOperations method nodeSetProperty.

@Override
public Property nodeSetProperty(KernelStatement state, long nodeId, DefinedProperty property) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException, ConstraintValidationException {
    try (Cursor<NodeItem> cursor = nodeCursorById(state, nodeId)) {
        NodeItem node = cursor.get();
        Iterator<ConstraintDescriptor> constraints = getConstraintsInvolvingProperty(state, property.propertyKeyId());
        Iterator<UniquenessConstraintDescriptor> uniquenessConstraints = new CastingIterator<>(constraints, UniquenessConstraintDescriptor.class);
        nodeSchemaMatcher.onMatchingSchema(state, uniquenessConstraints, node, property.propertyKeyId(), constraint -> {
            validateNoExistingNodeWithExactValues(state, constraint, getAllPropertyValues(state, constraint.schema(), node, property), node.id());
        });
    }
    return entityWriteOperations.nodeSetProperty(state, nodeId, property);
}
Also used : NodeItem(org.neo4j.storageengine.api.NodeItem) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) CastingIterator(org.neo4j.helpers.collection.CastingIterator)

Example 17 with ConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor 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)

Example 18 with ConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.

the class StateHandlingStatementOperations method uniquePropertyConstraintCreate.

@Override
public UniquenessConstraintDescriptor uniquePropertyConstraintCreate(KernelStatement state, LabelSchemaDescriptor descriptor) throws CreateConstraintFailureException {
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
    try {
        if (state.hasTxStateWithChanges() && // ..., DROP, *CREATE*
        state.txState().indexDoUnRemove(constraint.ownedIndexDescriptor())) {
            // creation is undoing a drop
            if (// CREATE, ..., DROP, *CREATE*
            !state.txState().constraintDoUnRemove(constraint)) {
                // ... the drop we are undoing did itself undo a prior create...
                state.txState().constraintDoAdd(constraint, state.txState().indexCreatedForConstraint(constraint));
            }
        } else // *CREATE*
        {
            // create from scratch
            Iterator<ConstraintDescriptor> it = storeLayer.constraintsGetForSchema(descriptor);
            while (it.hasNext()) {
                if (it.next().equals(constraint)) {
                    return constraint;
                }
            }
            long indexId = constraintIndexCreator.createUniquenessConstraintIndex(state, this, descriptor);
            state.txState().constraintDoAdd(constraint, indexId);
        }
        return constraint;
    } catch (UniquePropertyValueValidationException | DropIndexFailureException | TransactionFailureException e) {
        throw new CreateConstraintFailureException(constraint, e);
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) DropIndexFailureException(org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 19 with ConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaImpl method asConstraintDefinitions.

private Iterable<ConstraintDefinition> asConstraintDefinitions(Iterator<? extends ConstraintDescriptor> constraints, ReadOperations readOperations) {
    // Intentionally create an eager list so that used statement can be closed
    List<ConstraintDefinition> definitions = new ArrayList<>();
    while (constraints.hasNext()) {
        ConstraintDescriptor constraint = constraints.next();
        definitions.add(asConstraintDefinition(ConstraintBoundary.map(constraint), readOperations));
    }
    return definitions;
}
Also used : ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) ArrayList(java.util.ArrayList) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 20 with ConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaRuleSerialization method lengthOf.

/**
     * Compute the byte size needed to serialize the provided ConstraintRule using serialize.
     * @param constraintRule the ConstraintRule
     * @return the byte size of ConstraintRule
     */
public static int lengthOf(ConstraintRule constraintRule) {
    // legacy label or relType id
    int length = 4;
    // schema rule type
    length += 1;
    // constraint type
    length += 1;
    ConstraintDescriptor constraintDescriptor = constraintRule.getConstraintDescriptor();
    if (constraintDescriptor.type() == ConstraintDescriptor.Type.UNIQUE) {
        // owned index id
        length += 8;
    }
    length += constraintDescriptor.schema().computeWith(schemaSizeComputer);
    length += UTF8.computeRequiredByteBufferSize(constraintRule.getName());
    return length;
}
Also used : UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)

Aggregations

ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)25 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)15 Test (org.junit.Test)11 NodeExistenceConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor)6 RelExistenceConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor)6 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)3 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)3 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)3 ConstraintRule (org.neo4j.kernel.impl.store.record.ConstraintRule)3 ArrayList (java.util.ArrayList)2 ReadOperations (org.neo4j.kernel.api.ReadOperations)2 Statement (org.neo4j.kernel.api.Statement)2 PropertyConstraint (org.neo4j.kernel.api.constraints.PropertyConstraint)2 NodeItem (org.neo4j.storageengine.api.NodeItem)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1