Debian Codegraph @

valkey/commands/timeseries/__init__.py

raw
1 import valkey
2 from valkey._parsers.helpers import bool_ok
3
4 from ..helpers import get_protocol_version, parse_to_list
5 from .commands import (
6 ALTER_CMD,
7 CREATE_CMD,
8 CREATERULE_CMD,
9 DEL_CMD,
10 DELETERULE_CMD,
11 GET_CMD,
12 INFO_CMD,
13 MGET_CMD,
14 MRANGE_CMD,
15 MREVRANGE_CMD,
16 QUERYINDEX_CMD,
17 RANGE_CMD,
18 REVRANGE_CMD,
19 TimeSeriesCommands,
20 )
21 from .info import TSInfo
22 from .utils import parse_get, parse_m_get, parse_m_range, parse_range
23
24
25 class TimeSeries(TimeSeriesCommands):
26 """
27 This class subclasses valkey-py's `Valkey` and implements RedisTimeSeries's
28 commands (prefixed with "ts").
29 The client allows to interact with RedisTimeSeries and use all of it's
30 functionality.
31 """
32
33 def __init__(self, client=None, **kwargs):
34 """Create a new RedisTimeSeries client."""
35 # Set the module commands' callbacks
36 self._MODULE_CALLBACKS = {
37 ALTER_CMD: bool_ok,
38 CREATE_CMD: bool_ok,
39 CREATERULE_CMD: bool_ok,
40 DELETERULE_CMD: bool_ok,
41 }
42
43 _RESP2_MODULE_CALLBACKS = {
44 DEL_CMD: int,
45 GET_CMD: parse_get,
46 INFO_CMD: TSInfo,
47 MGET_CMD: parse_m_get,
48 MRANGE_CMD: parse_m_range,
49 MREVRANGE_CMD: parse_m_range,
50 RANGE_CMD: parse_range,
51 REVRANGE_CMD: parse_range,
52 QUERYINDEX_CMD: parse_to_list,
53 }
54 _RESP3_MODULE_CALLBACKS = {}
55
56 self.client = client
57 self.execute_command = client.execute_command
58
59 if get_protocol_version(self.client) in ["3", 3]:
60 self._MODULE_CALLBACKS.update(_RESP3_MODULE_CALLBACKS)
61 else:
62 self._MODULE_CALLBACKS.update(_RESP2_MODULE_CALLBACKS)
63
64 for k, v in self._MODULE_CALLBACKS.items():
65 self.client.set_response_callback(k, v)
66
67 def pipeline(self, transaction=True, shard_hint=None):
68 """Creates a pipeline for the TimeSeries module, that can be used
69 for executing only TimeSeries commands and core commands.
70
71 Usage example:
72
73 r = valkey.Valkey()
74 pipe = r.ts().pipeline()
75 for i in range(100):
76 pipeline.add("with_pipeline", i, 1.1 * i)
77 pipeline.execute()
78
79 """
80 if isinstance(self.client, valkey.ValkeyCluster):
81 p = ClusterPipeline(
82 nodes_manager=self.client.nodes_manager,
83 commands_parser=self.client.commands_parser,
84 startup_nodes=self.client.nodes_manager.startup_nodes,
85 result_callbacks=self.client.result_callbacks,
86 cluster_response_callbacks=self.client.cluster_response_callbacks,
87 cluster_error_retry_attempts=self.client.cluster_error_retry_attempts,
88 read_from_replicas=self.client.read_from_replicas,
89 reinitialize_steps=self.client.reinitialize_steps,
90 lock=self.client._lock,
91 )
92
93 else:
94 p = Pipeline(
95 connection_pool=self.client.connection_pool,
96 response_callbacks=self._MODULE_CALLBACKS,
97 transaction=transaction,
98 shard_hint=shard_hint,
99 )
100 return p
101
102
103 class ClusterPipeline(TimeSeriesCommands, valkey.cluster.ClusterPipeline):
104 """Cluster pipeline for the module."""
105
106
107 class Pipeline(TimeSeriesCommands, valkey.client.Pipeline):
108 """Pipeline for the module."""