Search in sources :

Example 86 with ConstraintDescriptor

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

the class UniquenessConstraintValidationIT method createConstraint.

private ConstraintDescriptor createConstraint(String label, String propertyKey) throws KernelException {
    int labelId;
    int propertyKeyId;
    TokenWrite tokenWrite = tokenWriteInNewTransaction();
    labelId = tokenWrite.labelGetOrCreateForName(label);
    propertyKeyId = tokenWrite.propertyKeyGetOrCreateForName(propertyKey);
    commit();
    SchemaWrite schemaWrite = schemaWriteInNewTransaction();
    ConstraintDescriptor constraint = schemaWrite.uniquePropertyConstraintCreate(uniqueForSchema(forLabel(labelId, propertyKeyId)));
    commit();
    return constraint;
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite)

Example 87 with ConstraintDescriptor

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

the class NodeGetUniqueFromIndexSeekIT method createUniquenessConstraint.

private IndexDescriptor createUniquenessConstraint(int labelId, int... propertyIds) throws Exception {
    KernelTransaction transaction = newTransaction(LoginContext.AUTH_DISABLED);
    LabelSchemaDescriptor schema = SchemaDescriptor.forLabel(labelId, propertyIds);
    ConstraintDescriptor constraint = transaction.schemaWrite().uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(schema));
    IndexDescriptor index = transaction.schemaRead().indexGetForName(constraint.getName());
    commit();
    return index;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 88 with ConstraintDescriptor

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

the class SchemaRuleSerialization35Test method assertParseUniqueConstraintRule.

private static void assertParseUniqueConstraintRule(String serialized, String name) throws MalformedSchemaRuleException {
    // GIVEN
    long ruleId = 1;
    int propertyKey = 3;
    int labelId = 55;
    long ownedIndexId = 2;
    UniquenessConstraintDescriptor constraint = uniqueForLabel(labelId, propertyKey);
    byte[] bytes = decodeBase64(serialized);
    // WHEN
    ConstraintDescriptor deserialized = assertConstraintRule(SchemaRuleSerialization35.deserialize(ruleId, ByteBuffer.wrap(bytes)));
    // THEN
    assertThat(deserialized.getId()).isEqualTo(ruleId);
    assertThat(deserialized).isEqualTo(constraint);
    assertThat(deserialized.schema()).isEqualTo(constraint.schema());
    assertThat(deserialized.asIndexBackedConstraint().ownedIndexId()).isEqualTo(ownedIndexId);
    assertThat(deserialized.getName()).isEqualTo(name);
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)

Example 89 with ConstraintDescriptor

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

the class SchemaRuleSerialization35Test method assertParseNodeKeyConstraintRule.

private static void assertParseNodeKeyConstraintRule(String serialized, String name) throws MalformedSchemaRuleException {
    // GIVEN
    long ruleId = 1;
    int propertyKey = 3;
    int labelId = 55;
    long ownedIndexId = 2;
    NodeKeyConstraintDescriptor constraint = nodeKeyForLabel(labelId, propertyKey);
    byte[] bytes = decodeBase64(serialized);
    // WHEN
    ConstraintDescriptor deserialized = assertConstraintRule(SchemaRuleSerialization35.deserialize(ruleId, ByteBuffer.wrap(bytes)));
    // THEN
    assertThat(deserialized.getId()).isEqualTo(ruleId);
    assertThat(deserialized).isEqualTo(constraint);
    assertThat(deserialized.schema()).isEqualTo(constraint.schema());
    assertThat(deserialized.asIndexBackedConstraint().ownedIndexId()).isEqualTo(ownedIndexId);
    assertThat(deserialized.getName()).isEqualTo(name);
}
Also used : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)

Example 90 with ConstraintDescriptor

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

the class SchemaRuleException method describe.

public static String describe(SchemaDescriptorSupplier schemaThing) {
    SchemaDescriptor schema = schemaThing.schema();
    String tagType;
    switch(schema.entityType()) {
        case NODE:
            tagType = "label";
            break;
        case RELATIONSHIP:
            tagType = "relationship type";
            break;
        default:
            throw new AssertionError("Unknown entity type: " + schema.entityType());
    }
    if (schemaThing instanceof ConstraintDescriptor) {
        ConstraintDescriptor constraint = (ConstraintDescriptor) schemaThing;
        switch(constraint.type()) {
            case UNIQUE:
                return tagType + " uniqueness constraint";
            case EXISTS:
                return tagType + " property existence constraint";
            case UNIQUE_EXISTS:
                return schema.entityType().name().toLowerCase() + " key constraint";
            default:
                throw new AssertionError("Unknown constraint type: " + constraint.type());
        }
    } else {
        IndexDescriptor index = (IndexDescriptor) schemaThing;
        IndexType indexType = index.getIndexType();
        if (indexType != IndexType.BTREE) {
            String indexTypeName = indexType.name().toLowerCase();
            return indexTypeName + " " + tagType + " index";
        } else {
            return tagType + " index";
        }
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexType(org.neo4j.internal.schema.IndexType)

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