|
| 1 | +from redis import DataError |
| 2 | +from redis.exceptions import ResponseError |
| 3 | + |
| 4 | +from .exceptions import VersionMismatchException |
| 5 | +from .query_result import QueryResult |
| 6 | + |
| 7 | + |
| 8 | +class GraphCommands: |
| 9 | + def commit(self): |
| 10 | + """ |
| 11 | + Create entire graph. |
| 12 | + For more information see `CREATE <https://oss.redis.com/redisgraph/master/commands/#create>`_. # noqa |
| 13 | + """ |
| 14 | + if len(self.nodes) == 0 and len(self.edges) == 0: |
| 15 | + return None |
| 16 | + |
| 17 | + query = "CREATE " |
| 18 | + for _, node in self.nodes.items(): |
| 19 | + query += str(node) + "," |
| 20 | + |
| 21 | + query += ",".join([str(edge) for edge in self.edges]) |
| 22 | + |
| 23 | + # Discard leading comma. |
| 24 | + if query[-1] == ",": |
| 25 | + query = query[:-1] |
| 26 | + |
| 27 | + return self.query(query) |
| 28 | + |
| 29 | + def query(self, q, params=None, timeout=None, read_only=False, profile=False): |
| 30 | + """ |
| 31 | + Executes a query against the graph. |
| 32 | + For more information see `GRAPH.QUERY <https://oss.redis.com/redisgraph/master/commands/#graphquery>`_. # noqa |
| 33 | +
|
| 34 | + Args: |
| 35 | +
|
| 36 | + ------- |
| 37 | + q : |
| 38 | + The query. |
| 39 | + params : dict |
| 40 | + Query parameters. |
| 41 | + timeout : int |
| 42 | + Maximum runtime for read queries in milliseconds. |
| 43 | + read_only : bool |
| 44 | + Executes a readonly query if set to True. |
| 45 | + profile : bool |
| 46 | + Return details on results produced by and time |
| 47 | + spent in each operation. |
| 48 | + """ |
| 49 | + |
| 50 | + # maintain original 'q' |
| 51 | + query = q |
| 52 | + |
| 53 | + # handle query parameters |
| 54 | + if params is not None: |
| 55 | + query = self._build_params_header(params) + query |
| 56 | + |
| 57 | + # construct query command |
| 58 | + # ask for compact result-set format |
| 59 | + # specify known graph version |
| 60 | + if profile: |
| 61 | + cmd = "GRAPH.PROFILE" |
| 62 | + else: |
| 63 | + cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY" |
| 64 | + command = [cmd, self.name, query, "--compact"] |
| 65 | + |
| 66 | + # include timeout is specified |
| 67 | + if timeout: |
| 68 | + if not isinstance(timeout, int): |
| 69 | + raise Exception("Timeout argument must be a positive integer") |
| 70 | + command += ["timeout", timeout] |
| 71 | + |
| 72 | + # issue query |
| 73 | + try: |
| 74 | + response = self.execute_command(*command) |
| 75 | + return QueryResult(self, response, profile) |
| 76 | + except ResponseError as e: |
| 77 | + if "wrong number of arguments" in str(e): |
| 78 | + print( |
| 79 | + "Note: RedisGraph Python requires server version 2.2.8 or above" |
| 80 | + ) # noqa |
| 81 | + if "unknown command" in str(e) and read_only: |
| 82 | + # `GRAPH.RO_QUERY` is unavailable in older versions. |
| 83 | + return self.query(q, params, timeout, read_only=False) |
| 84 | + raise e |
| 85 | + except VersionMismatchException as e: |
| 86 | + # client view over the graph schema is out of sync |
| 87 | + # set client version and refresh local schema |
| 88 | + self.version = e.version |
| 89 | + self._refresh_schema() |
| 90 | + # re-issue query |
| 91 | + return self.query(q, params, timeout, read_only) |
| 92 | + |
| 93 | + def merge(self, pattern): |
| 94 | + """ |
| 95 | + Merge pattern. |
| 96 | + For more information see `MERGE <https://oss.redis.com/redisgraph/master/commands/#merge>`_. # noqa |
| 97 | + """ |
| 98 | + query = "MERGE " |
| 99 | + query += str(pattern) |
| 100 | + |
| 101 | + return self.query(query) |
| 102 | + |
| 103 | + def delete(self): |
| 104 | + """ |
| 105 | + Deletes graph. |
| 106 | + For more information see `DELETE <https://oss.redis.com/redisgraph/master/commands/#delete>`_. # noqa |
| 107 | + """ |
| 108 | + self._clear_schema() |
| 109 | + return self.execute_command("GRAPH.DELETE", self.name) |
| 110 | + |
| 111 | + # declared here, to override the built in redis.db.flush() |
| 112 | + def flush(self): |
| 113 | + """ |
| 114 | + Commit the graph and reset the edges and the nodes to zero length. |
| 115 | + """ |
| 116 | + self.commit() |
| 117 | + self.nodes = {} |
| 118 | + self.edges = [] |
| 119 | + |
| 120 | + def explain(self, query, params=None): |
| 121 | + """ |
| 122 | + Get the execution plan for given query, |
| 123 | + Returns an array of operations. |
| 124 | + For more information see `GRAPH.EXPLAIN <https://oss.redis.com/redisgraph/master/commands/#graphexplain>`_. # noqa |
| 125 | +
|
| 126 | + Args: |
| 127 | +
|
| 128 | + ------- |
| 129 | + query: |
| 130 | + The query that will be executed. |
| 131 | + params: dict |
| 132 | + Query parameters. |
| 133 | + """ |
| 134 | + if params is not None: |
| 135 | + query = self._build_params_header(params) + query |
| 136 | + |
| 137 | + plan = self.execute_command("GRAPH.EXPLAIN", self.name, query) |
| 138 | + return "\n".join(plan) |
| 139 | + |
| 140 | + def bulk(self, **kwargs): |
| 141 | + """Internal only. Not supported.""" |
| 142 | + raise NotImplementedError( |
| 143 | + "GRAPH.BULK is internal only. " |
| 144 | + "Use https://github.com/redisgraph/redisgraph-bulk-loader." |
| 145 | + ) |
| 146 | + |
| 147 | + def profile(self, query): |
| 148 | + """ |
| 149 | + Execute a query and produce an execution plan augmented with metrics |
| 150 | + for each operation's execution. Return a string representation of a |
| 151 | + query execution plan, with details on results produced by and time |
| 152 | + spent in each operation. |
| 153 | + For more information see `GRAPH.PROFILE <https://oss.redis.com/redisgraph/master/commands/#graphprofile>`_. # noqa |
| 154 | + """ |
| 155 | + return self.query(query, profile=True) |
| 156 | + |
| 157 | + def slowlog(self): |
| 158 | + """ |
| 159 | + Get a list containing up to 10 of the slowest queries issued |
| 160 | + against the given graph ID. |
| 161 | + For more information see `GRAPH.SLOWLOG <https://oss.redis.com/redisgraph/master/commands/#graphslowlog>`_. # noqa |
| 162 | +
|
| 163 | + Each item in the list has the following structure: |
| 164 | + 1. A unix timestamp at which the log entry was processed. |
| 165 | + 2. The issued command. |
| 166 | + 3. The issued query. |
| 167 | + 4. The amount of time needed for its execution, in milliseconds. |
| 168 | + """ |
| 169 | + return self.execute_command("GRAPH.SLOWLOG", self.name) |
| 170 | + |
| 171 | + def config(self, name, value=None, set=False): |
| 172 | + """ |
| 173 | + Retrieve or update a RedisGraph configuration. |
| 174 | + For more information see `GRAPH.CONFIG <https://oss.redis.com/redisgraph/master/commands/#graphconfig>`_. # noqa |
| 175 | +
|
| 176 | + Args: |
| 177 | +
|
| 178 | + name : str |
| 179 | + The name of the configuration |
| 180 | + value : |
| 181 | + The value we want to ser (can be used only when `set` is on) |
| 182 | + set : bool |
| 183 | + Turn on to set a configuration. Default behavior is get. |
| 184 | + """ |
| 185 | + params = ["SET" if set else "GET", name] |
| 186 | + if value is not None: |
| 187 | + if set: |
| 188 | + params.append(value) |
| 189 | + else: |
| 190 | + raise DataError( |
| 191 | + "``value`` can be provided only when ``set`` is True" |
| 192 | + ) # noqa |
| 193 | + return self.execute_command("GRAPH.CONFIG", *params) |
| 194 | + |
| 195 | + def list_keys(self): |
| 196 | + """ |
| 197 | + Lists all graph keys in the keyspace. |
| 198 | + For more information see `GRAPH.LIST <https://oss.redis.com/redisgraph/master/commands/#graphlist>`_. # noqa |
| 199 | + """ |
| 200 | + return self.execute_command("GRAPH.LIST") |
0 commit comments