Search in sources :

Example 1 with IndexBackedConstraintDescriptor

use of org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor in project neo4j by neo4j.

the class TxStateTest method dataRevisionMustNotChangeOnSchemaChanges.

@Test
void dataRevisionMustNotChangeOnSchemaChanges() {
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.indexDoAdd(indexOn_1_1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.indexDoDrop(indexOn_1_1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.indexDoUnRemove(indexOn_1_1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    UniquenessConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForLabel(1, 17);
    state.constraintDoAdd(constraint1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.constraintDoDrop(constraint1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.constraintDoUnRemove(constraint1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    IndexBackedConstraintDescriptor constraint2 = ConstraintDescriptorFactory.nodeKeyForLabel(0, 0);
    state.constraintDoAdd(constraint2, IndexPrototype.uniqueForSchema(forLabel(0, 0)).withName("index").materialise(0));
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.labelDoCreateForName("Label", false, 0);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.relationshipTypeDoCreateForName("REL", false, 0);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.propertyKeyDoCreateForName("prop", false, 0);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    // This is not strictly a schema-change, but it is a "non-data" change in that these will not transform into store updates.
    // Or schema updates for that matter. We only do these to speed up the transaction state filtering of schema index query results.
    state.indexDoUpdateEntry(indexOn_1_1.schema(), 0, ValueTuple.of(Values.booleanValue(true)), ValueTuple.of(Values.booleanValue(false)));
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
}
Also used : IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 2 with IndexBackedConstraintDescriptor

use of org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor in project neo4j by neo4j.

the class IndexIT method shouldListAll.

@Test
void shouldListAll() throws Exception {
    // given
    SchemaWrite schemaWrite = schemaWriteInNewTransaction();
    IndexDescriptor index1 = schemaWrite.indexCreate(schema, "my index");
    IndexBackedConstraintDescriptor constraint = schemaWrite.uniquePropertyConstraintCreate(uniqueForSchema(schema2).withName("constraint name")).asIndexBackedConstraint();
    commit();
    // then/when
    SchemaRead schemaRead = newTransaction().schemaRead();
    IndexDescriptor index2 = Iterators.single(schemaRead.index(constraint.schema()));
    List<IndexDescriptor> indexes = Iterators.asList(schemaRead.indexesGetAll());
    assertThat(indexes).contains(index1, index2);
    commit();
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 3 with IndexBackedConstraintDescriptor

use of org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor in project neo4j by neo4j.

the class Operations method checkConstraintsAndAddLabelToNode.

private void checkConstraintsAndAddLabelToNode(long node, int nodeLabel) throws UniquePropertyValueValidationException, UnableToValidateConstraintException {
    // Load the property key id list for this node. We may need it for constraint validation if there are any related constraints,
    // but regardless we need it for tx state updating
    int[] existingPropertyKeyIds = loadSortedNodePropertyKeyList();
    // with the same label and property combination.
    if (existingPropertyKeyIds.length > 0) {
        for (IndexBackedConstraintDescriptor uniquenessConstraint : storageReader.uniquenessConstraintsGetRelated(new long[] { nodeLabel }, existingPropertyKeyIds, NODE)) {
            PropertyIndexQuery.ExactPredicate[] propertyValues = getAllPropertyValues(uniquenessConstraint.schema(), StatementConstants.NO_SUCH_PROPERTY_KEY, Values.NO_VALUE);
            if (propertyValues != null) {
                validateNoExistingNodeWithExactValues(uniquenessConstraint, propertyValues, node);
            }
        }
    }
    // node is there and doesn't already have the label, let's add
    ktx.txState().nodeDoAddLabel(nodeLabel, node);
    updater.onLabelChange(nodeLabel, existingPropertyKeyIds, nodeCursor, propertyCursor, ADDED_LABEL);
}
Also used : IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)

Example 4 with IndexBackedConstraintDescriptor

use of org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor in project neo4j by neo4j.

the class PlainOperationsTest method uniqueIndexesMustBeNamedAfterTheirConstraints.

@Test
void uniqueIndexesMustBeNamedAfterTheirConstraints() throws KernelException {
    when(creationContext.reserveSchema()).thenReturn(1L, 2L, 3L);
    when(storageReader.constraintsGetForSchema(any())).thenReturn(Iterators.emptyResourceIterator());
    when(storageReader.indexGetForSchema(any())).thenReturn(Iterators.emptyResourceIterator());
    String constraintName = "my_constraint";
    when(constraintIndexCreator.createUniquenessConstraintIndex(any(), any(), any())).thenAnswer(i -> {
        IndexPrototype prototype = i.getArgument(2);
        Optional<String> name = prototype.getName();
        assertTrue(name.isPresent());
        assertThat(name.get()).isEqualTo(constraintName);
        return prototype.materialise(2);
    });
    IndexPrototype prototype = IndexPrototype.uniqueForSchema(schema).withName(constraintName);
    IndexBackedConstraintDescriptor constraint = operations.uniquePropertyConstraintCreate(prototype).asIndexBackedConstraint();
    assertThat(constraint.ownedIndexId()).isEqualTo(2L);
}
Also used : IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Test(org.junit.jupiter.api.Test)

Example 5 with IndexBackedConstraintDescriptor

use of org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor in project neo4j by neo4j.

the class SchemaStatementProcedure method includeConstraint.

private static boolean includeConstraint(SchemaReadCore schemaRead, ConstraintDescriptor constraint) {
    // - Owned index must have this constraint as owning constraint
    if (constraint.isIndexBackedConstraint()) {
        IndexBackedConstraintDescriptor indexBackedConstraint = constraint.asIndexBackedConstraint();
        if (indexBackedConstraint.hasOwnedIndexId()) {
            IndexDescriptor backingIndex = schemaRead.indexGetForName(constraint.getName());
            if (backingIndex.getId() == indexBackedConstraint.ownedIndexId()) {
                try {
                    InternalIndexState internalIndexState = schemaRead.indexGetState(backingIndex);
                    OptionalLong owningConstraintId = backingIndex.getOwningConstraintId();
                    return internalIndexState == InternalIndexState.ONLINE && owningConstraintId.orElse(-1) == constraint.getId();
                } catch (IndexNotFoundKernelException e) {
                    return false;
                }
            }
        }
        return false;
    }
    return true;
}
Also used : InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) OptionalLong(java.util.OptionalLong) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)6 Test (org.junit.jupiter.api.Test)3 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)3 OptionalLong (java.util.OptionalLong)1 RepeatedTest (org.junit.jupiter.api.RepeatedTest)1 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)1 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)1 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)1 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)1 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)1 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)1 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)1 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)1