@Test public void shouldCallIndexSeek() throws KernelException { // GIVEN Read read = mock( Read.class ); IndexReference index = mock( IndexReference.class ); when( index.properties() ).thenReturn( new int[]{42} ); // WHEN CompiledIndexUtils.indexSeek( read, mock( CursorFactory.class ), index, "hello" ); // THEN verify( read, times( 1 ) ).nodeIndexSeek( any(), any(), any(), anyBoolean(), any() ); }
private static void singleRelationship( Read read, RelationshipScanCursor relationships, long relationship ) throws EntityNotFoundException { read.singleRelationship( relationship, relationships ); if ( !relationships.next() ) { throw new EntityNotFoundException( EntityType.NODE, relationship ); } } }
private long countsForNode() { return kernelTransactionSupplier.get().dataRead().countsForNode( StatementConstants.ANY_LABEL ); } }
@Parameterized.Parameters( name = "{0}" ) public static Collection<Object[]> parameters() { // Some samples of operations that should react to transaction killing. Not exhaustive. return Arrays.asList( operation( "nodeExists", ( read, write, schema ) -> read.nodeExists( 0 ) ), operation( "singleRelationship", ( read, write, schema ) -> read.singleRelationship( 0, null ) ), operation( "nodeCreate", ( read, write, schema ) -> write.nodeCreate() ), operation( "relationshipSetProperty", ( read, write, schema ) -> write.relationshipSetProperty( 0, 2, Values.longValue( 42 ) ) ), operation( "indexesGetAll", ( read, write, schema ) -> schema.indexesGetAll() ) ); }
when( schemaRead.indexesGetForLabel( anyInt() ) ).thenReturn( emptyIterator() ); when( schemaRead.constraintsGetForLabel( anyInt() ) ).thenReturn( emptyIterator() ); when( read.countsForNode( anyInt() ) ).thenReturn( 1L ); when( read.countsForRelationship( anyInt(), anyInt(), anyInt() ) ).thenReturn( 1L ); when( schemaRead.indexGetState( any( IndexReference.class ) ) ).thenReturn( InternalIndexState.ONLINE );
private static void singleNode( Read read, NodeCursor nodeCursor, long node ) throws EntityNotFoundException { read.singleNode( node, nodeCursor ); if ( !nodeCursor.next() ) { throw new EntityNotFoundException( EntityType.NODE, node ); } }
@Test public void shouldNotCountRemovedNodesFromTxState() throws Exception { // Given Node node1 = createNode( "label" ); Node node2 = createNode( "label" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction() ) { // when tx.dataWrite().nodeDelete( node2.node ); long countTxState = tx.dataRead().countsForNode( node1.labels[0] ); long countNoTxState = tx.dataRead().countsForNodeWithoutTxState( node1.labels[0] ); // then assertEquals( 1, countTxState ); assertEquals( 2, countNoTxState ); } }
private ResourceIterator<Node> getNodesByLabelAndPropertyWithoutIndex( Statement statement, int labelId, IndexQuery... queries ) { KernelTransaction transaction = statementContext.getKernelTransactionBoundToThisThread( true ); NodeLabelIndexCursor nodeLabelCursor = transaction.cursors().allocateNodeLabelIndexCursor(); NodeCursor nodeCursor = transaction.cursors().allocateNodeCursor(); PropertyCursor propertyCursor = transaction.cursors().allocatePropertyCursor(); transaction.dataRead().nodeLabelScan( labelId, nodeLabelCursor ); return new NodeLabelPropertyIterator( transaction.dataRead(), nodeLabelCursor, nodeCursor, propertyCursor, statement, this::newNodeProxy, queries ); }
@Test public void shouldScanNodes() { // given List<Long> ids = new ArrayList<>(); try ( NodeCursor nodes = cursors.allocateNodeCursor() ) { // when read.allNodesScan( nodes ); while ( nodes.next() ) { ids.add( nodes.nodeReference() ); } } // then assertEquals( NODE_IDS, ids ); }
private void loadRels(int labelId, int relTypeId) { this.relCount = (int) read.countsForRelationshipWithoutTxState(labelId, relTypeId, ANY_LABEL); this.rels = new int[relCount]; int[] relTypes = relTypeId == ANY_RELATIONSHIP_TYPE ? null : new int[]{relTypeId}; if (labelId == ANY_LABEL) { read.allNodesScan(nodeCursor); while (nodeCursor.next()) { idx = loadRelsForNode(nodeCursor, idx, relTypes); read.nodeLabelScan(labelId, nodeLabelIndexCursor); while (nodeLabelIndexCursor.next()) { nodeLabelIndexCursor.node(nodeCursor);
public static void readNodes(CursorFactory cursors, Read dataRead, int labelId, LongConsumer action) { if (labelId == Read.ANY_LABEL) { try (NodeCursor nodeCursor = cursors.allocateNodeCursor()) { dataRead.allNodesScan(nodeCursor); while (nodeCursor.next()) { action.accept(nodeCursor.nodeReference()); } } } else { try (NodeLabelIndexCursor nodeCursor = cursors.allocateNodeLabelIndexCursor()) { dataRead.nodeLabelScan(labelId, nodeCursor); while (nodeCursor.next()) { action.accept(nodeCursor.nodeReference()); } } } }
nodePropId = propertyKey(tokenRead, setup.shouldLoadNodeProperty(), setup.nodePropertyName); nodeCount = dataRead.countsForNode(labelId); allNodesCount = InternalReadOps.getHighestPossibleNodeCount(dataRead, api); maxRelCount = Math.max( dataRead.countsForRelationshipWithoutTxState( labelId, relationId == null ? Read.ANY_RELATIONSHIP_TYPE : relationId[0], Read.ANY_LABEL ), dataRead.countsForRelationshipWithoutTxState( Read.ANY_LABEL, relationId == null ? Read.ANY_RELATIONSHIP_TYPE : relationId[0],
private void noSide( KernelTransaction ktx, DbStructureVisitor visitor, RelationshipType relType, int relTypeId ) { String userDescription = format("MATCH ()-[%s]->() RETURN count(*)", colon( relType.name() )); long amount = ktx.dataRead().countsForRelationship( ANY_LABEL, relTypeId, ANY_LABEL ); visitor.visitRelCount( ANY_LABEL, relTypeId, ANY_LABEL, userDescription, amount ); }
@Test public void shouldNotCountRemovedRelationships() throws Exception { int relationshipId; long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); relationshipId = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), relationshipId, write.nodeCreate() ); tx.success(); } try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); write.relationshipDelete( relationship ); long countsTxState = tx.dataRead().countsForRelationship( -1, relationshipId, -1 ); long countsNoTxState = tx.dataRead().countsForRelationshipWithoutTxState( -1, relationshipId, -1 ); assertEquals( 0, countsTxState ); assertEquals( 1, countsNoTxState ); } }
long count = read.countsForNodeWithoutTxState(id); if (count > 0) { mergeMetaNode(Label.label(labelName), vNodes, count); long global = read.countsForRelationshipWithoutTxState(ANY_LABEL, typeId, ANY_LABEL); labels.forEach((labelNameA, labelIdA) -> { long relCountOut = read.countsForRelationshipWithoutTxState(labelIdA, typeId, ANY_LABEL); if (relCountOut > 0) { labels.forEach((labelNameB, labelIdB) -> { long relCountIn = read.countsForRelationshipWithoutTxState(ANY_LABEL, typeId, labelIdB); if (relCountIn > 0) { Node nodeA = vNodes.get(labelNameA);
private void collectStats(Collection<String> labelNames, Collection<String> relTypeNames, StatsCallback cb) { Read read = kernelTx.dataRead(); TokenRead tokenRead = kernelTx.tokenRead(); Map<String, Integer> labels = labelsInUse(tokenRead, labelNames); Map<String, Integer> relTypes = relTypesInUse(tokenRead, relTypeNames); labels.forEach((name, id) -> { long count = read.countsForNodeWithoutTxState(id); if (count > 0) { cb.label(id, name, count); relTypes.forEach((typeName, typeId) -> { long relCountOut = read.countsForRelationship(id, typeId, ANY_LABEL); long relCountIn = read.countsForRelationship(ANY_LABEL, typeId, id); cb.rel(typeId, typeName, id, name, relCountOut, relCountIn); }); } }); relTypes.forEach((typeName, typeId) -> { cb.rel(typeId, typeName, read.countsForRelationship(ANY_LABEL, typeId, ANY_LABEL)); }); }
private IntStream idStream(@Name(value = "start", defaultValue = "null") Object start, Graph graph, int limit) { int nodeCount = Math.toIntExact(graph.nodeCount()); if (start instanceof String) { String label = start.toString(); int labelId = transaction.tokenRead().nodeLabel(label); int countWithLabel = Math.toIntExact(transaction.dataRead().countsForNodeWithoutTxState(labelId)); NodeLabelIndexCursor cursor = transaction.cursors().allocateNodeLabelIndexCursor(); transaction.dataRead().nodeLabelScan(labelId, cursor); cursor.next(); LongStream ids; if (limit == -1) { ids = LongStream.range(0, countWithLabel).map( i -> cursor.next() ? cursor.nodeReference() : -1L ); } else { int[] indexes = ThreadLocalRandom.current().ints(limit + 1, 0, countWithLabel).sorted().toArray(); IntStream deltas = IntStream.range(0, limit).map(i -> indexes[i + 1] - indexes[i]); ids = deltas.mapToLong(delta -> { while (delta > 0 && cursor.next()) delta--;return cursor.nodeReference(); }); } return ids.mapToInt(graph::toMappedNodeId).onClose(cursor::close); } else if (start instanceof Collection) { return ((Collection)start).stream().mapToLong(e -> ((Number)e).longValue()).mapToInt(graph::toMappedNodeId); } else if (start instanceof Number) { return LongStream.of(((Number)start).longValue()).mapToInt(graph::toMappedNodeId); } else { if (nodeCount < limit) { return IntStream.range(0,nodeCount).limit(limit); } else { return IntStream.generate(() -> ThreadLocalRandom.current().nextInt(nodeCount)).limit(limit); } } }
@Test public void shouldScanRelationships() { // given List<Long> ids = new ArrayList<>(); try ( RelationshipScanCursor relationships = cursors.allocateRelationshipScanCursor() ) { // when read.allRelationshipsScan( relationships ); while ( relationships.next() ) { ids.add( relationships.relationshipReference() ); } } assertEquals( RELATIONSHIP_IDS, ids ); }
private void loadNodes(int labelId, int relTypeId) { this.labelId = labelId; this.relTypeId = relTypeId; int allNodeCount = (int) ktx.dataRead().nodesGetCount(); if (labelId == ANY_LABEL) { this.nodeRelOffsets = loadNodes(allNodeCount, relTypeId, OUTGOING); this.nodeCount = nodeRelOffsets.length; } else { this.nodeCount = (int) ktx.dataRead().countsForNodeWithoutTxState(labelId); float percentage = (float)nodeCount / (float)allNodeCount; this.nodeRelOffsets = (percentage > 0.5f) ? loadNodesForLabel(labelId, nodeCount, relTypeId, OUTGOING) : loadNodes( allNodeCount, relTypeId, OUTGOING); } }
private static List<Map<String,Object>> nodeCounts( TokenRead tokens, Read read, Anonymizer anonymizer ) { List<Map<String,Object>> nodeCounts = new ArrayList<>(); Map<String,Object> nodeCount = new HashMap<>(); nodeCount.put( "count", read.countsForNodeWithoutTxState( -1 ) ); nodeCounts.add( nodeCount ); tokens.labelsGetAllTokens().forEachRemaining( t -> { long count = read.countsForNodeWithoutTxState( t.id() ); Map<String,Object> labelCount = new HashMap<>(); labelCount.put( "label", anonymizer.label( t.name(), t.id() ) ); labelCount.put( "count", count ); nodeCounts.add( labelCount ); } ); return nodeCounts; }