From 3319ad184a5df1c4477fd4cb2d78510bf29a3085 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Tue, 14 Sep 2021 13:27:50 +0200 Subject: [PATCH 1/4] Only send qid with PULL and DISCARD when necessary --- neo4j/io/__init__.py | 5 +++++ neo4j/work/result.py | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/neo4j/io/__init__.py b/neo4j/io/__init__.py index 716170e6d..da3636a01 100644 --- a/neo4j/io/__init__.py +++ b/neo4j/io/__init__.py @@ -137,6 +137,11 @@ class Bolt(abc.ABC): #: The pool of which this connection is a member pool = None + # Store the id of the most recent ran query to be able reduce sent bits by + # using the default (-1) to refer to the most recent query when pulling + # results for it. + most_recent_qid = None + def __init__(self, unresolved_address, sock, max_connection_lifetime, *, auth=None, user_agent=None, routing_context=None): self.unresolved_address = unresolved_address self.socket = sock diff --git a/neo4j/work/result.py b/neo4j/work/result.py index e0c37159d..8f50561b0 100644 --- a/neo4j/work/result.py +++ b/neo4j/work/result.py @@ -48,11 +48,11 @@ def __init__(self, connection, on_error): connection raises of of the caught errors. :type on_error callable """ - self._connection = connection - self._on_error = on_error + self.connection = connection + self.on_error = on_error def __getattr__(self, item): - connection_attr = getattr(self._connection, item) + connection_attr = getattr(self.connection, item) if not callable(connection_attr): return connection_attr @@ -61,7 +61,7 @@ def inner(*args, **kwargs): try: func(*args, **kwargs) except (Neo4jError, ServiceUnavailable, SessionExpired) as exc: - self._on_error(exc) + self.on_error(exc) raise return inner @@ -83,7 +83,7 @@ def __init__(self, connection, hydrant, fetch_size, on_closed, self._record_buffer = deque() self._summary = None self._bookmark = None - self._qid = -1 + self._raw_qid = -1 self._fetch_size = fetch_size # states @@ -96,6 +96,13 @@ def __init__(self, connection, hydrant, fetch_size, on_closed, # the result has been fully iterated or consumed self._closed = False + @property + def _qid(self): + if self._raw_qid == self._connection.connection.most_recent_qid: + return -1 + else: + return self._raw_qid + def _tx_ready_run(self, query, parameters, **kwparameters): # BEGIN+RUN does not carry any extra on the RUN message. # BEGIN {extra} @@ -117,7 +124,10 @@ def _run(self, query, parameters, db, access_mode, bookmarks, **kwparameters): def on_attached(metadata): self._metadata.update(metadata) - self._qid = metadata.get("qid", -1) # For auto-commit there is no qid and Bolt 3 do not support qid + # For auto-commit there is no qid and Bolt 3 do not support qid + self._raw_qid = metadata.get("qid", -1) + if self._raw_qid != -1: + self._connection.connection.most_recent_qid = self._raw_qid self._keys = metadata.get("fields") self._attached = True From a642800ff382226da2fb2affc390b081d79db064 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Tue, 14 Sep 2021 15:10:44 +0200 Subject: [PATCH 2/4] Fix ConnectionStub in unit test --- tests/unit/work/test_result.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/work/test_result.py b/tests/unit/work/test_result.py index 56f92f524..7f2d15bb5 100644 --- a/tests/unit/work/test_result.py +++ b/tests/unit/work/test_result.py @@ -89,6 +89,7 @@ def __init__(self, records=None, run_meta=None, summary_meta=None, self._use_qid = force_qid self.fetch_idx = 0 self._qid = -1 + self.most_recent_qid = None self.record_idxs = [0] * len(self._records) self.to_pull = [None] * len(self._records) self._exhausted = [False] * len(self._records) From 72d490a3ff145568f23e198d9cec788377412d2d Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Tue, 14 Sep 2021 15:36:00 +0200 Subject: [PATCH 3/4] Update neo4j/io/__init__.py Co-authored-by: Florent Biville <445792+fbiville@users.noreply.github.com> --- neo4j/io/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo4j/io/__init__.py b/neo4j/io/__init__.py index da3636a01..65f120ccb 100644 --- a/neo4j/io/__init__.py +++ b/neo4j/io/__init__.py @@ -137,7 +137,7 @@ class Bolt(abc.ABC): #: The pool of which this connection is a member pool = None - # Store the id of the most recent ran query to be able reduce sent bits by + # Store the id of the most recent ran query to be able to reduce sent bits by # using the default (-1) to refer to the most recent query when pulling # results for it. most_recent_qid = None From 3cedf9440a25611eba94e54cde87338fa6945cd4 Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Tue, 14 Sep 2021 15:36:16 +0200 Subject: [PATCH 4/4] Update neo4j/work/result.py Co-authored-by: Florent Biville <445792+fbiville@users.noreply.github.com> --- neo4j/work/result.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo4j/work/result.py b/neo4j/work/result.py index 8f50561b0..de0246601 100644 --- a/neo4j/work/result.py +++ b/neo4j/work/result.py @@ -124,7 +124,7 @@ def _run(self, query, parameters, db, access_mode, bookmarks, **kwparameters): def on_attached(metadata): self._metadata.update(metadata) - # For auto-commit there is no qid and Bolt 3 do not support qid + # For auto-commit there is no qid and Bolt 3 does not support qid self._raw_qid = metadata.get("qid", -1) if self._raw_qid != -1: self._connection.connection.most_recent_qid = self._raw_qid