Search in sources :

Example 21 with ConstraintDescriptor

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

the class SchemaRuleSerialization method serialize.

/**
     * Serialize the provided ConstraintRule onto the target buffer
     * @param constraintRule the ConstraintRule to serialize
     * @throws IllegalStateException if the ConstraintRule is of type unique, but the owned index has not been set
     */
public static byte[] serialize(ConstraintRule constraintRule) {
    ByteBuffer target = ByteBuffer.allocate(lengthOf(constraintRule));
    target.putInt(LEGACY_LABEL_OR_REL_TYPE_ID);
    target.put(CONSTRAINT_RULE);
    ConstraintDescriptor constraintDescriptor = constraintRule.getConstraintDescriptor();
    switch(constraintDescriptor.type()) {
        case EXISTS:
            target.put(EXISTS_CONSTRAINT);
            break;
        case UNIQUE:
            target.put(UNIQUE_CONSTRAINT);
            target.putLong(constraintRule.getOwnedIndex());
            break;
        default:
            throw new UnsupportedOperationException(format("Got unknown index descriptor type '%s'.", constraintDescriptor.type()));
    }
    constraintDescriptor.schema().processWith(new SchemaDescriptorSerializer(target));
    UTF8.putEncodedStringInto(constraintRule.getName(), target);
    return target.array();
}
Also used : UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) ByteBuffer(java.nio.ByteBuffer)

Example 22 with ConstraintDescriptor

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

the class AbstractConstraintCreationIT method shouldBeAbleToStoreAndRetrieveConstraint.

@Test
public void shouldBeAbleToStoreAndRetrieveConstraint() throws Exception {
    // given
    Statement statement = statementInNewTransaction(SecurityContext.AUTH_DISABLED);
    // when
    ConstraintDescriptor constraint = createConstraint(statement.schemaWriteOperations(), descriptor);
    // then
    assertEquals(constraint, single(statement.readOperations().constraintsGetAll()));
    // given
    commit();
    ReadOperations readOperations = readOperationsInNewTransaction();
    // when
    Iterator<?> constraints = readOperations.constraintsGetAll();
    // then
    assertEquals(constraint, single(constraints));
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Statement(org.neo4j.kernel.api.Statement) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) Test(org.junit.Test)

Example 23 with ConstraintDescriptor

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

the class StateHandlingStatementOperationsTest method shouldGetAllConstraints.

@Test
public void shouldGetAllConstraints() throws Exception {
    // given
    UniquenessConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForLabel(2, 3);
    UniquenessConstraintDescriptor constraint2 = ConstraintDescriptorFactory.uniqueForLabel(4, 5);
    TransactionState txState = new TxState();
    KernelStatement state = mockedState(txState);
    when(inner.constraintsGetForSchema(constraint1.schema())).thenAnswer(invocation -> Iterators.emptyIterator());
    when(inner.constraintsGetForSchema(constraint2.schema())).thenAnswer(invocation -> Iterators.emptyIterator());
    when(inner.constraintsGetAll()).thenAnswer(invocation -> iterator(constraint2));
    StateHandlingStatementOperations context = newTxStateOps(inner);
    context.uniquePropertyConstraintCreate(state, constraint1.schema());
    context.uniquePropertyConstraintCreate(state, constraint2.schema());
    // when
    Set<ConstraintDescriptor> result = Iterables.asSet(asIterable(context.constraintsGetAll(state)));
    // then
    assertEquals(asSet(constraint1, constraint2), result);
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) Test(org.junit.Test)

Example 24 with ConstraintDescriptor

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

the class SchemaRuleSerializationTest method assertParseNodePropertyExistsRule.

private void assertParseNodePropertyExistsRule(String serialized, String name) throws Exception {
    // GIVEN
    long ruleId = 87;
    int propertyKey = 51;
    int labelId = 45;
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForLabel(labelId, propertyKey);
    byte[] bytes = decodeBase64(serialized);
    //        System.out.println( encodeBase64( ConstraintRule.constraintRule( ruleId, constraint, "custom_name" ).serialize() ) );
    // WHEN
    ConstraintRule deserialized = assertConstraintRule(SchemaRuleSerialization.deserialize(ruleId, ByteBuffer.wrap(bytes)));
    // THEN
    assertThat(deserialized.getId(), equalTo(ruleId));
    assertThat(deserialized.getConstraintDescriptor(), equalTo(constraint));
    assertThat(deserialized.schema(), equalTo(constraint.schema()));
    assertException(deserialized::getOwnedIndex, IllegalStateException.class, "");
    assertThat(deserialized.getName(), is(name));
}
Also used : UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)

Example 25 with ConstraintDescriptor

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

the class SchemaRuleSerializationTest method assertParseRelationshipPropertyExistsRule.

private void assertParseRelationshipPropertyExistsRule(String serialized, String name) throws Exception {
    // GIVEN
    long ruleId = 51;
    int propertyKey = 6119;
    int relTypeId = 8512;
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForRelType(relTypeId, propertyKey);
    byte[] bytes = decodeBase64(serialized);
    //        System.out.println( encodeBase64( ConstraintRule.constraintRule( ruleId, constraint, "custom_name" ).serialize() ) );
    // WHEN
    ConstraintRule deserialized = assertConstraintRule(SchemaRuleSerialization.deserialize(ruleId, ByteBuffer.wrap(bytes)));
    // THEN
    assertThat(deserialized.getId(), equalTo(ruleId));
    assertThat(deserialized.getConstraintDescriptor(), equalTo(constraint));
    assertThat(deserialized.schema(), equalTo(constraint.schema()));
    assertException(deserialized::getOwnedIndex, IllegalStateException.class, "");
    assertThat(deserialized.getName(), is(name));
}
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