Skip to content

Commit 3376cd1

Browse files
committed
Add handling for metadata footers
1 parent 5238a92 commit 3376cd1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Diff for: redisgraph/graph.py

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

61+
def refresh_metadata(self, version, labels, reltypes, properties):
62+
self.version = version
63+
self._labels = labels
64+
self._relationshipTypes = reltypes
65+
self._properties = properties
66+
6167
def get_label(self, idx):
6268
try:
6369
label = self._labels[idx]

Diff for: redisgraph/query_result.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ def __init__(self, graph, response):
5757
self.parse_statistics(response[0])
5858
else:
5959
# start by parsing statistics, matches the one we have
60-
self.parse_statistics(response[-1]) # Last element.
60+
self.parse_statistics(response[2]) # Third element, after header and records.
6161
self.parse_results(response)
62+
if len(response) == 4:
63+
self.parse_metadata(response[3])
6264

6365
def _check_for_errors(self, response):
6466
if isinstance(response[0], ResponseError):
@@ -94,6 +96,36 @@ def parse_statistics(self, raw_statistics):
9496
if v is not None:
9597
self.statistics[s] = v
9698

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

Diff for: tests/functional/test_all.py

+12
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,18 @@ def test_cache_sync(self):
328328
assert(A._relationshipTypes[0] == 'S')
329329
assert(A._relationshipTypes[1] == 'R')
330330

331+
def test_metadata(self):
332+
A = Graph('metadata', self.r)
333+
# Since this query modifies schemas and returns data, it should
334+
# update all graph metadata in a single call.
335+
A.query("CREATE (l:L {v: 1}) RETURN l")
336+
assert(len(A._labels) == 1)
337+
assert(len(A._properties) == 1)
338+
assert(len(A._relationshipTypes) == 0)
339+
assert(A._labels[0] == 'L')
340+
assert(A._properties[0] == 'v')
341+
assert(A.version != 0)
342+
331343

332344
if __name__ == '__main__':
333345
unittest.main()

0 commit comments

Comments
 (0)