Search in sources :

Example 1 with IndexCreator

use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.

the class SchemaStorageTest method shouldReturnIndexRuleForLabelAndVeryManyPropertiesComposite.

@Test
public void shouldReturnIndexRuleForLabelAndVeryManyPropertiesComposite() throws Exception {
    String[] props = "abcdefghijklmnopqrstuvwxyzABCDEFGHJILKMNOPQRSTUVWXYZ".split("\\B");
    createSchema(db -> {
        IndexCreator indexCreator = db.schema().indexFor(Label.label(LABEL1));
        for (String prop : props) {
            indexCreator = indexCreator.on(prop);
        }
        indexCreator.create();
    });
    IndexRule rule = storage.indexGetForSchema(NewIndexDescriptorFactory.forLabel(labelId(LABEL1), Arrays.stream(props).mapToInt(this::propId).toArray()));
    assertNotNull(rule);
    assertTrue(SchemaDescriptorPredicates.hasLabel(rule, labelId(LABEL1)));
    for (String prop : props) {
        assertTrue(SchemaDescriptorPredicates.hasProperty(rule, propId(prop)));
    }
    assertEquals(NewIndexDescriptor.Type.GENERAL, rule.getIndexDescriptor().type());
}
Also used : IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) IndexCreator(org.neo4j.graphdb.schema.IndexCreator) Test(org.junit.Test)

Example 2 with IndexCreator

use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.

the class FulltextIndexProviderTest method indexWithAnalyzerProviderThatReturnsNullWillBeMarkedAsFailedOnStartup.

@ResourceLock("BrokenAnalyzerProvider")
@Test
void indexWithAnalyzerProviderThatReturnsNullWillBeMarkedAsFailedOnStartup() {
    BrokenAnalyzerProvider.shouldThrow = false;
    BrokenAnalyzerProvider.shouldReturnNull = false;
    try (Transaction tx = db.beginTx()) {
        IndexCreator creator = tx.schema().indexFor(label("Label")).withIndexType(org.neo4j.graphdb.schema.IndexType.FULLTEXT).withIndexConfiguration(Map.of(IndexSetting.fulltext_Analyzer(), BrokenAnalyzerProvider.NAME)).on("prop").withName(NAME);
        // The analyzer no longer returns null.
        creator.create();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexOnline(NAME, 1, TimeUnit.MINUTES);
        IndexDefinition index = tx.schema().getIndexByName(NAME);
        Schema.IndexState indexState = tx.schema().getIndexState(index);
        assertThat(indexState).isEqualTo(Schema.IndexState.ONLINE);
    }
    BrokenAnalyzerProvider.shouldReturnNull = true;
    controller.restartDbms();
    try (Transaction tx = db.beginTx()) {
        IndexDefinition index = tx.schema().getIndexByName(NAME);
        Schema.IndexState indexState = tx.schema().getIndexState(index);
        assertThat(indexState).isEqualTo(Schema.IndexState.FAILED);
        String indexFailure = tx.schema().getIndexFailure(index);
        assertThat(indexFailure).contains("null");
        index.drop();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
        tx.commit();
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Schema(org.neo4j.graphdb.schema.Schema) IndexCreator(org.neo4j.graphdb.schema.IndexCreator) Test(org.junit.jupiter.api.Test) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock)

Example 3 with IndexCreator

use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.

the class FulltextIndexProviderTest method indexWithAnalyzerThatReturnsNullWillNotBeCreated.

@ResourceLock("BrokenAnalyzerProvider")
@Test
void indexWithAnalyzerThatReturnsNullWillNotBeCreated() {
    BrokenAnalyzerProvider.shouldThrow = false;
    BrokenAnalyzerProvider.shouldReturnNull = true;
    try (Transaction tx = db.beginTx()) {
        IndexCreator creator = tx.schema().indexFor(label("Label")).withIndexType(org.neo4j.graphdb.schema.IndexType.FULLTEXT).withIndexConfiguration(Map.of(IndexSetting.fulltext_Analyzer(), BrokenAnalyzerProvider.NAME)).on("prop").withName(NAME);
        // Validation must initially prevent this index from being created.
        var e = assertThrows(RuntimeException.class, creator::create);
        assertThat(e.getMessage()).contains("null");
        // Create the index anyway.
        BrokenAnalyzerProvider.shouldReturnNull = false;
        creator.create();
        BrokenAnalyzerProvider.shouldReturnNull = true;
        // The analyzer will now return null during the index population, and the index should then enter a FAILED state.
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        var e = assertThrows(IllegalStateException.class, () -> tx.schema().awaitIndexOnline(NAME, 1, TimeUnit.MINUTES));
        assertThat(e.getMessage()).contains("FAILED");
        IndexDefinition index = tx.schema().getIndexByName(NAME);
        assertThat(tx.schema().getIndexState(index)).isEqualTo(Schema.IndexState.FAILED);
        index.drop();
        tx.commit();
    }
    BrokenAnalyzerProvider.shouldReturnNull = false;
    try (Transaction tx = db.beginTx()) {
        IndexCreator creator = tx.schema().indexFor(label("Label")).withIndexType(org.neo4j.graphdb.schema.IndexType.FULLTEXT).withIndexConfiguration(Map.of(IndexSetting.fulltext_Analyzer(), BrokenAnalyzerProvider.NAME)).on("prop").withName(NAME);
        // The analyzer no longer returns null.
        creator.create();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexOnline(NAME, 1, TimeUnit.MINUTES);
        IndexDefinition index = tx.schema().getIndexByName(NAME);
        Schema.IndexState indexState = tx.schema().getIndexState(index);
        assertThat(indexState).isEqualTo(Schema.IndexState.ONLINE);
    }
    controller.restartDbms();
    try (Transaction tx = db.beginTx()) {
        IndexDefinition index = tx.schema().getIndexByName(NAME);
        Schema.IndexState indexState = tx.schema().getIndexState(index);
        assertThat(indexState).isEqualTo(Schema.IndexState.ONLINE);
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Schema(org.neo4j.graphdb.schema.Schema) IndexCreator(org.neo4j.graphdb.schema.IndexCreator) Test(org.junit.jupiter.api.Test) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock)

Example 4 with IndexCreator

use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.

the class SchemaStorageIT method shouldReturnIndexRuleForLabelAndVeryManyPropertiesComposite.

@Test
void shouldReturnIndexRuleForLabelAndVeryManyPropertiesComposite() {
    String[] props = "abcdefghijklmnopqrstuvwxyzABCDEFGHJILKMNOPQRSTUVWXYZ".split("\\B");
    createSchema(tx -> {
        IndexCreator indexCreator = tx.schema().indexFor(Label.label(LABEL1));
        for (String prop : props) {
            indexCreator = indexCreator.on(prop);
        }
        indexCreator.create();
    });
    IndexDescriptor rule = single(storage.indexGetForSchema(TestIndexDescriptorFactory.forLabel(labelId(LABEL1), Arrays.stream(props).mapToInt(this::propId).toArray()), NULL));
    assertNotNull(rule);
    assertTrue(SchemaDescriptorPredicates.hasLabel(rule, labelId(LABEL1)));
    for (String prop : props) {
        assertTrue(SchemaDescriptorPredicates.hasProperty(rule, propId(prop)));
    }
    assertFalse(rule.isUnique());
}
Also used : IndexCreator(org.neo4j.graphdb.schema.IndexCreator) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 5 with IndexCreator

use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.

the class CompositeStringLengthValidationIT method createIndex.

private IndexDescriptor createIndex(String... keys) {
    IndexDefinition indexDefinition;
    try (Transaction tx = db.beginTx()) {
        IndexCreator indexCreator = tx.schema().indexFor(LABEL);
        for (String key : keys) {
            indexCreator = indexCreator.on(key);
        }
        indexDefinition = indexCreator.create();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(30, SECONDS);
        tx.commit();
    }
    return ((IndexDefinitionImpl) indexDefinition).getIndexReference();
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexCreator(org.neo4j.graphdb.schema.IndexCreator) IndexDefinitionImpl(org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl)

Aggregations

IndexCreator (org.neo4j.graphdb.schema.IndexCreator)29 Test (org.junit.jupiter.api.Test)17 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Transaction (org.neo4j.graphdb.Transaction)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)5 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)5 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)4 Schema (org.neo4j.graphdb.schema.Schema)4 Label (org.neo4j.graphdb.Label)2 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)2 Description (org.neo4j.procedure.Description)2 Procedure (org.neo4j.procedure.Procedure)2 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 ConstraintCreator (org.neo4j.graphdb.schema.ConstraintCreator)1 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)1