Search in sources :

Example 96 with NodeCursor

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

the class SchemaCalculator method scanEverythingBelongingToNodes.

private void scanEverythingBelongingToNodes(NodeMappings nodeMappings, CursorContext cursorContext, MemoryTracker memoryTracker) {
    try (NodeCursor nodeCursor = cursors.allocateNodeCursor(cursorContext);
        PropertyCursor propertyCursor = cursors.allocatePropertyCursor(cursorContext, memoryTracker)) {
        dataRead.allNodesScan(nodeCursor);
        while (nodeCursor.next()) {
            // each node
            SortedLabels labels = SortedLabels.from(nodeCursor.labels());
            nodeCursor.properties(propertyCursor);
            MutableIntSet propertyIds = IntSets.mutable.empty();
            while (propertyCursor.next()) {
                Value currentValue = propertyCursor.propertyValue();
                int propertyKeyId = propertyCursor.propertyKey();
                Pair<SortedLabels, Integer> key = Pair.of(labels, propertyKeyId);
                updateValueTypeInMapping(currentValue, key, nodeMappings.labelSetANDNodePropertyKeyIdToValueType);
                propertyIds.add(propertyKeyId);
            }
            propertyCursor.close();
            MutableIntSet oldPropertyKeySet = nodeMappings.labelSetToPropertyKeys.getOrDefault(labels, emptyPropertyIdSet);
            // find out which old properties we did not visited and mark them as nullable
            if (oldPropertyKeySet == emptyPropertyIdSet) {
                if (propertyIds.size() == 0) {
                    // Even if we find property key on other nodes with those labels, set all of them nullable
                    nodeMappings.nullableLabelSets.add(labels);
                }
                propertyIds.addAll(oldPropertyKeySet);
            } else {
                MutableIntSet currentPropertyIdsHelperSet = new IntHashSet(propertyIds.size());
                currentPropertyIdsHelperSet.addAll(propertyIds);
                // only the brand new ones in propIds now
                propertyIds.removeAll(oldPropertyKeySet);
                // only the old ones that are not on the new node
                oldPropertyKeySet.removeAll(currentPropertyIdsHelperSet);
                propertyIds.addAll(oldPropertyKeySet);
                propertyIds.forEach(id -> {
                    Pair<SortedLabels, Integer> key = Pair.of(labels, id);
                    nodeMappings.labelSetANDNodePropertyKeyIdToValueType.get(key).setNullable();
                });
                propertyIds.addAll(currentPropertyIdsHelperSet);
            }
            nodeMappings.labelSetToPropertyKeys.put(labels, propertyIds);
        }
    }
}
Also used : MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) Value(org.neo4j.values.storable.Value) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

Example 97 with NodeCursor

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

the class CachingExpandIntoTest method shouldBeAbleToPreformAllCursorMethodsFromReused.

@Test
void shouldBeAbleToPreformAllCursorMethodsFromReused() throws KernelException {
    // given
    long start, end, r1, r2, r3;
    int t1, t2, t3;
    int prop;
    try (KernelTransaction tx = transaction()) {
        start = nodeWithDegree(tx, 43);
        end = nodeWithDegree(tx, 11);
        TokenWrite tokenWrite = tx.tokenWrite();
        t1 = tokenWrite.relationshipTypeGetOrCreateForName("R1");
        t2 = tokenWrite.relationshipTypeGetOrCreateForName("R2");
        t3 = tokenWrite.relationshipTypeGetOrCreateForName("R3");
        prop = tokenWrite.propertyKeyGetOrCreateForName("prop");
        Write write = tx.dataWrite();
        r1 = write.relationshipCreate(start, t1, end);
        r2 = write.relationshipCreate(start, t2, end);
        r3 = write.relationshipCreate(end, t3, start);
        write.relationshipSetProperty(r1, prop, stringValue("Relationship 1"));
        write.relationshipSetProperty(r2, prop, stringValue("Relationship 2"));
        write.relationshipSetProperty(r3, prop, stringValue("Relationship 3"));
        tx.commit();
    }
    try (KernelTransaction tx = transaction();
        NodeCursor nodes = tx.cursors().allocateNodeCursor(tx.cursorContext());
        RelationshipTraversalCursor traversal = tx.cursors().allocateRelationshipTraversalCursor(tx.cursorContext());
        PropertyCursor properties = tx.cursors().allocatePropertyCursor(tx.cursorContext(), tx.memoryTracker())) {
        int[] types = { t2, t3 };
        CachingExpandInto expandInto = new CachingExpandInto(tx.dataRead(), INCOMING, MEMORY_TRACKER);
        // Find r3 first time
        RelationshipTraversalCursor cursor = expandInto.connectingRelationships(nodes, traversal, start, types, end);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r3);
        assertThat(cursor.sourceNodeReference()).isEqualTo(end);
        assertThat(cursor.targetNodeReference()).isEqualTo(start);
        assertThat(cursor.otherNodeReference()).isEqualTo(start);
        assertThat(cursor.type()).isEqualTo(t3);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 3"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
        // Find r3 second time
        cursor = expandInto.connectingRelationships(nodes, traversal, start, types, end);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r3);
        assertThat(cursor.sourceNodeReference()).isEqualTo(end);
        assertThat(cursor.targetNodeReference()).isEqualTo(start);
        assertThat(cursor.otherNodeReference()).isEqualTo(start);
        assertThat(cursor.type()).isEqualTo(t3);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 3"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
        // Find r2 first time
        cursor = expandInto.connectingRelationships(nodes, traversal, end, types, start);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r2);
        assertThat(cursor.sourceNodeReference()).isEqualTo(start);
        assertThat(cursor.targetNodeReference()).isEqualTo(end);
        assertThat(cursor.otherNodeReference()).isEqualTo(end);
        assertThat(cursor.type()).isEqualTo(t2);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 2"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
        // Find r2 second time
        cursor = expandInto.connectingRelationships(nodes, traversal, end, types, start);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r2);
        assertThat(cursor.sourceNodeReference()).isEqualTo(start);
        assertThat(cursor.targetNodeReference()).isEqualTo(end);
        assertThat(cursor.otherNodeReference()).isEqualTo(end);
        assertThat(cursor.type()).isEqualTo(t2);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 2"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) Test(org.junit.jupiter.api.Test)

Example 98 with NodeCursor

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

the class CachingExpandIntoTest method shouldBeAbleToReuseWithoutTypes.

@Test
void shouldBeAbleToReuseWithoutTypes() throws KernelException {
    // given
    long start, end, r1, r2, r3;
    int t1, t2, t3;
    try (KernelTransaction tx = transaction()) {
        start = nodeWithDegree(tx, 43);
        end = nodeWithDegree(tx, 11);
        TokenWrite tokenWrite = tx.tokenWrite();
        t1 = tokenWrite.relationshipTypeGetOrCreateForName("R1");
        t2 = tokenWrite.relationshipTypeGetOrCreateForName("R2");
        t3 = tokenWrite.relationshipTypeGetOrCreateForName("R3");
        Write write = tx.dataWrite();
        r1 = write.relationshipCreate(start, t1, end);
        r2 = write.relationshipCreate(start, t2, end);
        r3 = write.relationshipCreate(end, t3, start);
        tx.commit();
    }
    try (KernelTransaction tx = transaction();
        NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext());
        RelationshipTraversalCursor traversalCursor = tx.cursors().allocateRelationshipTraversalCursor(tx.cursorContext())) {
        CachingExpandInto expandInto = new CachingExpandInto(tx.dataRead(), OUTGOING, MEMORY_TRACKER);
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, start, null, end))).isEqualTo(immutable.of(r1, r2));
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, end, null, start))).isEqualTo(immutable.of(r3));
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, start, null, end))).isEqualTo(immutable.of(r1, r2));
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, end, null, start))).isEqualTo(immutable.of(r3));
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 99 with NodeCursor

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

the class CachingExpandIntoTest method shouldComputeDegreeWithoutType.

@Test
void shouldComputeDegreeWithoutType() throws Exception {
    // GIVEN
    long node;
    try (KernelTransaction tx = transaction()) {
        Write write = tx.dataWrite();
        node = nodeWithDegree(tx, 42);
        relate(tx, node, "R1", write.nodeCreate());
        relate(tx, node, "R2", write.nodeCreate());
        relate(tx, write.nodeCreate(), "R3", node);
        relate(tx, node, "R4", node);
        tx.commit();
    }
    try (KernelTransaction tx = transaction()) {
        Read read = tx.dataRead();
        CursorFactory cursors = tx.cursors();
        try (NodeCursor nodes = cursors.allocateNodeCursor(tx.cursorContext())) {
            CachingExpandInto expand = new CachingExpandInto(tx.dataRead(), OUTGOING, MEMORY_TRACKER);
            read.singleNode(node, nodes);
            assertThat(nodes.next()).isEqualTo(true);
            assertThat(nodes.supportsFastDegreeLookup()).isEqualTo(true);
            Degrees degrees = nodes.degrees(ALL_RELATIONSHIPS);
            assertThat(degrees.outgoingDegree()).isEqualTo(45);
            assertThat(degrees.incomingDegree()).isEqualTo(2);
            assertThat(degrees.totalDegree()).isEqualTo(46);
        }
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) Read(org.neo4j.internal.kernel.api.Read) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) Degrees(org.neo4j.storageengine.api.Degrees) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 100 with NodeCursor

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

the class CachingExpandIntoTest method shouldBeAbleToReuseWithTypes.

@Test
void shouldBeAbleToReuseWithTypes() throws KernelException {
    // given
    long start, end, r1, r3;
    int t1, t2, t3;
    try (KernelTransaction tx = transaction()) {
        start = nodeWithDegree(tx, 43);
        end = nodeWithDegree(tx, 11);
        TokenWrite tokenWrite = tx.tokenWrite();
        t1 = tokenWrite.relationshipTypeGetOrCreateForName("R1");
        t2 = tokenWrite.relationshipTypeGetOrCreateForName("R2");
        t3 = tokenWrite.relationshipTypeGetOrCreateForName("R3");
        Write write = tx.dataWrite();
        r1 = write.relationshipCreate(start, t1, end);
        write.relationshipCreate(start, t2, end);
        r3 = write.relationshipCreate(end, t3, start);
        tx.commit();
    }
    try (KernelTransaction tx = transaction();
        NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext());
        RelationshipTraversalCursor traversalCursor = tx.cursors().allocateRelationshipTraversalCursor(tx.cursorContext())) {
        int[] types = { t1, t3 };
        CachingExpandInto expandInto = new CachingExpandInto(tx.dataRead(), OUTGOING, MEMORY_TRACKER);
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, start, types, end))).isEqualTo(immutable.of(r1));
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, end, types, start))).isEqualTo(immutable.of(r3));
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, start, types, end))).isEqualTo(immutable.of(r1));
        assertThat(toSet(expandInto.connectingRelationships(nodeCursor, traversalCursor, end, types, start))).isEqualTo(immutable.of(r3));
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Aggregations

NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)113 Test (org.junit.jupiter.api.Test)85 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)62 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)30 RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)26 Read (org.neo4j.internal.kernel.api.Read)20 Write (org.neo4j.internal.kernel.api.Write)17 Degrees (org.neo4j.storageengine.api.Degrees)11 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)10 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)7 LongList (org.eclipse.collections.api.list.primitive.LongList)7 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)7 CachingExpandInto (org.neo4j.internal.kernel.api.helpers.CachingExpandInto)6 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)5 RelationshipSelection (org.neo4j.storageengine.api.RelationshipSelection)5 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)4 Future (java.util.concurrent.Future)3 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3