Search in sources :

Example 11 with SchemaRule

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);
}
Also used : SchemaRule(org.neo4j.internal.schema.SchemaRule) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 12 with 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();
        }
    }
}
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 13 with SchemaRule

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));
}
Also used : IndexDirectoryStructure(org.neo4j.kernel.api.index.IndexDirectoryStructure) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) StoreVersion(org.neo4j.storageengine.api.StoreVersion) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.internal.schema.SchemaRule) SchemaIndexMigrator(org.neo4j.storageengine.migration.SchemaIndexMigrator) Test(org.junit.jupiter.api.Test)

Example 14 with SchemaRule

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);
    }
}
Also used : NeoStores(org.neo4j.kernel.impl.store.NeoStores) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.internal.schema.SchemaRule) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) TokenHolders(org.neo4j.token.TokenHolders) ReadOnlyTokenCreator(org.neo4j.token.ReadOnlyTokenCreator)

Example 15 with SchemaRule

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);
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.internal.schema.SchemaRule)

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