Search in sources :

Example 21 with SchemaWrite

use of org.neo4j.internal.kernel.api.SchemaWrite in project neo4j by neo4j.

the class BuiltInProceduresIT method listAllLabelsMustNotBlockOnConstraintCreatingTransaction.

@Test
@Timeout(value = 6, unit = MINUTES)
void listAllLabelsMustNotBlockOnConstraintCreatingTransaction() throws Throwable {
    // Given
    KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
    long nodeId = transaction.dataWrite().nodeCreate();
    int labelId = transaction.tokenWrite().labelGetOrCreateForName("MyLabel");
    int propKey = transaction.tokenWrite().propertyKeyCreateForName("prop", false);
    transaction.dataWrite().nodeAddLabel(nodeId, labelId);
    commit();
    CountDownLatch constraintLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);
    FutureTask<Void> createConstraintTask = new FutureTask<>(() -> {
        SchemaWrite schemaWrite = schemaWriteInNewTransaction();
        try (Resource ignore = captureTransaction()) {
            IndexPrototype prototype = IndexPrototype.uniqueForSchema(SchemaDescriptor.forLabel(labelId, propKey)).withName("constraint name");
            schemaWrite.uniquePropertyConstraintCreate(prototype);
            // We now hold a schema lock on the "MyLabel" label. Let the procedure calling transaction have a go.
            constraintLatch.countDown();
            commitLatch.await();
        }
        rollback();
        return null;
    });
    Thread constraintCreator = new Thread(createConstraintTask);
    constraintCreator.start();
    // When
    constraintLatch.await();
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "labels")).id(), new AnyValue[0], ProcedureCallContext.EMPTY);
    // Then
    try {
        assertThat(asList(stream)).containsExactly(new AnyValue[] { stringValue("MyLabel") });
    } finally {
        commitLatch.countDown();
    }
    createConstraintTask.get();
    constraintCreator.join();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) Resource(org.neo4j.graphdb.Resource) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) CountDownLatch(java.util.concurrent.CountDownLatch) FutureTask(java.util.concurrent.FutureTask) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 22 with SchemaWrite

use of org.neo4j.internal.kernel.api.SchemaWrite in project neo4j by neo4j.

the class KernelIT method createIndex.

private static IndexDescriptor createIndex(KernelTransaction transaction) throws KernelException {
    TokenWrite tokenWrite = transaction.tokenWrite();
    SchemaWrite schemaWrite = transaction.schemaWrite();
    LabelSchemaDescriptor schema = forLabel(tokenWrite.labelGetOrCreateForName("hello"), tokenWrite.propertyKeyGetOrCreateForName("hepp"));
    return schemaWrite.indexCreate(schema, null);
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor)

Example 23 with SchemaWrite

use of org.neo4j.internal.kernel.api.SchemaWrite in project neo4j by neo4j.

the class SystemBuiltInProceduresIT method listConstraints.

@Test
void listConstraints() throws Throwable {
    // Given
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    TokenWrite tokenWrite = transaction.tokenWrite();
    int labelId = tokenWrite.labelGetOrCreateForName("Label");
    int propId = tokenWrite.propertyKeyGetOrCreateForName("property");
    SchemaWrite schemaWrite = transaction.schemaWrite();
    schemaWrite.uniquePropertyConstraintCreate(uniqueForSchema(forLabel(labelId, propId)).withName("my_constraint"));
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        // When & Then
        assertFalse(tx.execute("CALL db.constraints").hasNext());
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Test(org.junit.jupiter.api.Test)

Example 24 with SchemaWrite

use of org.neo4j.internal.kernel.api.SchemaWrite in project neo4j by neo4j.

the class UniquenessConstraintValidationIT method createConstraint.

private ConstraintDescriptor createConstraint(String label, String propertyKey) throws KernelException {
    int labelId;
    int propertyKeyId;
    TokenWrite tokenWrite = tokenWriteInNewTransaction();
    labelId = tokenWrite.labelGetOrCreateForName(label);
    propertyKeyId = tokenWrite.propertyKeyGetOrCreateForName(propertyKey);
    commit();
    SchemaWrite schemaWrite = schemaWriteInNewTransaction();
    ConstraintDescriptor constraint = schemaWrite.uniquePropertyConstraintCreate(uniqueForSchema(forLabel(labelId, propertyKeyId)));
    commit();
    return constraint;
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite)

Example 25 with SchemaWrite

use of org.neo4j.internal.kernel.api.SchemaWrite in project neo4j by neo4j.

the class IndexIT method shouldDisallowDroppingIndexBySchemaThatDoesNotExist.

@Test
void shouldDisallowDroppingIndexBySchemaThatDoesNotExist() throws Exception {
    // given
    IndexDescriptor index;
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        index = statement.indexCreate(schema, "my index");
        commit();
    }
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        statement.indexDrop(index.schema());
        commit();
    }
    // when
    SchemaWrite statement = schemaWriteInNewTransaction();
    SchemaKernelException e = assertThrows(SchemaKernelException.class, () -> statement.indexDrop(index.schema()));
    assertEquals("Unable to drop index on (:" + LABEL + " {" + PROPERTY_KEY + "}). There is no such index.", e.getMessage());
    commit();
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Aggregations

SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)27 Test (org.junit.jupiter.api.Test)21 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)15 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)11 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)9 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)9 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)5 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)4 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)4 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)4 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)3 FulltextSchemaDescriptor (org.neo4j.internal.schema.FulltextSchemaDescriptor)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 FutureTask (java.util.concurrent.FutureTask)2 Timeout (org.junit.jupiter.api.Timeout)2 KernelException (org.neo4j.exceptions.KernelException)2 Resource (org.neo4j.graphdb.Resource)2 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)2 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)2