@Override public ConstraintRule createUniquenessConstraintRule( long ruleId, UniquenessConstraintDescriptor descriptor, long indexId ) { return ConstraintRule.constraintRule( ruleId, descriptor, indexId ); }
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 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() ) ); }
@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 ); } } } }
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 ); } } }
@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 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 ) ); }
@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 ); } } } } }
target.put( CONSTRAINT_RULE ); ConstraintDescriptor constraintDescriptor = constraintRule.getConstraintDescriptor(); switch ( constraintDescriptor.type() ) target.putLong( constraintRule.getOwnedIndex() ); break; target.putLong( constraintRule.getOwnedIndex() ); break; UTF8.putEncodedStringInto( constraintRule.getName(), target ); return target.array();
@Override protected ConstraintDescriptor readNonStandardConstraint( ConstraintRule rule, String errorMessage ) { if ( !rule.getConstraintDescriptor().enforcesPropertyExistence() ) { throw new IllegalStateException( "Unsupported constraint type: " + rule ); } return rule.getConstraintDescriptor(); } }
/** * 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; }
constraintRule.getId(), indexRule.getOwningConstraint().longValue() ); assertEquals( "constraint should reference index", indexRule.getId(), constraintRule.getOwnedIndex() );
private SchemaRule serialiseAndDeserialise( ConstraintRule constraintRule ) throws MalformedSchemaRuleException { ByteBuffer buffer = ByteBuffer.wrap( SchemaRuleSerialization.serialize( constraintRule ) ); return SchemaRuleSerialization.deserialize( constraintRule.getId(), buffer ); }
@Test public void rulesCreatedWithoutNameMustHaveComputedName() { assertThat( indexRegular.getName(), is( "index_1" ) ); assertThat( indexUnique.getName(), is( "index_2" ) ); assertThat( indexCompositeRegular.getName(), is( "index_1" ) ); assertThat( indexCompositeUnique.getName(), is( "index_2" ) ); assertThat( indexBigComposite.getName(), is( "index_1" ) ); assertThat( constraintExistsLabel.getName(), is( "constraint_1" ) ); assertThat( constraintUniqueLabel.getName(), is( "constraint_2" ) ); assertThat( constraintNodeKeyLabel.getName(), is( "constraint_2" ) ); assertThat( constraintExistsRelType.getName(), is( "constraint_2" ) ); assertThat( constraintCompositeLabel.getName(), is( "constraint_1" ) ); assertThat( constraintCompositeRelType.getName(), is( "constraint_2" ) ); }
@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 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 ) ); }
public void validateSchemaRule( SchemaRule schemaRule ) throws TransactionFailureException { if ( schemaRule instanceof ConstraintRule ) { ConstraintRule constraintRule = (ConstraintRule) schemaRule; if ( constraintRule.getConstraintDescriptor().enforcesUniqueness() ) { try { indexes.validateIndex( constraintRule.getOwnedIndex() ); } catch ( UniquePropertyValueValidationException e ) { throw new TransactionFailureException( Status.Transaction.TransactionValidationFailed, e, "Index validation failed" ); } catch ( IndexNotFoundKernelException | IndexPopulationFailedKernelException e ) { // We don't expect this to occur, and if they do, it is because we are in a very bad state - out of // disk or index corruption, or similar. This will kill the database such that it can be shut down // and have recovery performed. It's the safest bet to avoid loosing data. throw new TransactionFailureException( Status.Transaction.TransactionValidationFailed, e, "Index population failure" ); } } } } }
target.put( CONSTRAINT_RULE ); ConstraintDescriptor constraintDescriptor = constraintRule.getConstraintDescriptor(); switch ( constraintDescriptor.type() ) target.putLong( constraintRule.getOwnedIndex() ); break; target.putLong( constraintRule.getOwnedIndex() ); break; UTF8.putEncodedStringInto( constraintRule.getName(), target ); return target.array();
@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; } }
@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 ); }