Skip to content
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

Added StatementResult#summary #275

Merged
merged 1 commit into from
Nov 18, 2016
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 @@ -296,6 +296,17 @@ public ResultSummary consume()
return summary;
}

@Override
public ResultSummary summary()
{
while( !done )
{
connection.receiveOne();
}

return summary;
}

@Override
public void remove()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public ResultSummary consume()
}
}

@Override
public ResultSummary summary()
{
try
{
return delegate.summary();
}
catch ( ServiceUnavailableException e )
{
throw sessionExpired( e, onError, address );
}
catch ( ClientException e )
{
throw filterFailureToWrite( e, mode, onError, address );
}
}

public BoltServerAddress address()
{
return address;
Expand Down
13 changes: 13 additions & 0 deletions driver/src/main/java/org/neo4j/driver/v1/StatementResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,17 @@ public interface StatementResult extends Iterator<Record>
* @return a summary for the whole query result
*/
ResultSummary consume();

/**
* Return the result summary.
*
* If the records in the result is not fully consumed, then calling this method will force to pull all remaining
* records into buffer to yield the summary.
*
* If you want to obtain the summary but discard the records, use
* {@link StatementResult#consume()} instead.
*
* @return a summary for the whole query result.
*/
ResultSummary summary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@

import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.summary.ResultSummary;
import org.neo4j.driver.v1.util.TestNeo4jSession;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.neo4j.driver.v1.Values.parameters;

Expand Down Expand Up @@ -120,4 +124,67 @@ public void shouldBeAbleToReuseSessionAfterFailure() throws Throwable
assertTrue( res2.hasNext() );
assertEquals( res2.next().get("1").asLong(), 1L );
}

@Test
public void shouldBeAbleToAccessSummaryAfterFailure() throws Throwable
{
// Given
StatementResult res1 = session.run( "INVALID" );
ResultSummary summary;

// When
try
{
res1.consume();
}
catch ( Exception e )
{
//ignore
}
finally
{
summary = res1.summary();
}

// Then
assertThat( summary, notNullValue() );
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );
}


@Test
public void shouldBufferRecordsAfterSummary() throws Throwable
{
// Given
StatementResult result = session.run("UNWIND [1,2] AS a RETURN a");

// When
ResultSummary summary = result.summary();

// Then
assertThat( summary, notNullValue() );
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );

assertThat( result.next().get( "a" ).asInt(), equalTo( 1 ) );
assertThat( result.next().get( "a" ).asInt(), equalTo( 2 ) );
}

@Test
public void shouldDiscardRecordsAfterConsume() throws Throwable
{
// Given
StatementResult result = session.run("UNWIND [1,2] AS a RETURN a");

// When
ResultSummary summary = result.consume();

// Then
assertThat( summary, notNullValue() );
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );

assertThat( result.hasNext(), equalTo( false ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,32 +174,4 @@ public void shouldContainNotifications() throws Throwable
assertThat( notifications.get( 0 ).toString(), containsString("CartesianProduct") );

}


@Test
public void shouldBeAbleToAccessSummaryAfterFailure() throws Throwable
{
// Given
StatementResult res1 = session.run( "INVALID" );
ResultSummary summary;

// When
try
{
res1.consume();
}
catch ( Exception e )
{
//ignore
}
finally
{
summary = res1.consume();
}

// Then
assertThat( summary, notNullValue() );
assertThat( summary.server().address(), equalTo( "localhost:7687" ) );
assertThat( summary.counters().nodesCreated(), equalTo( 0 ) );
}
}