-
Notifications
You must be signed in to change notification settings - Fork 64
/
keys.rs
440 lines (357 loc) · 11.9 KB
/
keys.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use super::*;
use crate::{
error::*,
protocol::{command::CommandKind, utils as protocol_utils},
types::*,
utils,
};
use std::convert::TryInto;
fn check_empty_keys(keys: &MultipleKeys) -> Result<(), Error> {
if keys.len() == 0 {
Err(Error::new(ErrorKind::InvalidArgument, "At least one key is required."))
} else {
Ok(())
}
}
value_cmd!(randomkey, Randomkey);
pub async fn get<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_values_cmd(client, CommandKind::Get, key.into()).await
}
pub async fn set<C: ClientLike>(
client: &C,
key: Key,
value: Value,
expire: Option<Expiration>,
options: Option<SetOptions>,
get: bool,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(6);
args.push(key.into());
args.push(value);
if let Some(expire) = expire {
let (k, v) = expire.into_args();
args.push(k.into());
if let Some(v) = v {
args.push(v.into());
}
}
if let Some(options) = options {
args.push(options.to_str().into());
}
if get {
args.push(static_val!(GET));
}
Ok((CommandKind::Set, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn setnx<C: ClientLike>(client: &C, key: Key, value: Value) -> Result<Value, Error> {
args_value_cmd(client, CommandKind::Setnx, vec![key.into(), value]).await
}
pub async fn del<C: ClientLike>(client: &C, keys: MultipleKeys) -> Result<Value, Error> {
check_empty_keys(&keys)?;
let args: Vec<Value> = keys.inner().drain(..).map(|k| k.into()).collect();
let frame = utils::request_response(client, move || Ok((CommandKind::Del, args))).await?;
protocol_utils::frame_to_results(frame)
}
pub async fn unlink<C: ClientLike>(client: &C, keys: MultipleKeys) -> Result<Value, Error> {
check_empty_keys(&keys)?;
let args: Vec<Value> = keys.inner().drain(..).map(|k| k.into()).collect();
let frame = utils::request_response(client, move || Ok((CommandKind::Unlink, args))).await?;
protocol_utils::frame_to_results(frame)
}
pub async fn append<C: ClientLike>(client: &C, key: Key, value: Value) -> Result<Value, Error> {
args_value_cmd(client, CommandKind::Append, vec![key.into(), value]).await
}
pub async fn incr<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Incr, key.into()).await
}
pub async fn decr<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Decr, key.into()).await
}
pub async fn incr_by<C: ClientLike>(client: &C, key: Key, val: i64) -> Result<Value, Error> {
let frame =
utils::request_response(client, move || Ok((CommandKind::IncrBy, vec![key.into(), val.into()]))).await?;
protocol_utils::frame_to_results(frame)
}
pub async fn decr_by<C: ClientLike>(client: &C, key: Key, val: i64) -> Result<Value, Error> {
let frame =
utils::request_response(client, move || Ok((CommandKind::DecrBy, vec![key.into(), val.into()]))).await?;
protocol_utils::frame_to_results(frame)
}
pub async fn incr_by_float<C: ClientLike>(client: &C, key: Key, val: f64) -> Result<Value, Error> {
let val: Value = val.try_into()?;
let frame = utils::request_response(client, move || Ok((CommandKind::IncrByFloat, vec![key.into(), val]))).await?;
protocol_utils::frame_to_results(frame)
}
pub async fn ttl<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Ttl, key.into()).await
}
pub async fn pttl<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Pttl, key.into()).await
}
pub async fn persist<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Persist, key.into()).await
}
pub async fn expire<C: ClientLike>(
client: &C,
key: Key,
seconds: i64,
options: Option<ExpireOptions>,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let args = if let Some(options) = options {
vec![key.into(), seconds.into(), options.to_str().into()]
} else {
vec![key.into(), seconds.into()]
};
Ok((CommandKind::Expire, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn expire_at<C: ClientLike>(
client: &C,
key: Key,
timestamp: i64,
options: Option<ExpireOptions>,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let args = if let Some(options) = options {
vec![key.into(), timestamp.into(), options.to_str().into()]
} else {
vec![key.into(), timestamp.into()]
};
Ok((CommandKind::ExpireAt, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn expire_time<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::ExpireTime, key.into()).await
}
pub async fn pexpire_time<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::PexpireTime, key.into()).await
}
pub async fn pexpire<C: ClientLike>(
client: &C,
key: Key,
milliseconds: i64,
options: Option<ExpireOptions>,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let args = if let Some(options) = options {
vec![key.into(), milliseconds.into(), options.to_str().into()]
} else {
vec![key.into(), milliseconds.into()]
};
Ok((CommandKind::Pexpire, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn pexpire_at<C: ClientLike>(
client: &C,
key: Key,
timestamp: i64,
options: Option<ExpireOptions>,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let args = if let Some(options) = options {
vec![key.into(), timestamp.into(), options.to_str().into()]
} else {
vec![key.into(), timestamp.into()]
};
Ok((CommandKind::Pexpireat, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn exists<C: ClientLike>(client: &C, keys: MultipleKeys) -> Result<Value, Error> {
check_empty_keys(&keys)?;
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(keys.len());
for key in keys.inner().into_iter() {
args.push(key.into());
}
Ok((CommandKind::Exists, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn dump<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_values_cmd(client, CommandKind::Dump, key.into()).await
}
pub async fn restore<C: ClientLike>(
client: &C,
key: Key,
ttl: i64,
serialized: Value,
replace: bool,
absttl: bool,
idletime: Option<i64>,
frequency: Option<i64>,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(9);
args.push(key.into());
args.push(ttl.into());
args.push(serialized);
if replace {
args.push(static_val!(REPLACE));
}
if absttl {
args.push(static_val!(ABSTTL));
}
if let Some(idletime) = idletime {
args.push(static_val!(IDLE_TIME));
args.push(idletime.into());
}
if let Some(frequency) = frequency {
args.push(static_val!(FREQ));
args.push(frequency.into());
}
Ok((CommandKind::Restore, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn getrange<C: ClientLike>(client: &C, key: Key, start: usize, end: usize) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
Ok((CommandKind::GetRange, vec![
key.into(),
start.try_into()?,
end.try_into()?,
]))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn setrange<C: ClientLike>(client: &C, key: Key, offset: u32, value: Value) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
Ok((CommandKind::Setrange, vec![key.into(), offset.into(), value]))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn getset<C: ClientLike>(client: &C, key: Key, value: Value) -> Result<Value, Error> {
args_values_cmd(client, CommandKind::GetSet, vec![key.into(), value]).await
}
pub async fn rename<C: ClientLike>(client: &C, source: Key, destination: Key) -> Result<Value, Error> {
args_values_cmd(client, CommandKind::Rename, vec![source.into(), destination.into()]).await
}
pub async fn renamenx<C: ClientLike>(client: &C, source: Key, destination: Key) -> Result<Value, Error> {
args_values_cmd(client, CommandKind::Renamenx, vec![source.into(), destination.into()]).await
}
pub async fn getdel<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_values_cmd(client, CommandKind::GetDel, key.into()).await
}
pub async fn strlen<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Strlen, key.into()).await
}
pub async fn mget<C: ClientLike>(client: &C, keys: MultipleKeys) -> Result<Value, Error> {
check_empty_keys(&keys)?;
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(keys.len());
for key in keys.inner().into_iter() {
args.push(key.into());
}
Ok((CommandKind::Mget, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn mset<C: ClientLike>(client: &C, values: Map) -> Result<Value, Error> {
if values.len() == 0 {
return Err(Error::new(ErrorKind::InvalidArgument, "Values cannot be empty."));
}
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(values.len() * 2);
for (key, value) in values.inner().into_iter() {
args.push(key.into());
args.push(value);
}
Ok((CommandKind::Mset, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn msetnx<C: ClientLike>(client: &C, values: Map) -> Result<Value, Error> {
if values.len() == 0 {
return Err(Error::new(ErrorKind::InvalidArgument, "Values cannot be empty."));
}
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(values.len() * 2);
for (key, value) in values.inner().into_iter() {
args.push(key.into());
args.push(value);
}
Ok((CommandKind::Msetnx, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn copy<C: ClientLike>(
client: &C,
source: Key,
destination: Key,
db: Option<u8>,
replace: bool,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(5);
args.push(source.into());
args.push(destination.into());
if let Some(db) = db {
args.push(static_val!(DB));
args.push((db as i64).into());
}
if replace {
args.push(static_val!(REPLACE));
}
Ok((CommandKind::Copy, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}
pub async fn watch<C: ClientLike>(client: &C, keys: MultipleKeys) -> Result<(), Error> {
let args = keys.inner().into_iter().map(|k| k.into()).collect();
args_ok_cmd(client, CommandKind::Watch, args).await
}
ok_cmd!(unwatch, Unwatch);
pub async fn r#type<C: ClientLike>(client: &C, key: Key) -> Result<Value, Error> {
one_arg_value_cmd(client, CommandKind::Type, key.into()).await
}
pub async fn lcs<C: ClientLike>(
client: &C,
key1: Key,
key2: Key,
len: bool,
idx: bool,
minmatchlen: Option<i64>,
withmatchlen: bool,
) -> Result<Value, Error> {
let frame = utils::request_response(client, move || {
let mut args = Vec::with_capacity(7);
args.push(key1.into());
args.push(key2.into());
if len {
args.push(static_val!(LEN));
}
if idx {
args.push(static_val!(IDX));
}
if let Some(minmatchlen) = minmatchlen {
args.push(static_val!(MINMATCHLEN));
args.push(minmatchlen.into());
}
if withmatchlen {
args.push(static_val!(WITHMATCHLEN));
}
Ok((CommandKind::Lcs, args))
})
.await?;
protocol_utils::frame_to_results(frame)
}