-
Notifications
You must be signed in to change notification settings - Fork 49
update internal cached mapping upon graph version change #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d3b241
update internal cached mapping upon graph version change
swilly22 aa9c89d
test cache sync
swilly22 10c69cb
maintain graph version
swilly22 535524f
print require version on arity exception
swilly22 07e677f
Merge branch 'master' into graph-version
swilly22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class VersionMismatchException(Exception): | ||
def __init__(self, version): | ||
self.version = version | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
from .util import * | ||
import redis | ||
from .query_result import QueryResult | ||
from .exceptions import VersionMismatchException | ||
|
||
class Graph(object): | ||
""" | ||
Graph, collection of nodes and edges. | ||
|
@@ -9,39 +12,65 @@ def __init__(self, name, redis_con): | |
""" | ||
Create a new graph. | ||
""" | ||
self.name = name | ||
self.name = name # Graph key | ||
self.redis_con = redis_con | ||
self.nodes = {} | ||
self.edges = [] | ||
self._labels = [] # List of node labels. | ||
self._properties = [] # List of properties. | ||
self._relationshipTypes = [] # List of relation types. | ||
self._labels = [] # List of node labels. | ||
self._properties = [] # List of properties. | ||
self._relationshipTypes = [] # List of relation types. | ||
self.version = 0 # Graph version | ||
|
||
def _clear_schema(self): | ||
self._labels = [] | ||
self._properties = [] | ||
self._relationshipTypes = [] | ||
|
||
def _refresh_schema(self): | ||
self._clear_schema() | ||
self._refresh_labels() | ||
self._refresh_relations() | ||
self._refresh_attributes() | ||
|
||
def _refresh_labels(self): | ||
lbls = self.labels() | ||
|
||
# Unpack data. | ||
self._labels = [None] * len(lbls) | ||
for i, l in enumerate(lbls): | ||
self._labels[i] = l[0] | ||
|
||
def _refresh_relations(self): | ||
rels = self.relationshipTypes() | ||
|
||
# Unpack data. | ||
self._relationshipTypes = [None] * len(rels) | ||
for i, r in enumerate(rels): | ||
self._relationshipTypes[i] = r[0] | ||
|
||
def _refresh_attributes(self): | ||
props = self.propertyKeys() | ||
|
||
# Unpack data. | ||
self._properties = [None] * len(props) | ||
for i, p in enumerate(props): | ||
self._properties[i] = p[0] | ||
|
||
def get_label(self, idx): | ||
try: | ||
label = self._labels[idx] | ||
except IndexError: | ||
# Refresh graph labels. | ||
lbls = self.labels() | ||
# Unpack data. | ||
self._labels = [None] * len(lbls) | ||
for i, l in enumerate(lbls): | ||
self._labels[i] = l[0] | ||
|
||
# Refresh labels. | ||
self._refresh_labels() | ||
label = self._labels[idx] | ||
return label | ||
|
||
def get_relation(self, idx): | ||
try: | ||
relationshipType = self._relationshipTypes[idx] | ||
except IndexError: | ||
# Refresh graph relations. | ||
rels = self.relationshipTypes() | ||
# Unpack data. | ||
self._relationshipTypes = [None] * len(rels) | ||
for i, r in enumerate(rels): | ||
self._relationshipTypes[i] = r[0] | ||
|
||
# Refresh relationship types. | ||
self._refresh_relations() | ||
relationshipType = self._relationshipTypes[idx] | ||
return relationshipType | ||
|
||
|
@@ -50,12 +79,7 @@ def get_property(self, idx): | |
propertie = self._properties[idx] | ||
except IndexError: | ||
# Refresh properties. | ||
props = self.propertyKeys() | ||
# Unpack data. | ||
self._properties = [None] * len(props) | ||
for i, p in enumerate(props): | ||
self._properties[i] = p[0] | ||
|
||
self._refresh_attributes() | ||
propertie = self._properties[idx] | ||
return propertie | ||
|
||
|
@@ -121,16 +145,40 @@ def query(self, q, params=None, timeout=None): | |
""" | ||
Executes a query against the graph. | ||
""" | ||
|
||
# maintain original 'q' | ||
query = q | ||
|
||
# handle query parameters | ||
if params is not None: | ||
q = self.build_params_header(params) + q | ||
query = self.build_params_header(params) + query | ||
|
||
# construct query command | ||
# ask for compact result-set format | ||
# specify known graph version | ||
command = ["GRAPH.QUERY", self.name, query, "--compact", "version", self.version] | ||
|
||
command = ["GRAPH.QUERY", self.name, q, "--compact"] | ||
# include timeout is specified | ||
if timeout: | ||
if not isinstance(timeout, int): | ||
raise Exception("Timeout argument must be a positive integer") | ||
command += ["timeout", timeout] | ||
response = self.redis_con.execute_command(*command) | ||
return QueryResult(self, response) | ||
|
||
# issue query | ||
try: | ||
response = self.redis_con.execute_command(*command) | ||
return QueryResult(self, response) | ||
except redis.exceptions.ResponseError as e: | ||
if "wrong number of arguments" in str(e): | ||
print ("Note: RedisGraph Python requires server version 2.2.8 or above") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for now, this is ok |
||
raise e | ||
except VersionMismatchException as e: | ||
# client view over the graph schema is out of sync | ||
# set client version and refresh local schema | ||
self.version = e.version | ||
self._refresh_schema() | ||
# re-issue query | ||
return self.query(q, params, timeout) | ||
|
||
def _execution_plan_to_string(self, plan): | ||
return "\n".join(plan) | ||
|
@@ -151,6 +199,7 @@ def delete(self): | |
""" | ||
Deletes graph. | ||
""" | ||
self._clear_schema() | ||
return self.redis_con.execute_command("GRAPH.DELETE", self.name) | ||
|
||
def merge(self, pattern): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
version
only when server supports this feature, for older version of RedisGraph this will trigger arity error