Skip to content

Commit 1e4697e

Browse files
committed
Updated to new API changes
1 parent 55550a5 commit 1e4697e

20 files changed

+763
-139
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ public StatementResult run( String statementText )
6969
@Override
7070
public StatementResult run( String statementText, Map<String, Object> statementParameters )
7171
{
72-
return run( statementText, value( statementParameters ) );
72+
Value params = statementParameters == null ? Values.EmptyMap : value(statementParameters);
73+
return run( statementText, params );
7374
}
7475

7576
@Override
7677
public StatementResult run( String statementTemplate, Record statementParameters )
7778
{
78-
// TODO: This conversion to map here is pointless, it gets converted right back
79-
return run( statementTemplate, statementParameters.asMap() );
79+
Value params = statementParameters == null ? Values.EmptyMap : value( statementParameters.asMap() );
80+
return run( statementTemplate, params );
8081
}
8182

8283
@Override

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

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class InternalStatementResult implements StatementResult
5454
private List<String> keys = null;
5555
private ResultSummary summary = null;
5656

57-
private boolean open = true;
5857
private long position = -1;
5958
private boolean done = false;
6059

@@ -169,26 +168,17 @@ StreamCollector pullAllResponseCollector()
169168
@Override
170169
public List<String> keys()
171170
{
172-
tryFetching();
171+
if ( keys == null )
172+
{
173+
tryFetchNext();
174+
}
173175
return keys;
174176
}
175177

176178
@Override
177179
public boolean hasNext()
178180
{
179-
if (!recordBuffer.isEmpty())
180-
{
181-
return true;
182-
}
183-
else if (done)
184-
{
185-
return false;
186-
}
187-
else
188-
{
189-
tryFetching();
190-
return hasNext();
191-
}
181+
return tryFetchNext();
192182
}
193183

194184
@Override
@@ -200,67 +190,52 @@ public Record next()
200190
// in a way that makes the two equivalent in performance.
201191
// To get the intended benefit, we need to allocate Record in this method,
202192
// and have it copy out its fields from some lower level data structure.
203-
assertOpen();
204-
Record nextRecord = recordBuffer.poll();
205-
if ( nextRecord != null )
193+
if ( tryFetchNext() )
206194
{
207195
position += 1;
208-
return nextRecord;
209-
}
210-
else if ( done )
211-
{
212-
return null;
196+
return recordBuffer.poll();
213197
}
214198
else
215199
{
216-
tryFetching();
217-
return next();
200+
throw new NoSuchRecordException( "No more records" );
218201
}
219202
}
220203

221204
@Override
222205
public Record single()
223206
{
224-
if( position != -1 )
207+
if ( hasNext() )
225208
{
226-
throw new NoSuchRecordException(
227-
"Cannot retrieve the first record, because other operations have already used the first record. " +
228-
"Please ensure you are not calling `first` multiple times, or are mixing it with calls " +
229-
"to `next`, `single`, `list` or any other method that changes the position of the cursor." );
230-
}
209+
Record single = next();
210+
boolean hasMoreThanOne = hasNext();
231211

232-
if( !hasNext() )
233-
{
234-
throw new NoSuchRecordException( "Cannot retrieve the first record, because this result is empty." );
235-
}
212+
consume();
236213

237-
Record first = next();
238-
if( hasNext() )
214+
if ( hasMoreThanOne )
215+
{
216+
throw new NoSuchRecordException( "Expected a result with a single record, but this result contains at least one more. " +
217+
"Ensure your query returns only one record, or use `first` instead of `single` if " +
218+
"you do not care about the number of records in the result." );
219+
}
220+
221+
return single;
222+
}
223+
else
239224
{
240-
throw new NoSuchRecordException( "Expected a result with a single record, but this result contains at least one more. " +
241-
"Ensure your query returns only one record, or use `first` instead of `single` if " +
242-
"you do not care about the number of records in the result." );
225+
throw new NoSuchRecordException( "Cannot retrieve a single record, because this result is empty." );
243226
}
244-
return first;
245227
}
246228

247229
@Override
248230
public Record peek()
249231
{
250-
assertOpen();
251-
Record nextRecord = recordBuffer.peek();
252-
if ( nextRecord != null )
232+
if ( tryFetchNext() )
253233
{
254-
return nextRecord;
255-
}
256-
else if ( done )
257-
{
258-
return null;
234+
return recordBuffer.peek();
259235
}
260236
else
261237
{
262-
tryFetching();
263-
return peek();
238+
throw new NoSuchRecordException( "Cannot peek past the last record" );
264239
}
265240
}
266241

@@ -276,37 +251,38 @@ public <T> List<T> list( Function<Record, T> mapFunction )
276251
if ( hasNext() )
277252
{
278253
List<T> result = new ArrayList<>();
254+
279255
do
280256
{
281257
result.add( mapFunction.apply( next() ) );
282258
}
283259
while ( hasNext() );
284260

285-
consume();
286261
return result;
287262
}
288263
else
289264
{
290-
assertOpen();
291265
return emptyList();
292266
}
293267
}
294268

295-
@SuppressWarnings("StatementWithEmptyBody")
296269
@Override
297270
public ResultSummary consume()
298271
{
299-
if(!open)
272+
if ( done )
300273
{
301-
return summary;
274+
recordBuffer.clear();
302275
}
303-
304-
while ( !done )
276+
else
305277
{
306-
connection.receiveOne();
278+
do
279+
{
280+
connection.receiveOne();
281+
recordBuffer.clear();
282+
}
283+
while ( !done );
307284
}
308-
recordBuffer.clear();
309-
open = false;
285+
310286
return summary;
311287
}
312288

@@ -316,20 +292,17 @@ public void remove()
316292
throw new ClientException( "Removing records from a result is not supported." );
317293
}
318294

319-
private void assertOpen()
320-
{
321-
if ( !open )
322-
{
323-
throw new ClientException( "Result has been closed" );
324-
}
325-
}
326-
327-
private void tryFetching()
295+
private boolean tryFetchNext()
328296
{
329-
while ( recordBuffer.isEmpty() && !done )
297+
while ( recordBuffer.isEmpty() )
330298
{
299+
if ( done )
300+
{
301+
return false;
302+
}
331303
connection.receiveOne();
332304
}
333-
}
334305

306+
return true;
307+
}
335308
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.neo4j.driver.v1.types.TypeSystem;
3636

3737
import static org.neo4j.driver.v1.Values.ofValue;
38+
import static org.neo4j.driver.v1.Values.value;
3839

3940
public class InternalTransaction implements Transaction
4041
{
@@ -142,14 +143,15 @@ public StatementResult run( String statementText )
142143
@Override
143144
public StatementResult run( String statementText, Map<String,Object> statementParameters )
144145
{
145-
return run( statementText, Values.value( statementParameters ) );
146+
Value params = statementParameters == null ? Values.EmptyMap : value(statementParameters);
147+
return run( statementText, params );
146148
}
147149

148150
@Override
149151
public StatementResult run( String statementTemplate, Record statementParameters )
150152
{
151-
// TODO: This conversion to map here is pointless, it gets converted right back
152-
return run( statementTemplate, statementParameters.asMap() );
153+
Value params = statementParameters == null ? Values.EmptyMap : value( statementParameters.asMap() );
154+
return run( statementTemplate, params );
153155
}
154156

155157
@Override

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
22
* Copyright (c) 2002-2016 "Neo Technology,"
33
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4-
*
4+
* <p>
55
* This file is part of Neo4j.
6-
*
6+
* <p>
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
11-
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
10+
* <p>
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* <p>
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -38,6 +38,8 @@
3838
import org.neo4j.driver.v1.exceptions.ClientException;
3939
import org.neo4j.driver.v1.exceptions.Neo4jException;
4040

41+
import static java.lang.String.format;
42+
4143
/**
4244
* A basic connection pool that optimizes for threads being long-lived, acquiring/releasing many connections.
4345
* It uses a global queue as a fallback pool, but tries to avoid coordination by storing connections in a ThreadLocal.
@@ -105,11 +107,11 @@ public Connection acquire( URI sessionURI )
105107
try
106108
{
107109
Connection conn = pool( sessionURI ).acquire( acquireSessionTimeout, TimeUnit.MILLISECONDS );
108-
if( conn == null )
110+
if ( conn == null )
109111
{
110112
throw new ClientException(
111113
"Failed to acquire a session with Neo4j " +
112-
"as all the connections in the connection pool are already occupied by other sessions. "+
114+
"as all the connections in the connection pool are already occupied by other sessions. " +
113115
"Please close unused session and retry. " +
114116
"Current Pool size: " + config.connectionPoolSize() +
115117
". If your application requires running more sessions concurrently than the current pool " +
@@ -183,8 +185,8 @@ public PooledConnection allocate( Consumer<PooledConnection> release )
183185
if ( connector == null )
184186
{
185187
throw new ClientException(
186-
"'" + uri.getScheme() + "' is not a supported transport (in '" +
187-
uri + "', available transports are: " + connectorSchemes() + "." );
188+
format( "Unsupported transport: '%s' in url: '%s'. Supported transports are: '%s'.",
189+
uri.getScheme(), uri, connectorSchemes() ) );
188190
}
189191
Connection conn = connector.connect( uri, config, authToken );
190192
return new PooledConnection( conn, release );

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public static Driver driver( String url, AuthToken authToken, Config config )
122122
*/
123123
public static Driver driver( URI url, AuthToken authToken, Config config )
124124
{
125-
return new InternalDriver( url, authToken, config );
125+
AuthToken tokenToUse = authToken != null ? authToken: AuthTokens.none();
126+
Config configToUse = config != null ? config: Config.defaultConfig();
127+
128+
return new InternalDriver( url, tokenToUse, configToUse );
126129
}
127130
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323

24-
import org.neo4j.driver.v1.exceptions.ClientException;
2524
import org.neo4j.driver.v1.exceptions.NoSuchRecordException;
2625
import org.neo4j.driver.v1.summary.ResultSummary;
2726
import org.neo4j.driver.v1.util.Function;
@@ -70,23 +69,28 @@ public interface StatementResult extends Iterator<Record>
7069

7170
/**
7271
* Navigate to and retrieve the next {@link Record} in this result.
72+
*
73+
* @throws NoSuchRecordException if there is no record left in the stream
7374
* @return the next record
7475
*/
7576
@Override Record next();
7677

7778
/**
7879
* Return the first record in the result, failing if there is not exactly
79-
* one record, or if this result has already been used to move past the first record.
80+
* one record left in the stream
81+
*
82+
* Calling this method always exhausts the result, even when {@link NoSuchRecordException} is thrown.
8083
*
8184
* @return the first and only record in the stream
82-
* @throws NoSuchRecordException if there is not exactly one record in the stream, or if the cursor has been used already
85+
* @throws NoSuchRecordException if there is not exactly one record left in the stream
8386
*/
8487
Record single() throws NoSuchRecordException;
8588

8689
/**
8790
* Investigate the next upcoming record without moving forward in the result.
8891
*
89-
* @return the next record, or null if there is no next record
92+
* @throws NoSuchRecordException if there is no record left in the stream
93+
* @return the next record
9094
*/
9195
Record peek();
9296

@@ -102,8 +106,7 @@ public interface StatementResult extends Iterator<Record>
102106
*
103107
* Calling this method exhausts the result.
104108
*
105-
* @throws ClientException if the result has already been used
106-
* @return list of all immutable records
109+
* @return list of all remaining immutable records
107110
*/
108111
List<Record> list();
109112

@@ -119,11 +122,10 @@ public interface StatementResult extends Iterator<Record>
119122
*
120123
* Calling this method exhausts the result.
121124
*
122-
* @throws ClientException if the result has already been used
123125
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
124126
* as {@link Values#ofBoolean()}, {@link Values#ofList(Function)}.
125127
* @param <T> the type of result list elements
126-
* @return list of all mapped immutable records
128+
* @return list of all mapped remaining immutable records
127129
*/
128130
<T> List<T> list( Function<Record, T> mapFunction );
129131

@@ -138,7 +140,7 @@ public interface StatementResult extends Iterator<Record>
138140
* }
139141
* </pre>
140142
*
141-
* @return a summary for the whole query
143+
* @return a summary for the whole query result
142144
*/
143145
ResultSummary consume();
144-
}
146+
}

0 commit comments

Comments
 (0)