Search in sources :

Example 21 with ConstraintDescriptor

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

the class SchemaRuleSerialization35Test method assertSerializeAndDeserializeConstraintRule.

private static void assertSerializeAndDeserializeConstraintRule(ConstraintDescriptor constraintRule) throws MalformedSchemaRuleException {
    ConstraintDescriptor deserialized = assertConstraintRule(serialiseAndDeserialise(constraintRule));
    assertThat(deserialized.getId()).isEqualTo(constraintRule.getId());
    assertThat(deserialized).isEqualTo(constraintRule);
    assertThat(deserialized.schema()).isEqualTo(constraintRule.schema());
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)

Example 22 with ConstraintDescriptor

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

the class SchemaRuleSerialization35Test method assertParseNodePropertyExistsRule.

private static void assertParseNodePropertyExistsRule(String serialized, String name) throws Exception {
    // GIVEN
    long ruleId = 87;
    int propertyKey = 51;
    int labelId = 45;
    ConstraintDescriptor constraint = existsForLabel(labelId, propertyKey);
    byte[] bytes = decodeBase64(serialized);
    // WHEN
    ConstraintDescriptor deserialized = assertConstraintRule(SchemaRuleSerialization35.deserialize(ruleId, ByteBuffer.wrap(bytes)));
    // THEN
    assertThat(deserialized.getId()).isEqualTo(ruleId);
    assertThat(deserialized).isEqualTo(constraint);
    assertThat(deserialized.schema()).isEqualTo(constraint.schema());
    assertThatThrownBy(() -> deserialized.asIndexBackedConstraint().ownedIndexId()).isInstanceOf(IllegalStateException.class);
    assertThat(deserialized.getName()).isEqualTo(name);
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)

Example 23 with ConstraintDescriptor

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

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

the class KernelSchemaStateFlushingTest method shouldInvalidateSchemaStateOnDropConstraint.

@Test
void shouldInvalidateSchemaStateOnDropConstraint() throws Exception {
    // given
    ConstraintDescriptor descriptor = createConstraint();
    commitToSchemaState("test", "before");
    dropConstraint(descriptor);
    // when
    String after = commitToSchemaState("test", "after");
    // then
    assertEquals("after", after);
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 25 with ConstraintDescriptor

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

the class TransactionRecordStateTest method preparingConstraintRulesMustMarkSchemaRecordAsChanged.

@Test
void preparingConstraintRulesMustMarkSchemaRecordAsChanged() throws Exception {
    neoStores = createStores();
    TransactionRecordState state = newTransactionRecordState();
    long ruleId = neoStores.getSchemaStore().nextId(NULL);
    ConstraintDescriptor rule = ConstraintDescriptorFactory.existsForLabel(0, 1).withId(ruleId);
    state.schemaRuleCreate(ruleId, true, rule);
    List<StorageCommand> commands = new ArrayList<>();
    state.extractCommands(commands, INSTANCE);
    assertThat(commands.size()).isEqualTo(1);
    SchemaRuleCommand command = (SchemaRuleCommand) commands.get(0);
    assertThat(command.getMode()).isEqualTo(Command.Mode.CREATE);
    assertThat(command.getSchemaRule()).isEqualTo(rule);
    assertThat(command.getKey()).isEqualTo(ruleId);
    assertThat(command.getBefore().inUse()).isEqualTo(false);
    assertThat(command.getAfter().inUse()).isEqualTo(true);
    assertThat(command.getAfter().isConstraint()).isEqualTo(true);
    assertThat(command.getAfter().isCreated()).isEqualTo(true);
    assertThat(command.getAfter().getNextProp()).isEqualTo(Record.NO_NEXT_PROPERTY.longValue());
}
Also used : SchemaRuleCommand(org.neo4j.internal.recordstorage.Command.SchemaRuleCommand) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ArrayList(java.util.ArrayList) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)107 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)34 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)32 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)21 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)20 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)19 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)16 TokenRead (org.neo4j.internal.kernel.api.TokenRead)9 ArrayList (java.util.ArrayList)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)6 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)6 SchemaStore (org.neo4j.kernel.impl.store.SchemaStore)6 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)5 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)5 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)5 SchemaRule (org.neo4j.internal.schema.SchemaRule)5 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)5 OptionalLong (java.util.OptionalLong)4