Search in sources :

Example 36 with IndexReadSession

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

the class IndexProvidedValuesNativeBTree10Test method shouldGetAllDoublePropertyValues.

@Test
void shouldGetAllDoublePropertyValues() throws Exception {
    IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_PRIP_INDEX));
    var values = getEntityControl().findValuePairs(tx, cursors, index);
    assertThat(values).as("index should return all double property values").containsExactlyInAnyOrderElementsOf(doublePropValues);
}
Also used : IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 37 with IndexReadSession

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

the class IndexProvidedValuesNativeBTree10Test method shouldGetAllSinglePropertyValues.

@Test
void shouldGetAllSinglePropertyValues() throws Exception {
    IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_INDEX));
    var values = getEntityControl().findValues(tx, cursors, index);
    assertThat(values).as("index should return all single property values").containsExactlyInAnyOrderElementsOf(singlePropValues);
}
Also used : IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 38 with IndexReadSession

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

the class KernelReadTracerTxStateTest method shouldTraceRelationshipIndexCursor.

@Test
void shouldTraceRelationshipIndexCursor() throws KernelException, TimeoutException {
    // 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();
    }
    try (KernelTransaction tx = beginTransaction()) {
        Predicates.awaitEx(() -> tx.schemaRead().indexGetState(index) == ONLINE, 1, MINUTES);
        long n1 = tx.dataWrite().nodeCreate();
        long n2 = tx.dataWrite().nodeCreate();
        long r = tx.dataWrite().relationshipCreate(n1, connection, n2);
        tx.dataWrite().relationshipSetProperty(r, name, Values.stringValue("transformational"));
        tx.commit();
    }
    // when
    TestKernelReadTracer tracer = new TestKernelReadTracer();
    try (KernelTransaction tx = beginTransaction();
        RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(NULL, tx.memoryTracker())) {
        cursor.setTracer(tracer);
        IndexReadSession indexReadSession = tx.dataRead().indexReadSession(index);
        tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch("transformational"));
        assertTrue(cursor.next());
        tracer.assertEvents(OnIndexSeek(), OnRelationship(cursor.relationshipReference()));
        assertFalse(cursor.next());
        tracer.assertEvents();
    }
}
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 39 with IndexReadSession

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

the class FulltextProcedures method queryFulltextForRelationships.

@SystemProcedure
@Description("Query the given full-text index. Returns the matching relationships, and their Lucene query score, ordered by score. " + "Valid keys for the options map are: 'skip' to skip the top N results; 'limit' to limit the number of results returned.")
@Procedure(name = "db.index.fulltext.queryRelationships", mode = READ)
public Stream<RelationshipOutput> queryFulltextForRelationships(@Name("indexName") String name, @Name("queryString") String query, @Name(value = "options", defaultValue = "{}") Map<String, Object> options) throws Exception {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    IndexDescriptor indexReference = getValidIndex(name);
    awaitOnline(indexReference);
    EntityType entityType = indexReference.schema().entityType();
    if (entityType != RELATIONSHIP) {
        throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for relationships.");
    }
    RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
    IndexReadSession indexReadSession = tx.dataRead().indexReadSession(indexReference);
    IndexQueryConstraints constraints = queryConstraints(options);
    tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
    Spliterator<RelationshipOutput> spliterator = new SpliteratorAdaptor<>() {

        @Override
        public boolean tryAdvance(Consumer<? super RelationshipOutput> action) {
            while (cursor.next()) {
                long relationshipReference = cursor.relationshipReference();
                float score = cursor.score();
                RelationshipOutput relationshipOutput = RelationshipOutput.forExistingEntityOrNull(transaction, relationshipReference, score);
                if (relationshipOutput != null) {
                    action.accept(relationshipOutput);
                    return true;
                }
            }
            cursor.close();
            return false;
        }
    };
    return StreamSupport.stream(spliterator, false).onClose(cursor::close);
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) EntityType(org.neo4j.common.EntityType) Consumer(java.util.function.Consumer) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 40 with IndexReadSession

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

the class IndexStatisticsTest method assertIndexedNodesMatchesStoreNodes.

private void assertIndexedNodesMatchesStoreNodes(IndexDescriptor index) throws Exception {
    int nodesInStore = 0;
    Label label = Label.label(PERSON_LABEL);
    try (Transaction transaction = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) transaction).kernelTransaction();
        List<String> mismatches = new ArrayList<>();
        int propertyKeyId = ktx.tokenRead().propertyKey(NAME_PROPERTY);
        IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
        try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            // Node --> Index
            for (Node node : filter(n -> n.hasLabel(label) && n.hasProperty(NAME_PROPERTY), transaction.getAllNodes())) {
                nodesInStore++;
                String name = (String) node.getProperty(NAME_PROPERTY);
                ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.exact(propertyKeyId, name));
                boolean found = false;
                while (cursor.next()) {
                    long indexedNode = cursor.nodeReference();
                    if (indexedNode == node.getId()) {
                        if (found) {
                            mismatches.add("Index has multiple entries for " + name + " and " + indexedNode);
                        }
                        found = true;
                    }
                }
                if (!found) {
                    mismatches.add("Index is missing entry for " + name + " " + node);
                }
            }
            if (!mismatches.isEmpty()) {
                fail(String.join(format("%n"), mismatches));
            }
            // Node count == indexed node count
            ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.exists(propertyKeyId));
            int nodesInIndex = 0;
            while (cursor.next()) {
                nodesInIndex++;
            }
            assertEquals(nodesInStore, nodesInIndex);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) SchemaDescriptor.forLabel(org.neo4j.internal.schema.SchemaDescriptor.forLabel) ArrayList(java.util.ArrayList) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) 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)

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