Search in sources :

Example 21 with IndexPrototype

use of org.neo4j.internal.schema.IndexPrototype 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 22 with IndexPrototype

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

the class PlainOperationsTest method indexedBackedConstraintCreateMustThrowOnNonUniqueIndexPrototypes.

@Test
void indexedBackedConstraintCreateMustThrowOnNonUniqueIndexPrototypes() throws Exception {
    // given
    when(tokenHolders.labelTokens().getTokenById(anyInt())).thenReturn(new NamedToken("Label", 123));
    when(tokenHolders.propertyKeyTokens().getTokenById(anyInt())).thenReturn(new NamedToken("prop", 456));
    IndexPrototype prototype = IndexPrototype.forSchema(schema).withName("constraint name").withIndexProvider(GenericNativeIndexProvider.DESCRIPTOR);
    IndexDescriptor constraintIndex = prototype.materialise(42);
    when(constraintIndexCreator.createUniquenessConstraintIndex(any(), any(), eq(prototype))).thenReturn(constraintIndex);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexProxy.getDescriptor()).thenReturn(constraintIndex);
    when(indexingService.getIndexProxy(constraintIndex)).thenReturn(indexProxy);
    when(storageReader.constraintsGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    when(storageReader.indexGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    // when
    var e = assertThrows(KernelException.class, () -> operations.uniquePropertyConstraintCreate(prototype));
    assertThat(e.getUserMessage(tokenHolders)).containsIgnoringCase("index prototype").containsIgnoringCase("not unique");
}
Also used : IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) NamedToken(org.neo4j.token.api.NamedToken) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 23 with IndexPrototype

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

the class PlainOperationsTest method shouldAcquireSchemaWriteLockBeforeCreatingUniquenessConstraint.

@Test
void shouldAcquireSchemaWriteLockBeforeCreatingUniquenessConstraint() throws Exception {
    // given
    IndexPrototype prototype = IndexPrototype.uniqueForSchema(schema).withName("constraint name").withIndexProvider(GenericNativeIndexProvider.DESCRIPTOR);
    IndexDescriptor constraintIndex = prototype.materialise(42);
    when(constraintIndexCreator.createUniquenessConstraintIndex(any(), any(), eq(prototype))).thenReturn(constraintIndex);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexProxy.getDescriptor()).thenReturn(constraintIndex);
    when(indexingService.getIndexProxy(constraintIndex)).thenReturn(indexProxy);
    when(storageReader.constraintsGetForSchema(schema.schema())).thenReturn(Collections.emptyIterator());
    when(storageReader.indexGetForSchema(schema.schema())).thenReturn(Collections.emptyIterator());
    // when
    operations.uniquePropertyConstraintCreate(prototype);
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.LABEL, schema.getLabelId());
    order.verify(txState).constraintDoAdd(ConstraintDescriptorFactory.uniqueForSchema(schema), constraintIndex);
}
Also used : IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 24 with IndexPrototype

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

the class SchemaStorageIT method makeIndexRuleForConstraint.

private IndexDescriptor makeIndexRuleForConstraint(long ruleId, String label, String propertyKey, long constraintId) {
    LabelSchemaDescriptor schema = forLabel(labelId(label), propId(propertyKey));
    IndexPrototype prototype = uniqueForSchema(schema, GenericNativeIndexProvider.DESCRIPTOR);
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
    prototype = prototype.withName(SchemaRule.generateName(constraint, new String[] { label }, new String[] { propertyKey }));
    return prototype.materialise(ruleId).withOwningConstraintId(constraintId);
}
Also used : IndexPrototype(org.neo4j.internal.schema.IndexPrototype) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)

Example 25 with IndexPrototype

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

the class SchemaStorageIT method makeIndexRule.

private IndexDescriptor makeIndexRule(long ruleId, String label, String propertyKey) {
    LabelSchemaDescriptor schema = forLabel(labelId(label), propId(propertyKey));
    IndexPrototype prototype = forSchema(schema, GenericNativeIndexProvider.DESCRIPTOR);
    prototype = prototype.withName(SchemaRule.generateName(prototype, new String[] { label }, new String[] { propertyKey }));
    return prototype.materialise(ruleId);
}
Also used : IndexPrototype(org.neo4j.internal.schema.IndexPrototype) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor)

Aggregations

IndexPrototype (org.neo4j.internal.schema.IndexPrototype)45 Test (org.junit.jupiter.api.Test)25 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)24 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)16 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)9 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)8 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)7 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)7 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)7 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)5 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)4 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)3 IndexPopulator (org.neo4j.kernel.api.index.IndexPopulator)3 NamedToken (org.neo4j.token.api.NamedToken)3 KernelException (org.neo4j.exceptions.KernelException)2 Transaction (org.neo4j.graphdb.Transaction)2 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)2 RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)2 IndexConfig (org.neo4j.internal.schema.IndexConfig)2 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)2