Search in sources :

Example 1 with SchemaRuleAccess

use of org.neo4j.internal.recordstorage.SchemaRuleAccess in project neo4j by neo4j.

the class DropBrokenUniquenessConstraintIT method shouldDropUniquenessConstraintWithBackingIndexHavingNoOwner.

@Test
void shouldDropUniquenessConstraintWithBackingIndexHavingNoOwner() throws KernelException {
    // given
    try (Transaction tx = db.beginTx()) {
        tx.schema().constraintFor(label).assertPropertyIsUnique(key).create();
        tx.commit();
    }
    // when intentionally breaking the schema by setting the backing index rule to unused
    SchemaRuleAccess schemaRules = storageEngine.testAccessSchemaRules();
    writeSchemaRulesWithoutConstraint(schemaRules);
    // At this point the SchemaCache doesn't know about this change so we have to reload it
    storageEngine.loadSchemaCache();
    try (Transaction tx = db.beginTx()) {
        single(tx.schema().getConstraints(label).iterator()).drop();
        tx.commit();
    }
// then
// AfterEach
}
Also used : Transaction(org.neo4j.graphdb.Transaction) SchemaRuleAccess(org.neo4j.internal.recordstorage.SchemaRuleAccess) Test(org.junit.jupiter.api.Test)

Example 2 with SchemaRuleAccess

use of org.neo4j.internal.recordstorage.SchemaRuleAccess in project neo4j by neo4j.

the class DropBrokenUniquenessConstraintIT method shouldDropUniquenessConstraintWhereConstraintRecordIsMissingAndIndexHasNoOwner.

@Test
void shouldDropUniquenessConstraintWhereConstraintRecordIsMissingAndIndexHasNoOwner() throws KernelException {
    // given
    try (Transaction tx = db.beginTx()) {
        tx.schema().constraintFor(label).assertPropertyIsUnique(key).create();
        tx.commit();
    }
    // when intentionally breaking the schema by setting the backing index rule to unused
    SchemaRuleAccess schemaRules = storageEngine.testAccessSchemaRules();
    schemaRules.constraintsGetAllIgnoreMalformed(NULL).forEachRemaining(rule -> schemaRules.deleteSchemaRule(rule, NULL));
    writeSchemaRulesWithoutConstraint(schemaRules);
    // At this point the SchemaCache doesn't know about this change so we have to reload it
    storageEngine.loadSchemaCache();
    try (Transaction tx = db.beginTx()) {
        // We don't use single() here, because it is okay for the schema cache reload to clean up after us.
        tx.schema().getConstraints(label).forEach(ConstraintDefinition::drop);
        tx.schema().getIndexes(label).forEach(IndexDefinition::drop);
        tx.commit();
    }
// then
// AfterEach
}
Also used : Transaction(org.neo4j.graphdb.Transaction) SchemaRuleAccess(org.neo4j.internal.recordstorage.SchemaRuleAccess) Test(org.junit.jupiter.api.Test)

Example 3 with SchemaRuleAccess

use of org.neo4j.internal.recordstorage.SchemaRuleAccess in project neo4j by neo4j.

the class DropBrokenUniquenessConstraintIT method shouldDropUniquenessConstraintWithBackingIndexNotInUse.

@Test
void shouldDropUniquenessConstraintWithBackingIndexNotInUse() {
    // given
    String backingIndexName;
    try (Transaction tx = db.beginTx()) {
        tx.schema().constraintFor(label).assertPropertyIsUnique(key).create();
        backingIndexName = single(tx.schema().getIndexes(label).iterator()).getName();
        tx.commit();
    }
    // when intentionally breaking the schema by setting the backing index rule to unused
    SchemaRuleAccess schemaRules = storageEngine.testAccessSchemaRules();
    schemaRules.deleteSchemaRule(schemaRules.indexGetForName(backingIndexName, NULL), NULL);
    // At this point the SchemaCache doesn't know about this change so we have to reload it
    storageEngine.loadSchemaCache();
    try (Transaction tx = db.beginTx()) {
        single(tx.schema().getConstraints(label).iterator()).drop();
        tx.commit();
    }
// then
// AfterEach
}
Also used : Transaction(org.neo4j.graphdb.Transaction) SchemaRuleAccess(org.neo4j.internal.recordstorage.SchemaRuleAccess) Test(org.junit.jupiter.api.Test)

Example 4 with SchemaRuleAccess

use of org.neo4j.internal.recordstorage.SchemaRuleAccess in project neo4j by neo4j.

the class BatchInsertTest method shouldCreateConsistentUniquenessConstraint.

@ParameterizedTest
@MethodSource("params")
void shouldCreateConsistentUniquenessConstraint(int denseNodeThreshold) throws Exception {
    // given
    BatchInserter inserter = newBatchInserter(denseNodeThreshold);
    // when
    inserter.createDeferredConstraint(label("Hacker")).assertPropertyIsUnique("handle").create();
    // then
    GraphDatabaseAPI graphdb = switchToEmbeddedGraphDatabaseService(inserter, denseNodeThreshold);
    try {
        NeoStores neoStores = graphdb.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores();
        SchemaStore store = neoStores.getSchemaStore();
        TokenHolders tokenHolders = graphdb.getDependencyResolver().resolveDependency(TokenHolders.class);
        SchemaRuleAccess schemaRuleAccess = SchemaRuleAccess.getSchemaRuleAccess(store, tokenHolders, () -> KernelVersion.LATEST);
        List<Long> inUse = new ArrayList<>();
        SchemaRecord record = store.newRecord();
        for (long i = 1, high = store.getHighestPossibleIdInUse(NULL); i <= high; i++) {
            store.getRecord(i, record, RecordLoad.FORCE, NULL);
            if (record.inUse()) {
                inUse.add(i);
            }
        }
        assertEquals(2, inUse.size(), "records in use");
        SchemaRule rule0 = schemaRuleAccess.loadSingleSchemaRule(inUse.get(0), NULL);
        SchemaRule rule1 = schemaRuleAccess.loadSingleSchemaRule(inUse.get(1), NULL);
        IndexDescriptor indexRule;
        ConstraintDescriptor constraint;
        if (rule0 instanceof IndexDescriptor) {
            indexRule = (IndexDescriptor) rule0;
            constraint = (ConstraintDescriptor) rule1;
        } else {
            constraint = (ConstraintDescriptor) rule0;
            indexRule = (IndexDescriptor) rule1;
        }
        OptionalLong owningConstraintId = indexRule.getOwningConstraintId();
        assertTrue(owningConstraintId.isPresent(), "index should have owning constraint");
        assertEquals(constraint.getId(), owningConstraintId.getAsLong(), "index should reference constraint");
        assertEquals(indexRule.getId(), constraint.asIndexBackedConstraint().ownedIndexId(), "constraint should reference index");
    } finally {
        managementService.shutdown();
    }
}
Also used : SchemaStore(org.neo4j.kernel.impl.store.SchemaStore) SchemaRuleAccess(org.neo4j.internal.recordstorage.SchemaRuleAccess) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.internal.schema.SchemaRule) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) BatchInserter(org.neo4j.batchinsert.BatchInserter) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) RecordStorageEngine(org.neo4j.internal.recordstorage.RecordStorageEngine) NeoStores(org.neo4j.kernel.impl.store.NeoStores) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) OptionalLong(java.util.OptionalLong) OptionalLong(java.util.OptionalLong) TokenHolders(org.neo4j.token.TokenHolders) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with SchemaRuleAccess

use of org.neo4j.internal.recordstorage.SchemaRuleAccess in project neo4j by neo4j.

the class FullCheckIntegrationTest method writeToSchemaStore.

private void writeToSchemaStore(SchemaStore schemaStore, SchemaRule rule) throws KernelException {
    SchemaRuleAccess schemaRuleAccess = SchemaRuleAccess.getSchemaRuleAccess(schemaStore, fixture.writableTokenHolders(), () -> KernelVersion.LATEST);
    schemaRuleAccess.writeSchemaRule(rule, NULL, INSTANCE);
}
Also used : SchemaRuleAccess(org.neo4j.internal.recordstorage.SchemaRuleAccess)

Aggregations

SchemaRuleAccess (org.neo4j.internal.recordstorage.SchemaRuleAccess)7 Test (org.junit.jupiter.api.Test)5 Transaction (org.neo4j.graphdb.Transaction)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2 TokenHolders (org.neo4j.token.TokenHolders)2 ArrayList (java.util.ArrayList)1 OptionalLong (java.util.OptionalLong)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 BatchInserter (org.neo4j.batchinsert.BatchInserter)1 RecordStorageEngine (org.neo4j.internal.recordstorage.RecordStorageEngine)1 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)1 SchemaRule (org.neo4j.internal.schema.SchemaRule)1 IndexStatisticsStore (org.neo4j.kernel.impl.api.index.stats.IndexStatisticsStore)1 NeoStores (org.neo4j.kernel.impl.store.NeoStores)1 SchemaStore (org.neo4j.kernel.impl.store.SchemaStore)1 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)1 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)1