-
Notifications
You must be signed in to change notification settings - Fork 64
/
hashes.rs
358 lines (297 loc) · 10.7 KB
/
hashes.rs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use super::*;
use crate::{
protocol::{command::CommandKind, utils as protocol_utils},
types::*,
utils,
};
use redis_protocol::resp3::types::{BytesFrame as Resp3Frame, Resp3Frame as _Resp3Frame};
use std::{convert::TryInto, str};
pub static FIELDS: &str = "FIELDS";
fn frame_is_queued(frame: &Resp3Frame) -> bool {
match frame {
Resp3Frame::SimpleString { ref data, .. } | Resp3Frame::BlobString { ref data, .. } => {
str::from_utf8(data).ok().map(|s| s == QUEUED).unwrap_or(false)
},
_ => false,
}
}
pub async fn hdel<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(1 + fields.len());
args.push(key.into());
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HDel, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hexists<C: ClientLike>(client: &C, key: Key, field: Key) -> Result<Value, Error> {
let args: Vec<Value> = vec![key.into(), field.into()];
args_value_cmd(client, CommandKind::HExists, args).await
}
pub async fn hget<C: ClientLike>(client: &C, key: Key, field: Key) -> Result<Value, Error> {
let args: Vec<Value> = vec![key.into(), field.into()];
args_value_cmd(client, CommandKind::HGet, args).await
}
pub async fn hgetall<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
let frame = utils::request_response(client, move || Ok((CommandKind::HGetAll, vec![key.into()]))).await?;
if frame.as_str().map(|s| s == QUEUED).unwrap_or(false) {
protocol_utils::frame_to_results(frame)
} else {
Ok(Value::Map(protocol_utils::frame_to_map(frame)?))
}
}
pub async fn hincrby<C: ClientLike>(client: &C, key: Key, field: Key, increment: i64) -> Result<Value, Error> {
let args: Vec<Value> = vec![key.into(), field.into(), increment.into()];
args_value_cmd(client, CommandKind::HIncrBy, args).await
}
pub async fn hincrbyfloat<C: ClientLike>(client: &C, key: Key, field: Key, increment: f64) -> Result<Value, Error> {
let args: Vec<Value> = vec![key.into(), field.into(), increment.try_into()?];
args_value_cmd(client, CommandKind::HIncrByFloat, args).await
}
pub async fn hkeys<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
let frame = utils::request_response(client, move || Ok((CommandKind::HKeys, vec![key.into()]))).await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hlen<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::HLen, key.into()).await
}
pub async fn hmget<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(1 + fields.len());
args.push(key.into());
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HMGet, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hmset<C: ClientLike>(client: &C, key: Key, values: Map) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(1 + (values.len() * 2));
args.push(key.into());
for (key, value) in values.inner().into_iter().filter(|x| !x.1.is_null()) {
args.push(key.into());
args.push(value);
}
Ok((CommandKind::HMSet, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hset<C: ClientLike>(client: &C, key: Key, values: Map) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(1 + (values.len() * 2));
args.push(key.into());
for (key, value) in values.inner().into_iter().filter(|x| !x.1.is_null()) {
args.push(key.into());
args.push(value);
}
Ok((CommandKind::HSet, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hsetnx<C: ClientLike>(client: &C, key: Key, field: Key, value: Value) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
Ok((CommandKind::HSetNx, vec![key.into(), field.into(), value]))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hrandfield<C: ClientLike>(client: &C, key: Key, count: Option<(i64, bool)>) -> Result<Value, Error> {
let (has_count, has_values) = count.as_ref().map(|(_c, b)| (true, *b)).unwrap_or((false, false));
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(3);
args.push(key.into());
if let Some((count, with_values)) = count {
args.push(count.into());
if with_values {
args.push(static_val!(WITH_VALUES));
}
}
Ok((CommandKind::HRandField, args))
})
.await?;
if has_count {
if has_values && frame.as_str().map(|s| s != QUEUED).unwrap_or(true) {
let frame = protocol_utils::flatten_frame(frame);
protocol_utils::frame_to_map(frame).map(Value::Map)
} else {
protocol_utils::frame_to_results(frame)
}
} else {
protocol_utils::frame_to_results(frame)
}
}
pub async fn hstrlen<C: ClientLike>(client: &C, key: Key, field: Key) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
Ok((CommandKind::HStrLen, vec![key.into(), field.into()]))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn hvals<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_values_cmd(client, CommandKind::HVals, key.into()).await
}
#[cfg(feature = "i-hexpire")]
pub async fn httl<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 3);
args.extend([key.into(), static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HTtl, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hexpire<C: ClientLike>(
client: &C,
key: Key,
seconds: i64,
options: Option<ExpireOptions>,
fields: MultipleKeys,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 4);
args.extend([key.into(), seconds.into()]);
if let Some(options) = options {
args.push(options.to_str().into());
}
args.extend([static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HExpire, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hexpire_at<C: ClientLike>(
client: &C,
key: Key,
time: i64,
options: Option<ExpireOptions>,
fields: MultipleKeys,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 4);
args.extend([key.into(), time.into()]);
if let Some(options) = options {
args.push(options.to_str().into());
}
args.extend([static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HExpireAt, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hexpire_time<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 3);
args.extend([key.into(), static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HExpireTime, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hpttl<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 3);
args.extend([key.into(), static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HPTtl, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hpexpire<C: ClientLike>(
client: &C,
key: Key,
milliseconds: i64,
options: Option<ExpireOptions>,
fields: MultipleKeys,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 4);
args.extend([key.into(), milliseconds.into()]);
if let Some(options) = options {
args.push(options.to_str().into());
}
args.extend([static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HPExpire, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hpexpire_at<C: ClientLike>(
client: &C,
key: Key,
time: i64,
options: Option<ExpireOptions>,
fields: MultipleKeys,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 4);
args.extend([key.into(), time.into()]);
if let Some(options) = options {
args.push(options.to_str().into());
}
args.extend([static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HPExpireAt, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hpexpire_time<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 3);
args.extend([key.into(), static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HPExpireTime, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
#[cfg(feature = "i-hexpire")]
pub async fn hpersist<C: ClientLike>(client: &C, key: Key, fields: MultipleKeys) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(fields.len() + 3);
args.extend([key.into(), static_val!(FIELDS), fields.len().try_into()?]);
for field in fields.inner().into_iter() {
args.push(field.into());
}
Ok((CommandKind::HPersist, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}