Search in sources :

Example 6 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class SchemaRuleMigrationTest method setUp.

@BeforeEach
void setUp() {
    srcTokenHolders = new TokenHolders(StoreTokens.createReadOnlyTokenHolder(TokenHolder.TYPE_PROPERTY_KEY), StoreTokens.createReadOnlyTokenHolder(TokenHolder.TYPE_LABEL), StoreTokens.createReadOnlyTokenHolder(TokenHolder.TYPE_RELATIONSHIP_TYPE));
    src = mock(SchemaStorage35.class);
    writtenRules = new ArrayList<>();
    dst = new SchemaRuleMigrationAccess() {

        @Override
        public void writeSchemaRule(SchemaRule rule) {
            writtenRules.add(rule);
        }

        @Override
        public Iterable<SchemaRule> getAll() {
            return List.of();
        }

        @Override
        public void close() {
        }
    };
}
Also used : SchemaStorage35(org.neo4j.kernel.impl.storemigration.legacy.SchemaStorage35) SchemaRule(org.neo4j.internal.schema.SchemaRule) TokenHolders(org.neo4j.token.TokenHolders) SchemaRuleMigrationAccess(org.neo4j.storageengine.migration.SchemaRuleMigrationAccess) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class SchemaRuleMigrationTest method mustOverwritePreviousDefaultConstraintNames.

@Test
void mustOverwritePreviousDefaultConstraintNames() throws KernelException {
    SchemaRule rule = ConstraintDescriptorFactory.uniqueForSchema(SchemaDescriptor.forLabel(1, 2)).withId(1).withName("constraint_1");
    srcTokenHolders.labelTokens().setInitialTokens(List.of(new NamedToken("Label", 1)));
    srcTokenHolders.propertyKeyTokens().setInitialTokens(List.of(new NamedToken("prop", 2)));
    when(src.getAll(any())).thenReturn(List.of(rule));
    RecordStorageMigrator.migrateSchemaRules(srcTokenHolders, src, dst, NULL);
    assertEquals(1, writtenRules.size());
    assertEquals("constraint_952591e6", writtenRules.get(0).getName());
}
Also used : SchemaRule(org.neo4j.internal.schema.SchemaRule) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 8 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class SchemaRuleMigrationTest method mustOverwritePreviousDefaultIndexNames.

@Test
void mustOverwritePreviousDefaultIndexNames() throws KernelException {
    SchemaRule rule = IndexPrototype.forSchema(SchemaDescriptor.forLabel(1, 2)).withName("index_1").materialise(1);
    srcTokenHolders.labelTokens().setInitialTokens(List.of(new NamedToken("Label", 1)));
    srcTokenHolders.propertyKeyTokens().setInitialTokens(List.of(new NamedToken("prop", 2)));
    when(src.getAll(any())).thenReturn(List.of(rule));
    RecordStorageMigrator.migrateSchemaRules(srcTokenHolders, src, dst, NULL);
    assertEquals(1, writtenRules.size());
    assertEquals("index_c3fbd584", writtenRules.get(0).getName());
}
Also used : SchemaRule(org.neo4j.internal.schema.SchemaRule) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 9 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule 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 10 with SchemaRule

use of org.neo4j.internal.schema.SchemaRule in project neo4j by neo4j.

the class TransactionRecordStateTest method shouldCreateEqualEntityPropertyUpdatesOnRecoveryOfCreatedEntities.

@Test
void shouldCreateEqualEntityPropertyUpdatesOnRecoveryOfCreatedEntities() throws Exception {
    neoStores = createStores();
    /* There was an issue where recovering a tx where a node with a label and a property
         * was created resulted in two exact copies of NodePropertyUpdates. */
    // GIVEN
    long nodeId = 0;
    long relId = 1;
    int labelId = 5;
    int relTypeId = 4;
    int propertyKeyId = 7;
    // -- indexes
    long nodeRuleId = 0;
    TransactionRecordState recordState = newTransactionRecordState();
    SchemaRule nodeRule = IndexPrototype.forSchema(forLabel(labelId, propertyKeyId)).withName("index_" + nodeRuleId).materialise(nodeRuleId);
    recordState.schemaRuleCreate(nodeRuleId, false, nodeRule);
    long relRuleId = 1;
    SchemaRule relRule = IndexPrototype.forSchema(forRelType(relTypeId, propertyKeyId)).withName("index_" + relRuleId).materialise(relRuleId);
    recordState.schemaRuleCreate(relRuleId, false, relRule);
    apply(recordState);
    // -- and a tx creating a node and a rel for those indexes
    recordState = newTransactionRecordState();
    recordState.nodeCreate(nodeId);
    recordState.addLabelToNode(labelId, nodeId);
    recordState.nodeAddProperty(nodeId, propertyKeyId, Values.of("Neo"));
    recordState.relModify(singleCreate(relId, relTypeId, nodeId, nodeId));
    recordState.relAddProperty(relId, propertyKeyId, Values.of("Oen"));
    // WHEN
    CommandsToApply transaction = transaction(recordState);
    IndexUpdatesExtractor extractor = new IndexUpdatesExtractor();
    transaction.accept(extractor);
    // THEN
    // -- later recovering that tx, there should be only one update for each type
    assertTrue(extractor.containsAnyEntityOrPropertyUpdate());
    MutableLongSet recoveredNodeIds = new LongHashSet();
    recoveredNodeIds.addAll(entityIds(extractor.getNodeCommands()));
    assertEquals(1, recoveredNodeIds.size());
    assertEquals(nodeId, recoveredNodeIds.longIterator().next());
    MutableLongSet recoveredRelIds = new LongHashSet();
    recoveredRelIds.addAll(entityIds(extractor.getRelationshipCommands()));
    assertEquals(1, recoveredRelIds.size());
    assertEquals(relId, recoveredRelIds.longIterator().next());
}
Also used : CommandsToApply(org.neo4j.storageengine.api.CommandsToApply) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) SchemaRule(org.neo4j.internal.schema.SchemaRule) Test(org.junit.jupiter.api.Test)

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