Search in sources :

Example 11 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class RecordStorageReaderSchemaTest method shouldListAllConstraintsForLabelAtTimeOfSnapshot.

@Test
void shouldListAllConstraintsForLabelAtTimeOfSnapshot() throws Exception {
    // Given
    createUniquenessConstraint(label1, propertyKey);
    createUniquenessConstraint(label2, propertyKey);
    // When
    StorageSchemaReader snapshot = storageReader.schemaSnapshot();
    createUniquenessConstraint(label1, otherPropertyKey);
    Set<ConstraintDescriptor> constraints = asSet(snapshot.constraintsGetForLabel(labelId(label1)));
    // Then
    Set<ConstraintDescriptor> expectedConstraints = asSet(uniqueConstraintDescriptor(label1, propertyKey));
    assertEquals(expectedConstraints, constraints);
}
Also used : StorageSchemaReader(org.neo4j.storageengine.api.StorageSchemaReader) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 12 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class TxState method accept.

@Override
public void accept(final TxStateVisitor visitor) throws KernelException {
    if (nodes != null) {
        nodes.getAdded().each(visitor::visitCreatedNode);
    }
    if (relationships != null) {
        try (HeapTrackingArrayList<NodeRelationshipIds> sortedNodeRelState = HeapTrackingArrayList.newArrayList(nodeStatesMap.size(), memoryTracker)) {
            nodeStatesMap.forEachValue(nodeState -> {
                if (nodeState.isDeleted() && nodeState.isAddedInThisTx()) {
                    return;
                }
                if (nodeState.hasAddedRelationships() || nodeState.hasRemovedRelationships()) {
                    sortedNodeRelState.add(StateNodeRelationshipIds.createStateNodeRelationshipIds(nodeState, this::relationshipVisit, memoryTracker));
                }
            });
            sortedNodeRelState.sort(Comparator.comparingLong(NodeRelationshipIds::nodeId));
            // Visit relationships, this will grab all the locks needed to do the updates
            visitor.visitRelationshipModifications(new RelationshipModifications() {

                @Override
                public void forEachSplit(IdsVisitor visitor) {
                    sortedNodeRelState.forEach(visitor);
                }

                @Override
                public RelationshipBatch creations() {
                    return idsAsBatch(relationships.getAdded(), TxState.this::relationshipVisit);
                }

                @Override
                public RelationshipBatch deletions() {
                    return idsAsBatch(relationships.getRemoved());
                }
            });
        }
    }
    if (nodes != null) {
        nodes.getRemoved().each(visitor::visitDeletedNode);
    }
    for (NodeState node : modifiedNodes()) {
        if (node.hasPropertyChanges()) {
            visitor.visitNodePropertyChanges(node.getId(), node.addedProperties(), node.changedProperties(), node.removedProperties());
        }
        final LongDiffSets labelDiffSets = node.labelDiffSets();
        if (!labelDiffSets.isEmpty()) {
            visitor.visitNodeLabelChanges(node.getId(), labelDiffSets.getAdded(), labelDiffSets.getRemoved());
        }
    }
    for (RelationshipState rel : modifiedRelationships()) {
        visitor.visitRelPropertyChanges(rel.getId(), rel.addedProperties(), rel.changedProperties(), rel.removedProperties());
    }
    if (indexChanges != null) {
        for (IndexDescriptor indexDescriptor : indexChanges.getAdded()) {
            visitor.visitAddedIndex(indexDescriptor);
        }
        indexChanges.getRemoved().forEach(visitor::visitRemovedIndex);
    }
    if (constraintsChanges != null) {
        for (ConstraintDescriptor added : constraintsChanges.getAdded()) {
            visitor.visitAddedConstraint(added);
        }
        constraintsChanges.getRemoved().forEach(visitor::visitRemovedConstraint);
    }
    if (createdLabelTokens != null) {
        createdLabelTokens.forEachKeyValue((id, token) -> visitor.visitCreatedLabelToken(id, token.name, token.internal));
    }
    if (createdPropertyKeyTokens != null) {
        createdPropertyKeyTokens.forEachKeyValue((id, token) -> visitor.visitCreatedPropertyKeyToken(id, token.name, token.internal));
    }
    if (createdRelationshipTypeTokens != null) {
        createdRelationshipTypeTokens.forEachKeyValue((id, token) -> visitor.visitCreatedRelationshipTypeToken(id, token.name, token.internal));
    }
}
Also used : NodeRelationshipIds(org.neo4j.storageengine.api.txstate.RelationshipModifications.NodeRelationshipIds) RelationshipModifications(org.neo4j.storageengine.api.txstate.RelationshipModifications) NodeState(org.neo4j.storageengine.api.txstate.NodeState) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) RelationshipState(org.neo4j.storageengine.api.txstate.RelationshipState) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TrackableDiffSets.newMutableLongDiffSets(org.neo4j.kernel.impl.util.diffsets.TrackableDiffSets.newMutableLongDiffSets) MutableLongDiffSets(org.neo4j.kernel.impl.util.diffsets.MutableLongDiffSets) LongDiffSets(org.neo4j.storageengine.api.txstate.LongDiffSets)

Example 13 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class IntegrityValidatorTest method shouldValidateUniquenessIndexes.

@Test
void shouldValidateUniquenessIndexes() throws Exception {
    // Given
    NeoStores store = mock(NeoStores.class);
    IndexUpdateListener indexes = mock(IndexUpdateListener.class);
    IntegrityValidator validator = new IntegrityValidator(store);
    validator.setIndexValidator(indexes);
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForLabel(1, 1);
    doThrow(new ConstraintViolationException("error", new RuntimeException())).when(indexes).validateIndex(2L);
    ConstraintDescriptor record = constraint.withId(1).withOwnedIndexId(2);
    // When
    assertThrows(Exception.class, () -> validator.validateSchemaRule(record));
}
Also used : NeoStores(org.neo4j.kernel.impl.store.NeoStores) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexUpdateListener(org.neo4j.storageengine.api.IndexUpdateListener) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 14 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class ConstraintTestBase method shouldFindConstraintsByLabel.

@Test
void shouldFindConstraintsByLabel() throws Exception {
    // GIVEN
    addConstraints("FOO", "prop1", "FOO", "prop2");
    try (KernelTransaction tx = beginTransaction()) {
        int label = tx.tokenWrite().labelGetOrCreateForName("FOO");
        // WHEN
        List<ConstraintDescriptor> constraints = asList(tx.schemaRead().constraintsGetForLabel(label));
        // THEN
        assertThat(constraints).hasSize(2);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 15 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class ConstraintTestBase method shouldFindAllConstraints.

@Test
void shouldFindAllConstraints() throws Exception {
    // GIVEN
    addConstraints("FOO", "prop1", "BAR", "prop2", "BAZ", "prop3");
    try (KernelTransaction tx = beginTransaction()) {
        // WHEN
        List<ConstraintDescriptor> constraints = asList(tx.schemaRead().constraintsGetAll());
        // THEN
        assertThat(constraints).hasSize(3);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)107 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)34 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)32 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)21 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)20 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)19 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)16 TokenRead (org.neo4j.internal.kernel.api.TokenRead)9 ArrayList (java.util.ArrayList)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)6 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)6 SchemaStore (org.neo4j.kernel.impl.store.SchemaStore)6 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)5 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)5 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)5 SchemaRule (org.neo4j.internal.schema.SchemaRule)5 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)5 OptionalLong (java.util.OptionalLong)4