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() ) ); }
private SchemaRule serialiseAndDeserialise( ConstraintRule constraintRule ) throws MalformedSchemaRuleException { ByteBuffer buffer = ByteBuffer.wrap( SchemaRuleSerialization.serialize( constraintRule ) ); return SchemaRuleSerialization.deserialize( constraintRule.getId(), buffer ); }
void addSchemaRule( SchemaRule rule ) { if ( rule instanceof ConstraintRule ) { ConstraintRule constraintRule = (ConstraintRule) rule; constraintRuleById.put( constraintRule.getId(), constraintRule ); constraints.add( constraintSemantics.readConstraint( constraintRule ) ); } else if ( rule instanceof StoreIndexDescriptor ) { CapableIndexDescriptor index = indexProviderMap.withCapabilities( (StoreIndexDescriptor) rule ); indexDescriptorById.put( index.getId(), index ); SchemaDescriptor schemaDescriptor = index.schema(); indexDescriptors.put( schemaDescriptor, index ); indexDescriptorsByName.put( rule.getName(), index ); for ( int entityTokenId : schemaDescriptor.getEntityTokenIds() ) { Set<CapableIndexDescriptor> forLabel = indexDescriptorsByLabel.getIfAbsentPut( entityTokenId, HashSet::new ); forLabel.add( index ); } for ( int propertyId : index.schema().getPropertyIds() ) { List<CapableIndexDescriptor> indexesForProperty = indexByProperty.getIfAbsentPut( propertyId, ArrayList::new ); indexesForProperty.add( index ); } } }
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 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 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 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 ); }
constraintRule.getId(), indexRule.getOwningConstraint().longValue() ); assertEquals( "constraint should reference index", indexRule.getId(), constraintRule.getOwnedIndex() );
void addSchemaRule( SchemaRule rule ) { if ( rule instanceof ConstraintRule ) { ConstraintRule constraintRule = (ConstraintRule) rule; constraintRuleById.put( constraintRule.getId(), constraintRule ); constraints.add( constraintSemantics.readConstraint( constraintRule ) ); } else if ( rule instanceof StoreIndexDescriptor ) { CapableIndexDescriptor index = indexProviderMap.withCapabilities( (StoreIndexDescriptor) rule ); indexDescriptorById.put( index.getId(), index ); SchemaDescriptor schemaDescriptor = index.schema(); indexDescriptors.put( schemaDescriptor, index ); indexDescriptorsByName.put( rule.getName(), index ); for ( int entityTokenId : schemaDescriptor.getEntityTokenIds() ) { Set<CapableIndexDescriptor> forLabel = indexDescriptorsByLabel.getIfAbsentPut( entityTokenId, HashSet::new ); forLabel.add( index ); } for ( int propertyId : index.schema().getPropertyIds() ) { List<CapableIndexDescriptor> indexesForProperty = indexByProperty.getIfAbsentPut( propertyId, ArrayList::new ); indexesForProperty.add( index ); } } }