Skip to content

Commit 5acd107

Browse files
committed
Simplify Response impl
By making `InternalFuture` implement this public interface. Previously a special adapter object was needed. Now we can directly return internal classes.
1 parent 587301a commit 5acd107

File tree

7 files changed

+47
-124
lines changed

7 files changed

+47
-124
lines changed

driver/src/main/java/org/neo4j/driver/internal/ExplicitTransaction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ private void rollbackTx()
216216
@Override
217217
public Response<Void> commitAsync()
218218
{
219-
return internalCommitAsync().asResponse();
219+
return internalCommitAsync();
220220
}
221221

222-
InternalFuture<Void> internalCommitAsync()
222+
private InternalFuture<Void> internalCommitAsync()
223223
{
224224
if ( state == State.COMMITTED )
225225
{
@@ -239,7 +239,7 @@ else if ( state == State.ROLLED_BACK )
239239
@Override
240240
public Response<Void> rollbackAsync()
241241
{
242-
return internalRollbackAsync().asResponse();
242+
return internalRollbackAsync();
243243
}
244244

245245
InternalFuture<Void> internalRollbackAsync()
@@ -390,7 +390,7 @@ public StatementResult run( Statement statement )
390390
public Response<StatementResultCursor> runAsync( Statement statement )
391391
{
392392
ensureNotFailed();
393-
return QueryRunner.runAsync( asyncConnection, statement, this ).asResponse();
393+
return QueryRunner.runAsync( asyncConnection, statement, this );
394394
}
395395

396396
@Override

driver/src/main/java/org/neo4j/driver/internal/NetworkSession.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.neo4j.driver.ResultResourcesHandler;
2727
import org.neo4j.driver.internal.async.AsyncConnection;
2828
import org.neo4j.driver.internal.async.InternalFuture;
29-
import org.neo4j.driver.internal.async.InternalResponse;
29+
import org.neo4j.driver.internal.async.InternalPromise;
3030
import org.neo4j.driver.internal.async.QueryRunner;
3131
import org.neo4j.driver.internal.logging.DelegatingLogger;
3232
import org.neo4j.driver.internal.retry.RetryLogic;
@@ -160,7 +160,7 @@ public InternalFuture<StatementResultCursor> apply( AsyncConnection connection )
160160
{
161161
return QueryRunner.runAsync( connection, statement );
162162
}
163-
} ).asResponse();
163+
} );
164164
}
165165

166166
public static StatementResult run( Connection connection, Statement statement,
@@ -247,7 +247,7 @@ public InternalFuture<Void> apply( AsyncConnection connection )
247247
{
248248
return connection.forceRelease();
249249
}
250-
} ).asResponse();
250+
} );
251251
}
252252
else if ( currentAsyncTransactionFuture != null )
253253
{
@@ -258,11 +258,11 @@ public InternalFuture<Void> apply( ExplicitTransaction tx )
258258
{
259259
return tx.internalRollbackAsync();
260260
}
261-
} ).asResponse();
261+
} );
262262
}
263263
else
264264
{
265-
return new InternalResponse<>( GlobalEventExecutor.INSTANCE.<Void>newSucceededFuture( null ) );
265+
return new InternalPromise<Void>( GlobalEventExecutor.INSTANCE ).setSuccess( null );
266266
}
267267
}
268268

@@ -431,7 +431,7 @@ public InternalFuture<ExplicitTransaction> apply( AsyncConnection connection )
431431
} );
432432

433433
//noinspection unchecked
434-
return (Response) currentAsyncTransactionFuture.asResponse();
434+
return (Response) currentAsyncTransactionFuture;
435435
}
436436

437437
private void ensureNoUnrecoverableError()

driver/src/main/java/org/neo4j/driver/internal/async/Futures.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private Futures()
3333

3434
public static <T, U> InternalFuture<U> thenApply( InternalFuture<T> future, Function<T,U> fn )
3535
{
36-
InternalPromise<U> result = new InternalPromise<>( future.eventLoop() );
36+
InternalPromise<U> result = new InternalPromise<>( future.eventExecutor() );
3737
future.addListener( new ThenApplyListener<>( result, fn ) );
3838
return result;
3939
}
@@ -47,14 +47,14 @@ public static <T, U> InternalFuture<U> thenApply( Future<T> future, Bootstrap bo
4747

4848
public static <T, U> InternalFuture<U> thenCombine( InternalFuture<T> future, Function<T,InternalFuture<U>> fn )
4949
{
50-
InternalPromise<U> result = new InternalPromise<>( future.eventLoop() );
50+
InternalPromise<U> result = new InternalPromise<>( future.eventExecutor() );
5151
future.addListener( new ThenCombineListener<>( result, fn ) );
5252
return result;
5353
}
5454

5555
public static <T> InternalFuture<T> whenComplete( InternalFuture<T> future, Runnable action )
5656
{
57-
InternalPromise<T> result = new InternalPromise<>( future.eventLoop() );
57+
InternalPromise<T> result = new InternalPromise<>( future.eventExecutor() );
5858
future.addListener( new CompletionListener<>( result, action ) );
5959
return result;
6060
}

driver/src/main/java/org/neo4j/driver/internal/async/InternalFuture.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
*/
1919
package org.neo4j.driver.internal.async;
2020

21-
import io.netty.channel.EventLoop;
21+
import io.netty.util.concurrent.EventExecutor;
2222
import io.netty.util.concurrent.Future;
2323

2424
import org.neo4j.driver.v1.Response;
2525
import org.neo4j.driver.v1.util.Function;
2626

27-
public interface InternalFuture<T> extends Future<T>
27+
public interface InternalFuture<T> extends Future<T>, Response<T>
2828
{
29-
EventLoop eventLoop();
30-
31-
Response<T> asResponse();
29+
EventExecutor eventExecutor();
3230

3331
<U> InternalFuture<U> thenApply( Function<T,U> fn );
3432

driver/src/main/java/org/neo4j/driver/internal/async/InternalPromise.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,39 @@
1919
package org.neo4j.driver.internal.async;
2020

2121
import io.netty.bootstrap.Bootstrap;
22-
import io.netty.channel.EventLoop;
22+
import io.netty.util.concurrent.EventExecutor;
2323
import io.netty.util.concurrent.Future;
24+
import io.netty.util.concurrent.FutureListener;
2425
import io.netty.util.concurrent.GenericFutureListener;
2526
import io.netty.util.concurrent.Promise;
2627

2728
import java.util.concurrent.ExecutionException;
2829
import java.util.concurrent.TimeUnit;
2930
import java.util.concurrent.TimeoutException;
3031

31-
import org.neo4j.driver.v1.Response;
32+
import org.neo4j.driver.v1.ResponseListener;
3233
import org.neo4j.driver.v1.util.Function;
3334

3435
public class InternalPromise<T> implements InternalFuture<T>, Promise<T>
3536
{
36-
private final EventLoop eventLoop;
37+
private final EventExecutor eventExecutor;
3738
private final Promise<T> delegate;
3839

3940
public InternalPromise( Bootstrap bootstrap )
4041
{
4142
this( bootstrap.config().group().next() );
4243
}
4344

44-
public InternalPromise( EventLoop eventLoop )
45+
public InternalPromise( EventExecutor eventExecutor )
4546
{
46-
this.eventLoop = eventLoop;
47-
this.delegate = eventLoop.newPromise();
47+
this.eventExecutor = eventExecutor;
48+
this.delegate = eventExecutor.newPromise();
4849
}
4950

5051
@Override
51-
public EventLoop eventLoop()
52+
public EventExecutor eventExecutor()
5253
{
53-
return eventLoop;
54-
}
55-
56-
@Override
57-
public Response<T> asResponse()
58-
{
59-
return new InternalResponse<>( this );
54+
return eventExecutor;
6055
}
6156

6257
@Override
@@ -77,6 +72,26 @@ public InternalFuture<T> whenComplete( Runnable action )
7772
return Futures.whenComplete( this, action );
7873
}
7974

75+
@Override
76+
public void addListener( final ResponseListener<T> listener )
77+
{
78+
delegate.addListener( new FutureListener<T>()
79+
{
80+
@Override
81+
public void operationComplete( Future<T> future )
82+
{
83+
if ( future.isSuccess() )
84+
{
85+
listener.operationCompleted( future.getNow(), null );
86+
}
87+
else
88+
{
89+
listener.operationCompleted( null, future.cause() );
90+
}
91+
}
92+
} );
93+
}
94+
8095
@Override
8196
public InternalPromise<T> setSuccess( T result )
8297
{

driver/src/main/java/org/neo4j/driver/internal/async/InternalResponse.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

driver/src/main/java/org/neo4j/driver/internal/async/InternalStatementResultCursor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public List<String> keys()
4949
@Override
5050
public Response<ResultSummary> summaryAsync()
5151
{
52-
return pullAllHandler.summaryAsync().asResponse();
52+
return pullAllHandler.summaryAsync();
5353
}
5454

5555
@Override
5656
public Response<Boolean> fetchAsync()
5757
{
58-
return pullAllHandler.fetchRecordAsync().asResponse();
58+
return pullAllHandler.fetchRecordAsync();
5959
}
6060

6161
@Override

0 commit comments

Comments
 (0)