Search in sources :

Example 6 with ConstraintDescriptor

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

the class IntegrityValidator method validateSchemaRule.

void validateSchemaRule(SchemaRule schemaRule) throws TransactionFailureException {
    Preconditions.checkState(indexValidator != null, "No index validator installed");
    KernelVersion currentVersion = neoStores.getMetaDataStore().kernelVersion();
    if (currentVersion.isLessThan(VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED)) {
        if (schemaRule instanceof IndexDescriptor) {
            IndexDescriptor index = (IndexDescriptor) schemaRule;
            if (index.isTokenIndex() || isBtreeRelationshipPropertyIndex(index)) {
                throw new TransactionFailureException(Status.General.UpgradeRequired, "Index operation on index '%s' not allowed. " + "Required kernel version for this transaction is %s, but actual version was %s.", index, VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED.name(), currentVersion.name());
            }
        }
    }
    if (schemaRule instanceof ConstraintDescriptor) {
        ConstraintDescriptor constraint = (ConstraintDescriptor) schemaRule;
        if (constraint.isIndexBackedConstraint()) {
            long ownedIndex = constraint.asIndexBackedConstraint().ownedIndexId();
            try {
                indexValidator.validateIndex(ownedIndex);
            } catch (KernelException e) {
                // and have recovery performed. It's the safest bet to avoid loosing data.
                throw new TransactionFailureException(Status.Transaction.TransactionValidationFailed, e, "Index validation of " + schemaRule + " failed, specifically for its owned index " + ownedIndex, e);
            }
        }
    }
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) KernelException(org.neo4j.exceptions.KernelException)

Example 7 with ConstraintDescriptor

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

the class TxStateTest method shouldAddUniquenessConstraint.

@Test
void shouldAddUniquenessConstraint() {
    // when
    LabelSchemaDescriptor schema = forLabel(1, 17);
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
    state.constraintDoAdd(constraint, IndexPrototype.uniqueForSchema(schema).withName("constraint_7").materialise(7));
    // then
    DiffSets<ConstraintDescriptor> diff = state.constraintsChangesForLabel(1);
    assertEquals(singleton(constraint), diff.getAdded());
    assertTrue(diff.getRemoved().isEmpty());
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 8 with ConstraintDescriptor

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

the class TxStateTest method shouldAddRelationshipPropertyExistenceConstraint.

@Test
void shouldAddRelationshipPropertyExistenceConstraint() {
    // Given
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForRelType(1, 42);
    // When
    state.constraintDoAdd(constraint);
    // Then
    assertEquals(singleton(constraint), state.constraintsChangesForRelationshipType(1).getAdded());
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 9 with ConstraintDescriptor

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

the class SchemaStatementProcedureTest method schemaStatementsMustNotIncludeIndexBackedConstraintsWithFailedIndexIndex.

@Test
void schemaStatementsMustNotIncludeIndexBackedConstraintsWithFailedIndexIndex() throws IndexNotFoundKernelException, ProcedureException {
    IndexDescriptor index = someOrphanedIndex();
    ConstraintDescriptor constraint = indexBackedConstraint(index);
    index = indexBoundToConstraint(index, constraint);
    InternalIndexState internalIndexState = InternalIndexState.FAILED;
    SchemaReadCore schemaReadCore = getSchemaReadCore(constraint, index, internalIndexState);
    TokenRead tokenRead = mock(TokenRead.class);
    Collection<BuiltInProcedures.SchemaStatementResult> result = createSchemaStatementResults(schemaReadCore, tokenRead);
    assertEquals(0, result.size());
}
Also used : InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 10 with ConstraintDescriptor

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

the class CompositeIndexingIT method setup.

void setup(PrototypeFactory prototypeFactory) throws Exception {
    var prototype = prototypeFactory.build(labelId, relTypeId, propIds);
    try (Transaction tx = graphDatabaseAPI.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        if (prototype.isUnique()) {
            ConstraintDescriptor constraint = ktx.schemaWrite().uniquePropertyConstraintCreate(prototype);
            index = ktx.schemaRead().indexGetForName(constraint.getName());
        } else {
            index = ktx.schemaWrite().indexCreate(prototype.schema(), null);
        }
        tx.commit();
    }
    try (Transaction tx = graphDatabaseAPI.beginTx()) {
        tx.schema().awaitIndexesOnline(5, MINUTES);
        tx.commit();
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction)

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