Search in sources :

Example 56 with IndexReadSession

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

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

the class TransactionImpl method nodesByLabelAndProperties.

private ResourceIterator<Node> nodesByLabelAndProperties(KernelTransaction transaction, int labelId, PropertyIndexQuery.ExactPredicate... queries) {
    Read read = transaction.dataRead();
    if (isInvalidQuery(labelId, queries)) {
        return emptyResourceIterator();
    }
    int[] propertyIds = getPropertyIds(queries);
    IndexDescriptor index = findUsableMatchingCompositeIndex(transaction, SchemaDescriptor.forLabel(labelId, propertyIds), propertyIds, () -> transaction.schemaRead().indexesGetForLabel(labelId));
    if (index != IndexDescriptor.NO_INDEX) {
        try {
            NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.nodeIndexSeek(indexSession, cursor, unconstrained(), getReorderedIndexQueries(index.schema().getPropertyIds(), queries));
            return new CursorIterator<>(cursor, NodeIndexCursor::nodeReference, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a label scan
        }
    }
    return getNodesByLabelAndPropertyWithoutPropertyIndex(transaction, labelId, queries);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) NodeIndexCursor(org.neo4j.internal.kernel.api.NodeIndexCursor) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) 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) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 58 with IndexReadSession

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

the class TransactionImpl method nodesByLabelAndProperty.

private ResourceIterator<Node> nodesByLabelAndProperty(KernelTransaction transaction, int labelId, PropertyIndexQuery query) {
    Read read = transaction.dataRead();
    if (query.propertyKeyId() == TokenRead.NO_TOKEN || labelId == TokenRead.NO_TOKEN) {
        return emptyResourceIterator();
    }
    var index = findUsableMatchingIndex(transaction, SchemaDescriptor.forLabel(labelId, query.propertyKeyId()));
    if (index != IndexDescriptor.NO_INDEX) {
        // Ha! We found an index - let's use it to find matching nodes
        try {
            NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
            IndexReadSession indexSession = read.indexReadSession(index);
            read.nodeIndexSeek(indexSession, cursor, unconstrained(), query);
            return new CursorIterator<>(cursor, NodeIndexCursor::nodeReference, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker);
        } catch (KernelException e) {
        // weird at this point but ignore and fallback to a label scan
        }
    }
    return getNodesByLabelAndPropertyWithoutPropertyIndex(transaction, labelId, query);
}
Also used : SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) NodeIndexCursor(org.neo4j.internal.kernel.api.NodeIndexCursor) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) 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) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 59 with IndexReadSession

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

the class IndexOrderTestBase method shouldNodeIndexScanInOrderPointsOnly.

@ParameterizedTest
@EnumSource(value = IndexOrder.class, names = { "ASCENDING", "DESCENDING" })
void shouldNodeIndexScanInOrderPointsOnly(IndexOrder indexOrder) throws Exception {
    List<Pair<Long, Value>> expected = new ArrayList<>();
    try (KernelTransaction tx = beginTransaction()) {
        expected.add(entityWithProp(tx, pointValue(Cartesian, -500000, -500000)));
        expected.add(entityWithProp(tx, pointValue(Cartesian, 500000, -500000)));
        expected.add(entityWithProp(tx, pointValue(Cartesian, -500000, 500000)));
        expected.add(entityWithProp(tx, pointValue(Cartesian, 500000, 500000)));
        tx.commit();
    }
    createIndex();
    // when
    try (KernelTransaction tx = beginTransaction()) {
        IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
        try (var cursor = getEntityValueIndexCursor(tx)) {
            expected.add(entityWithProp(tx, pointValue(Cartesian, -400000, -400000)));
            expected.add(entityWithProp(tx, pointValue(Cartesian, 400000, -400000)));
            expected.add(entityWithProp(tx, pointValue(Cartesian, -400000, 400000)));
            expected.add(entityWithProp(tx, pointValue(Cartesian, 400000, 400000)));
            entityIndexScan(tx, index, cursor, constrained(indexOrder, true));
            assertResultsInOrder(expected, cursor, indexOrder);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ArrayList(java.util.ArrayList) Pair(org.neo4j.internal.helpers.collection.Pair) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 60 with IndexReadSession

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

the class IndexOrderTestBase method shouldNodeCompositeIndexScanInOrderWithPointsAndSingleNodeAfterwards.

@ParameterizedTest
@EnumSource(value = IndexOrder.class, names = { "ASCENDING", "DESCENDING" })
void shouldNodeCompositeIndexScanInOrderWithPointsAndSingleNodeAfterwards(IndexOrder indexOrder) throws Exception {
    List<Pair<Long, Value[]>> expected = new ArrayList<>();
    try (KernelTransaction tx = beginTransaction()) {
        expected.add(entityWithTwoProps(tx, new String[] { "a" }, new String[] { "b" }));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, -500000), "a"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, -500000), "a"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, 500000), "a"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, 500000), "a"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, -500000), "b"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, -500000), "b"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, 500000), "b"));
        expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, 500000), "b"));
        expected.add(entityWithTwoProps(tx, "a", pointValue(Cartesian, 500000, 500000)));
        expected.add(entityWithTwoProps(tx, "b", new String[] { "b" }));
        expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, -500000, -500000)));
        expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, 500000, -500000)));
        expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, -500000, 500000)));
        expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, 500000, 500000)));
        expected.add(entityWithTwoProps(tx, "c", new String[] { "b" }));
        tx.commit();
    }
    createCompositeIndex();
    // when
    try (KernelTransaction tx = beginTransaction()) {
        IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
        try (var cursor = getEntityValueIndexCursor(tx)) {
            entityIndexScan(tx, index, cursor, constrained(indexOrder, true));
            assertCompositeResultsInOrder(expected, cursor, indexOrder);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Value(org.neo4j.values.storable.Value) Values.pointValue(org.neo4j.values.storable.Values.pointValue) TextValue(org.neo4j.values.storable.TextValue) Values.stringValue(org.neo4j.values.storable.Values.stringValue) PointValue(org.neo4j.values.storable.PointValue) ArrayList(java.util.ArrayList) Pair(org.neo4j.internal.helpers.collection.Pair) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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