Skip to content

Commit 13294cd

Browse files
authored
Merge pull request #275 from zhenlineo/1.1-summary-in-result
Added StatementResult#summary
2 parents 8381e64 + d40e715 commit 13294cd

File tree

5 files changed

+108
-28
lines changed

5 files changed

+108
-28
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ public ResultSummary consume()
296296
return summary;
297297
}
298298

299+
@Override
300+
public ResultSummary summary()
301+
{
302+
while( !done )
303+
{
304+
connection.receiveOne();
305+
}
306+
307+
return summary;
308+
}
309+
299310
@Override
300311
public void remove()
301312
{

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ public ResultSummary consume()
192192
}
193193
}
194194

195+
@Override
196+
public ResultSummary summary()
197+
{
198+
try
199+
{
200+
return delegate.summary();
201+
}
202+
catch ( ServiceUnavailableException e )
203+
{
204+
throw sessionExpired( e, onError, address );
205+
}
206+
catch ( ClientException e )
207+
{
208+
throw filterFailureToWrite( e, mode, onError, address );
209+
}
210+
}
211+
195212
public BoltServerAddress address()
196213
{
197214
return address;

driver/src/main/java/org/neo4j/driver/v1/StatementResult.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,17 @@ public interface StatementResult extends Iterator<Record>
142142
* @return a summary for the whole query result
143143
*/
144144
ResultSummary consume();
145+
146+
/**
147+
* Return the result summary.
148+
*
149+
* If the records in the result is not fully consumed, then calling this method will force to pull all remaining
150+
* records into buffer to yield the summary.
151+
*
152+
* If you want to obtain the summary but discard the records, use
153+
* {@link StatementResult#consume()} instead.
154+
*
155+
* @return a summary for the whole query result.
156+
*/
157+
ResultSummary summary();
145158
}

driver/src/test/java/org/neo4j/driver/v1/integration/ResultStreamIT.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323

2424
import org.neo4j.driver.v1.Record;
2525
import org.neo4j.driver.v1.StatementResult;
26+
import org.neo4j.driver.v1.summary.ResultSummary;
2627
import org.neo4j.driver.v1.util.TestNeo4jSession;
2728

29+
import static org.hamcrest.Matchers.equalTo;
30+
import static org.hamcrest.Matchers.notNullValue;
2831
import static org.junit.Assert.assertEquals;
2932
import static org.junit.Assert.assertNotNull;
33+
import static org.junit.Assert.assertThat;
3034
import static org.junit.Assert.assertTrue;
3135
import static org.neo4j.driver.v1.Values.parameters;
3236

@@ -120,4 +124,67 @@ public void shouldBeAbleToReuseSessionAfterFailure() throws Throwable
120124
assertTrue( res2.hasNext() );
121125
assertEquals( res2.next().get("1").asLong(), 1L );
122126
}
127+
128+
@Test
129+
public void shouldBeAbleToAccessSummaryAfterFailure() throws Throwable
130+
{
131+
// Given
132+
StatementResult res1 = session.run( "INVALID" );
133+
ResultSummary summary;
134+
135+
// When
136+
try
137+
{
138+
res1.consume();
139+
}
140+
catch ( Exception e )
141+
{
142+
//ignore
143+
}
144+
finally
145+
{
146+
summary = res1.summary();
147+
}
148+
149+
// Then
150+
assertThat( summary, notNullValue() );
151+
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
152+
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );
153+
}
154+
155+
156+
@Test
157+
public void shouldBufferRecordsAfterSummary() throws Throwable
158+
{
159+
// Given
160+
StatementResult result = session.run("UNWIND [1,2] AS a RETURN a");
161+
162+
// When
163+
ResultSummary summary = result.summary();
164+
165+
// Then
166+
assertThat( summary, notNullValue() );
167+
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
168+
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );
169+
170+
assertThat( result.next().get( "a" ).asInt(), equalTo( 1 ) );
171+
assertThat( result.next().get( "a" ).asInt(), equalTo( 2 ) );
172+
}
173+
174+
@Test
175+
public void shouldDiscardRecordsAfterConsume() throws Throwable
176+
{
177+
// Given
178+
StatementResult result = session.run("UNWIND [1,2] AS a RETURN a");
179+
180+
// When
181+
ResultSummary summary = result.consume();
182+
183+
// Then
184+
assertThat( summary, notNullValue() );
185+
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
186+
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );
187+
188+
assertThat( result.hasNext(), equalTo( false ) );
189+
}
123190
}

driver/src/test/java/org/neo4j/driver/v1/integration/SummaryIT.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,32 +174,4 @@ public void shouldContainNotifications() throws Throwable
174174
assertThat( notifications.get( 0 ).toString(), containsString("CartesianProduct") );
175175

176176
}
177-
178-
179-
@Test
180-
public void shouldBeAbleToAccessSummaryAfterFailure() throws Throwable
181-
{
182-
// Given
183-
StatementResult res1 = session.run( "INVALID" );
184-
ResultSummary summary;
185-
186-
// When
187-
try
188-
{
189-
res1.consume();
190-
}
191-
catch ( Exception e )
192-
{
193-
//ignore
194-
}
195-
finally
196-
{
197-
summary = res1.consume();
198-
}
199-
200-
// Then
201-
assertThat( summary, notNullValue() );
202-
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
203-
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );
204-
}
205177
}

0 commit comments

Comments
 (0)