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());
}
}
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"));
}
}
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();
}
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();
}
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();
}
Aggregations