@Override protected ConstraintDescriptor readNonStandardConstraint( ConstraintRule rule, String errorMessage ) { if ( !rule.getConstraintDescriptor().enforcesPropertyExistence() ) { throw new IllegalStateException( "Unsupported constraint type: " + rule ); } return rule.getConstraintDescriptor(); } }
@Override public ConstraintDescriptor readConstraint( ConstraintRule rule ) { ConstraintDescriptor desc = rule.getConstraintDescriptor(); switch ( desc.type() ) { case EXISTS: return readNonStandardConstraint( rule, ERROR_MESSAGE_EXISTS ); case UNIQUE_EXISTS: return readNonStandardConstraint( rule, ERROR_MESSAGE_NODE_KEY ); default: return desc; } }
@Override public void checkConstraintRule( ConstraintRule rule, DynamicRecord record, RecordAccess records, CheckerEngine<DynamicRecord,ConsistencyReport.SchemaConsistencyReport> engine ) { checkSchema( rule, record, records, engine ); if ( rule.getConstraintDescriptor().enforcesUniqueness() ) { DynamicRecord previousObligation = indexObligations.put( rule.getOwnedIndex(), record.clone() ); if ( previousObligation != null ) { engine.report().duplicateObligation( previousObligation ); } } } }
private void assertSerializeAndDeserializeConstraintRule( ConstraintRule constraintRule ) throws MalformedSchemaRuleException { ConstraintRule deserialized = assertConstraintRule( serialiseAndDeserialise( constraintRule ) ); assertThat( deserialized.getId(), equalTo( constraintRule.getId() ) ); assertThat( deserialized.getConstraintDescriptor(), equalTo( constraintRule.getConstraintDescriptor() ) ); assertThat( deserialized.schema(), equalTo( constraintRule.schema() ) ); }
public MandatoryProperties( StoreAccess storeAccess ) { this.storeAccess = storeAccess; SchemaStorage schemaStorage = new SchemaStorage( storeAccess.getSchemaStore() ); for ( ConstraintRule rule : constraintsIgnoringMalformed( schemaStorage ) ) { if ( rule.getConstraintDescriptor().enforcesPropertyExistence() ) { rule.schema().processWith( constraintRecorder ); } } }
/** * Compute the byte size needed to serialize the provided ConstraintRule using serialize. * @param constraintRule the ConstraintRule * @return the byte size of ConstraintRule */ static int lengthOf( ConstraintRule constraintRule ) { int length = 4; // legacy label or relType id length += 1; // schema rule type length += 1; // constraint type ConstraintDescriptor constraintDescriptor = constraintRule.getConstraintDescriptor(); if ( constraintDescriptor.enforcesUniqueness() ) { length += 8; // owned index id } length += constraintDescriptor.schema().computeWith( schemaSizeComputer ); length += UTF8.computeRequiredByteBufferSize( constraintRule.getName() ); return length; }
private void assertParseNodeKeyConstraintRule( String serialized, String name ) throws MalformedSchemaRuleException { // GIVEN long ruleId = 1; int propertyKey = 3; int labelId = 55; long ownedIndexId = 2; NodeKeyConstraintDescriptor constraint = ConstraintDescriptorFactory.nodeKeyForLabel( labelId, propertyKey ); byte[] bytes = decodeBase64( serialized ); // 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() ) ); assertThat( deserialized.getOwnedIndex(), equalTo( ownedIndexId ) ); assertThat( deserialized.getName(), is( name ) ); }
private void assertParseUniqueConstraintRule( String serialized, String name ) throws MalformedSchemaRuleException { // GIVEN long ruleId = 1; int propertyKey = 3; int labelId = 55; long ownedIndexId = 2; UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForLabel( labelId, propertyKey ); byte[] bytes = decodeBase64( serialized ); // 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() ) ); assertThat( deserialized.getOwnedIndex(), equalTo( ownedIndexId ) ); assertThat( deserialized.getName(), is( name ) ); }
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 ); // 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 ) ); }
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 ); // 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 ) ); }
@Override public void checkConstraintRule( ConstraintRule rule, DynamicRecord record, RecordAccess records, CheckerEngine<DynamicRecord,ConsistencyReport.SchemaConsistencyReport> engine ) { if ( rule.getConstraintDescriptor().enforcesUniqueness() ) { DynamicRecord obligation = constraintObligations.get( rule.getId() ); if ( obligation == null ) { engine.report().missingObligation( SchemaRule.Kind.CONSTRAINT_INDEX_RULE ); } else { if ( obligation.getId() != rule.getOwnedIndex() ) { engine.report().uniquenessConstraintNotReferencingBack( obligation ); } } } } }
@Test public void shouldCreateUniquenessConstraintWithOwnedIndex() { // GIVEN UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ); ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor, RULE_ID_2 ); // THEN assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); assertThat( constraintRule.getOwnedIndex(), equalTo( RULE_ID_2 ) ); }
@Test public void shouldCreateNodeKeyConstraintWithOwnedIndex() { // GIVEN NodeKeyConstraintDescriptor descriptor = nodeKeyForLabel( LABEL_ID, PROPERTY_ID_1 ); ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor, RULE_ID_2 ); // THEN assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); assertThat( constraintRule.getOwnedIndex(), equalTo( RULE_ID_2 ) ); }
@Test public void shouldCreateNodeKeyConstraint() { // GIVEN ConstraintDescriptor descriptor = nodeKeyForLabel( LABEL_ID, PROPERTY_ID_1 ); ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor ); // THEN assertThat( constraintRule.getId(), equalTo( RULE_ID ) ); assertThat( constraintRule.schema(), equalTo( descriptor.schema() ) ); assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); assertException( constraintRule::getOwnedIndex, IllegalStateException.class ); }
@Test public void shouldCreateUniquenessConstraint() { // GIVEN ConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForLabel( LABEL_ID, PROPERTY_ID_1 ); ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor ); // THEN assertThat( constraintRule.getId(), equalTo( RULE_ID ) ); assertThat( constraintRule.schema(), equalTo( descriptor.schema() ) ); assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); assertException( constraintRule::getOwnedIndex, IllegalStateException.class ); }
@Test public void shouldCreateExistenceConstraint() { // GIVEN ConstraintDescriptor descriptor = existsForLabel( LABEL_ID, PROPERTY_ID_1 ); ConstraintRule constraintRule = ConstraintRule.constraintRule( RULE_ID, descriptor ); // THEN assertThat( constraintRule.getId(), equalTo( RULE_ID ) ); assertThat( constraintRule.schema(), equalTo( descriptor.schema() ) ); assertThat( constraintRule.getConstraintDescriptor(), equalTo( descriptor ) ); assertException( constraintRule::getOwnedIndex, IllegalStateException.class ); }
@Test public void shouldListConstraintsForRelationshipType() { // Given ConstraintRule rule1 = relPropertyExistenceConstraintRule( 0, 1, 1 ); ConstraintRule rule2 = relPropertyExistenceConstraintRule( 0, 2, 1 ); ConstraintRule rule3 = relPropertyExistenceConstraintRule( 0, 1, 2 ); SchemaCache cache = newSchemaCache(); cache.addSchemaRule( rule1 ); cache.addSchemaRule( rule2 ); cache.addSchemaRule( rule3 ); // When Set<ConstraintDescriptor> listed = asSet( cache.constraintsForRelationshipType( 1 ) ); // Then Set<ConstraintDescriptor> expected = asSet( rule1.getConstraintDescriptor(), rule3.getConstraintDescriptor() ); assertEquals( expected, listed ); }
@Test public void shouldListConstraintsForLabel() { // Given ConstraintRule rule1 = uniquenessConstraintRule( 0, 1, 1, 0 ); ConstraintRule rule2 = uniquenessConstraintRule( 1, 2, 1, 0 ); ConstraintRule rule3 = nodePropertyExistenceConstraintRule( 2, 1, 2 ); SchemaCache cache = newSchemaCache(); cache.addSchemaRule( rule1 ); cache.addSchemaRule( rule2 ); cache.addSchemaRule( rule3 ); // When Set<ConstraintDescriptor> listed = asSet( cache.constraintsForLabel( 1 ) ); // Then Set<ConstraintDescriptor> expected = asSet( rule1.getConstraintDescriptor(), rule3.getConstraintDescriptor() ); assertEquals( expected, listed ); }
private void assertRule( ConstraintRule rule, String label, String propertyKey, ConstraintDescriptor.Type type ) { assertTrue( SchemaDescriptorPredicates.hasLabel( rule, labelId( label ) ) ); assertTrue( SchemaDescriptorPredicates.hasProperty( rule, propId( propertyKey ) ) ); assertEquals( type, rule.getConstraintDescriptor().type() ); }
@Test public void shouldListConstraintsForSchema() { // Given ConstraintRule rule1 = uniquenessConstraintRule( 0, 1, 1, 0 ); ConstraintRule rule2 = uniquenessConstraintRule( 1, 2, 1, 0 ); ConstraintRule rule3 = nodePropertyExistenceConstraintRule( 2, 1, 2 ); SchemaCache cache = newSchemaCache(); cache.addSchemaRule( rule1 ); cache.addSchemaRule( rule2 ); cache.addSchemaRule( rule3 ); // When Set<ConstraintDescriptor> listed = asSet( cache.constraintsForSchema( rule3.schema() ) ); // Then assertEquals( singleton( rule3.getConstraintDescriptor() ), listed ); }