Search in sources :

Example 46 with IndexReadSession

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

the class TransactionImpl method relationshipsByTypeAndProperty.

private ResourceIterator<Relationship> relationshipsByTypeAndProperty(KernelTransaction transaction, int typeId, PropertyIndexQuery query) {
    Read read = transaction.dataRead();
    if (query.propertyKeyId() == TokenRead.NO_TOKEN || typeId == TokenRead.NO_TOKEN) {
        return emptyResourceIterator();
    }
    var index = findUsableMatchingIndex(transaction, SchemaDescriptor.forRelType(typeId, query.propertyKeyId()));
    if (index != IndexDescriptor.NO_INDEX) {
        // Ha! We found an index - let's use it to find matching relationships
        try {
            var cursor = transaction.cursors().allocateRelationshipValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.relationshipIndexSeek(indexSession, cursor, unconstrained(), query);
            return new CursorIterator<>(cursor, RelationshipIndexCursor::relationshipReference, c -> newRelationshipEntity(c.relationshipReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a type scan
        }
    }
    return getRelationshipsByTypeAndPropertyWithoutPropertyIndex(transaction, typeId, query);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) CursorIterator(org.neo4j.kernel.impl.coreapi.internal.CursorIterator) 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 47 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession 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 48 with IndexReadSession

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

the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method countIndexedNodes.

private static int countIndexedNodes(GraphDatabaseAPI db, Label label, String... keys) throws Exception {
    try (InternalTransaction tx = (InternalTransaction) db.beginTx()) {
        KernelTransaction ktx = tx.kernelTransaction();
        TokenRead tokenRead = ktx.tokenRead();
        int labelId = tokenRead.nodeLabel(label.name());
        int[] propertyKeyIds = new int[keys.length];
        for (int i = 0; i < propertyKeyIds.length; i++) {
            propertyKeyIds[i] = tokenRead.propertyKey(keys[i]);
        }
        PropertyIndexQuery[] predicates = new PropertyIndexQuery[propertyKeyIds.length];
        for (int i = 0; i < propertyKeyIds.length; i++) {
            predicates[i] = PropertyIndexQuery.exists(propertyKeyIds[i]);
        }
        IndexDescriptor index = single(ktx.schemaRead().index(SchemaDescriptor.forLabel(labelId, propertyKeyIds)));
        IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
        int count = 0;
        try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), predicates);
            while (cursor.next()) {
                count++;
            }
        }
        tx.commit();
        return count;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 49 with IndexReadSession

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

the class ReadTracingIT method noPageCacheTracingAvailableOnRelationshipIndexSeek.

@Test
void noPageCacheTracingAvailableOnRelationshipIndexSeek() throws KernelException {
    createRelationshipIndex();
    try (Transaction tx = database.beginTx()) {
        var source = tx.createNode(label);
        var target = tx.createNode(label);
        var relationship = source.createRelationshipTo(target, type);
        relationship.setProperty(property, testPropertyValue);
        tx.commit();
    }
    try (InternalTransaction transaction = (InternalTransaction) database.beginTx()) {
        var kernelTransaction = transaction.kernelTransaction();
        var dataRead = kernelTransaction.dataRead();
        var indexDescriptor = kernelTransaction.schemaRead().indexGetForName(indexName);
        IndexReadSession indexReadSession = kernelTransaction.dataRead().indexReadSession(indexDescriptor);
        var cursorContext = kernelTransaction.cursorContext();
        assertZeroCursor(cursorContext);
        try (var cursor = kernelTransaction.cursors().allocateRelationshipValueIndexCursor(kernelTransaction.cursorContext(), kernelTransaction.memoryTracker())) {
            dataRead.relationshipIndexSeek(indexReadSession, cursor, unconstrained(), fulltextSearch(testPropertyValue));
            consumeCursor(cursor);
        }
        assertZeroCursor(cursorContext);
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 50 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession 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)

Aggregations

IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)93 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)47 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)22 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)20 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)17 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)15 ArrayList (java.util.ArrayList)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EnumSource (org.junit.jupiter.params.provider.EnumSource)12 RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)12 IndexValueCapability (org.neo4j.internal.schema.IndexValueCapability)12 Pair (org.neo4j.internal.helpers.collection.Pair)11 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)10 Transaction (org.neo4j.graphdb.Transaction)9 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)9 IndexOrderCapability (org.neo4j.internal.schema.IndexOrderCapability)7 Read (org.neo4j.internal.kernel.api.Read)6 TokenRead (org.neo4j.internal.kernel.api.TokenRead)6 PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)5