Search in sources :

Example 31 with IndexPrototype

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

the class RecordStorageReaderTestBase method createIndex.

protected IndexDescriptor createIndex(RelationshipType relType, String propertyKey) throws Exception {
    TxState txState = new TxState();
    int relTypeId = getOrCreateRelationshipTypeId(relType);
    int propertyKeyId = getOrCreatePropertyKeyId(propertyKey);
    long id = commitContext.reserveSchema();
    IndexPrototype prototype = IndexPrototype.forSchema(forRelType(relTypeId, propertyKeyId)).withName("index_" + id);
    IndexDescriptor index = prototype.materialise(id);
    txState.indexDoAdd(index);
    apply(txState);
    return index;
}
Also used : TxState(org.neo4j.kernel.impl.api.state.TxState) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 32 with IndexPrototype

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

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

the class IndexIT method shouldListCompositeMultiTokenRelationshipIndexesInTheCoreAPI.

@Test
void shouldListCompositeMultiTokenRelationshipIndexesInTheCoreAPI() throws Exception {
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
    SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.RELATIONSHIP, new int[] { relType, relType2 }, new int[] { propertyKeyId, propertyKeyId2 });
    IndexPrototype prototype = IndexPrototype.forSchema(schema, FulltextIndexProviderFactory.DESCRIPTOR).withIndexType(IndexType.FULLTEXT).withName("index name");
    transaction.schemaWrite().indexCreate(prototype);
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        // then
        Set<IndexDefinition> indexes = Iterables.asSet(tx.schema().getIndexes());
        assertThat(indexes.size()).isEqualTo(initialIndexCount + 1);
        IndexDefinition index = tx.schema().getIndexByName("index name");
        assertThrows(IllegalStateException.class, index::getLabels);
        assertThat(index.getRelationshipTypes()).containsOnly(withName(REL_TYPE), withName(REL_TYPE2));
        assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY, PROPERTY_KEY2);
        assertFalse(index.isConstraintIndex(), "should not be a constraint index");
        assertTrue(index.isMultiTokenIndex(), "should be a multi-token index");
        assertTrue(index.isCompositeIndex(), "should be a composite index");
        assertFalse(index.isNodeIndex(), "should not be a node index");
        assertTrue(index.isRelationshipIndex(), "should be a relationship index");
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 34 with IndexPrototype

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

the class IndexPopulationJobTest method tracePageCacheAccessIndexWithOneRelationship.

@Test
void tracePageCacheAccessIndexWithOneRelationship() {
    String value = "value";
    long nodeId = createNode(map(name, value), FIRST);
    long relationship = createRelationship(map(name, age), likes, nodeId, nodeId);
    int rel = tokenHolders.relationshipTypeTokens().getIdByName(likes.name());
    int prop = tokenHolders.propertyKeyTokens().getIdByName(name);
    IndexPrototype descriptor = IndexPrototype.forSchema(SchemaDescriptor.forRelType(rel, prop));
    IndexPopulator actualPopulator = indexPopulator(descriptor);
    TrackingIndexPopulator populator = new TrackingIndexPopulator(actualPopulator);
    var pageCacheTracer = new DefaultPageCacheTracer();
    IndexPopulationJob job = newIndexPopulationJob(populator, new FlippableIndexProxy(), EntityType.RELATIONSHIP, descriptor, pageCacheTracer);
    job.run();
    IndexEntryUpdate<?> update = IndexEntryUpdate.add(relationship, descriptor, Values.of(age));
    assertTrue(populator.created);
    assertEquals(Collections.singletonList(update), populator.includedSamples);
    assertEquals(1, populator.adds.size());
    assertTrue(populator.resultSampled);
    assertTrue(populator.closeCall);
    assertThat(pageCacheTracer.pins()).isEqualTo(17);
    assertThat(pageCacheTracer.unpins()).isEqualTo(17);
    assertThat(pageCacheTracer.hits()).isEqualTo(16);
    assertThat(pageCacheTracer.faults()).isEqualTo(1);
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Test(org.junit.jupiter.api.Test)

Example 35 with IndexPrototype

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

the class IndexPopulationJobTest method shouldPopulateIndexWithOneRelationship.

@Test
void shouldPopulateIndexWithOneRelationship() {
    // GIVEN
    String value = "Taylor";
    long nodeId = createNode(map(name, value), FIRST);
    long relationship = createRelationship(map(name, age), likes, nodeId, nodeId);
    int relType = tokenHolders.relationshipTypeTokens().getIdByName(likes.name());
    int propertyId = tokenHolders.propertyKeyTokens().getIdByName(name);
    IndexPrototype descriptor = IndexPrototype.forSchema(SchemaDescriptor.forRelType(relType, propertyId));
    IndexPopulator actualPopulator = indexPopulator(descriptor);
    TrackingIndexPopulator populator = new TrackingIndexPopulator(actualPopulator);
    IndexPopulationJob job = newIndexPopulationJob(populator, new FlippableIndexProxy(), EntityType.RELATIONSHIP, descriptor);
    // WHEN
    job.run();
    // THEN
    IndexEntryUpdate<?> update = IndexEntryUpdate.add(relationship, descriptor, Values.of(age));
    assertTrue(populator.created);
    assertEquals(Collections.singletonList(update), populator.includedSamples);
    assertEquals(1, populator.adds.size());
    assertTrue(populator.resultSampled);
    assertTrue(populator.closeCall);
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Test(org.junit.jupiter.api.Test)

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