diff --git a/redisgraph/graph.py b/redisgraph/graph.py index f4d802f..2b5677d 100644 --- a/redisgraph/graph.py +++ b/redisgraph/graph.py @@ -58,6 +58,12 @@ def _refresh_attributes(self): for i, p in enumerate(props): self._properties[i] = p[0] + def refresh_metadata(self, version, labels, reltypes, properties): + self.version = version + self._labels = labels + self._relationshipTypes = reltypes + self._properties = properties + def get_label(self, idx): try: label = self._labels[idx] @@ -161,8 +167,8 @@ def query(self, q, params=None, timeout=None, read_only=False): # ask for compact result-set format # specify known graph version cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY" - # command = [cmd, self.name, query, "--compact", "version", self.version] - command = [cmd, self.name, query, "--compact"] + command = [cmd, self.name, query, "--compact", "version", self.version] + # command = [cmd, self.name, query, "--compact"] # include timeout is specified if timeout: diff --git a/redisgraph/query_result.py b/redisgraph/query_result.py index 7f9b4de..293cc51 100644 --- a/redisgraph/query_result.py +++ b/redisgraph/query_result.py @@ -58,8 +58,10 @@ def __init__(self, graph, response): self.parse_statistics(response[0]) else: # start by parsing statistics, matches the one we have - self.parse_statistics(response[-1]) # Last element. + self.parse_statistics(response[2]) # Third element, after header and records. self.parse_results(response) + if len(response) == 4: + self.parse_metadata(response[3]) def _check_for_errors(self, response): if isinstance(response[0], ResponseError): @@ -95,6 +97,23 @@ def parse_statistics(self, raw_statistics): if v is not None: self.statistics[s] = v + def parse_metadata(self, raw_metadata): + # Decode metadata: + # { + # "version", VERSION, + # "labels", [[VALUE_STRING, "label_1"] ... ], + # "relationship types ", [[VALUE_STRING, "reltype_1"] ... ], + # "property keys", [[VALUE_STRING, "prop_1"] ... ] + # } + metadata = self.parse_map(raw_metadata) + version = metadata["version"] + labels = metadata["labels"] + reltypes = metadata["relationship types"] + properties = metadata["property keys"] + + # Update the graph's internal metadata. + self.graph.refresh_metadata(version, labels, reltypes, properties) + def parse_header(self, raw_result_set): # An array of column name/column type pairs. header = raw_result_set[0] diff --git a/tests/functional/test_all.py b/tests/functional/test_all.py index 865ea9c..5762046 100644 --- a/tests/functional/test_all.py +++ b/tests/functional/test_all.py @@ -348,6 +348,18 @@ def test_cache_sync(self): assert(A._relationshipTypes[0] == 'S') assert(A._relationshipTypes[1] == 'R') + def test_metadata(self): + A = Graph('metadata', self.r) + # Since this query modifies schemas and returns data, it should + # update all graph metadata in a single call. + A.query("CREATE (l:L {v: 1}) RETURN l") + assert(len(A._labels) == 1) + assert(len(A._properties) == 1) + assert(len(A._relationshipTypes) == 0) + assert(A._labels[0] == 'L') + assert(A._properties[0] == 'v') + assert(A.version != 0) + if __name__ == '__main__': unittest.main()