Skip to content

Commit

Permalink
outgoingCount 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 0079c91 commit 7fd4499
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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.count;
import static org.neo4j.internal.kernel.api.RelationshipTestSupport.sparse;
import static org.neo4j.values.storable.Values.NO_VALUE;
import static org.neo4j.values.storable.Values.stringValue;

Expand Down Expand Up @@ -274,7 +275,7 @@ public void shouldSeeRelationshipInTransactionBeforeCursorInitialization() throw
@Test
public void shouldTraverseSparseNodeWithoutGroups() throws Exception
{
traverseWithoutGroups( RelationshipTestSupport.sparse( graphDb ), false );
traverseWithoutGroups( sparse( graphDb ), false );
}

@Test
Expand All @@ -286,7 +287,7 @@ public void shouldTraverseDenseNodeWithoutGroups() throws Exception
@Test
public void shouldTraverseSparseNodeWithoutGroupsWithDetachedReferences() throws Exception
{
traverseWithoutGroups( RelationshipTestSupport.sparse( graphDb ), true );
traverseWithoutGroups( sparse( graphDb ), true );
}

@Test
Expand All @@ -298,7 +299,7 @@ public void shouldTraverseDenseNodeWithoutGroupsWithDetachedReferences() throws
@Test
public void shouldTraverseSparseNodeViaGroups() throws Exception
{
traverseViaGroups( RelationshipTestSupport.sparse( graphDb ), false );
traverseViaGroups( sparse( graphDb ), false );
}

@Test
Expand All @@ -310,7 +311,7 @@ public void shouldTraverseDenseNodeViaGroups() throws Exception
@Test
public void shouldTraverseSparseNodeViaGroupsWithDetachedReferences() throws Exception
{
traverseViaGroups( RelationshipTestSupport.sparse( graphDb ), true );
traverseViaGroups( sparse( graphDb ), true );
}

@Test
Expand Down Expand Up @@ -623,6 +624,14 @@ public void shouldSeeRemovedThenAddedPropertyInTransaction() throws Exception
}
}


@Test
public void shouldCountOutgoingNodesFromTxState() throws Exception
{
assertOutgoingCount( 100 );//node will be dense
assertOutgoingCount( 1 );//node will be sparse
}

private void traverseWithoutGroups( RelationshipTestSupport.StartNode start, boolean detached ) throws Exception
{
try ( Transaction tx = session.beginTransaction() )
Expand Down Expand Up @@ -787,4 +796,39 @@ private void assertCountRelationships(
}
assertEquals( expectedCount, count );
}

private void assertOutgoingCount( int count ) throws Exception
{
long start;
int type;
try ( Transaction tx = session.beginTransaction() )
{
Write write = tx.dataWrite();
start = write.nodeCreate();
type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" );
for ( int i = 0; i < count; i++ )
{
write.relationshipCreate( start, type, write.nodeCreate() );
}
tx.success();
}

try ( Transaction tx = session.beginTransaction() )
{
Write write = tx.dataWrite();
write.relationshipCreate( start, type, write.nodeCreate() );
try ( NodeCursor node = cursors.allocateNodeCursor();
RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor() )
{
Read read = tx.dataRead();
read.singleNode( start, node );
assertTrue( node.next() );
node.relationships( group );
assertTrue( group.next() );

assertEquals( count + 1, group.outgoingCount() );
assertEquals( count + 1, group.totalCount() );
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.neo4j.kernel.impl.newapi.DefaultRelationshipTraversalCursor.Record;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.storageengine.api.Direction;

import static org.neo4j.kernel.impl.newapi.RelationshipReferenceEncoding.encodeForFiltering;
import static org.neo4j.kernel.impl.newapi.RelationshipReferenceEncoding.encodeForTxStateFiltering;
Expand Down Expand Up @@ -183,11 +184,22 @@ public int relationshipLabel()
@Override
public int outgoingCount()
{
if ( isBuffered() )
int count;
if ( read.hasTxStateWithChanges() && read.txState().nodeIsAddedInThisTx( getOwningNode() ) )
{
count = 0;
}
else if ( isBuffered() )
{
count = bufferedGroup.outgoingCount;
}
else
{
return bufferedGroup.outgoingCount;
count = count( outgoingRawId() );
}
return count( outgoingRawId() );
return read.hasTxStateWithChanges()
? read.txState().getNodeState( getOwningNode() )
.augmentDegree( Direction.OUTGOING, count, getType() ) : count;
}

@Override
Expand Down

0 comments on commit 7fd4499

Please sign in to comment.