Search in sources :

Example 1 with MalformedSchemaRuleException

use of org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException in project neo4j by neo4j.

the class SchemaRuleSerialization35 method readConstraintRule.

// READ CONSTRAINT
private static ConstraintDescriptor readConstraintRule(long id, ByteBuffer source) throws MalformedSchemaRuleException {
    SchemaDescriptor schema;
    byte constraintRuleType = source.get();
    String name;
    switch(constraintRuleType) {
        case EXISTS_CONSTRAINT:
            schema = readSchema(source);
            name = readRuleName(source).orElse(null);
            return ConstraintDescriptorFactory.existsForSchema(schema).withId(id).withName(name);
        case UNIQUE_CONSTRAINT:
            long ownedUniqueIndex = source.getLong();
            schema = readSchema(source);
            UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema(schema);
            name = readRuleName(source).orElse(null);
            return descriptor.withId(id).withOwnedIndexId(ownedUniqueIndex).withName(name);
        case UNIQUE_EXISTS_CONSTRAINT:
            long ownedNodeKeyIndex = source.getLong();
            schema = readSchema(source);
            NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
            name = readRuleName(source).orElse(null);
            return nodeKeyConstraintDescriptor.withId(id).withOwnedIndexId(ownedNodeKeyIndex).withName(name);
        default:
            throw new MalformedSchemaRuleException(format("Got unknown constraint rule type '%d'.", constraintRuleType));
    }
}
Also used : LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)

Example 2 with MalformedSchemaRuleException

use of org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException in project neo4j by neo4j.

the class SchemaStore method buildConstraintRule.

private static SchemaRule buildConstraintRule(long id, Map<String, Value> props) throws MalformedSchemaRuleException {
    SchemaDescriptor schema = buildSchemaDescriptor(props);
    String constraintRuleType = getString(PROP_CONSTRAINT_RULE_TYPE, props);
    String name = getString(PROP_SCHEMA_RULE_NAME, props);
    OptionalLong ownedIndex = getOptionalLong(PROP_OWNED_INDEX, props);
    ConstraintDescriptor constraint;
    switch(constraintRuleType) {
        case "UNIQUE":
            constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
            if (ownedIndex.isPresent()) {
                constraint = constraint.withOwnedIndexId(ownedIndex.getAsLong());
            }
            return constraint.withId(id).withName(name);
        case "EXISTS":
            constraint = ConstraintDescriptorFactory.existsForSchema(schema);
            return constraint.withId(id).withName(name);
        case "UNIQUE_EXISTS":
            constraint = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
            if (ownedIndex.isPresent()) {
                constraint = constraint.withOwnedIndexId(ownedIndex.getAsLong());
            }
            return constraint.withId(id).withName(name);
        default:
            throw new MalformedSchemaRuleException("Did not recognize constraint rule type: " + constraintRuleType);
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) OptionalLong(java.util.OptionalLong)

Example 3 with MalformedSchemaRuleException

use of org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException in project neo4j by neo4j.

the class SchemaChecker method performSchemaCheck.

private void performSchemaCheck(long highId, RecordReader<SchemaRecord> reader, Map<Long, SchemaRecord> indexObligations, Map<Long, SchemaRecord> constraintObligations, SchemaStorage schemaStorage, MutableIntObjectMap<MutableIntSet> mandatoryNodeProperties, MutableIntObjectMap<MutableIntSet> mandatoryRelationshipProperties, CursorContext cursorContext) {
    SchemaRecord record = reader.record();
    SchemaProcessor basicSchemaCheck = new BasicSchemaCheck(record, cursorContext);
    SchemaProcessor mandatoryPropertiesBuilder = new MandatoryPropertiesBuilder(mandatoryNodeProperties, mandatoryRelationshipProperties);
    for (long id = schemaStore.getNumberOfReservedLowIds(); id < highId && !context.isCancelled(); id++) {
        try {
            reader.read(id);
            if (record.inUse()) {
                SchemaRule schemaRule = schemaStorage.loadSingleSchemaRule(id, cursorContext);
                schemaRule.schema().processWith(basicSchemaCheck);
                if (schemaRule instanceof IndexDescriptor) {
                    IndexDescriptor rule = (IndexDescriptor) schemaRule;
                    if (rule.isUnique()) {
                        SchemaRecord obligation = indexObligations.get(rule.getId());
                        if (// no pointer to here
                        obligation == null) {
                            if (// we only expect a pointer if we have an owner
                            rule.getOwningConstraintId().isPresent()) {
                                reporter.forSchema(record).missingObligation(SchemaRuleKind.UNIQUENESS_CONSTRAINT.name());
                            }
                        } else {
                            // if someone points to here, it must be our owner
                            OptionalLong owningConstraintId = rule.getOwningConstraintId();
                            if (owningConstraintId.isEmpty() || obligation.getId() != owningConstraintId.getAsLong()) {
                                reporter.forSchema(record).constraintIndexRuleNotReferencingBack(obligation);
                            }
                        }
                    }
                    if (indexAccessors.notOnlineRules().contains(rule)) {
                        reporter.forSchema(record).schemaRuleNotOnline(rule);
                    }
                    if (indexAccessors.inconsistentRules().contains(rule)) {
                        reporter.forSchema(record).malformedSchemaRule();
                    }
                } else if (schemaRule instanceof ConstraintDescriptor) {
                    ConstraintDescriptor rule = (ConstraintDescriptor) schemaRule;
                    if (rule.enforcesUniqueness()) {
                        SchemaRecord obligation = constraintObligations.get(rule.getId());
                        if (obligation == null) {
                            reporter.forSchema(record).missingObligation(SchemaRuleKind.CONSTRAINT_INDEX_RULE.name());
                        } else {
                            if (obligation.getId() != rule.asIndexBackedConstraint().ownedIndexId()) {
                                reporter.forSchema(record).uniquenessConstraintNotReferencingBack(obligation);
                            }
                        }
                    }
                    if (rule.enforcesPropertyExistence()) {
                        rule.schema().processWith(mandatoryPropertiesBuilder);
                    }
                } else {
                    reporter.forSchema(record).unsupportedSchemaRuleType(null);
                }
            }
        } catch (MalformedSchemaRuleException e) {
            reporter.forSchema(record).malformedSchemaRule();
        }
    }
}
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) OptionalLong(java.util.OptionalLong) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaProcessor(org.neo4j.internal.schema.SchemaProcessor)

Example 4 with MalformedSchemaRuleException

use of org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException in project neo4j by neo4j.

the class LogCommandSerializationV3_0_10 method readSchemaRule.

private static SchemaRule readSchemaRule(Collection<DynamicRecord> recordsBefore) {
    // TODO: Why was this assertion here?
    // assert first(recordsBefore).inUse() : "Asked to deserialize schema records that were not in
    // use.";
    SchemaRule rule;
    ByteBuffer deserialized = AbstractDynamicStore.concatData(recordsBefore, new byte[100]);
    try {
        rule = SchemaRuleSerialization35.deserialize(Iterables.first(recordsBefore).getId(), deserialized);
    } catch (MalformedSchemaRuleException e) {
        return null;
    }
    return rule;
}
Also used : MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRule(org.neo4j.internal.schema.SchemaRule) ByteBuffer(java.nio.ByteBuffer)

Example 5 with MalformedSchemaRuleException

use of org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException in project neo4j by neo4j.

the class SchemaStorage35 method loadAllSchemaRules.

/**
 * Scans the schema store and loads all {@link SchemaRule rules} in it. This method is written with the assumption
 * that there's no id reuse on schema records.
 *
 * @param returnType type of {@link SchemaRule} to load.
 * @return {@link Iterator} of the loaded schema rules, lazily loaded when advancing the iterator.
 */
private <ReturnType extends SchemaRule> Iterator<ReturnType> loadAllSchemaRules(final Class<ReturnType> returnType, CursorContext cursorContext, ThrowingConsumer<Exception, RuntimeException> malformedExceptionHandler) {
    return new PrefetchingIterator<>() {

        private final long highestId = schemaStore.getHighestPossibleIdInUse(cursorContext);

        private long currentId = 1;

        /*record 0 contains the block size*/
        private final byte[] scratchData = newRecordBuffer();

        private final DynamicRecord record = schemaStore.newRecord();

        @Override
        protected ReturnType fetchNextOrNull() {
            while (currentId <= highestId) {
                try {
                    long id = currentId++;
                    schemaStore.getRecord(id, record, RecordLoad.LENIENT_CHECK, cursorContext);
                    if (!record.inUse()) {
                        continue;
                    }
                    schemaStore.getRecord(id, record, RecordLoad.NORMAL, cursorContext);
                    if (record.isStartRecord()) {
                        // that we're reading and that rule may have spanned multiple dynamic records.
                        try {
                            Collection<DynamicRecord> records;
                            try {
                                records = schemaStore.getRecords(id, RecordLoad.NORMAL, false, cursorContext);
                            } catch (InvalidRecordException e) {
                                // This may have been due to a concurrent drop of this rule.
                                continue;
                            }
                            SchemaRule schemaRule = SchemaStore35.readSchemaRule(id, records, scratchData);
                            if (returnType.isInstance(schemaRule)) {
                                return returnType.cast(schemaRule);
                            }
                        } catch (MalformedSchemaRuleException e) {
                            throw new RuntimeException(e);
                        }
                    }
                } catch (Exception e) {
                    malformedExceptionHandler.accept(e);
                }
            }
            return null;
        }
    };
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) PrefetchingIterator(org.neo4j.internal.helpers.collection.PrefetchingIterator) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRule(org.neo4j.internal.schema.SchemaRule) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)

Aggregations

MalformedSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)10 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)4 SchemaRule (org.neo4j.internal.schema.SchemaRule)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)3 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)3 OptionalLong (java.util.OptionalLong)2 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)2 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)2 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 IntObjectHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap)1 EntityType (org.neo4j.common.EntityType)1 SchemaRuleKey (org.neo4j.consistency.checking.SchemaRuleKey)1 PrefetchingIterator (org.neo4j.internal.helpers.collection.PrefetchingIterator)1 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)1 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)1 SchemaProcessor (org.neo4j.internal.schema.SchemaProcessor)1 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)1 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)1