summaryrefslogtreecommitdiff
path: root/redis/commands/graph/commands.py
blob: 1db8275223b5c1c23ab4c08cc2fcb0b8f9876551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from redis import DataError
from redis.exceptions import ResponseError

from .exceptions import VersionMismatchException
from .query_result import QueryResult


class GraphCommands:
    """RedisGraph Commands"""

    def commit(self):
        """
        Create entire graph.
        For more information see `CREATE <https://oss.redis.com/redisgraph/master/commands/#create>`_. # noqa
        """
        if len(self.nodes) == 0 and len(self.edges) == 0:
            return None

        query = "CREATE "
        for _, node in self.nodes.items():
            query += str(node) + ","

        query += ",".join([str(edge) for edge in self.edges])

        # Discard leading comma.
        if query[-1] == ",":
            query = query[:-1]

        return self.query(query)

    def query(self, q, params=None, timeout=None, read_only=False, profile=False):
        """
        Executes a query against the graph.
        For more information see `GRAPH.QUERY <https://oss.redis.com/redisgraph/master/commands/#graphquery>`_. # noqa

        Args:

        q :
            The query.
        params : dict
            Query parameters.
        timeout : int
            Maximum runtime for read queries in milliseconds.
        read_only : bool
            Executes a readonly query if set to True.
        profile : bool
            Return details on results produced by and time
            spent in each operation.
        """

        # maintain original 'q'
        query = q

        # handle query parameters
        if params is not None:
            query = self._build_params_header(params) + query

        # construct query command
        # ask for compact result-set format
        # specify known graph version
        if profile:
            cmd = "GRAPH.PROFILE"
        else:
            cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY"
        command = [cmd, self.name, query, "--compact"]

        # include timeout is specified
        if timeout:
            if not isinstance(timeout, int):
                raise Exception("Timeout argument must be a positive integer")
            command += ["timeout", timeout]

        # issue query
        try:
            response = self.execute_command(*command)
            return QueryResult(self, response, profile)
        except ResponseError as e:
            if "wrong number of arguments" in str(e):
                print(
                    "Note: RedisGraph Python requires server version 2.2.8 or above"
                )  # noqa
            if "unknown command" in str(e) and read_only:
                # `GRAPH.RO_QUERY` is unavailable in older versions.
                return self.query(q, params, timeout, read_only=False)
            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, read_only)

    def merge(self, pattern):
        """
        Merge pattern.
        For more information see `MERGE <https://oss.redis.com/redisgraph/master/commands/#merge>`_. # noqa
        """
        query = "MERGE "
        query += str(pattern)

        return self.query(query)

    def delete(self):
        """
        Deletes graph.
        For more information see `DELETE <https://oss.redis.com/redisgraph/master/commands/#delete>`_. # noqa
        """
        self._clear_schema()
        return self.execute_command("GRAPH.DELETE", self.name)

    # declared here, to override the built in redis.db.flush()
    def flush(self):
        """
        Commit the graph and reset the edges and the nodes to zero length.
        """
        self.commit()
        self.nodes = {}
        self.edges = []

    def explain(self, query, params=None):
        """
        Get the execution plan for given query,
        Returns an array of operations.
        For more information see `GRAPH.EXPLAIN <https://oss.redis.com/redisgraph/master/commands/#graphexplain>`_. # noqa

        Args:

        query:
            The query that will be executed.
        params: dict
            Query parameters.
        """
        if params is not None:
            query = self._build_params_header(params) + query

        plan = self.execute_command("GRAPH.EXPLAIN", self.name, query)
        return "\n".join(plan)

    def bulk(self, **kwargs):
        """Internal only. Not supported."""
        raise NotImplementedError(
            "GRAPH.BULK is internal only. "
            "Use https://github.com/redisgraph/redisgraph-bulk-loader."
        )

    def profile(self, query):
        """
        Execute a query and produce an execution plan augmented with metrics
        for each operation's execution. Return a string representation of a
        query execution plan, with details on results produced by and time
        spent in each operation.
        For more information see `GRAPH.PROFILE <https://oss.redis.com/redisgraph/master/commands/#graphprofile>`_. # noqa
        """
        return self.query(query, profile=True)

    def slowlog(self):
        """
        Get a list containing up to 10 of the slowest queries issued
        against the given graph ID.
        For more information see `GRAPH.SLOWLOG <https://oss.redis.com/redisgraph/master/commands/#graphslowlog>`_. # noqa

        Each item in the list has the following structure:
        1. A unix timestamp at which the log entry was processed.
        2. The issued command.
        3. The issued query.
        4. The amount of time needed for its execution, in milliseconds.
        """
        return self.execute_command("GRAPH.SLOWLOG", self.name)

    def config(self, name, value=None, set=False):
        """
        Retrieve or update a RedisGraph configuration.
        For more information see `GRAPH.CONFIG <https://oss.redis.com/redisgraph/master/commands/#graphconfig>`_. # noqa

        Args:

        name : str
            The name of the configuration
        value :
            The value we want to ser (can be used only when `set` is on)
        set : bool
            Turn on to set a configuration. Default behavior is get.
        """
        params = ["SET" if set else "GET", name]
        if value is not None:
            if set:
                params.append(value)
            else:
                raise DataError(
                    "``value`` can be provided only when ``set`` is True"
                )  # noqa
        return self.execute_command("GRAPH.CONFIG", *params)

    def list_keys(self):
        """
        Lists all graph keys in the keyspace.
        For more information see `GRAPH.LIST <https://oss.redis.com/redisgraph/master/commands/#graphlist>`_. # noqa
        """
        return self.execute_command("GRAPH.LIST")