Skip to content

Commit c52af42

Browse files
committed
Merge branch '1.0' into 1.0-tck-tests
2 parents 5f55135 + 45cf932 commit c52af42

28 files changed

+759
-447
lines changed

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
24-
import org.neo4j.driver.v1.Function;
25-
import org.neo4j.driver.v1.Record;
26-
import org.neo4j.driver.v1.RecordAccessor;
27-
import org.neo4j.driver.v1.ResultCursor;
28-
import org.neo4j.driver.v1.ResultSummary;
29-
import org.neo4j.driver.v1.Value;
21+
import org.neo4j.driver.v1.*;
3022
import org.neo4j.driver.v1.exceptions.ClientException;
3123
import org.neo4j.driver.v1.exceptions.NoSuchRecordException;
3224

25+
import java.util.ArrayList;
26+
import java.util.List;
27+
3328
import static java.lang.String.format;
3429
import static java.util.Collections.emptyList;
3530
import static org.neo4j.driver.v1.Records.recordAsIs;
@@ -178,7 +173,7 @@ else if ( records == 0) {
178173
@Override
179174
public Record first()
180175
{
181-
if( position() > 0 )
176+
if( position() >= 1 )
182177
{
183178
throw new NoSuchRecordException( "Cannot retrieve the first record, because this result cursor has been moved already. " +
184179
"Please ensure you are not calling `first` multiple times, or are mixing it with calls " +
@@ -197,6 +192,19 @@ public Record first()
197192
return record();
198193
}
199194

195+
196+
@Override
197+
public Value first(String fieldName) throws NoSuchRecordException
198+
{
199+
return first().get( fieldName );
200+
}
201+
202+
@Override
203+
public Value first(int index) throws NoSuchRecordException
204+
{
205+
return first().get( index );
206+
}
207+
200208
@Override
201209
public Record single()
202210
{
@@ -210,6 +218,18 @@ public Record single()
210218
return first;
211219
}
212220

221+
@Override
222+
public Value single( String fieldName ) throws NoSuchRecordException
223+
{
224+
return single().get( fieldName );
225+
}
226+
227+
@Override
228+
public Value single( int index ) throws NoSuchRecordException
229+
{
230+
return single().get( index );
231+
}
232+
213233
@Override
214234
public Record peek()
215235
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ protected void finalize() throws Throwable
139139
"method on Sessions before disposing of the objects.", null );
140140
connection.close();
141141
}
142+
super.finalize();
142143
}
143144

144145
private void ensureNoOpenTransaction()

driver/src/main/java/org/neo4j/driver/internal/pool/InternalConnectionPool.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,22 @@ public class InternalConnectionPool implements ConnectionPool
6565
*/
6666
private final ValidationStrategy<PooledConnection> connectionValidation;
6767

68+
/**
69+
* Timeout in milliseconds if there are no available sessions.
70+
*/
71+
private final long acquireSessionTimeout;
72+
6873
private final Clock clock;
6974
private final Config config;
7075

7176
public InternalConnectionPool( Config config )
7277
{
73-
this( loadConnectors(), Clock.SYSTEM, config );
78+
this( loadConnectors(), Clock.SYSTEM, config, Long.getLong( "neo4j.driver.acquireSessionTimeout", 30_000 ) );
7479
}
7580

76-
public InternalConnectionPool( Collection<Connector> conns, Clock clock, Config config )
81+
public InternalConnectionPool( Collection<Connector> conns, Clock clock, Config config, long acquireTimeout )
7782
{
83+
this.acquireSessionTimeout = acquireTimeout;
7884
this.config = config;
7985
this.clock = clock;
8086
this.connectionValidation = new PooledConnectionValidator( config.idleTimeBeforeConnectionTest() );
@@ -92,7 +98,7 @@ public Connection acquire( URI sessionURI )
9298
{
9399
try
94100
{
95-
Connection conn = pool( sessionURI ).acquire( 30, TimeUnit.SECONDS );
101+
Connection conn = pool( sessionURI ).acquire( acquireSessionTimeout, TimeUnit.MILLISECONDS );
96102
if( conn == null )
97103
{
98104
throw new ClientException(

driver/src/main/java/org/neo4j/driver/internal/summary/InternalNotification.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.neo4j.driver.v1.Notification;
2424
import org.neo4j.driver.v1.Value;
2525

26+
import static org.neo4j.driver.internal.value.NullValue.NULL;
27+
2628
public class InternalNotification implements Notification
2729
{
2830
public static final Function<Value, Notification> VALUE_TO_NOTIFICATION = new Function<Value,Notification>()
@@ -36,7 +38,7 @@ public Notification apply( Value value )
3638

3739
Value posValue = value.get( "position" );
3840
InputPosition position = null;
39-
if( posValue != null )
41+
if( posValue != NULL )
4042
{
4143
position = new InternalInputPosition( posValue.get( "offset" ).asInt(),
4244
posValue.get( "line" ).asInt(),

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,45 @@ public ConfigBuilder withLogging( Logging logging )
161161
}
162162

163163
/**
164-
* The max number of connections to open at any given time per Neo4j instance.
165-
* @param size the size of the connection pool
164+
* The max number of sessions to keep open at once. Configure this
165+
* higher if you want more concurrent sessions, or lower if you want
166+
* to lower the pressure on the database instance.
167+
*
168+
* If the driver is asked to provide more sessions than this, it will
169+
* block waiting for another session to be closed, with a timeout.
170+
*
171+
* @param size the max number of sessions to keep open
166172
* @return this builder
167173
*/
168-
public ConfigBuilder withConnectionPoolSize( int size )
174+
public ConfigBuilder withMaxSessions( int size )
169175
{
170176
this.connectionPoolSize = size;
171177
return this;
172178
}
173179

174180
/**
175-
* Pooled connections that have been unused for longer than this timeout will be tested before they are
176-
* used again, to ensure they are still live.
177-
* @param milliSecond minimum idle time in milliseconds
181+
* Pooled sessions that have been unused for longer than this timeout
182+
* will be tested before they are used again, to ensure they are still live.
183+
*
184+
* If this option is set too low, an additional network call will be
185+
* incurred when acquiring a session, which causes a performance hit.
186+
*
187+
* If this is set high, you may receive sessions that are no longer live,
188+
* which will lead to exceptions in your application. Assuming the
189+
* database is running, these exceptions will go away if you retry acquiring
190+
* sessions.
191+
*
192+
* Hence, this parameter tunes a balance between the likelihood of your
193+
* application seeing connection problems, and performance.
194+
*
195+
* You normally should not need to tune this parameter.
196+
*
197+
* @param timeout minimum idle time in milliseconds
178198
* @return this builder
179199
*/
180-
public ConfigBuilder withMinIdleTimeBeforeConnectionTest( long milliSecond )
200+
public ConfigBuilder withSessionLivenessCheckTimeout( long timeout )
181201
{
182-
this.idleTimeBeforeConnectionTest = milliSecond;
202+
this.idleTimeBeforeConnectionTest = timeout;
183203
return this;
184204
}
185205

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class GraphDatabase
3434
*/
3535
public static Driver driver( String url )
3636
{
37-
return driver( URI.create( url ) );
37+
return driver( url, Config.defaultConfig() );
3838
}
3939

4040
/**

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
*/
2626
public abstract class Records
2727
{
28-
private Records()
29-
{
30-
throw new UnsupportedOperationException();
31-
}
32-
3328
public static Function<RecordAccessor, Record> recordAsIs()
3429
{
3530
return RECORD;

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
*/
1919
package org.neo4j.driver.v1;
2020

21-
import java.util.List;
22-
2321
import org.neo4j.driver.v1.exceptions.ClientException;
2422
import org.neo4j.driver.v1.exceptions.NoSuchRecordException;
2523

24+
import java.util.List;
25+
2626

2727
/**
2828
* The result of running a statement, a stream of records represented as a cursor.
@@ -92,23 +92,65 @@ public interface ResultCursor extends RecordAccessor, Resource
9292

9393
/**
9494
* Return the first record in the stream. Fail with an exception if the stream is empty
95-
* or if this cursor has already been used to move "into" the stream.
95+
* or if this cursor has already been used to move past the first record.
9696
*
9797
* @return the first record in the stream
9898
* @throws NoSuchRecordException if there is no first record or the cursor has been used already
9999
*
100100
*/
101101
Record first() throws NoSuchRecordException;
102102

103+
/**
104+
* Return a field value from the first record in the stream. Fail with an exception if the stream is empty
105+
* or if this cursor has already been used to move past the first record.
106+
*
107+
* @param fieldName the name of the field to return from the first record in the stream
108+
* @return the specified field from the first record in the stream
109+
* @throws NoSuchRecordException if there is no first record or the cursor has been used already
110+
*
111+
*/
112+
Value first( String fieldName ) throws NoSuchRecordException;
113+
114+
/**
115+
* Return a field value from the first record in the stream. Fail with an exception if the stream is empty
116+
* or if this cursor has already been used to move past the first record.
117+
*
118+
* @param index the index of the field to return from the first record in the stream
119+
* @return the specified field from the first record in the stream
120+
* @throws NoSuchRecordException if there is no first record or the cursor has been used already
121+
*
122+
*/
123+
Value first( int index ) throws NoSuchRecordException;
124+
103125
/**
104126
* Move to the first record and return an immutable copy of it, failing if there is not exactly
105-
* one record in the stream, or if this cursor has already been used to move "into" the stream.
127+
* one record in the stream, or if this cursor has already been used to move past the first record.
106128
*
107129
* @return the first and only record in the stream
108130
* @throws NoSuchRecordException if there is not exactly one record in the stream, or if the cursor has been used already
109131
*/
110132
Record single() throws NoSuchRecordException;
111133

134+
/**
135+
* Move to the first record and return a field value from it, failing if there is not exactly
136+
* one record in the stream, or if this cursor has already been used to move past the first record.
137+
*
138+
* @param fieldName the name of the field to return from the first and only record in the stream
139+
* @return the value of the specified field of the first and only record in the stream
140+
* @throws NoSuchRecordException if there is not exactly one record in the stream, or if the cursor has been used already
141+
*/
142+
Value single( String fieldName ) throws NoSuchRecordException;
143+
144+
/**
145+
* Move to the first record and return a field value from it, failing if there is not exactly
146+
* one record in the stream, or if this cursor has already been used to move past the first record.
147+
*
148+
* @param index the index of the field to return from the first and only record in the stream
149+
* @return the value of the specified field of the first and only record in the stream
150+
* @throws NoSuchRecordException if there is not exactly one record in the stream, or if the cursor has been used already
151+
*/
152+
Value single( int index ) throws NoSuchRecordException;
153+
112154
/**
113155
* Investigate the next upcoming record without changing the position of this cursor.
114156
*
@@ -157,4 +199,4 @@ public interface ResultCursor extends RecordAccessor, Resource
157199
* @return a summary for the whole query
158200
*/
159201
ResultSummary summarize();
160-
}
202+
}

driver/src/main/java/org/neo4j/driver/v1/exceptions/DatabaseException.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
*/
2525
public class DatabaseException extends Neo4jException
2626
{
27-
public DatabaseException( String message )
28-
{
29-
super( message );
30-
}
31-
3227
public DatabaseException( String code, String message )
3328
{
3429
super( code, message );

driver/src/main/java/org/neo4j/driver/v1/exceptions/TransientException.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
*/
2525
public class TransientException extends Neo4jException
2626
{
27-
public TransientException( String message )
28-
{
29-
super( message );
30-
}
31-
3227
public TransientException( String code, String message )
3328
{
3429
super( code, message );

driver/src/test/java/org/neo4j/driver/internal/ConfigTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21-
import java.io.File;
22-
2321
import org.junit.Test;
2422

23+
import java.io.File;
24+
2525
import org.neo4j.driver.v1.Config;
2626

27+
import static org.hamcrest.CoreMatchers.equalTo;
2728
import static org.junit.Assert.assertEquals;
2829
import static org.junit.Assert.assertFalse;
30+
import static org.junit.Assert.assertThat;
2931
import static org.junit.Assert.assertTrue;
3032

3133
public class ConfigTest
@@ -76,6 +78,16 @@ public void shouldChangeToTrustedCert()
7678
assertEquals( trustedCert.getAbsolutePath(), authConfig.certFile().getAbsolutePath() );
7779
}
7880

81+
@Test
82+
public void shouldConfigureMinIdleTime() throws Throwable
83+
{
84+
// when
85+
Config config = Config.build().withSessionLivenessCheckTimeout( 1337 ).toConfig();
86+
87+
// then
88+
assertThat( config.idleTimeBeforeConnectionTest(), equalTo( 1337l ));
89+
}
90+
7991
public static void deleteDefaultKnownCertFileIfExists()
8092
{
8193
if( DEFAULT_KNOWN_CERTS.exists() )

0 commit comments

Comments
 (0)