Skip to content

Commit

Permalink
Add collect to RawCursor
Browse files Browse the repository at this point in the history
Simplify code
  • Loading branch information
davidegrohmann committed Jan 9, 2017
1 parent f11932b commit aa7afd0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 40 deletions.
Expand Up @@ -23,7 +23,7 @@
import java.util.function.Consumer; import java.util.function.Consumer;


import org.neo4j.collection.primitive.Primitive; import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntCollections; import org.neo4j.collection.primitive.PrimitiveIntCollection;
import org.neo4j.collection.primitive.PrimitiveIntSet; import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.cursor.Cursor; import org.neo4j.cursor.Cursor;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException; import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
Expand Down Expand Up @@ -69,28 +69,22 @@ public void visitCreatedNode( long id )
public void visitDeletedNode( long id ) public void visitDeletedNode( long id )
{ {
counts.incrementNodeCount( ANY_LABEL, -1 ); counts.incrementNodeCount( ANY_LABEL, -1 );
statement.acquireSingleNodeCursor( id ).forAll( node -> statement.acquireSingleNodeCursor( id ).forAll( this::decrementCountForLabelsAndRelationships );
{ super.visitDeletedNode( id );
PrimitiveIntSet set = }
node.labels().mapReduce( Primitive.intSet(), LabelItem::getAsInt, ( labelId, current ) ->
{
current.add( labelId );
counts.incrementNodeCount( labelId, -1 );
return current;
} );

int[] labelIds = PrimitiveIntCollections.asArray( set.iterator() );
node.degrees().forAll( degree ->
{
for ( int labelId : labelIds )
{
updateRelationshipsCountsFromDegrees( degree.type(), labelId, -degree.outgoing(),
-degree.incoming() );
}
} );


private void decrementCountForLabelsAndRelationships( NodeItem node )
{
PrimitiveIntSet labelIds = node.labels().collect( Primitive.intSet(), ( label ) ->
{
int labelId = label.getAsInt();
counts.incrementNodeCount( labelId, -1 );
return labelId;
} ); } );
super.visitDeletedNode( id );
node.degrees().forAll(
degree -> updateRelationshipsCountsFromDegrees( labelIds, degree.type(), -degree.outgoing(),
-degree.incoming() ) );
} }


@Override @Override
Expand Down Expand Up @@ -149,14 +143,20 @@ public void visitNodeLabelChanges( long id, final Set<Integer> added, final Set<
super.visitNodeLabelChanges( id, added, removed ); super.visitNodeLabelChanges( id, added, removed );
} }


private void updateRelationshipsCountsFromDegrees( int type, int label, long outgoing, long incoming ) private void updateRelationshipsCountsFromDegrees( PrimitiveIntCollection labels, int type, long outgoing,
long incoming )
{
labels.visitKeys( (label) -> updateRelationshipsCountsFromDegrees( type, label, outgoing, incoming ) );
}
private boolean updateRelationshipsCountsFromDegrees( int type, int label, long outgoing, long incoming )
{ {
// untyped // untyped
counts.incrementRelationshipCount( label, ANY_RELATIONSHIP_TYPE, ANY_LABEL, outgoing ); counts.incrementRelationshipCount( label, ANY_RELATIONSHIP_TYPE, ANY_LABEL, outgoing );
counts.incrementRelationshipCount( ANY_LABEL, ANY_RELATIONSHIP_TYPE, label, incoming ); counts.incrementRelationshipCount( ANY_LABEL, ANY_RELATIONSHIP_TYPE, label, incoming );
// typed // typed
counts.incrementRelationshipCount( label, type, ANY_LABEL, outgoing ); counts.incrementRelationshipCount( label, type, ANY_LABEL, outgoing );
counts.incrementRelationshipCount( ANY_LABEL, type, label, incoming ); counts.incrementRelationshipCount( ANY_LABEL, type, label, incoming );
return true;
} }


private void updateRelationshipCount( long startNode, int type, long endNode, int delta ) private void updateRelationshipCount( long startNode, int type, long endNode, int delta )
Expand Down
Expand Up @@ -30,6 +30,9 @@
import java.util.Map; import java.util.Map;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;


import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntCollections;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveLongCollections; import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException; import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.index.IndexDescriptor; import org.neo4j.kernel.api.index.IndexDescriptor;
Expand Down Expand Up @@ -354,10 +357,13 @@ private void commitLabels( Integer... labels ) throws Exception
commitLabels( labels( nodeId, labels ) ); commitLabels( labels( nodeId, labels ) );
} }


private void assertLabels( Integer... labels ) throws EntityNotFoundException private void assertLabels( int... labels ) throws EntityNotFoundException
{ {
txContext.nodeCursorById( state, nodeId ).forAll( node -> assertEquals( asSet( labels ), txContext.nodeCursorById( state, nodeId ).forAll( node ->
node.labels().mapReduce( new HashSet<>(), IntSupplier::getAsInt, this::addToCollection ) ) ); {
PrimitiveIntSet collect = node.labels().collect( Primitive.intSet(), IntSupplier::getAsInt );
assertEquals( PrimitiveIntCollections.asSet( labels ), collect );
} );


txContext.nodeCursorById( state, nodeId ).forAll( node -> txContext.nodeCursorById( state, nodeId ).forAll( node ->
{ {
Expand All @@ -367,10 +373,4 @@ private void assertLabels( Integer... labels ) throws EntityNotFoundException
} }
} ); } );
} }

private <T, C extends Collection<T>> C addToCollection( T value, C collection)
{
collection.add( value );
return collection;
}
} }
Expand Up @@ -26,6 +26,9 @@
import java.util.Set; import java.util.Set;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;


import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntCollections;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveLongCollections; import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator; import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.cursor.Cursor; import org.neo4j.cursor.Cursor;
Expand Down Expand Up @@ -66,18 +69,11 @@ public void should_be_able_to_list_labels_for_node() throws Exception
// THEN // THEN
disk.newStatement().acquireSingleNodeCursor( nodeId ).forAll( node -> disk.newStatement().acquireSingleNodeCursor( nodeId ).forAll( node ->
{ {
Set<Integer> actual = node.labels().mapReduce( new HashSet<>(), IntSupplier::getAsInt, PrimitiveIntSet actual = node.labels().collect( Primitive.intSet(), IntSupplier::getAsInt );
this::addToCollection ); assertEquals( PrimitiveIntCollections.asSet( new int[]{labelId1, labelId2} ), actual );
assertEquals( new HashSet<>( asList( labelId1, labelId2 ) ), actual );
} ); } );
} }


private <T, C extends Collection<T>> C addToCollection( T value, C collection)
{
collection.add( value );
return collection;
}

@Test @Test
public void should_be_able_to_get_label_name_for_label() throws Exception public void should_be_able_to_get_label_name_for_label() throws Exception
{ {
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.neo4j.collection.primitive; package org.neo4j.collection.primitive;


@FunctionalInterface
public interface PrimitiveIntVisitor<E extends Exception> public interface PrimitiveIntVisitor<E extends Exception>
{ {
/** /**
Expand Down
Expand Up @@ -22,7 +22,12 @@
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.function.ToIntFunction;

import org.neo4j.collection.primitive.PrimitiveIntCollection;
import org.neo4j.collection.primitive.PrimitiveIntSet;


/** /**
* A cursor is an object that moves to point to different locations in a data structure. * A cursor is an object that moves to point to different locations in a data structure.
Expand Down Expand Up @@ -55,6 +60,22 @@ default void forAll( Consumer<T> consumer ) throws EXCEPTION
mapForAll( Function.identity(), consumer ); mapForAll( Function.identity(), consumer );
} }


default <R extends PrimitiveIntSet> R collect( R set, ToIntFunction<T> map ) throws EXCEPTION
{
try
{
while ( next() )
{
set.add( map.applyAsInt( get() ) );
}
return set;
}
finally
{
close();
}
}

default <R, E> E mapReduce( E initialValue, Function<T,R> map, BiFunction<R,E,E> reduce ) throws EXCEPTION default <R, E> E mapReduce( E initialValue, Function<T,R> map, BiFunction<R,E,E> reduce ) throws EXCEPTION
{ {
try try
Expand Down

0 comments on commit aa7afd0

Please sign in to comment.