Search in sources :

Example 56 with ConstraintDescriptor

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

the class SchemaReadWriteTestBase method shouldDropNodePropertyExistenceConstraint.

@Test
void shouldDropNodePropertyExistenceConstraint() throws Exception {
    ConstraintDescriptor constraint;
    try (KernelTransaction transaction = beginTransaction()) {
        constraint = transaction.schemaWrite().nodePropertyExistenceConstraintCreate(forLabel(label, prop1), "constraint name");
        transaction.commit();
    }
    try (KernelTransaction transaction = beginTransaction()) {
        transaction.schemaWrite().constraintDrop(constraint);
        transaction.commit();
    }
    try (KernelTransaction transaction = beginTransaction()) {
        SchemaRead schemaRead = transaction.schemaRead();
        assertFalse(schemaRead.constraintExists(constraint));
        assertThat(asList(schemaRead.constraintsGetForLabel(label))).isEmpty();
        assertThat(asList(schemaRead.snapshot().constraintsGetForLabel(label))).isEmpty();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 57 with ConstraintDescriptor

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

the class SchemaReadWriteTestBase method shouldCreateUniquePropertyConstraint.

@Test
void shouldCreateUniquePropertyConstraint() throws Exception {
    ConstraintDescriptor constraint;
    try (KernelTransaction transaction = beginTransaction()) {
        constraint = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop1)));
        transaction.commit();
    }
    try (KernelTransaction transaction = beginTransaction()) {
        SchemaRead schemaRead = transaction.schemaRead();
        assertTrue(schemaRead.constraintExists(constraint));
        assertThat(asList(schemaRead.constraintsGetForLabel(label))).isEqualTo(singletonList(constraint));
        assertThat(asList(schemaRead.snapshot().constraintsGetForLabel(label))).isEqualTo(singletonList(constraint));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 58 with ConstraintDescriptor

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

the class SchemaReadWriteTestBase method shouldListAllConstraintsInSnapshot.

@Test
void shouldListAllConstraintsInSnapshot() throws Exception {
    ConstraintDescriptor toRetain;
    ConstraintDescriptor toRetain2;
    ConstraintDescriptor toDrop;
    ConstraintDescriptor created;
    try (KernelTransaction tx = beginTransaction()) {
        toRetain = tx.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop1)).withName("first constraint"));
        toRetain2 = tx.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label2, prop1)).withName("second constraint"));
        toDrop = tx.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop2)).withName("third constraint"));
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        SchemaReadCore before = tx.schemaRead().snapshot();
        created = tx.schemaWrite().nodePropertyExistenceConstraintCreate(forLabel(label, prop1), "new constraint");
        tx.schemaWrite().constraintDrop(toDrop);
        Iterable<ConstraintDescriptor> allConstraints = () -> tx.schemaRead().snapshot().constraintsGetAll();
        assertThat(allConstraints).contains(toRetain, toRetain2, created);
        assertThat(before.constraintsGetAll()).toIterable().contains(toRetain, toRetain2, created);
        tx.commit();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Test(org.junit.jupiter.api.Test)

Example 59 with ConstraintDescriptor

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

the class GraphCountsSection method constraints.

private static List<Map<String, Object>> constraints(TokenRead tokens, SchemaRead schemaRead, Anonymizer anonymizer) {
    List<Map<String, Object>> constraints = new ArrayList<>();
    Iterator<ConstraintDescriptor> iterator = schemaRead.constraintsGetAll();
    while (iterator.hasNext()) {
        ConstraintDescriptor constraint = iterator.next();
        EntityType entityType = constraint.schema().entityType();
        Map<String, Object> data = new HashMap<>();
        data.put("properties", map(constraint.schema().getPropertyIds(), id -> anonymizer.propertyKey(tokens.propertyKeyGetName(id), id)));
        data.put("type", constraintType(constraint));
        int entityTokenId = constraint.schema().getEntityTokenIds()[0];
        switch(entityType) {
            case NODE:
                data.put("label", anonymizer.label(tokens.labelGetName(entityTokenId), entityTokenId));
                constraints.add(data);
                break;
            case RELATIONSHIP:
                data.put("relationshipType", anonymizer.relationshipType(tokens.relationshipTypeGetName(entityTokenId), entityTokenId));
                constraints.add(data);
                break;
            default:
        }
    }
    return constraints;
}
Also used : EntityType(org.neo4j.common.EntityType) Arrays(java.util.Arrays) Iterator(java.util.Iterator) Read(org.neo4j.internal.kernel.api.Read) Iterators(org.neo4j.internal.helpers.collection.Iterators) IndexType(org.neo4j.internal.schema.IndexType) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) Kernel(org.neo4j.kernel.api.Kernel) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) Stream(java.util.stream.Stream) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) EntityType(org.neo4j.common.EntityType) Map(java.util.Map) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NamedToken(org.neo4j.token.api.NamedToken) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) IntFunction(java.util.function.IntFunction) HashMap(java.util.HashMap) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with ConstraintDescriptor

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

the class SchemaImpl method asConstraintDefinitions.

private Iterable<ConstraintDefinition> asConstraintDefinitions(Iterator<? extends ConstraintDescriptor> constraints, TokenRead tokenRead) {
    // 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(constraint, tokenRead));
    }
    return definitions;
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) ArrayList(java.util.ArrayList) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

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