Skip to content

Couple tweaks to generate less garbage #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.UnknownHostException;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

/**
* Holds a host and port pair that denotes a Bolt server address.
Expand All @@ -49,23 +50,23 @@ public BoltServerAddress( URI uri )

public BoltServerAddress( String host, int port )
{
this.host = host;
this.host = requireNonNull( host );
this.port = port;
}

@Override
public boolean equals( Object obj )
public boolean equals( Object o )
{
if ( this == obj )
if ( this == o )
{
return true;
}
if ( !(obj instanceof BoltServerAddress) )
if ( o == null || getClass() != o.getClass() )
{
return false;
}
BoltServerAddress address = (BoltServerAddress) obj;
return host.equals( address.host ) && port == address.port;
BoltServerAddress that = (BoltServerAddress) o;
return port == that.port && host.equals( that.host );
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions driver/src/main/java/org/neo4j/driver/internal/Bookmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.neo4j.driver.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
Expand All @@ -28,6 +27,7 @@

import static java.util.Collections.emptyMap;
import static java.util.Collections.singleton;
import static org.neo4j.driver.internal.util.Iterables.newHashMapWithSize;
import static org.neo4j.driver.v1.Values.value;

public final class Bookmark
Expand Down Expand Up @@ -93,7 +93,7 @@ public Map<String,Value> asBeginTransactionParameters()
// {bookmarks: ["one", "two", "max"]} for backwards compatibility reasons. Old servers can only accept single
// bookmark that is why driver has to parse and compare given list of bookmarks. This functionality will
// eventually be removed.
Map<String,Value> parameters = new HashMap<>( 4 );
Map<String,Value> parameters = newHashMapWithSize( 2 );
parameters.put( BOOKMARK_KEY, value( maxValue ) );
parameters.put( BOOKMARKS_KEY, value( values ) );
return parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import static java.util.Collections.emptyMap;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.completedWithNull;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.v1.Values.value;

Expand Down Expand Up @@ -163,7 +164,7 @@ else if ( state == State.ACTIVE || state == State.MARKED_FAILED || state == Stat
}
else
{
return completedFuture( null );
return completedWithNull();
}
}

Expand All @@ -172,7 +173,7 @@ public CompletionStage<Void> commitAsync()
{
if ( state == State.COMMITTED )
{
return completedFuture( null );
return completedWithNull();
}
else if ( state == State.ROLLED_BACK )
{
Expand Down Expand Up @@ -200,13 +201,13 @@ public CompletionStage<Void> rollbackAsync()
}
else if ( state == State.ROLLED_BACK )
{
return completedFuture( null );
return completedWithNull();
}
else if ( state == State.TERMINATED )
{
// transaction has been terminated by RESET and should be rolled back by the database
state = State.ROLLED_BACK;
return completedFuture( null );
return completedWithNull();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.Session;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.completedWithNull;

public class InternalDriver implements Driver
{
Expand Down Expand Up @@ -116,7 +116,7 @@ public CompletionStage<Void> closeAsync()
log.info( "Closing driver instance %s", this );
return sessionFactory.close();
}
return completedFuture( null );
return completedWithNull();
}

public CompletionStage<Void> verifyConnectivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.neo4j.driver.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -118,9 +117,7 @@ public CompletionStage<List<Record>> listAsync()
@Override
public <T> CompletionStage<List<T>> listAsync( Function<Record,T> mapFunction )
{
CompletableFuture<List<T>> resultFuture = new CompletableFuture<>();
internalListAsync( new ArrayList<>(), resultFuture, mapFunction );
return resultFuture;
return pullAllHandler.listAsync( mapFunction );
}

public CompletionStage<Throwable> failureAsync()
Expand Down Expand Up @@ -160,40 +157,4 @@ else if ( record != null )
}
} );
}

private <T> void internalListAsync( List<T> result, CompletableFuture<List<T>> resultFuture,
Function<Record,T> mapFunction )
{
CompletionStage<Record> recordFuture = nextAsync();

// use async completion listener because of recursion, otherwise it is possible for
// the caller thread to get StackOverflowError when result is large and buffered
recordFuture.whenCompleteAsync( ( record, completionError ) ->
{
Throwable error = Futures.completionExceptionCause( completionError );
if ( error != null )
{
resultFuture.completeExceptionally( error );
}
else if ( record != null )
{
T value;
try
{
value = mapFunction.apply( record );
}
catch ( Throwable mapError )
{
resultFuture.completeExceptionally( mapError );
return;
}
result.add( value );
internalListAsync( result, resultFuture, mapFunction );
}
else
{
resultFuture.complete( result );
}
} );
}
}
33 changes: 12 additions & 21 deletions driver/src/main/java/org/neo4j/driver/internal/NetworkSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.types.TypeSystem;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.completedWithNull;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.v1.Values.value;

Expand All @@ -60,9 +60,9 @@ public class NetworkSession implements Session
protected final Logger logger;

private volatile Bookmark bookmark = Bookmark.empty();
private volatile CompletionStage<ExplicitTransaction> transactionStage = completedFuture( null );
private volatile CompletionStage<Connection> connectionStage = completedFuture( null );
private volatile CompletionStage<InternalStatementResultCursor> resultCursorStage = completedFuture( null );
private volatile CompletionStage<ExplicitTransaction> transactionStage = completedWithNull();
private volatile CompletionStage<Connection> connectionStage = completedWithNull();
private volatile CompletionStage<InternalStatementResultCursor> resultCursorStage = completedWithNull();

private final AtomicBoolean open = new AtomicBoolean( true );

Expand Down Expand Up @@ -168,7 +168,7 @@ public CompletionStage<Void> closeAsync()
{
if ( cursor == null )
{
return completedFuture( null );
return completedWithNull();
}
return cursor.failureAsync();
} ).thenCompose( error -> releaseResources().thenApply( ignore ->
Expand All @@ -186,7 +186,7 @@ public CompletionStage<Void> closeAsync()
}
} ) );
}
return completedFuture( null );
return completedWithNull();
}

@Override
Expand Down Expand Up @@ -274,10 +274,6 @@ public TypeSystem typeSystem()

CompletionStage<Boolean> currentConnectionIsOpen()
{
if ( connectionStage == null )
{
return completedFuture( false );
}
return connectionStage.handle( ( connection, error ) ->
error == null && // no acquisition error
connection != null && // some connection has actually been acquired
Expand Down Expand Up @@ -363,7 +359,7 @@ private <T> CompletionStage<T> safeExecuteWork( ExplicitTransaction tx, Transact
CompletionStage<T> result = work.execute( tx );

// protect from given transaction function returning null
return result == null ? completedFuture( null ) : result;
return result == null ? completedWithNull() : result;
}
catch ( Throwable workError )
{
Expand Down Expand Up @@ -459,7 +455,7 @@ private CompletionStage<Connection> acquireConnection( AccessMode mode )
{
if ( cursor == null )
{
return completedFuture( null );
return completedWithNull();
}
// make sure previous result is fully consumed and connection is released back to the pool
return cursor.failureAsync();
Expand Down Expand Up @@ -508,7 +504,7 @@ private CompletionStage<Void> rollbackTransaction()
{
return tx.rollbackAsync();
}
return completedFuture( null );
return completedWithNull();
} ).exceptionally( error ->
{
Throwable cause = Futures.completionExceptionCause( error );
Expand All @@ -519,13 +515,13 @@ private CompletionStage<Void> rollbackTransaction()

private CompletionStage<Void> releaseConnection()
{
return existingConnectionOrNull().thenCompose( connection ->
return connectionStage.thenCompose( connection ->
{
if ( connection != null )
{
return connection.release();
}
return completedFuture( null );
return completedWithNull();
} );
}

Expand Down Expand Up @@ -574,15 +570,10 @@ private CompletionStage<Void> ensureNoOpenTx( String errorMessage )
private CompletionStage<ExplicitTransaction> existingTransactionOrNull()
{
return transactionStage
.exceptionally( error -> null ) // handle previous acquisition failures
.exceptionally( error -> null ) // handle previous connection acquisition and tx begin failures
.thenApply( tx -> tx != null && tx.isOpen() ? tx : null );
}

private CompletionStage<Connection> existingConnectionOrNull()
{
return connectionStage.exceptionally( error -> null ); // handle previous acquisition failures
}

private void ensureSessionIsOpen()
{
if ( !open.get() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.neo4j.driver.internal.InternalStatementResultCursor;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.completedWithNull;

public class ResultCursorsHolder
{
Expand All @@ -41,14 +42,14 @@ public CompletionStage<Throwable> retrieveNotConsumedError()
{
return cursorStages.stream()
.map( this::retrieveFailure )
.reduce( completedFuture( null ), this::nonNullFailureFromEither );
.reduce( completedWithNull(), this::nonNullFailureFromEither );
}

private CompletionStage<Throwable> retrieveFailure( CompletionStage<InternalStatementResultCursor> cursorStage )
{
return cursorStage
.exceptionally( cursor -> null )
.thenCompose( cursor -> cursor == null ? completedFuture( null ) : cursor.failureAsync() );
.thenCompose( cursor -> cursor == null ? completedWithNull() : cursor.failureAsync() );
}

private CompletionStage<Throwable> nonNullFailureFromEither( CompletionStage<Throwable> stage1,
Expand Down
Loading