redis_tower_commands/
hyperloglog.rs1use crate::CommandArg;
21use redis_tower_core::{Command, Frame, RedisError};
22use redis_tower_protocol::helpers::{array, bulk};
23
24#[derive(Clone)]
30pub struct PfAdd {
31 key: CommandArg,
32 elements: Vec<CommandArg>,
33}
34
35impl PfAdd {
36 pub fn new(key: impl Into<CommandArg>, element: impl Into<CommandArg>) -> Self {
38 Self {
39 key: key.into(),
40 elements: vec![element.into()],
41 }
42 }
43
44 pub fn elements(
46 key: impl Into<CommandArg>,
47 elements: impl IntoIterator<Item = impl Into<CommandArg>>,
48 ) -> Self {
49 Self {
50 key: key.into(),
51 elements: elements.into_iter().map(Into::into).collect(),
52 }
53 }
54}
55
56impl Command for PfAdd {
57 type Response = bool;
58
59 fn to_frame(&self) -> Frame {
60 let mut args = vec![bulk("PFADD"), bulk(&self.key)];
61 for element in &self.elements {
62 args.push(bulk(element));
63 }
64 array(args)
65 }
66
67 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
68 match frame {
69 Frame::Integer(n) => Ok(n == 1),
70 Frame::Boolean(b) => Ok(b),
71 other => Err(RedisError::UnexpectedResponse {
72 expected: "integer or boolean",
73 actual: format!("{other:?}"),
74 }),
75 }
76 }
77
78 fn name(&self) -> &str {
79 "PFADD"
80 }
81}
82
83#[derive(Clone)]
89pub struct PfCount {
90 keys: Vec<CommandArg>,
91}
92
93impl PfCount {
94 pub fn new(key: impl Into<CommandArg>) -> Self {
96 Self {
97 keys: vec![key.into()],
98 }
99 }
100
101 pub fn keys(keys: impl IntoIterator<Item = impl Into<CommandArg>>) -> Self {
103 Self {
104 keys: keys.into_iter().map(Into::into).collect(),
105 }
106 }
107}
108
109impl Command for PfCount {
110 type Response = i64;
111
112 fn to_frame(&self) -> Frame {
113 let mut args = vec![bulk("PFCOUNT")];
114 for key in &self.keys {
115 args.push(bulk(key));
116 }
117 array(args)
118 }
119
120 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
121 match frame {
122 Frame::Integer(n) => Ok(n),
123 other => Err(RedisError::UnexpectedResponse {
124 expected: "integer",
125 actual: format!("{other:?}"),
126 }),
127 }
128 }
129
130 fn name(&self) -> &str {
131 "PFCOUNT"
132 }
133}
134
135#[derive(Clone)]
140pub struct PfMerge {
141 destkey: CommandArg,
142 sourcekeys: Vec<CommandArg>,
143}
144
145impl PfMerge {
146 pub fn new(
148 destkey: impl Into<CommandArg>,
149 sourcekeys: impl IntoIterator<Item = impl Into<CommandArg>>,
150 ) -> Self {
151 Self {
152 destkey: destkey.into(),
153 sourcekeys: sourcekeys.into_iter().map(Into::into).collect(),
154 }
155 }
156}
157
158impl Command for PfMerge {
159 type Response = ();
160
161 fn to_frame(&self) -> Frame {
162 let mut args = vec![bulk("PFMERGE"), bulk(&self.destkey)];
163 for key in &self.sourcekeys {
164 args.push(bulk(key));
165 }
166 array(args)
167 }
168
169 fn parse_response(&self, frame: Frame) -> Result<Self::Response, RedisError> {
170 match frame {
171 Frame::SimpleString(s) if &s[..] == b"OK" => Ok(()),
172 other => Err(RedisError::UnexpectedResponse {
173 expected: "OK",
174 actual: format!("{other:?}"),
175 }),
176 }
177 }
178
179 fn name(&self) -> &str {
180 "PFMERGE"
181 }
182}