Skip to content

Commit 582f7ce

Browse files
committed
Add handling for metadata footers
1 parent a3476dc commit 582f7ce

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

redisgraph/graph.py

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def _refresh_attributes(self):
5656
for i, p in enumerate(props):
5757
self._properties[i] = p[0]
5858

59+
def refresh_metadata(self, version, labels, reltypes, properties):
60+
self.version = version
61+
self._labels = labels
62+
self._relationshipTypes = reltypes
63+
self._properties = properties
64+
5965
def get_label(self, idx):
6066
try:
6167
label = self._labels[idx]

redisgraph/query_result.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ def __init__(self, graph, response):
5252
self.parse_statistics(response[0])
5353
else:
5454
# start by parsing statistics, matches the one we have
55-
self.parse_statistics(response[-1]) # Last element.
55+
self.parse_statistics(response[2]) # Third element, after header and records.
5656
self.parse_results(response)
57+
if len(response) == 4:
58+
self.parse_metadata(response[3])
5759

5860
def _check_for_errors(self, response):
5961
if isinstance(response[0], ResponseError):
@@ -89,6 +91,36 @@ def parse_statistics(self, raw_statistics):
8991
if v is not None:
9092
self.statistics[s] = v
9193

94+
def parse_metadata(self, raw_metadata):
95+
# Decode metadata:
96+
# [
97+
# ["version", VERSION],
98+
# ["labels", [[VALUE_STRING, "label_1"] ... ]],
99+
# ["relationship types ", [[VALUE_STRING, "reltype_1"] ... ]],
100+
# ["property keys", [[VALUE_STRING, "prop_1"] ... ]]
101+
# ]
102+
version = raw_metadata[0][1]
103+
raw_labels = raw_metadata[1][1]
104+
raw_reltypes = raw_metadata[2][1]
105+
raw_props = raw_metadata[3][1]
106+
107+
# Arrays to be passed into the internal graph structure.
108+
labels = [None] * len(raw_labels)
109+
reltypes = [None] * len(raw_reltypes)
110+
properties = [None] * len(raw_props)
111+
112+
for idx, label in enumerate(raw_labels):
113+
labels[idx] = self.parse_scalar(label)
114+
115+
for idx, reltype in enumerate(raw_reltypes):
116+
reltypes[idx] = self.parse_scalar(reltype)
117+
118+
for idx, prop in enumerate(raw_props):
119+
properties[idx] = self.parse_scalar(prop)
120+
121+
# Update the graph's internal metadata.
122+
self.graph.refresh_metadata(version, labels, reltypes, properties)
123+
92124
def parse_header(self, raw_result_set):
93125
# An array of column name/column type pairs.
94126
header = raw_result_set[0]

test.py

+13
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,19 @@ def test_cache_sync(self):
311311
assert(A._relationshipTypes[0] == 'S')
312312
assert(A._relationshipTypes[1] == 'R')
313313

314+
def test_metadata(self):
315+
A = Graph('metadata', self.r)
316+
# Since this query modifies schemas and returns data, it should
317+
# update all graph metadata in a single call.
318+
A.query("CREATE (l:L {v: 1}) RETURN l")
319+
assert(len(A._labels) == 1)
320+
assert(len(A._properties) == 1)
321+
assert(len(A._relationshipTypes) == 0)
322+
assert(A._labels[0] == 'L')
323+
assert(A._properties[0] == 'v')
324+
assert(A.version != 0)
325+
326+
314327
if __name__ == '__main__':
315328
unittest.main()
316329

0 commit comments

Comments
 (0)