Search in sources :

Example 6 with SchemaWrite

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

the class IndexProcedures method createIndex.

private Stream<BuiltInProcedures.SchemaIndexInfo> createIndex(String name, List<String> labels, List<String> properties, IndexProviderDescriptor indexProviderDescriptor, Map<String, Object> configMap, String statusMessage, IndexCreator indexCreator) throws ProcedureException {
    IndexConfig indexConfig = IndexSettingUtil.toIndexConfigFromStringObjectMap(configMap);
    assertSingleLabel(labels);
    assertValidIndexProvider(indexProviderDescriptor);
    int labelId = getOrCreateLabelId(labels.get(0));
    int[] propertyKeyIds = getOrCreatePropertyIds(properties);
    try {
        SchemaWrite schemaWrite = ktx.schemaWrite();
        LabelSchemaDescriptor labelSchemaDescriptor = SchemaDescriptor.forLabel(labelId, propertyKeyIds);
        indexCreator.create(schemaWrite, name, labelSchemaDescriptor, indexProviderDescriptor, indexConfig);
        return Stream.of(new BuiltInProcedures.SchemaIndexInfo(name, labels, properties, indexProviderDescriptor.name(), statusMessage));
    } catch (KernelException e) {
        throw new ProcedureException(e.status(), e, e.getMessage());
    }
}
Also used : IndexConfig(org.neo4j.internal.schema.IndexConfig) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException)

Example 7 with SchemaWrite

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

the class SchemaProcedureIT method testLabelIndex.

@Test
void testLabelIndex() throws Throwable {
    KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
    long nbrIndexesOnStartup = Iterators.count(transaction.schemaRead().indexesGetAll());
    // Given there is label with index and a constraint
    long nodeId = transaction.dataWrite().nodeCreate();
    int labelId = transaction.tokenWrite().labelGetOrCreateForName("Person");
    transaction.dataWrite().nodeAddLabel(nodeId, labelId);
    int propertyIdName = transaction.tokenWrite().propertyKeyGetOrCreateForName("name");
    int propertyIdAge = transaction.tokenWrite().propertyKeyGetOrCreateForName("age");
    transaction.dataWrite().nodeSetProperty(nodeId, propertyIdName, Values.of("Emil"));
    commit();
    SchemaWrite schemaOps = schemaWriteInNewTransaction();
    schemaOps.indexCreate(forLabel(labelId, propertyIdName), "my index");
    schemaOps.uniquePropertyConstraintCreate(uniqueForSchema(forLabel(labelId, propertyIdAge)).withName("constraint name"));
    commit();
    // When
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "schema", "visualization")).id(), new AnyValue[0], ProcedureCallContext.EMPTY);
    // Then
    while (stream.hasNext()) {
        AnyValue[] next = stream.next();
        assertEquals(2, next.length);
        ListValue nodes = (ListValue) next[0];
        assertEquals(1, nodes.size());
        NodeValue node = (NodeValue) nodes.value(0);
        assertThat(node.labels()).isEqualTo(Values.stringArray("Person"));
        assertEquals(stringValue("Person"), node.properties().get("name"));
        assertEquals(VirtualValues.list(stringValue("name")), node.properties().get("indexes"));
        assertEquals(VirtualValues.list(stringValue("Constraint( id=" + (3 + nbrIndexesOnStartup) + ", name='constraint name', type='UNIQUENESS', schema=(:Person {age}), " + "ownedIndex=" + (2 + nbrIndexesOnStartup) + " )")), node.properties().get("constraints"));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValue(org.neo4j.values.virtual.NodeValue) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ListValue(org.neo4j.values.virtual.ListValue) AnyValue(org.neo4j.values.AnyValue) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 8 with SchemaWrite

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

the class IndexIT method shouldDisallowDroppingIndexByNameThatDoesNotExist.

@Test
void shouldDisallowDroppingIndexByNameThatDoesNotExist() throws KernelException {
    // given
    String indexName = "My fancy index";
    IndexDescriptor index;
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        index = statement.indexCreate(schema, indexName);
        commit();
    }
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        statement.indexDrop(index);
        commit();
    }
    // when
    SchemaWrite statement = schemaWriteInNewTransaction();
    SchemaKernelException e = assertThrows(SchemaKernelException.class, () -> statement.indexDrop(indexName));
    assertEquals(e.getMessage(), "Unable to drop index called `My fancy index`. There is no such index.");
    rollback();
}
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)

Example 9 with SchemaWrite

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

the class IndexIT method shouldFailToCreateIndexWhereAConstraintAlreadyExists.

@Test
void shouldFailToCreateIndexWhereAConstraintAlreadyExists() throws Exception {
    // given
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        statement.uniquePropertyConstraintCreate(uniqueForSchema(schema).withName("constraint name"));
        commit();
    }
    var e = assertThrows(SchemaKernelException.class, () -> {
        SchemaWrite statement = schemaWriteInNewTransaction();
        statement.indexCreate(schema, "my index");
        commit();
    });
    assertEquals("There is a uniqueness constraint on (:" + LABEL + " {" + PROPERTY_KEY + "}), so an index is " + "already created that matches this.", e.getMessage());
    commit();
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 10 with SchemaWrite

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

the class IndexIT method shouldDisallowDroppingConstraintByNameThatDoesNotExist.

@Test
void shouldDisallowDroppingConstraintByNameThatDoesNotExist() throws KernelException {
    // given
    String constraintName = "my constraint";
    ConstraintDescriptor constraint;
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        constraint = statement.uniquePropertyConstraintCreate(uniqueForSchema(schema).withName("constraint name"));
        commit();
    }
    {
        SchemaWrite statement = schemaWriteInNewTransaction();
        statement.constraintDrop(constraint);
        commit();
    }
    // when
    SchemaWrite statement = schemaWriteInNewTransaction();
    SchemaKernelException e = assertThrows(SchemaKernelException.class, () -> statement.constraintDrop(constraintName));
    assertEquals("Unable to drop constraint `my constraint`: No such constraint my constraint.", e.getMessage());
    rollback();
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) 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