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));
}
}
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);
}
}
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();
}
}
}
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;
}
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;
}
};
}
Aggregations