Skip to content

Commit

Permalink
incomingCount should check tx-state
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Feb 25, 2018
1 parent 7fd4499 commit f3ee486
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
Expand Up @@ -24,8 +24,11 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;


import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.Relationship;
import org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.internal.kernel.api.exceptions.KernelException; import org.neo4j.internal.kernel.api.exceptions.KernelException;


import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -37,7 +40,6 @@
import static org.neo4j.graphdb.Direction.BOTH; import static org.neo4j.graphdb.Direction.BOTH;
import static org.neo4j.graphdb.Direction.INCOMING; import static org.neo4j.graphdb.Direction.INCOMING;
import static org.neo4j.graphdb.Direction.OUTGOING; import static org.neo4j.graphdb.Direction.OUTGOING;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.assertCount;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.assertCounts; import static org.neo4j.internal.kernel.api.RelationshipTestSupport.assertCounts;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.computeKey; import static org.neo4j.internal.kernel.api.RelationshipTestSupport.computeKey;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.count; import static org.neo4j.internal.kernel.api.RelationshipTestSupport.count;
Expand Down Expand Up @@ -626,10 +628,32 @@ public void shouldSeeRemovedThenAddedPropertyInTransaction() throws Exception




@Test @Test
public void shouldCountOutgoingNodesFromTxState() throws Exception public void shouldCountFromTxState() throws Exception
{ {
assertOutgoingCount( 100 );//node will be dense //dense outgoing
assertOutgoingCount( 1 );//node will be sparse assertCount( 100, OUTGOING, group ->
{
assertEquals( 101, group.outgoingCount() );
assertEquals( 101, group.totalCount() );
} );
//sparse outgoing
assertCount( 1, OUTGOING, group ->
{
assertEquals( 2, group.outgoingCount() );
assertEquals( 2, group.totalCount() );
} );
//dense incoming
assertCount( 100, INCOMING, group ->
{
assertEquals( 101, group.incomingCount() );
assertEquals( 101, group.totalCount() );
} );
//sparse incoming
assertCount( 1, INCOMING, group ->
{
assertEquals( 2, group.incomingCount() );
assertEquals( 2, group.totalCount() );
} );
} }


private void traverseWithoutGroups( RelationshipTestSupport.StartNode start, boolean detached ) throws Exception private void traverseWithoutGroups( RelationshipTestSupport.StartNode start, boolean detached ) throws Exception
Expand Down Expand Up @@ -701,7 +725,8 @@ private void traverseViaGroups( RelationshipTestSupport.StartNode start, boolean
group.outgoing( relationship ); group.outgoing( relationship );
} }
// then // then
assertCount( session, relationship, expectedCounts, group.relationshipLabel(), OUTGOING ); RelationshipTestSupport
.assertCount( session, relationship, expectedCounts, group.relationshipLabel(), OUTGOING );


// incoming // incoming
if ( detached ) if ( detached )
Expand All @@ -713,7 +738,8 @@ private void traverseViaGroups( RelationshipTestSupport.StartNode start, boolean
group.incoming( relationship ); group.incoming( relationship );
} }
// then // then
assertCount( session, relationship, expectedCounts, group.relationshipLabel(), INCOMING ); RelationshipTestSupport
.assertCount( session, relationship, expectedCounts, group.relationshipLabel(), INCOMING );


// loops // loops
if ( detached ) if ( detached )
Expand All @@ -725,7 +751,8 @@ private void traverseViaGroups( RelationshipTestSupport.StartNode start, boolean
group.loops( relationship ); group.loops( relationship );
} }
// then // then
assertCount( session, relationship, expectedCounts, group.relationshipLabel(), BOTH ); RelationshipTestSupport
.assertCount( session, relationship, expectedCounts, group.relationshipLabel(), BOTH );
} }
} }
} }
Expand Down Expand Up @@ -797,7 +824,7 @@ private void assertCountRelationships(
assertEquals( expectedCount, count ); assertEquals( expectedCount, count );
} }


private void assertOutgoingCount( int count ) throws Exception private void assertCount( int count, Direction direction, Consumer<RelationshipGroupCursor> asserter) throws Exception
{ {
long start; long start;
int type; int type;
Expand All @@ -808,15 +835,15 @@ private void assertOutgoingCount( int count ) throws Exception
type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" );
for ( int i = 0; i < count; i++ ) for ( int i = 0; i < count; i++ )
{ {
write.relationshipCreate( start, type, write.nodeCreate() ); createRelationship( direction, start, type, write );
} }
tx.success(); tx.success();
} }


try ( Transaction tx = session.beginTransaction() ) try ( Transaction tx = session.beginTransaction() )
{ {
Write write = tx.dataWrite(); Write write = tx.dataWrite();
write.relationshipCreate( start, type, write.nodeCreate() ); createRelationship( direction, start, type, write );
try ( NodeCursor node = cursors.allocateNodeCursor(); try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor() ) RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor() )
{ {
Expand All @@ -825,10 +852,28 @@ private void assertOutgoingCount( int count ) throws Exception
assertTrue( node.next() ); assertTrue( node.next() );
node.relationships( group ); node.relationships( group );
assertTrue( group.next() ); assertTrue( group.next() );

asserter.accept( group );
assertEquals( count + 1, group.outgoingCount() );
assertEquals( count + 1, group.totalCount() );
} }
} }
} }

private void createRelationship( Direction direction, long start, int type, Write write )
throws EntityNotFoundException
{
switch ( direction )
{
case OUTGOING:
write.relationshipCreate( start, type, write.nodeCreate() );
break;
case INCOMING:
write.relationshipCreate( write.nodeCreate(), type, start );
break;
case BOTH:
write.relationshipCreate( start, type, write.nodeCreate() );
write.relationshipCreate( write.nodeCreate(), type, start );
break;
default:
throw new IllegalStateException( "Checkstyle, you win again!" );
}
}
} }
Expand Up @@ -205,11 +205,22 @@ else if ( isBuffered() )
@Override @Override
public int incomingCount() public int incomingCount()
{ {
if ( isBuffered() ) int count;
if ( read.hasTxStateWithChanges() && read.txState().nodeIsAddedInThisTx( getOwningNode() ) )
{
count = 0;
}
else if ( isBuffered() )
{
count = bufferedGroup.incomingCount;
}
else
{ {
return bufferedGroup.incomingCount; count = count( incomingRawId() );
} }
return count( incomingRawId() ); return read.hasTxStateWithChanges()
? read.txState().getNodeState( getOwningNode() )
.augmentDegree( Direction.INCOMING, count, getType() ) : count;
} }


@Override @Override
Expand Down

0 comments on commit f3ee486

Please sign in to comment.