20
20
21
21
import java .util .Collections ;
22
22
import java .util .Map ;
23
+ import java .util .concurrent .CompletableFuture ;
24
+ import java .util .concurrent .CompletionStage ;
25
+ import java .util .function .BiConsumer ;
23
26
24
27
import org .neo4j .driver .ResultResourcesHandler ;
25
28
import org .neo4j .driver .internal .async .AsyncConnection ;
26
- import org .neo4j .driver .internal .async .InternalFuture ;
27
- import org .neo4j .driver .internal .async .InternalPromise ;
28
29
import org .neo4j .driver .internal .async .QueryRunner ;
29
30
import org .neo4j .driver .internal .handlers .BeginTxResponseHandler ;
30
31
import org .neo4j .driver .internal .handlers .BookmarkResponseHandler ;
33
34
import org .neo4j .driver .internal .handlers .RollbackTxResponseHandler ;
34
35
import org .neo4j .driver .internal .spi .Connection ;
35
36
import org .neo4j .driver .internal .types .InternalTypeSystem ;
36
- import org .neo4j .driver .internal .util .BiConsumer ;
37
37
import org .neo4j .driver .v1 .Record ;
38
- import org .neo4j .driver .v1 .Response ;
39
38
import org .neo4j .driver .v1 .Statement ;
40
39
import org .neo4j .driver .v1 .StatementResult ;
41
40
import org .neo4j .driver .v1 .StatementResultCursor ;
45
44
import org .neo4j .driver .v1 .exceptions .ClientException ;
46
45
import org .neo4j .driver .v1 .exceptions .Neo4jException ;
47
46
import org .neo4j .driver .v1 .types .TypeSystem ;
48
- import org .neo4j .driver .v1 .util .Function ;
49
47
48
+ import static java .util .concurrent .CompletableFuture .completedFuture ;
49
+ import static org .neo4j .driver .internal .async .Futures .failedFuture ;
50
50
import static org .neo4j .driver .internal .util .ErrorUtil .isRecoverable ;
51
51
import static org .neo4j .driver .v1 .Values .ofValue ;
52
52
import static org .neo4j .driver .v1 .Values .value ;
@@ -118,25 +118,23 @@ public void begin( Bookmark initialBookmark )
118
118
}
119
119
}
120
120
121
- public InternalFuture <ExplicitTransaction > beginAsync ( Bookmark initialBookmark )
121
+ public CompletionStage <ExplicitTransaction > beginAsync ( Bookmark initialBookmark )
122
122
{
123
- InternalPromise <ExplicitTransaction > beginTxPromise = asyncConnection .newPromise ();
124
-
125
123
Map <String ,Value > parameters = initialBookmark .asBeginTransactionParameters ();
126
124
asyncConnection .run ( BEGIN_QUERY , parameters , NoOpResponseHandler .INSTANCE );
127
125
128
126
if ( initialBookmark .isEmpty () )
129
127
{
130
128
asyncConnection .pullAll ( NoOpResponseHandler .INSTANCE );
131
- beginTxPromise . setSuccess ( this );
129
+ return completedFuture ( this );
132
130
}
133
131
else
134
132
{
135
- asyncConnection .pullAll ( new BeginTxResponseHandler <>( beginTxPromise , this ) );
133
+ CompletableFuture <ExplicitTransaction > beginFuture = new CompletableFuture <>();
134
+ asyncConnection .pullAll ( new BeginTxResponseHandler <>( beginFuture , this ) );
136
135
asyncConnection .flush ();
136
+ return beginFuture ;
137
137
}
138
-
139
- return beginTxPromise ;
140
138
}
141
139
142
140
@ Override
@@ -215,21 +213,15 @@ private void rollbackTx()
215
213
}
216
214
217
215
@ Override
218
- public Response <Void > commitAsync ()
219
- {
220
- return internalCommitAsync ();
221
- }
222
-
223
- InternalFuture <Void > internalCommitAsync ()
216
+ public CompletionStage <Void > commitAsync ()
224
217
{
225
218
if ( state == State .COMMITTED )
226
219
{
227
- return asyncConnection .< Void > newPromise (). setSuccess ( null );
220
+ return completedFuture ( null );
228
221
}
229
222
else if ( state == State .ROLLED_BACK )
230
223
{
231
- return asyncConnection .<Void >newPromise ().setFailure (
232
- new ClientException ( "Can't commit, transaction has already been rolled back" ) );
224
+ return failedFuture ( new ClientException ( "Can't commit, transaction has already been rolled back" ) );
233
225
}
234
226
else
235
227
{
@@ -238,21 +230,15 @@ else if ( state == State.ROLLED_BACK )
238
230
}
239
231
240
232
@ Override
241
- public Response <Void > rollbackAsync ()
242
- {
243
- return internalRollbackAsync ();
244
- }
245
-
246
- InternalFuture <Void > internalRollbackAsync ()
233
+ public CompletionStage <Void > rollbackAsync ()
247
234
{
248
235
if ( state == State .COMMITTED )
249
236
{
250
- return asyncConnection .<Void >newPromise ()
251
- .setFailure ( new ClientException ( "Can't rollback, transaction has already been committed" ) );
237
+ return failedFuture ( new ClientException ( "Can't rollback, transaction has already been committed" ) );
252
238
}
253
239
else if ( state == State .ROLLED_BACK )
254
240
{
255
- return asyncConnection .< Void > newPromise (). setSuccess ( null );
241
+ return completedFuture ( null );
256
242
}
257
243
else
258
244
{
@@ -262,51 +248,39 @@ else if ( state == State.ROLLED_BACK )
262
248
263
249
private BiConsumer <Void ,Throwable > releaseConnectionAndNotifySession ()
264
250
{
265
- return new BiConsumer < Void , Throwable >()
251
+ return ( ignore , error ) ->
266
252
{
267
- @ Override
268
- public void accept ( Void result , Throwable error )
269
- {
270
- asyncConnection .release ();
271
- session .asyncTransactionClosed ( ExplicitTransaction .this );
272
- }
253
+ asyncConnection .release ();
254
+ session .asyncTransactionClosed ( ExplicitTransaction .this );
273
255
};
274
256
}
275
257
276
- private InternalFuture <Void > doCommitAsync ()
258
+ private CompletionStage <Void > doCommitAsync ()
277
259
{
278
- InternalPromise <Void > commitTxPromise = asyncConnection . newPromise ();
260
+ CompletableFuture <Void > commitFuture = new CompletableFuture <> ();
279
261
280
- asyncConnection .run ( COMMIT_QUERY , Collections .< String , Value > emptyMap (), NoOpResponseHandler .INSTANCE );
281
- asyncConnection .pullAll ( new CommitTxResponseHandler ( commitTxPromise , this ) );
262
+ asyncConnection .run ( COMMIT_QUERY , Collections .emptyMap (), NoOpResponseHandler .INSTANCE );
263
+ asyncConnection .pullAll ( new CommitTxResponseHandler ( commitFuture , this ) );
282
264
asyncConnection .flush ();
283
265
284
- return commitTxPromise .thenApply ( new Function < Void , Void >()
266
+ return commitFuture .thenApply ( ignore ->
285
267
{
286
- @ Override
287
- public Void apply ( Void ignore )
288
- {
289
- ExplicitTransaction .this .state = State .COMMITTED ;
290
- return null ;
291
- }
268
+ ExplicitTransaction .this .state = State .COMMITTED ;
269
+ return null ;
292
270
} );
293
271
}
294
272
295
- private InternalFuture <Void > doRollbackAsync ()
273
+ private CompletionStage <Void > doRollbackAsync ()
296
274
{
297
- InternalPromise <Void > rollbackTxPromise = asyncConnection . newPromise ();
298
- asyncConnection .run ( ROLLBACK_QUERY , Collections .< String , Value > emptyMap (), NoOpResponseHandler .INSTANCE );
299
- asyncConnection .pullAll ( new RollbackTxResponseHandler ( rollbackTxPromise ) );
275
+ CompletableFuture <Void > rollbackFuture = new CompletableFuture <> ();
276
+ asyncConnection .run ( ROLLBACK_QUERY , Collections .emptyMap (), NoOpResponseHandler .INSTANCE );
277
+ asyncConnection .pullAll ( new RollbackTxResponseHandler ( rollbackFuture ) );
300
278
asyncConnection .flush ();
301
279
302
- return rollbackTxPromise .thenApply ( new Function < Void , Void >()
280
+ return rollbackFuture .thenApply ( ignore ->
303
281
{
304
- @ Override
305
- public Void apply ( Void ignore )
306
- {
307
- ExplicitTransaction .this .state = State .ROLLED_BACK ;
308
- return null ;
309
- }
282
+ ExplicitTransaction .this .state = State .ROLLED_BACK ;
283
+ return null ;
310
284
} );
311
285
}
312
286
@@ -317,7 +291,7 @@ public StatementResult run( String statementText, Value statementParameters )
317
291
}
318
292
319
293
@ Override
320
- public Response <StatementResultCursor > runAsync ( String statementText , Value parameters )
294
+ public CompletionStage <StatementResultCursor > runAsync ( String statementText , Value parameters )
321
295
{
322
296
return runAsync ( new Statement ( statementText , parameters ) );
323
297
}
@@ -329,7 +303,7 @@ public StatementResult run( String statementText )
329
303
}
330
304
331
305
@ Override
332
- public Response <StatementResultCursor > runAsync ( String statementTemplate )
306
+ public CompletionStage <StatementResultCursor > runAsync ( String statementTemplate )
333
307
{
334
308
return runAsync ( statementTemplate , Values .EmptyMap );
335
309
}
@@ -342,7 +316,8 @@ public StatementResult run( String statementText, Map<String,Object> statementPa
342
316
}
343
317
344
318
@ Override
345
- public Response <StatementResultCursor > runAsync ( String statementTemplate , Map <String ,Object > statementParameters )
319
+ public CompletionStage <StatementResultCursor > runAsync ( String statementTemplate ,
320
+ Map <String ,Object > statementParameters )
346
321
{
347
322
Value params = statementParameters == null ? Values .EmptyMap : value ( statementParameters );
348
323
return runAsync ( statementTemplate , params );
@@ -356,7 +331,7 @@ public StatementResult run( String statementTemplate, Record statementParameters
356
331
}
357
332
358
333
@ Override
359
- public Response <StatementResultCursor > runAsync ( String statementTemplate , Record statementParameters )
334
+ public CompletionStage <StatementResultCursor > runAsync ( String statementTemplate , Record statementParameters )
360
335
{
361
336
Value params = statementParameters == null ? Values .EmptyMap : value ( statementParameters .asMap () );
362
337
return runAsync ( statementTemplate , params );
@@ -388,7 +363,7 @@ public StatementResult run( Statement statement )
388
363
}
389
364
390
365
@ Override
391
- public Response <StatementResultCursor > runAsync ( Statement statement )
366
+ public CompletionStage <StatementResultCursor > runAsync ( Statement statement )
392
367
{
393
368
ensureNotFailed ();
394
369
return QueryRunner .runAsync ( asyncConnection , statement , this );
0 commit comments