Search in sources :

Example 6 with RelationshipValueIndexCursor

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

the class TransactionImpl method relationshipsByTypeAndProperties.

private ResourceIterator<Relationship> relationshipsByTypeAndProperties(KernelTransaction tx, int typeId, PropertyIndexQuery.ExactPredicate... queries) {
    Read read = tx.dataRead();
    if (isInvalidQuery(typeId, queries)) {
        return emptyResourceIterator();
    }
    int[] propertyIds = getPropertyIds(queries);
    IndexDescriptor index = findUsableMatchingCompositeIndex(tx, SchemaDescriptor.forRelType(typeId, propertyIds), propertyIds, () -> tx.schemaRead().indexesGetForRelationshipType(typeId));
    if (index != IndexDescriptor.NO_INDEX) {
        try {
            RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.relationshipIndexSeek(indexSession, cursor, unconstrained(), getReorderedIndexQueries(index.schema().getPropertyIds(), queries));
            return new CursorIterator<>(cursor, RelationshipIndexCursor::relationshipReference, c -> newRelationshipEntity(c.relationshipReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a label scan
        }
    }
    return getRelationshipsByTypeAndPropertyWithoutPropertyIndex(tx, typeId, queries);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) KernelException(org.neo4j.exceptions.KernelException) RelationshipIndexCursor(org.neo4j.internal.kernel.api.RelationshipIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 7 with RelationshipValueIndexCursor

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

the class RelationshipIndexTransactionStateTest method assertEntityAndValueForSeek.

@Override
void assertEntityAndValueForSeek(Set<Pair<Long, Value>> expected, KernelTransaction tx, IndexDescriptor index, boolean needsValues, Object anotherValueFoundByQuery, PropertyIndexQuery... queries) throws Exception {
    try (RelationshipValueIndexCursor relationships = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker())) {
        IndexReadSession indexSession = tx.dataRead().indexReadSession(index);
        tx.dataRead().relationshipIndexSeek(indexSession, relationships, unordered(needsValues), queries);
        assertEntityAndValue(expected, tx, needsValues, anotherValueFoundByQuery, new RelationshipCursorAdapter(relationships));
    }
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 8 with RelationshipValueIndexCursor

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

the class DefaultPooledCursorsTestBase method shouldReuseRelationshipIndexCursors.

@Test
void shouldReuseRelationshipIndexCursors() throws Exception {
    // given
    int connection;
    int name;
    String indexName = "myIndex";
    IndexDescriptor index;
    try (KernelTransaction tx = beginTransaction()) {
        connection = tx.tokenWrite().relationshipTypeGetOrCreateForName("Connection");
        name = tx.tokenWrite().propertyKeyGetOrCreateForName("name");
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.RELATIONSHIP, array(connection), array(name));
        IndexPrototype prototype = IndexPrototype.forSchema(schema, DESCRIPTOR).withName(indexName).withIndexType(IndexType.FULLTEXT);
        index = tx.schemaWrite().indexCreate(prototype);
        tx.commit();
    }
    Predicates.awaitEx(() -> tx.schemaRead().indexGetState(index) == ONLINE, 1, MINUTES);
    RelationshipValueIndexCursor c1 = cursors.allocateRelationshipValueIndexCursor(NULL, EmptyMemoryTracker.INSTANCE);
    IndexReadSession indexSession = tx.dataRead().indexReadSession(index);
    read.relationshipIndexSeek(indexSession, c1, IndexQueryConstraints.unconstrained(), PropertyIndexQuery.fulltextSearch("hello"));
    c1.close();
    RelationshipValueIndexCursor c2 = cursors.allocateRelationshipValueIndexCursor(NULL, EmptyMemoryTracker.INSTANCE);
    assertThat(c1).isSameAs(c2);
    c2.close();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 9 with RelationshipValueIndexCursor

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

the class RecoveryIT method recoverDatabaseWithRelationshipIndex.

@Test
void recoverDatabaseWithRelationshipIndex() throws Throwable {
    GraphDatabaseService database = createDatabase();
    int numberOfRelationships = 10;
    RelationshipType type = RelationshipType.withName("TYPE");
    String property = "prop";
    String indexName = "my index";
    try (Transaction transaction = database.beginTx()) {
        transaction.schema().indexFor(type).on(property).withIndexType(IndexType.FULLTEXT).withName(indexName).create();
        transaction.commit();
    }
    awaitIndexesOnline(database);
    try (Transaction transaction = database.beginTx()) {
        Node start = transaction.createNode();
        Node stop = transaction.createNode();
        for (int i = 0; i < numberOfRelationships; i++) {
            Relationship relationship = start.createRelationshipTo(stop, type);
            relationship.setProperty(property, "value");
        }
        transaction.commit();
    }
    managementService.shutdown();
    RecoveryHelpers.removeLastCheckpointRecordFromLastLogFile(databaseLayout, fileSystem);
    recoverDatabase();
    GraphDatabaseAPI recoveredDatabase = createDatabase();
    awaitIndexesOnline(recoveredDatabase);
    try (Transaction transaction = recoveredDatabase.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) transaction).kernelTransaction();
        IndexDescriptor index = ktx.schemaRead().indexGetForName(indexName);
        IndexReadSession indexReadSession = ktx.dataRead().indexReadSession(index);
        int relationshipsInIndex = 0;
        try (RelationshipValueIndexCursor cursor = ktx.cursors().allocateRelationshipValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            ktx.dataRead().relationshipIndexSeek(indexReadSession, cursor, unconstrained(), fulltextSearch("*"));
            while (cursor.next()) {
                relationshipsInIndex++;
            }
        }
        assertEquals(numberOfRelationships, relationshipsInIndex);
    } finally {
        managementService.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.jupiter.api.Test)

Example 10 with RelationshipValueIndexCursor

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

the class KernelReadTracerTest method shouldTraceRelationshipIndexSeek.

@Test
void shouldTraceRelationshipIndexSeek() throws KernelException {
    // given
    TestKernelReadTracer tracer = new TestKernelReadTracer();
    try (RelationshipValueIndexCursor cursor = cursors.allocateRelationshipValueIndexCursor(NULL, INSTANCE)) {
        int p1 = token.propertyKey("p1");
        IndexReadSession session = read.indexReadSession(relIndex);
        assertRelationshipIndexSeekTracing(tracer, cursor, session, IndexOrder.NONE, p1);
        assertRelationshipIndexSeekTracing(tracer, cursor, session, IndexOrder.ASCENDING, p1);
    }
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Aggregations

RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)13 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)12 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)9 Test (org.junit.jupiter.api.Test)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)5 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)3 Transaction (org.neo4j.graphdb.Transaction)2 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)2 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)2 Consumer (java.util.function.Consumer)1 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)1 EntityType (org.neo4j.common.EntityType)1 KernelException (org.neo4j.exceptions.KernelException)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Node (org.neo4j.graphdb.Node)1 Relationship (org.neo4j.graphdb.Relationship)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)1 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)1 Read (org.neo4j.internal.kernel.api.Read)1