Skip to content

Commit

Permalink
Simplify degree counting code by reusing degree visiting code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed May 8, 2017
1 parent bd99e3a commit 68bb78b
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 252 deletions.
Expand Up @@ -80,7 +80,7 @@ private void decrementCountForLabelsAndRelationships( NodeItem node )
} ); } );


storeLayer.degrees( statement, node, storeLayer.degrees( statement, node,
( type, out, in ) -> updateRelationshipsCountsFromDegrees( labelIds, type, -out, -in ) ); ( type, out, in, loop ) -> updateRelationshipsCountsFromDegrees( labelIds, type, -out, -in, -loop ) );
} }


@Override @Override
Expand Down Expand Up @@ -124,37 +124,39 @@ public void visitNodeLabelChanges( long id, final Set<Integer> added, final Set<
// get the relationship counts from *before* this transaction, // get the relationship counts from *before* this transaction,
// the relationship changes will compensate for what happens during the transaction // the relationship changes will compensate for what happens during the transaction
storeLayer.nodeCursor( statement, id, null ) storeLayer.nodeCursor( statement, id, null )
.forAll( node -> storeLayer.degrees( statement, node, ( type, out, in ) -> .forAll( node -> storeLayer.degrees( statement, node, ( type, out, in, loop ) ->
{ {
added.forEach( label -> updateRelationshipsCountsFromDegrees( type, label, out, in ) ); added.forEach( label -> updateRelationshipsCountsFromDegrees( type, label, out, in, loop ) );
removed.forEach( label -> updateRelationshipsCountsFromDegrees( type, label, -out, -in ) ); removed.forEach( label -> updateRelationshipsCountsFromDegrees( type, label, -out, -in, -loop ) );
return true;
} ) ); } ) );
} }
super.visitNodeLabelChanges( id, added, removed ); super.visitNodeLabelChanges( id, added, removed );
} }


private void updateRelationshipsCountsFromDegrees( PrimitiveIntCollection labels, int type, long outgoing, private boolean updateRelationshipsCountsFromDegrees( PrimitiveIntCollection labels, int type, long outgoing,
long incoming ) long incoming, long loop )
{ {
labels.visitKeys( ( label ) -> updateRelationshipsCountsFromDegrees( type, label, outgoing, incoming ) ); labels.visitKeys( label -> updateRelationshipsCountsFromDegrees( type, label, outgoing, incoming, loop ) );
return true;
} }


private boolean updateRelationshipsCountsFromDegrees( int type, int label, long outgoing, long incoming ) private boolean updateRelationshipsCountsFromDegrees( int type, int label, long out, long in, long loop )
{ {
// untyped // untyped
counts.incrementRelationshipCount( label, ANY_RELATIONSHIP_TYPE, ANY_LABEL, outgoing ); counts.incrementRelationshipCount( label, ANY_RELATIONSHIP_TYPE, ANY_LABEL, out + loop );
counts.incrementRelationshipCount( ANY_LABEL, ANY_RELATIONSHIP_TYPE, label, incoming ); counts.incrementRelationshipCount( ANY_LABEL, ANY_RELATIONSHIP_TYPE, label, in + loop );
// typed // typed
counts.incrementRelationshipCount( label, type, ANY_LABEL, outgoing ); counts.incrementRelationshipCount( label, type, ANY_LABEL, out + loop );
counts.incrementRelationshipCount( ANY_LABEL, type, label, incoming ); counts.incrementRelationshipCount( ANY_LABEL, type, label, in + loop );
return false; return false;
} }


private void updateRelationshipCount( long startNode, int type, long endNode, int delta ) private void updateRelationshipCount( long startNode, int type, long endNode, int delta )
{ {
updateRelationshipsCountsFromDegrees( type, ANY_LABEL, delta, 0 ); updateRelationshipsCountsFromDegrees( type, ANY_LABEL, delta, 0, 0 );
visitLabels( startNode, ( labelId ) -> updateRelationshipsCountsFromDegrees( type, labelId, delta, 0 ) ); visitLabels( startNode, ( labelId ) -> updateRelationshipsCountsFromDegrees( type, labelId, delta, 0, 0 ) );
visitLabels( endNode, ( labelId ) -> updateRelationshipsCountsFromDegrees( type, labelId, 0, delta ) ); visitLabels( endNode, ( labelId ) -> updateRelationshipsCountsFromDegrees( type, labelId, 0, delta, 0 ) );
} }


private void visitLabels( long nodeId, PrimitiveIntVisitor<RuntimeException> visitor ) private void visitLabels( long nodeId, PrimitiveIntVisitor<RuntimeException> visitor )
Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.api;

import org.neo4j.storageengine.api.Direction;

public class CountingDegreeVisitor implements DegreeVisitor
{
private final Direction direction;
private final int relType;
private final boolean dense;

private int count;

public CountingDegreeVisitor( Direction direction, boolean dense )
{
this( direction, -1, dense );
}

public CountingDegreeVisitor( Direction direction, int relType, boolean dense )
{
this.direction = direction;
this.relType = relType;
this.dense = dense;
}

@Override
public boolean visitDegree( int type, long outgoing, long incoming, long loop )
{
if ( relType == -1 || type == relType )
{
switch ( direction )
{
case OUTGOING:
count += outgoing + loop;
break;
case INCOMING:
count += incoming + loop;
break;
case BOTH:
count += outgoing + incoming + loop;
break;
default:
throw new IllegalStateException( "Unknown direction: " + direction );
}
// continue only if we are counting all types or the node is not dense (i.e., we visit one rel at the time)
return relType == -1 || !dense;
}
return true;
}

public int count()
{
return count;
}
}
Expand Up @@ -19,7 +19,8 @@
*/ */
package org.neo4j.kernel.impl.api; package org.neo4j.kernel.impl.api;


@FunctionalInterface
public interface DegreeVisitor public interface DegreeVisitor
{ {
void visitDegree( int type, long outgoing, long incoming ); boolean visitDegree( int type, long outgoing, long incoming, long loop );
} }
Expand Up @@ -97,7 +97,6 @@
import org.neo4j.storageengine.api.PropertyItem; import org.neo4j.storageengine.api.PropertyItem;
import org.neo4j.storageengine.api.RelationshipItem; import org.neo4j.storageengine.api.RelationshipItem;
import org.neo4j.storageengine.api.StorageProperty; import org.neo4j.storageengine.api.StorageProperty;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreReadLayer; import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.Token; import org.neo4j.storageengine.api.Token;
import org.neo4j.storageengine.api.schema.IndexReader; import org.neo4j.storageengine.api.schema.IndexReader;
Expand All @@ -118,7 +117,6 @@
import static org.neo4j.kernel.api.properties.DefinedProperty.NO_SUCH_PROPERTY; import static org.neo4j.kernel.api.properties.DefinedProperty.NO_SUCH_PROPERTY;
import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.ADDED_LABEL; import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.ADDED_LABEL;
import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.REMOVED_LABEL; import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.REMOVED_LABEL;
import static org.neo4j.kernel.impl.util.Cursors.count;
import static org.neo4j.register.Registers.newDoubleLongRegister; import static org.neo4j.register.Registers.newDoubleLongRegister;
import static org.neo4j.storageengine.api.txstate.TxStateVisitor.EMPTY; import static org.neo4j.storageengine.api.txstate.TxStateVisitor.EMPTY;


Expand Down Expand Up @@ -1654,7 +1652,7 @@ public int degree( KernelStatement statement, NodeItem node, Direction direction
{ {
int degree = statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) int degree = statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() )
? 0 ? 0
: computeDegree( statement, node, direction, null ); : visitDegrees( statement, node, new CountingDegreeVisitor( direction, node.isDense() ) ).count();


return statement.hasTxStateWithChanges() return statement.hasTxStateWithChanges()
? statement.txState().getNodeState( node.id() ).augmentDegree( direction, degree ) ? statement.txState().getNodeState( node.id() ).augmentDegree( direction, degree )
Expand All @@ -1666,27 +1664,16 @@ public int degree( KernelStatement statement, NodeItem node, Direction direction
{ {
int degree = statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) int degree = statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() )
? 0 ? 0
: computeDegree( statement, node, direction, relType ); : visitDegrees( statement, node, new CountingDegreeVisitor( direction, relType, node.isDense() ) ).count();


return statement.hasTxStateWithChanges() return statement.hasTxStateWithChanges()
? statement.txState().getNodeState( node.id() ).augmentDegree( direction, degree, relType ) ? statement.txState().getNodeState( node.id() ).augmentDegree( direction, degree, relType )
: degree; : degree;
} }


private int computeDegree( KernelStatement statement, NodeItem node, Direction direction, Integer relType ) private <T extends DegreeVisitor> T visitDegrees( KernelStatement statement, NodeItem node, T visitor )
{ {
StorageStatement storeStatement = statement.getStoreStatement(); storeLayer.degrees( statement.getStoreStatement(), node, visitor );
if ( node.isDense() ) return visitor;
{
return storeLayer
.degreeRelationshipsInGroup( storeStatement, node.id(), node.nextGroupId(), direction, relType );
}
else
{
return count( relType == null
? storeLayer.nodeGetRelationships( storeStatement, node, direction, null )
: storeLayer.nodeGetRelationships( storeStatement, node, direction, new int[]{relType},null )
);
}
} }
} }

This file was deleted.

0 comments on commit 68bb78b

Please sign in to comment.