28
28
SessionExpired ,
29
29
)
30
30
from neo4j .work .summary import ResultSummary
31
+ from neo4j .exceptions import ResultConsumedError
31
32
32
33
33
34
class _ConnectionErrorHandler :
@@ -223,20 +224,37 @@ def __iter__(self):
223
224
self ._closed = True
224
225
225
226
def _attach (self ):
226
- """Sets the Result object in an attached state by fetching messages from the connection to the buffer.
227
+ """Sets the Result object in an attached state by fetching messages from
228
+ the connection to the buffer.
227
229
"""
228
230
if self ._closed is False :
229
231
while self ._attached is False :
230
232
self ._connection .fetch_message ()
231
233
232
- def _buffer_all (self ):
233
- """Sets the Result object in an detached state by fetching all records from the connection to the buffer.
234
+ def _buffer (self , n = None ):
235
+ """Try to fill `self_record_buffer` with n records.
236
+
237
+ Might end up with more records in the buffer if the fetch size makes it
238
+ overshoot.
239
+ Might ent up with fewer records in the buffer if there are not enough
240
+ records available.
234
241
"""
235
242
record_buffer = deque ()
236
243
for record in self :
237
244
record_buffer .append (record )
245
+ if n is not None and len (record_buffer ) >= n :
246
+ break
238
247
self ._closed = False
239
- self ._record_buffer = record_buffer
248
+ if n is None :
249
+ self ._record_buffer = record_buffer
250
+ else :
251
+ self ._record_buffer .extend (record_buffer )
252
+
253
+ def _buffer_all (self ):
254
+ """Sets the Result object in an detached state by fetching all records
255
+ from the connection to the buffer.
256
+ """
257
+ self ._buffer ()
240
258
241
259
def _obtain_summary (self ):
242
260
"""Obtain the summary of this result, buffering any remaining records.
@@ -309,6 +327,13 @@ def single(self):
309
327
:returns: the next :class:`neo4j.Record` or :const:`None` if none remain
310
328
:warns: if more than one record is available
311
329
"""
330
+ # TODO in 5.0 replace with this code that raises an error if there's not
331
+ # exactly one record in the left result stream.
332
+ # self._buffer(2).
333
+ # if len(self._record_buffer) != 1:
334
+ # raise SomeError("Expected exactly 1 record, found %i"
335
+ # % len(self._record_buffer))
336
+ # return self._record_buffer.popleft()
312
337
records = list (self ) # TODO: exhausts the result with self.consume if there are more records.
313
338
size = len (records )
314
339
if size == 0 :
@@ -323,16 +348,9 @@ def peek(self):
323
348
324
349
:returns: the next :class:`.Record` or :const:`None` if none remain
325
350
"""
351
+ self ._buffer (1 )
326
352
if self ._record_buffer :
327
353
return self ._record_buffer [0 ]
328
- if not self ._attached :
329
- return None
330
- while self ._attached :
331
- self ._connection .fetch_message ()
332
- if self ._record_buffer :
333
- return self ._record_buffer [0 ]
334
-
335
- return None
336
354
337
355
def graph (self ):
338
356
"""Return a :class:`neo4j.graph.Graph` instance containing all the graph objects
0 commit comments