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();
}
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));
}
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);
}
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));
}
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));
}
Aggregations