Search in sources :

Example 26 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class SchemaChecker method buildObligationsMap.

private void buildObligationsMap(long highId, RecordReader<SchemaRecord> reader, SchemaStorage schemaStorage, Map<Long, SchemaRecord> indexObligations, Map<Long, SchemaRecord> constraintObligations, Map<SchemaRuleKey, SchemaRecord> verifiedRulesWithRecords, CursorContext cursorContext) {
    for (long id = schemaStore.getNumberOfReservedLowIds(); id < highId && !context.isCancelled(); id++) {
        try {
            SchemaRecord record = reader.read(id);
            if (!record.inUse()) {
                continue;
            }
            SchemaRule schemaRule = schemaStorage.loadSingleSchemaRule(id, cursorContext);
            SchemaRecord previousContentRecord = verifiedRulesWithRecords.put(new SchemaRuleKey(schemaRule), record.copy());
            if (previousContentRecord != null) {
                reporter.forSchema(record).duplicateRuleContent(previousContentRecord);
            }
            if (schemaRule instanceof IndexDescriptor) {
                IndexDescriptor rule = (IndexDescriptor) schemaRule;
                if (rule.isUnique() && rule.getOwningConstraintId().isPresent()) {
                    SchemaRecord previousObligation = constraintObligations.put(rule.getOwningConstraintId().getAsLong(), record.copy());
                    if (previousObligation != null) {
                        reporter.forSchema(record).duplicateObligation(previousObligation);
                    }
                }
            } else if (schemaRule instanceof ConstraintDescriptor) {
                ConstraintDescriptor rule = (ConstraintDescriptor) schemaRule;
                if (rule.enforcesUniqueness()) {
                    SchemaRecord previousObligation = indexObligations.put(rule.asIndexBackedConstraint().ownedIndexId(), record.copy());
                    if (previousObligation != null) {
                        reporter.forSchema(record).duplicateObligation(previousObligation);
                    }
                }
            }
        } catch (MalformedSchemaRuleException e) {
        // This is OK, we'll report it below
        }
    }
}
Also used : MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaRule(org.neo4j.internal.schema.SchemaRule) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaRuleKey(org.neo4j.consistency.checking.SchemaRuleKey)

Example 27 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class WriteTransactionCommandOrderingTest method injectAllPossibleCommands.

private static TransactionRecordState injectAllPossibleCommands() {
    RecordChangeSet recordChangeSet = mock(RecordChangeSet.class);
    RecordChanges<LabelTokenRecord, Void> labelTokenChanges = mock(RecordChanges.class);
    RecordChanges<RelationshipTypeTokenRecord, Void> relationshipTypeTokenChanges = mock(RecordChanges.class);
    RecordChanges<PropertyKeyTokenRecord, Void> propertyKeyTokenChanges = mock(RecordChanges.class);
    RecordChanges<NodeRecord, Void> nodeRecordChanges = mock(RecordChanges.class);
    RecordChanges<RelationshipRecord, Void> relationshipRecordChanges = mock(RecordChanges.class);
    RecordChanges<PropertyRecord, PrimitiveRecord> propertyRecordChanges = mock(RecordChanges.class);
    RecordChanges<RelationshipGroupRecord, Integer> relationshipGroupChanges = mock(RecordChanges.class);
    RecordChanges<SchemaRecord, SchemaRule> schemaRuleChanges = mock(RecordChanges.class);
    when(recordChangeSet.getLabelTokenChanges()).thenReturn(labelTokenChanges);
    when(recordChangeSet.getRelationshipTypeTokenChanges()).thenReturn(relationshipTypeTokenChanges);
    when(recordChangeSet.getPropertyKeyTokenChanges()).thenReturn(propertyKeyTokenChanges);
    when(recordChangeSet.getNodeRecords()).thenReturn(nodeRecordChanges);
    when(recordChangeSet.getRelRecords()).thenReturn(relationshipRecordChanges);
    when(recordChangeSet.getPropertyRecords()).thenReturn(propertyRecordChanges);
    when(recordChangeSet.getRelGroupRecords()).thenReturn(relationshipGroupChanges);
    when(recordChangeSet.getSchemaRuleChanges()).thenReturn(schemaRuleChanges);
    List<RecordProxy<NodeRecord, Void>> nodeChanges = new LinkedList<>();
    RecordChange<NodeRecord, Void> deletedNode = mock(RecordChange.class);
    when(deletedNode.getBefore()).thenReturn(inUseNode());
    when(deletedNode.forReadingLinkage()).thenReturn(missingNode());
    nodeChanges.add(deletedNode);
    RecordChange<NodeRecord, Void> createdNode = mock(RecordChange.class);
    when(createdNode.getBefore()).thenReturn(missingNode());
    when(createdNode.forReadingLinkage()).thenReturn(createdNode());
    nodeChanges.add(createdNode);
    RecordChange<NodeRecord, Void> updatedNode = mock(RecordChange.class);
    when(updatedNode.getBefore()).thenReturn(inUseNode());
    when(updatedNode.forReadingLinkage()).thenReturn(inUseNode());
    nodeChanges.add(updatedNode);
    when(nodeRecordChanges.changes()).thenReturn(nodeChanges);
    when(nodeRecordChanges.changeSize()).thenReturn(3);
    when(recordChangeSet.changeSize()).thenReturn(3);
    when(labelTokenChanges.changes()).thenReturn(Collections.emptyList());
    when(relationshipTypeTokenChanges.changes()).thenReturn(Collections.emptyList());
    when(propertyKeyTokenChanges.changes()).thenReturn(Collections.emptyList());
    when(relationshipRecordChanges.changes()).thenReturn(Collections.emptyList());
    when(propertyRecordChanges.changes()).thenReturn(Collections.emptyList());
    when(relationshipGroupChanges.changes()).thenReturn(Collections.emptyList());
    when(schemaRuleChanges.changes()).thenReturn(Collections.emptyList());
    NeoStores neoStores = mock(NeoStores.class);
    NodeStore store = mock(NodeStore.class);
    when(neoStores.getNodeStore()).thenReturn(store);
    RelationshipGroupStore relationshipGroupStore = mock(RelationshipGroupStore.class);
    when(neoStores.getRelationshipGroupStore()).thenReturn(relationshipGroupStore);
    RelationshipStore relationshipStore = mock(RelationshipStore.class);
    when(neoStores.getRelationshipStore()).thenReturn(relationshipStore);
    return new TransactionRecordState(neoStores, mock(IntegrityValidator.class), recordChangeSet, 0, null, LockTracer.NONE, null, null, null, NULL, INSTANCE, LATEST_LOG_SERIALIZATION);
}
Also used : RelationshipTypeTokenRecord(org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) SchemaRule(org.neo4j.internal.schema.SchemaRule) PropertyKeyTokenRecord(org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) NodeStore(org.neo4j.kernel.impl.store.NodeStore) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) LabelTokenRecord(org.neo4j.kernel.impl.store.record.LabelTokenRecord) PrimitiveRecord(org.neo4j.kernel.impl.store.record.PrimitiveRecord) RecordProxy(org.neo4j.internal.recordstorage.RecordAccess.RecordProxy) RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord) RelationshipGroupStore(org.neo4j.kernel.impl.store.RelationshipGroupStore) LinkedList(java.util.LinkedList) NeoStores(org.neo4j.kernel.impl.store.NeoStores) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore)

Example 28 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class SchemaStoreMapificationTest method mapificationMustPreserveSchemaRulesAccurately.

@RepeatedTest(500)
void mapificationMustPreserveSchemaRulesAccurately() throws MalformedSchemaRuleException {
    SchemaRule rule = RANDOM_SCHEMA.get();
    Map<String, Value> mapified = SchemaStore.mapifySchemaRule(rule);
    SchemaRule unmapified = SchemaStore.unmapifySchemaRule(rule.getId(), mapified);
    if (!rule.equals(unmapified) || !unmapified.equals(rule)) {
        fail("Mapification of schema rule was not fully reversible.\n" + "Expected: " + rule + "\n" + "But got:  " + unmapified + "\n" + "Mapified rule: " + mapified);
    }
}
Also used : Value(org.neo4j.values.storable.Value) SchemaRule(org.neo4j.internal.schema.SchemaRule) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 29 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class SchemaRuleCommandTest method writeAndReadOfArbitrarySchemaRules.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@RepeatedTest(1000)
void writeAndReadOfArbitrarySchemaRules() throws Exception {
    RandomSchema randomSchema = new RandomSchema();
    SchemaRule rule = randomSchema.schemaRules().filter(indexBackedConstraintsWithoutIndexes()).findFirst().get();
    long ruleId = rule.getId();
    SchemaRecord before = new SchemaRecord(ruleId).initialize(false, NO_NEXT_PROPERTY.longValue());
    SchemaRecord after = new SchemaRecord(ruleId).initialize(true, 42);
    after.setCreated();
    SchemaRuleCommand command = new SchemaRuleCommand(serialization, before, after, rule);
    InMemoryClosableChannel buffer = new InMemoryClosableChannel((int) ByteUnit.kibiBytes(5));
    when(neoStores.getSchemaStore()).thenReturn(schemaStore);
    // WHEN
    command.serialize(buffer);
    SchemaRuleCommand readCommand = (SchemaRuleCommand) serialization.read(buffer);
    // THEN
    assertEquals(ruleId, readCommand.getKey());
    assertThat(readCommand.getSchemaRule()).isEqualTo(rule);
}
Also used : SchemaRuleCommand(org.neo4j.internal.recordstorage.Command.SchemaRuleCommand) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) InMemoryClosableChannel(org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel) SchemaRule(org.neo4j.internal.schema.SchemaRule) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Aggregations

SchemaRule (org.neo4j.internal.schema.SchemaRule)29 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)12 Test (org.junit.jupiter.api.Test)8 ArrayList (java.util.ArrayList)7 MalformedSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)5 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)5 TokenHolders (org.neo4j.token.TokenHolders)5 NamedToken (org.neo4j.token.api.NamedToken)5 RepeatedTest (org.junit.jupiter.api.RepeatedTest)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)4 NeoStores (org.neo4j.kernel.impl.store.NeoStores)4 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)3 PropertyRecord (org.neo4j.kernel.impl.store.record.PropertyRecord)3 OptionalLong (java.util.OptionalLong)2 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)2 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 DefaultIdGeneratorFactory (org.neo4j.internal.id.DefaultIdGeneratorFactory)2 SchemaStore (org.neo4j.kernel.impl.store.SchemaStore)2