Skip to content

Commit 6774c46

Browse files
refactored routing protocol call into bolt classes (#440)
* refactored routing protocol call into bolt classes * fixed cx to self
1 parent 459e981 commit 6774c46

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

neo4j/io/__init__.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -686,43 +686,12 @@ def fail(md):
686686

687687
try:
688688
with self._acquire(address, timeout) as cx:
689-
_, _, server_version = (cx.server_info.agent or "").partition("/")
690689
log.debug("[#%04X] C: <ROUTING> query=%r", cx.local_port, self.routing_context or {})
691690

692691
if database is None:
693692
database = self.workspace_config.database
694693

695-
# TODO: This logic should be inside the Bolt subclasses, because it can change depending on Bolt Protocol Version.
696-
if cx.PROTOCOL_VERSION == Bolt3.PROTOCOL_VERSION:
697-
if database != DEFAULT_DATABASE:
698-
raise ConfigurationError("Database name parameter for selecting database is not supported in Bolt Protocol {!r}. Database name {!r}. Server Agent {!r}.".format(
699-
Bolt3.PROTOCOL_VERSION, database, cx.server_info.agent))
700-
cx.run(
701-
"CALL dbms.cluster.routing.getRoutingTable($context)", # This is an internal procedure call. Only available if the Neo4j 3.5 is setup with clustering.
702-
{"context": self.routing_context},
703-
mode="r", # Bolt Protocol Version(3, 0) supports mode
704-
on_success=metadata.update,
705-
on_failure=fail,
706-
)
707-
elif cx.PROTOCOL_VERSION in (Bolt4x0.PROTOCOL_VERSION, Bolt4x1.PROTOCOL_VERSION):
708-
if database == DEFAULT_DATABASE:
709-
cx.run(
710-
"CALL dbms.routing.getRoutingTable($context)",
711-
{"context": self.routing_context},
712-
mode="r",
713-
db=SYSTEM_DATABASE,
714-
on_success=metadata.update,
715-
on_failure=fail,
716-
)
717-
else:
718-
cx.run(
719-
"CALL dbms.routing.getRoutingTable($context, $database)",
720-
{"context": self.routing_context, "database": database},
721-
mode="r",
722-
db=SYSTEM_DATABASE,
723-
on_success=metadata.update,
724-
on_failure=fail,
725-
)
694+
cx.run_get_routing_table(on_success=metadata.update, on_failure=fail, database=database)
726695
cx.pull(on_success=metadata.update, on_records=records.extend)
727696
cx.send_all()
728697
cx.fetch_all()

neo4j/io/_bolt3.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
Version,
2828
READ_ACCESS,
2929
WRITE_ACCESS,
30+
DEFAULT_DATABASE,
3031
)
3132
from neo4j.io._courier import MessageInbox
3233
from neo4j.meta import get_user_agent
@@ -177,6 +178,17 @@ def run(self, query, parameters=None, mode=None, bookmarks=None, metadata=None,
177178
self._append(b"\x10", fields, Response(self, **handlers))
178179
self._is_reset = False
179180

181+
def run_get_routing_table(self, on_success, on_failure, database=DEFAULT_DATABASE):
182+
if database != DEFAULT_DATABASE:
183+
raise ConfigurationError("Database name parameter for selecting database is not supported in Bolt Protocol {!r}. Database name {!r}. Server Agent {!r}.".format(Bolt3.PROTOCOL_VERSION, database, self.server_info.agent))
184+
self.run(
185+
"CALL dbms.cluster.routing.getRoutingTable($context)", # This is an internal procedure call. Only available if the Neo4j 3.5 is setup with clustering.
186+
{"context": self.routing_context},
187+
mode="r", # Bolt Protocol Version(3, 0) supports mode="r"
188+
on_success=on_success,
189+
on_failure=on_failure,
190+
)
191+
180192
def discard(self, n=-1, qid=-1, **handlers):
181193
# Just ignore n and qid, it is not supported in the Bolt 3 Protocol.
182194
log.debug("[#%04X] C: DISCARD_ALL", self.local_port)

neo4j/io/_bolt4x0.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
Version,
2828
READ_ACCESS,
2929
WRITE_ACCESS,
30+
DEFAULT_DATABASE,
31+
SYSTEM_DATABASE,
3032
)
3133
from neo4j.io._courier import MessageInbox
3234
from neo4j.meta import get_user_agent
@@ -176,6 +178,26 @@ def run(self, query, parameters=None, mode=None, bookmarks=None, metadata=None,
176178
self._append(b"\x10", fields, Response(self, **handlers))
177179
self._is_reset = False
178180

181+
def run_get_routing_table(self, on_success, on_failure, database=DEFAULT_DATABASE):
182+
if database == DEFAULT_DATABASE:
183+
self.run(
184+
"CALL dbms.routing.getRoutingTable($context)",
185+
{"context": self.routing_context},
186+
mode="r",
187+
db=SYSTEM_DATABASE,
188+
on_success=on_success,
189+
on_failure=on_failure,
190+
)
191+
else:
192+
self.run(
193+
"CALL dbms.routing.getRoutingTable($context, $database)",
194+
{"context": self.routing_context, "database": database},
195+
mode="r",
196+
db=SYSTEM_DATABASE,
197+
on_success=on_success,
198+
on_failure=on_failure,
199+
)
200+
179201
def discard(self, n=-1, qid=-1, **handlers):
180202
extra = {"n": n}
181203
if qid != -1:

neo4j/io/_bolt4x1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
Version,
2828
READ_ACCESS,
2929
WRITE_ACCESS,
30+
DEFAULT_DATABASE,
31+
SYSTEM_DATABASE,
3032
)
3133
from neo4j.io._courier import MessageInbox
3234
from neo4j.meta import get_user_agent
@@ -177,6 +179,26 @@ def run(self, query, parameters=None, mode=None, bookmarks=None, metadata=None,
177179
self._append(b"\x10", fields, Response(self, **handlers))
178180
self._is_reset = False
179181

182+
def run_get_routing_table(self, on_success, on_failure, database=DEFAULT_DATABASE):
183+
if database == DEFAULT_DATABASE:
184+
self.run(
185+
"CALL dbms.routing.getRoutingTable($context)",
186+
{"context": self.routing_context},
187+
mode="r",
188+
db=SYSTEM_DATABASE,
189+
on_success=on_success,
190+
on_failure=on_failure,
191+
)
192+
else:
193+
self.run(
194+
"CALL dbms.routing.getRoutingTable($context, $database)",
195+
{"context": self.routing_context, "database": database},
196+
mode="r",
197+
db=SYSTEM_DATABASE,
198+
on_success=on_success,
199+
on_failure=on_failure,
200+
)
201+
180202
def discard(self, n=-1, qid=-1, **handlers):
181203
extra = {"n": n}
182204
if qid != -1:

0 commit comments

Comments
 (0)