Skip to content

Commit 13aaebf

Browse files
committed
Simplify summary handling
Summary is created when driver receives SUCCESS or FAILURE for PULL_ALL message. It previously had it's own special handling of SUCCESS and FAILURE in the response handler. This commit simplifies it by making summary piggyback on FAILURE.
1 parent a3c62a7 commit 13aaebf

File tree

1 file changed

+16
-61
lines changed

1 file changed

+16
-61
lines changed

driver/src/main/java/org/neo4j/driver/internal/handlers/PullAllResponseHandler.java

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.neo4j.driver.internal.InternalRecord;
2828
import org.neo4j.driver.internal.spi.Connection;
2929
import org.neo4j.driver.internal.spi.ResponseHandler;
30+
import org.neo4j.driver.internal.util.Futures;
3031
import org.neo4j.driver.internal.util.MetadataUtil;
3132
import org.neo4j.driver.v1.Record;
3233
import org.neo4j.driver.v1.Statement;
@@ -55,7 +56,6 @@ public abstract class PullAllResponseHandler implements ResponseHandler
5556
private ResultSummary summary;
5657

5758
private CompletableFuture<Record> recordFuture;
58-
private CompletableFuture<ResultSummary> summaryFuture;
5959
private CompletableFuture<Throwable> failureFuture;
6060

6161
public PullAllResponseHandler( Statement statement, RunResponseHandler runResponseHandler, Connection connection )
@@ -74,7 +74,6 @@ public synchronized void onSuccess( Map<String,Value> metadata )
7474
afterSuccess();
7575

7676
completeRecordFuture( null );
77-
completeSummaryFuture( summary );
7877
completeFailureFuture( null );
7978
}
8079

@@ -91,26 +90,16 @@ public synchronized void onFailure( Throwable error )
9190
boolean failedRecordFuture = failRecordFuture( error );
9291
if ( failedRecordFuture )
9392
{
94-
// error propagated through record future, complete other two
95-
completeSummaryFuture( summary );
93+
// error propagated through the record future
9694
completeFailureFuture( null );
9795
}
9896
else
9997
{
100-
boolean failedSummaryFuture = failSummaryFuture( error );
101-
if ( failedSummaryFuture )
98+
boolean completedFailureFuture = completeFailureFuture( error );
99+
if ( !completedFailureFuture )
102100
{
103-
// error propagated through summary future, complete other one
104-
completeFailureFuture( null );
105-
}
106-
else
107-
{
108-
boolean completedFailureFuture = completeFailureFuture( error );
109-
if ( !completedFailureFuture )
110-
{
111-
// error has not been propagated to the user, remember it
112-
failure = error;
113-
}
101+
// error has not been propagated to the user, remember it
102+
failure = error;
114103
}
115104
}
116105
}
@@ -121,7 +110,7 @@ public synchronized void onFailure( Throwable error )
121110
public synchronized void onRecord( Value[] fields )
122111
{
123112
Record record = new InternalRecord( runResponseHandler.statementKeys(), fields );
124-
queueRecord( record );
113+
enqueueRecord( record );
125114
completeRecordFuture( record );
126115
}
127116

@@ -159,26 +148,14 @@ public synchronized CompletionStage<Record> nextAsync()
159148

160149
public synchronized CompletionStage<ResultSummary> summaryAsync()
161150
{
162-
if ( failure != null )
151+
return failureAsync().thenApply( error ->
163152
{
164-
return failedFuture( extractFailure() );
165-
}
166-
else if ( summary != null )
167-
{
168-
return completedFuture( summary );
169-
}
170-
else
171-
{
172-
if ( summaryFuture == null )
153+
if ( error != null )
173154
{
174-
// neither SUCCESS nor FAILURE message has arrived, register future to be notified when it arrives
175-
// future will be completed with summary on SUCCESS and completed exceptionally on FAILURE
176-
// enable auto-read, otherwise we might not read SUCCESS/FAILURE if records are not consumed
177-
connection.enableAutoRead();
178-
summaryFuture = new CompletableFuture<>();
155+
throw Futures.asCompletionException( error );
179156
}
180-
return summaryFuture;
181-
}
157+
return summary;
158+
} );
182159
}
183160

184161
public synchronized CompletionStage<Throwable> failureAsync()
@@ -205,14 +182,14 @@ else if ( finished )
205182
}
206183
}
207184

208-
private void queueRecord( Record record )
185+
private void enqueueRecord( Record record )
209186
{
210187
records.add( record );
211188

212-
boolean shouldBufferAllRecords = summaryFuture != null || failureFuture != null;
213-
// when summary or failure is requested we have to buffer all remaining records and then return summary/failure
189+
boolean shouldBufferAllRecords = failureFuture != null;
190+
// when failure is requested we have to buffer all remaining records and then return the error
214191
// do not disable auto-read in this case, otherwise records will not be consumed and trailing
215-
// SUCCESS or FAILURE message will not arrive as well, so callers will get stuck waiting for summary/failure
192+
// SUCCESS or FAILURE message will not arrive as well, so callers will get stuck waiting for the error
216193
if ( !shouldBufferAllRecords && records.size() > RECORD_BUFFER_HIGH_WATERMARK )
217194
{
218195
// more than high watermark records are already queued, tell connection to stop auto-reading from network
@@ -270,28 +247,6 @@ private boolean failRecordFuture( Throwable error )
270247
return false;
271248
}
272249

273-
private void completeSummaryFuture( ResultSummary summary )
274-
{
275-
if ( summaryFuture != null )
276-
{
277-
CompletableFuture<ResultSummary> future = summaryFuture;
278-
summaryFuture = null;
279-
future.complete( summary );
280-
}
281-
}
282-
283-
private boolean failSummaryFuture( Throwable error )
284-
{
285-
if ( summaryFuture != null )
286-
{
287-
CompletableFuture<ResultSummary> future = summaryFuture;
288-
summaryFuture = null;
289-
future.completeExceptionally( error );
290-
return true;
291-
}
292-
return false;
293-
}
294-
295250
private boolean completeFailureFuture( Throwable error )
296251
{
297252
if ( failureFuture != null )

0 commit comments

Comments
 (0)