From 39b763c243cde33526e7851dd0988a82d6999a5d Mon Sep 17 00:00:00 2001 From: Davide Grohmann Date: Thu, 2 Feb 2017 16:03:33 +0100 Subject: [PATCH] Simplify code and remove unused code --- .../impl/api/store/StoreSingleNodeCursor.java | 32 ++--- .../org/neo4j/kernel/impl/util/Cursors.java | 113 ++---------------- .../StateOperationsAutoIndexingTest.java | 31 ++--- .../kernel/impl/api/state/StubCursors.java | 61 +++++++++- .../kernel/impl/api/state/TxStateTest.java | 3 +- .../main/java/org/neo4j/cursor/RawCursor.java | 76 +----------- .../catchup/tx/TxPullRequestHandlerTest.java | 38 +++++- 7 files changed, 138 insertions(+), 216 deletions(-) diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/StoreSingleNodeCursor.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/StoreSingleNodeCursor.java index 2daa82402862b..cf2ced3eed4cd 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/StoreSingleNodeCursor.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/StoreSingleNodeCursor.java @@ -50,13 +50,13 @@ import org.neo4j.storageengine.api.PropertyItem; import org.neo4j.storageengine.api.RelationshipItem; -import static java.util.function.Function.identity; import static org.neo4j.collection.primitive.Primitive.intSet; import static org.neo4j.kernel.impl.api.store.DegreeCounter.countRelationshipsInGroup; import static org.neo4j.kernel.impl.locking.LockService.NO_LOCK_SERVICE; import static org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP; import static org.neo4j.kernel.impl.store.record.RecordLoad.CHECK; import static org.neo4j.kernel.impl.store.record.RecordLoad.FORCE; +import static org.neo4j.kernel.impl.util.Cursors.count; import static org.neo4j.kernel.impl.util.IoPrimitiveUtils.safeCastLongToInt; /** @@ -264,7 +264,7 @@ public PrimitiveIntSet relationshipTypes() } else { - relationships( Direction.BOTH ).collect( set, RelationshipItem::type ); + relationships( Direction.BOTH ).forAll( relationship -> set.add( relationship.type() ) ); } return set; } @@ -279,7 +279,7 @@ public int degree( Direction direction ) } else { - return relationships( direction ).count(); + return count( relationships( direction ) ); } } @@ -293,7 +293,7 @@ public int degree( Direction direction, int relType ) } else { - return relationships( direction, relType ).count(); + return count( relationships( direction, relType ) ); } } @@ -306,17 +306,21 @@ public Cursor degrees() private PrimitiveIntObjectMap buildDegreeMap() { - return relationships( Direction.BOTH ) - .mapReduce( Primitive.intObjectMap( 5 ), identity(), ( rel, currentDegrees ) -> + PrimitiveIntObjectMap currentDegrees = Primitive.intObjectMap( 5 ); + try( Cursor relationships = relationships( Direction.BOTH ) ) + { + while ( relationships.next() ) + { + RelationshipItem rel = relationships.get(); + int[] byType = currentDegrees.get( rel.type() ); + if ( byType == null ) { - int[] byType = currentDegrees.get( rel.type() ); - if ( byType == null ) - { - currentDegrees.put( rel.type(), byType = new int[3] ); - } - byType[directionOf( nodeRecord.getId(), rel.id(), rel.startNode(), rel.endNode() ).ordinal()]++; - return currentDegrees; - } ); + currentDegrees.put( rel.type(), byType = new int[3] ); + } + byType[directionOf( nodeRecord.getId(), rel.id(), rel.startNode(), rel.endNode() ).ordinal()]++; + } + } + return currentDegrees; } @Override diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/util/Cursors.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/util/Cursors.java index 59fe9fe38c78a..2ed512bf82bdf 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/util/Cursors.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/util/Cursors.java @@ -19,15 +19,7 @@ */ package org.neo4j.kernel.impl.util; -import java.io.IOException; -import java.util.Iterator; - import org.neo4j.cursor.Cursor; -import org.neo4j.cursor.IOCursor; -import org.neo4j.helpers.collection.Iterables; -import org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation; -import org.neo4j.kernel.impl.transaction.log.LogPosition; -import org.neo4j.kernel.impl.transaction.log.TransactionCursor; public class Cursors { @@ -57,105 +49,20 @@ public static Cursor empty() return (Cursor) EMPTY; } - public static Cursor cursor( final T... items ) - { - return cursor( Iterables.asIterable( items ) ); - } - - public static Cursor cursor( final Iterable items ) + public static int count( Cursor cursor ) { - return new Cursor() + try { - Iterator iterator = items.iterator(); - - T current; - - @Override - public boolean next() + int count = 0; + while ( cursor.next() ) { - if ( iterator.hasNext() ) - { - current = iterator.next(); - return true; - } - else - { - return false; - } + count++; } - - @Override - public void close() - { - iterator = items.iterator(); - current = null; - } - - @Override - public T get() - { - if ( current == null ) - { - throw new IllegalStateException(); - } - - return current; - } - }; - } - - public static TransactionCursor txCursor( Cursor cursor ) - { - return new TransactionCursor() - { - @Override - public LogPosition position() - { - throw new UnsupportedOperationException( - "LogPosition does not apply when moving a generic cursor over a list of transactions" ); - } - - @Override - public boolean next() throws IOException - { - return cursor.next(); - } - - @Override - public void close() throws IOException - { - cursor.close(); - } - - @Override - public CommittedTransactionRepresentation get() - { - return cursor.get(); - } - }; - } - - public static IOCursor io( Cursor cursor ) - { - return new IOCursor() + return count; + } + finally { - @Override - public boolean next() throws IOException - { - return cursor.next(); - } - - @Override - public void close() throws IOException - { - cursor.close(); - } - - @Override - public T get() - { - return cursor.get(); - } - }; + cursor.close(); + } } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StateOperationsAutoIndexingTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StateOperationsAutoIndexingTest.java index 5ab8674f35da4..280ea07b8d793 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StateOperationsAutoIndexingTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StateOperationsAutoIndexingTest.java @@ -33,7 +33,6 @@ import org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexOperations; import org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing; import org.neo4j.kernel.impl.index.LegacyIndexStore; -import org.neo4j.kernel.impl.util.Cursors; import org.neo4j.storageengine.api.NodeItem; import org.neo4j.storageengine.api.PropertyItem; import org.neo4j.storageengine.api.RelationshipItem; @@ -47,6 +46,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.neo4j.kernel.api.properties.Property.property; +import static org.neo4j.kernel.impl.api.state.StubCursors.cursor; +import static org.neo4j.kernel.impl.util.Cursors.empty; public class StateOperationsAutoIndexingTest { @@ -73,7 +74,7 @@ public void setup() throws InvalidTransactionTypeKernelException public void shouldSignalNodeRemovedToAutoIndex() throws Exception { // Given - when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( Cursors.cursor( mock( NodeItem.class )) ); + when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( cursor( mock( NodeItem.class )) ); // When context.nodeDelete( stmt, 1337 ); @@ -86,7 +87,7 @@ public void shouldSignalNodeRemovedToAutoIndex() throws Exception public void shouldSignalRelationshipRemovedToAutoIndex() throws Exception { // Given - when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( Cursors.cursor( mock( RelationshipItem.class )) ); + when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( cursor( mock( RelationshipItem.class )) ); // When context.relationshipDelete( stmt, 1337 ); @@ -102,9 +103,9 @@ public void shouldSignalNodePropertyAddedToAutoIndex() throws Exception DefinedProperty property = property( 1, "Hello!" ); NodeItem node = mock( NodeItem.class ); - when( node.property( property.propertyKeyId() )).thenReturn( Cursors.empty() ); + when( node.property( property.propertyKeyId() )).thenReturn( empty() ); when( node.labels() ).thenReturn( PrimitiveIntCollections.emptySet() ); - when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( Cursors.cursor( node ) ); + when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( cursor( node ) ); // When context.nodeSetProperty( stmt, 1337, property ); @@ -120,8 +121,8 @@ public void shouldSignalRelationshipPropertyAddedToAutoIndex() throws Exception DefinedProperty property = property( 1, "Hello!" ); RelationshipItem rel = mock( RelationshipItem.class ); - when( rel.property( property.propertyKeyId() )).thenReturn( Cursors.empty() ); - when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( Cursors.cursor( rel ) ); + when( rel.property( property.propertyKeyId() )).thenReturn( empty() ); + when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( cursor( rel ) ); // When context.relationshipSetProperty( stmt, 1337, property ); @@ -141,9 +142,9 @@ public void shouldSignalNodePropertyChangedToAutoIndex() throws Exception when(existingProperty.value()).thenReturn( "Goodbye!" ); NodeItem node = mock( NodeItem.class ); - when( node.property( property.propertyKeyId() )).thenReturn( Cursors.cursor( existingProperty ) ); + when( node.property( property.propertyKeyId() )).thenReturn( cursor( existingProperty ) ); when( node.labels() ).thenReturn( PrimitiveIntCollections.emptySet() ); - when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( Cursors.cursor( node ) ); + when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( cursor( node ) ); // When context.nodeSetProperty( stmt, 1337, property ); @@ -163,8 +164,8 @@ public void shouldSignalRelationshipPropertyChangedToAutoIndex() throws Exceptio when(existingProperty.value()).thenReturn( "Goodbye!" ); RelationshipItem rel = mock( RelationshipItem.class ); - when( rel.property( property.propertyKeyId() )).thenReturn( Cursors.cursor( existingProperty ) ); - when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( Cursors.cursor( rel ) ); + when( rel.property( property.propertyKeyId() )).thenReturn( cursor( existingProperty ) ); + when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( cursor( rel ) ); // When context.relationshipSetProperty( stmt, 1337, property ); @@ -183,9 +184,9 @@ public void shouldSignalNodePropertyRemovedToAutoIndex() throws Exception when(existingProperty.value()).thenReturn( "Goodbye!" ); NodeItem node = mock( NodeItem.class ); - when( node.property( existingProperty.propertyKeyId() )).thenReturn( Cursors.cursor( existingProperty ) ); + when( node.property( existingProperty.propertyKeyId() )).thenReturn( cursor( existingProperty ) ); when( node.labels() ).thenReturn( PrimitiveIntCollections.emptySet() ); - when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( Cursors.cursor( node ) ); + when( storeStmt.acquireSingleNodeCursor( 1337 ) ).thenReturn( cursor( node ) ); // When context.nodeRemoveProperty( stmt, 1337, existingProperty.propertyKeyId() ); @@ -204,8 +205,8 @@ public void shouldSignalRelationshipPropertyRemovedToAutoIndex() throws Exceptio when(existingProperty.value()).thenReturn( "Goodbye!" ); RelationshipItem rel = mock( RelationshipItem.class ); - when( rel.property( existingProperty.propertyKeyId() )).thenReturn( Cursors.cursor( existingProperty ) ); - when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( Cursors.cursor( rel ) ); + when( rel.property( existingProperty.propertyKeyId() )).thenReturn( cursor( existingProperty ) ); + when( storeStmt.acquireSingleRelationshipCursor( 1337 ) ).thenReturn( cursor( rel ) ); // When context.relationshipRemoveProperty( stmt, 1337, existingProperty.propertyKeyId() ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StubCursors.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StubCursors.java index 4a099c6dabaf0..ce02183897693 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StubCursors.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/StubCursors.java @@ -20,14 +20,15 @@ package org.neo4j.kernel.impl.api.state; import java.util.Arrays; +import java.util.Iterator; import org.neo4j.collection.primitive.PrimitiveIntCollections; import org.neo4j.collection.primitive.PrimitiveIntSet; import org.neo4j.cursor.Cursor; +import org.neo4j.helpers.collection.Iterables; import org.neo4j.kernel.api.cursor.EntityItemHelper; import org.neo4j.kernel.api.cursor.RelationshipItemHelper; import org.neo4j.kernel.api.properties.DefinedProperty; -import org.neo4j.kernel.impl.util.Cursors; import org.neo4j.storageengine.api.DegreeItem; import org.neo4j.storageengine.api.Direction; import org.neo4j.storageengine.api.NodeItem; @@ -55,20 +56,20 @@ public static Cursor asNodeCursor( final long... nodeIds ) { nodeItems[i] = new StubNodeItem( nodeIds[i], empty(), emptySet() ); } - return Cursors.cursor( nodeItems ); + return cursor( nodeItems ); } public static Cursor asNodeCursor( final long nodeId, final Cursor propertyCursor ) { - return Cursors.cursor( new StubNodeItem( nodeId, propertyCursor, emptySet() ) ); + return cursor( new StubNodeItem( nodeId, propertyCursor, emptySet() ) ); } public static Cursor asNodeCursor( final long nodeId, final Cursor propertyCursor, final PrimitiveIntSet labels ) { - return Cursors.cursor( new StubNodeItem( nodeId, propertyCursor, labels ) ); + return cursor( new StubNodeItem( nodeId, propertyCursor, labels ) ); } private static class StubNodeItem extends EntityItemHelper implements NodeItem @@ -189,7 +190,7 @@ public Cursor degrees() public static Cursor asRelationshipCursor( final long relId, final int type, final long startNode, final long endNode, final Cursor propertyCursor ) { - return Cursors.cursor( new RelationshipItemHelper() + return cursor( new RelationshipItemHelper() { @Override public long id() @@ -271,7 +272,7 @@ public static PrimitiveIntSet labels( final int... labels ) public static Cursor asPropertyCursor( final DefinedProperty... properties ) { - return Cursors.cursor( map( StubCursors::asPropertyItem, Arrays.asList( properties ) ) ); + return cursor( map( StubCursors::asPropertyItem, Arrays.asList( properties ) ) ); } private static PropertyItem asPropertyItem( final DefinedProperty property ) @@ -291,4 +292,52 @@ public Object value() } }; } + + @SafeVarargs + public static Cursor cursor( final T... items ) + { + return cursor( Iterables.asIterable( items ) ); + } + + public static Cursor cursor( final Iterable items ) + { + return new Cursor() + { + Iterator iterator = items.iterator(); + + T current; + + @Override + public boolean next() + { + if ( iterator.hasNext() ) + { + current = iterator.next(); + return true; + } + else + { + return false; + } + } + + @Override + public void close() + { + iterator = items.iterator(); + current = null; + } + + @Override + public T get() + { + if ( current == null ) + { + throw new IllegalStateException(); + } + + return current; + } + }; + } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/TxStateTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/TxStateTest.java index 83583a8ac8e07..991d61d30611f 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/TxStateTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/TxStateTest.java @@ -80,6 +80,7 @@ import static org.neo4j.kernel.api.properties.Property.noNodeProperty; import static org.neo4j.kernel.api.properties.Property.numberProperty; import static org.neo4j.kernel.api.properties.Property.stringProperty; +import static org.neo4j.kernel.impl.api.state.StubCursors.cursor; public class TxStateTest { @@ -1454,7 +1455,7 @@ public void shouldObserveCorrectAugmentedNodeRelationshipsState() throws Excepti { Direction direction = Direction.values()[random.nextInt( Direction.values().length )]; int[] relationshipTypes = randomTypes( relationshipTypeCount, random.random() ); - Cursor committed = Cursors.cursor( + Cursor committed = cursor( relationshipsForNode( i, committedRelationships, direction, relationshipTypes ).values() ); Cursor augmented = state.augmentNodeRelationshipCursor( committed, state.getNodeState( i ), direction, diff --git a/community/primitive-collections/src/main/java/org/neo4j/cursor/RawCursor.java b/community/primitive-collections/src/main/java/org/neo4j/cursor/RawCursor.java index 0f5bb502f507f..5efde36cd9eb1 100644 --- a/community/primitive-collections/src/main/java/org/neo4j/cursor/RawCursor.java +++ b/community/primitive-collections/src/main/java/org/neo4j/cursor/RawCursor.java @@ -19,15 +19,8 @@ */ package org.neo4j.cursor; -import java.util.function.BiFunction; import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.IntFunction; 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. @@ -56,80 +49,13 @@ public interface RawCursor extends Supplier, void close() throws EXCEPTION; default void forAll( Consumer consumer ) throws EXCEPTION - { - mapForAll( Function.identity(), consumer ); - } - - default R collect( R set, ToIntFunction map ) throws EXCEPTION - { - try - { - while ( next() ) - { - set.add( map.applyAsInt( get() ) ); - } - return set; - } - finally - { - close(); - } - } - - default E mapReduce( E initialValue, Function map, BiFunction reduce ) throws EXCEPTION - { - try - { - E current = initialValue; - while ( next() ) - { - current = reduce.apply( map.apply( get() ), current ); - } - return current; - } - finally - { - close(); - } - } - - default void mapForAll( Function function, Consumer consumer ) throws EXCEPTION { try { while ( next() ) { - consumer.accept( function.apply( get() ) ); - } - } - finally - { - close(); - } - } - - default boolean exists() throws EXCEPTION - { - try - { - return next(); - } - finally - { - close(); - } - } - - default int count() throws EXCEPTION - { - try - { - int count = 0; - while( next() ) - { - count++; + consumer.accept( get() ); } - return count; } finally { diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/catchup/tx/TxPullRequestHandlerTest.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/catchup/tx/TxPullRequestHandlerTest.java index e0e5bcd32d473..2c5d449bd1124 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/catchup/tx/TxPullRequestHandlerTest.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/catchup/tx/TxPullRequestHandlerTest.java @@ -22,14 +22,18 @@ import io.netty.channel.ChannelHandlerContext; import org.junit.Test; +import java.io.IOException; + import org.neo4j.causalclustering.catchup.CatchupServerProtocol; import org.neo4j.causalclustering.catchup.ResponseMessageType; import org.neo4j.causalclustering.identity.StoreId; +import org.neo4j.cursor.Cursor; import org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation; import org.neo4j.kernel.impl.transaction.command.Commands; import org.neo4j.kernel.impl.transaction.log.LogPosition; import org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore; import org.neo4j.kernel.impl.transaction.log.NoSuchTransactionException; +import org.neo4j.kernel.impl.transaction.log.TransactionCursor; import org.neo4j.kernel.impl.transaction.log.TransactionIdStore; import org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart; import org.neo4j.kernel.impl.transaction.log.entry.OnePhaseCommit; @@ -47,9 +51,8 @@ import static org.neo4j.causalclustering.catchup.CatchupResult.E_TRANSACTION_PRUNED; import static org.neo4j.causalclustering.catchup.CatchupResult.SUCCESS_END_OF_BATCH; import static org.neo4j.causalclustering.catchup.CatchupResult.SUCCESS_END_OF_STREAM; +import static org.neo4j.kernel.impl.api.state.StubCursors.cursor; import static org.neo4j.kernel.impl.transaction.command.Commands.createNode; -import static org.neo4j.kernel.impl.util.Cursors.cursor; -import static org.neo4j.kernel.impl.util.Cursors.txCursor; import static org.neo4j.logging.AssertableLogProvider.inLog; public class TxPullRequestHandlerTest @@ -225,4 +228,35 @@ private static CommittedTransactionRepresentation tx( int id ) new LogEntryStart( id, id, id, id - 1, new byte[]{}, LogPosition.UNSPECIFIED ), Commands.transactionRepresentation( createNode( 0 ) ), new OnePhaseCommit( id, id ) ); } + + private static TransactionCursor txCursor( Cursor cursor ) + { + return new TransactionCursor() + { + @Override + public LogPosition position() + { + throw new UnsupportedOperationException( + "LogPosition does not apply when moving a generic cursor over a list of transactions" ); + } + + @Override + public boolean next() throws IOException + { + return cursor.next(); + } + + @Override + public void close() throws IOException + { + cursor.close(); + } + + @Override + public CommittedTransactionRepresentation get() + { + return cursor.get(); + } + }; + } }