Skip to content

Commit 752bcd3

Browse files
robsdedudefbiville
andauthored
Only send qid with PULL and DISCARD when necessary (#585)
Co-authored-by: Florent Biville <445792+fbiville@users.noreply.github.com>
1 parent fbefd15 commit 752bcd3

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

neo4j/io/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ class Bolt(abc.ABC):
137137
#: The pool of which this connection is a member
138138
pool = None
139139

140+
# Store the id of the most recent ran query to be able to reduce sent bits by
141+
# using the default (-1) to refer to the most recent query when pulling
142+
# results for it.
143+
most_recent_qid = None
144+
140145
def __init__(self, unresolved_address, sock, max_connection_lifetime, *, auth=None, user_agent=None, routing_context=None):
141146
self.unresolved_address = unresolved_address
142147
self.socket = sock

neo4j/work/result.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def __init__(self, connection, on_error):
4848
connection raises of of the caught errors.
4949
:type on_error callable
5050
"""
51-
self._connection = connection
52-
self._on_error = on_error
51+
self.connection = connection
52+
self.on_error = on_error
5353

5454
def __getattr__(self, item):
55-
connection_attr = getattr(self._connection, item)
55+
connection_attr = getattr(self.connection, item)
5656
if not callable(connection_attr):
5757
return connection_attr
5858

@@ -61,7 +61,7 @@ def inner(*args, **kwargs):
6161
try:
6262
func(*args, **kwargs)
6363
except (Neo4jError, ServiceUnavailable, SessionExpired) as exc:
64-
self._on_error(exc)
64+
self.on_error(exc)
6565
raise
6666
return inner
6767

@@ -83,7 +83,7 @@ def __init__(self, connection, hydrant, fetch_size, on_closed,
8383
self._record_buffer = deque()
8484
self._summary = None
8585
self._bookmark = None
86-
self._qid = -1
86+
self._raw_qid = -1
8787
self._fetch_size = fetch_size
8888

8989
# states
@@ -96,6 +96,13 @@ def __init__(self, connection, hydrant, fetch_size, on_closed,
9696
# the result has been fully iterated or consumed
9797
self._closed = False
9898

99+
@property
100+
def _qid(self):
101+
if self._raw_qid == self._connection.connection.most_recent_qid:
102+
return -1
103+
else:
104+
return self._raw_qid
105+
99106
def _tx_ready_run(self, query, parameters, **kwparameters):
100107
# BEGIN+RUN does not carry any extra on the RUN message.
101108
# BEGIN {extra}
@@ -117,7 +124,10 @@ def _run(self, query, parameters, db, access_mode, bookmarks, **kwparameters):
117124

118125
def on_attached(metadata):
119126
self._metadata.update(metadata)
120-
self._qid = metadata.get("qid", -1) # For auto-commit there is no qid and Bolt 3 do not support qid
127+
# For auto-commit there is no qid and Bolt 3 does not support qid
128+
self._raw_qid = metadata.get("qid", -1)
129+
if self._raw_qid != -1:
130+
self._connection.connection.most_recent_qid = self._raw_qid
121131
self._keys = metadata.get("fields")
122132
self._attached = True
123133

tests/unit/work/test_result.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def __init__(self, records=None, run_meta=None, summary_meta=None,
8989
self._use_qid = force_qid
9090
self.fetch_idx = 0
9191
self._qid = -1
92+
self.most_recent_qid = None
9293
self.record_idxs = [0] * len(self._records)
9394
self.to_pull = [None] * len(self._records)
9495
self._exhausted = [False] * len(self._records)

0 commit comments

Comments
 (0)