Skip to content

Commit

Permalink
Simplify code and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Feb 2, 2017
1 parent 546b404 commit 39b763c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 216 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -279,7 +279,7 @@ public int degree( Direction direction )
}
else
{
return relationships( direction ).count();
return count( relationships( direction ) );
}
}

Expand All @@ -293,7 +293,7 @@ public int degree( Direction direction, int relType )
}
else
{
return relationships( direction, relType ).count();
return count( relationships( direction, relType ) );
}
}

Expand All @@ -306,17 +306,21 @@ public Cursor<DegreeItem> degrees()

private PrimitiveIntObjectMap<int[]> buildDegreeMap()
{
return relationships( Direction.BOTH )
.mapReduce( Primitive.<int[]>intObjectMap( 5 ), identity(), ( rel, currentDegrees ) ->
PrimitiveIntObjectMap<int[]> currentDegrees = Primitive.intObjectMap( 5 );
try( Cursor<RelationshipItem> 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
Expand Down
113 changes: 10 additions & 103 deletions community/kernel/src/main/java/org/neo4j/kernel/impl/util/Cursors.java
Expand Up @@ -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
{
Expand Down Expand Up @@ -57,105 +49,20 @@ public static <T> Cursor<T> empty()
return (Cursor<T>) EMPTY;
}

public static <T> Cursor<T> cursor( final T... items )
{
return cursor( Iterables.asIterable( items ) );
}

public static <T> Cursor<T> cursor( final Iterable<T> items )
public static int count( Cursor<?> cursor )
{
return new Cursor<T>()
try
{
Iterator<T> 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<CommittedTransactionRepresentation> 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 <T> IOCursor<T> io( Cursor<T> cursor )
{
return new IOCursor<T>()
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();
}
}
}
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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() );
Expand All @@ -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() );
Expand Down

0 comments on commit 39b763c

Please sign in to comment.