Search in sources :

Example 16 with NodeValueIndexCursor

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

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

the class LockingNodeUniqueIndexSeekTest method shouldHoldSharedIndexLockIfNodeIsExists.

@Test
void shouldHoldSharedIndexLockIfNodeIsExists() throws Exception {
    // given
    NodeValueIndexCursor cursor = mock(NodeValueIndexCursor.class);
    when(cursor.next()).thenReturn(true);
    when(cursor.nodeReference()).thenReturn(42L);
    // when
    long nodeId = LockingNodeUniqueIndexSeek.apply(locks, LockTracer.NONE, cursor, uniqueNodeIndexSeeker, read, index, predicate);
    // then
    assertEquals(42L, nodeId);
    verify(locks).acquireShared(LockTracer.NONE, INDEX_ENTRY, resourceId);
    verifyNoMoreInteractions(locks);
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Test(org.junit.jupiter.api.Test)

Example 18 with NodeValueIndexCursor

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

the class LockingNodeUniqueIndexSeekTest method shouldHoldSharedIndexLockIfNodeIsConcurrentlyCreated.

@Test
void shouldHoldSharedIndexLockIfNodeIsConcurrentlyCreated() throws Exception {
    // given
    NodeValueIndexCursor cursor = mock(NodeValueIndexCursor.class);
    when(cursor.next()).thenReturn(false, true);
    when(cursor.nodeReference()).thenReturn(42L);
    // when
    long nodeId = LockingNodeUniqueIndexSeek.apply(locks, LockTracer.NONE, cursor, uniqueNodeIndexSeeker, read, index, predicate);
    // then
    assertEquals(42L, nodeId);
    order.verify(locks).acquireShared(LockTracer.NONE, INDEX_ENTRY, resourceId);
    order.verify(locks).releaseShared(INDEX_ENTRY, resourceId);
    order.verify(locks).acquireExclusive(LockTracer.NONE, INDEX_ENTRY, resourceId);
    order.verify(locks).acquireShared(LockTracer.NONE, INDEX_ENTRY, resourceId);
    order.verify(locks).releaseExclusive(INDEX_ENTRY, resourceId);
    verifyNoMoreInteractions(locks);
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Test(org.junit.jupiter.api.Test)

Example 19 with NodeValueIndexCursor

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

the class LockingNodeUniqueIndexSeekTest method shouldHoldExclusiveIndexLockIfNodeDoesNotExist.

@Test
void shouldHoldExclusiveIndexLockIfNodeDoesNotExist() throws Exception {
    // given
    NodeValueIndexCursor cursor = mock(NodeValueIndexCursor.class);
    when(cursor.next()).thenReturn(false, false);
    when(cursor.nodeReference()).thenReturn(-1L);
    // when
    long nodeId = LockingNodeUniqueIndexSeek.apply(locks, LockTracer.NONE, cursor, uniqueNodeIndexSeeker, read, index, predicate);
    // then
    assertEquals(-1L, nodeId);
    order.verify(locks).acquireShared(LockTracer.NONE, INDEX_ENTRY, resourceId);
    order.verify(locks).releaseShared(INDEX_ENTRY, resourceId);
    order.verify(locks).acquireExclusive(LockTracer.NONE, INDEX_ENTRY, resourceId);
    verifyNoMoreInteractions(locks);
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Test(org.junit.jupiter.api.Test)

Example 20 with NodeValueIndexCursor

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

the class CompositeStringLengthValidationIT method shouldHandleCompositeSizesCloseToTheLimit.

@Test
void shouldHandleCompositeSizesCloseToTheLimit() throws KernelException {
    String firstSlot = random.nextAlphaNumericString(firstSlotLength, firstSlotLength);
    String secondSlot = random.nextAlphaNumericString(secondSlotLength, secondSlotLength);
    // given
    IndexDescriptor index = createIndex(KEY, KEY2);
    Node node;
    try (Transaction tx = db.beginTx()) {
        node = tx.createNode(LABEL);
        node.setProperty(KEY, firstSlot);
        node.setProperty(KEY2, secondSlot);
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        int propertyKeyId1 = ktx.tokenRead().propertyKey(KEY);
        int propertyKeyId2 = ktx.tokenRead().propertyKey(KEY2);
        try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            IndexReadSession indexReadSession = ktx.dataRead().indexReadSession(index);
            ktx.dataRead().nodeIndexSeek(indexReadSession, cursor, unconstrained(), PropertyIndexQuery.exact(propertyKeyId1, firstSlot), PropertyIndexQuery.exact(propertyKeyId2, secondSlot));
            assertTrue(cursor.next());
            assertEquals(node.getId(), cursor.nodeReference());
            assertFalse(cursor.next());
        }
        tx.commit();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Node(org.neo4j.graphdb.Node) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Aggregations

NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)42 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)25 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)20 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)20 Test (org.junit.jupiter.api.Test)18 Transaction (org.neo4j.graphdb.Transaction)16 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)16 ArrayList (java.util.ArrayList)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 TokenRead (org.neo4j.internal.kernel.api.TokenRead)7 Read (org.neo4j.internal.kernel.api.Read)6 Value (org.neo4j.values.storable.Value)6 KernelException (org.neo4j.exceptions.KernelException)3 Node (org.neo4j.graphdb.Node)3 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)2 Label (org.neo4j.graphdb.Label)2 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)2 NodeIndexCursor (org.neo4j.internal.kernel.api.NodeIndexCursor)2 PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)2