use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.
the class SchemaStorageReadAndWriteTest method shouldPerfectlyPreserveSchemaRules.
@RepeatedTest(2000)
void shouldPerfectlyPreserveSchemaRules() throws Exception {
SchemaRule schemaRule = randomSchema.nextSchemaRule();
storage.writeSchemaRule(schemaRule, NULL, INSTANCE);
SchemaRule returnedRule = storage.loadSingleSchemaRule(schemaRule.getId(), NULL);
assertTrue(RandomSchema.schemaDeepEquals(returnedRule, schemaRule), () -> "\n" + returnedRule + "\nwas not equal to\n" + schemaRule);
}
use of org.neo4j.internal.schema.SchemaRule 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.schema.SchemaRule in project neo4j by neo4j.
the class SchemaIndexMigratorTest method shouldDeleteRelationshipIndexesAfterCrossFormatFamilyMigration.
@Test
void shouldDeleteRelationshipIndexesAfterCrossFormatFamilyMigration() throws IOException {
// given
IndexProviderDescriptor provider = new IndexProviderDescriptor("k", "v");
IndexDirectoryStructure directoryStructure = directoriesByProvider(databaseLayout.databaseDirectory()).forProvider(provider);
StorageEngineFactory storageEngineFactory = mock(StorageEngineFactory.class);
StoreVersion fromVersion = mock(StoreVersion.class);
StoreVersion toVersion = mock(StoreVersion.class);
when(fromVersion.hasCompatibleCapabilities(toVersion, CapabilityType.FORMAT)).thenReturn(false);
when(storageEngineFactory.versionInformation("from")).thenReturn(fromVersion);
when(storageEngineFactory.versionInformation("to")).thenReturn(toVersion);
List<SchemaRule> schemaRules = new ArrayList<>();
schemaRules.add(forSchema(SchemaDescriptor.forLabel(1, 2, 3)).withName("n1").materialise(1L));
schemaRules.add(forSchema(SchemaDescriptor.forRelType(5, 3)).withName("r1").materialise(2L));
schemaRules.add(forSchema(SchemaDescriptor.fulltext(RELATIONSHIP, new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 })).withName("r2").materialise(3L));
schemaRules.add(forSchema(SchemaDescriptor.fulltext(NODE, new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 })).withName("n2").materialise(4L));
when(storageEngineFactory.loadSchemaRules(any(), any(), any(), any(), any())).thenReturn(schemaRules);
SchemaIndexMigrator migrator = new SchemaIndexMigrator("Test migrator", fs, pageCache, directoryStructure, storageEngineFactory, false);
// when
migrator.migrate(databaseLayout, migrationLayout, progressReporter, "from", "to", IndexImporterFactory.EMPTY);
migrator.moveMigratedFiles(databaseLayout, migrationLayout, "from", "to");
// then
verify(fs, never()).deleteRecursively(directoryStructure.directoryForIndex(1L));
verify(fs).deleteRecursively(directoryStructure.directoryForIndex(2L));
verify(fs).deleteRecursively(directoryStructure.directoryForIndex(3L));
verify(fs, never()).deleteRecursively(directoryStructure.directoryForIndex(4L));
}
use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.
the class RecordStorageEngineFactory method loadSchemaRules.
@Override
public List<SchemaRule> loadSchemaRules(FileSystemAbstraction fs, PageCache pageCache, Config config, DatabaseLayout databaseLayout, CursorContext cursorContext) {
StoreFactory factory = new StoreFactory(databaseLayout, config, new DefaultIdGeneratorFactory(fs, immediate(), databaseLayout.getDatabaseName()), pageCache, fs, NullLogProvider.nullLogProvider(), PageCacheTracer.NULL, readOnly());
try (NeoStores stores = factory.openNeoStores(false, StoreType.SCHEMA, StoreType.PROPERTY_KEY_TOKEN, StoreType.PROPERTY)) {
stores.start(cursorContext);
TokenHolders tokenHolders = tokenHoldersForSchemaStore(stores, new ReadOnlyTokenCreator(), cursorContext);
List<SchemaRule> rules = new ArrayList<>();
new SchemaStorage(stores.getSchemaStore(), tokenHolders, () -> KernelVersion.LATEST).getAll(cursorContext).forEach(rules::add);
return rules;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.
the class LogCommandSerializationV3_0_10 method readLegacySchemaRuleCommand.
@Override
protected Command readLegacySchemaRuleCommand(ReadableChannel channel) throws IOException {
Collection<DynamicRecord> recordsBefore = new ArrayList<>();
readDynamicRecords(channel, recordsBefore, COLLECTION_DYNAMIC_RECORD_ADDER);
Collection<DynamicRecord> recordsAfter = new ArrayList<>();
readDynamicRecords(channel, recordsAfter, COLLECTION_DYNAMIC_RECORD_ADDER);
byte isCreated = channel.get();
if (1 == isCreated) {
for (DynamicRecord record : recordsAfter) {
record.setCreated();
}
}
SchemaRule rule = Iterables.first(recordsAfter).inUse() ? readSchemaRule(recordsAfter) : readSchemaRule(recordsBefore);
return new Command.SchemaRuleCommand(this, new SchemaRecord(0), new SchemaRecord(0), rule);
}
Aggregations