Search in sources :

Example 1 with SchemaRuleKey

use of org.neo4j.consistency.checking.SchemaRuleKey in project neo4j by neo4j.

the class SchemaChecker method checkSchema.

private void checkSchema(MutableIntObjectMap<MutableIntSet> mandatoryNodeProperties, MutableIntObjectMap<MutableIntSet> mandatoryRelationshipProperties, CursorContext cursorContext) {
    long highId = schemaStore.getHighId();
    try (RecordReader<SchemaRecord> schemaReader = new RecordReader<>(schemaStore, true, cursorContext)) {
        Map<Long, SchemaRecord> indexObligations = new HashMap<>();
        Map<Long, SchemaRecord> constraintObligations = new HashMap<>();
        Map<SchemaRuleKey, SchemaRecord> verifiedRulesWithRecords = new HashMap<>();
        // KernelVersion only controls if there will be an injected NLI on non-upgraded stores. That index is not in the store and will
        // not be found when going through the file in performSchemaCheck anyway so we can use latest KernelVersion here.
        // If an injected NLI exist but is not online that will not be reported, but this is an unlikely corner case that we
        // ignore. The index itself will be checked as long as it is online (it is found by IndexAccessors).
        SchemaStorage schemaStorage = new SchemaStorage(schemaStore, tokenHolders, () -> KernelVersion.LATEST);
        // Build map of obligations and such
        buildObligationsMap(highId, schemaReader, schemaStorage, indexObligations, constraintObligations, verifiedRulesWithRecords, cursorContext);
        // Verify all things, now that we have the complete map of obligations back and forth
        performSchemaCheck(highId, schemaReader, indexObligations, constraintObligations, schemaStorage, mandatoryNodeProperties, mandatoryRelationshipProperties, cursorContext);
    }
}
Also used : SchemaStorage(org.neo4j.internal.recordstorage.SchemaStorage) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) HashMap(java.util.HashMap) OptionalLong(java.util.OptionalLong) SchemaRuleKey(org.neo4j.consistency.checking.SchemaRuleKey)

Example 2 with SchemaRuleKey

use of org.neo4j.consistency.checking.SchemaRuleKey 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)

Aggregations

SchemaRuleKey (org.neo4j.consistency.checking.SchemaRuleKey)2 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)2 HashMap (java.util.HashMap)1 OptionalLong (java.util.OptionalLong)1 MalformedSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)1 SchemaStorage (org.neo4j.internal.recordstorage.SchemaStorage)1 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)1 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)1 SchemaRule (org.neo4j.internal.schema.SchemaRule)1