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
/*!
Strings相关的命令定义、解析

所有涉及到的命令参考[Redis Command Reference]

[Redis Command Reference]: https://redis.io/commands#string
*/

use core::slice::Iter;

use crate::cmd::strings::Op::{AND, NOT, OR, XOR};

#[derive(Debug)]
pub struct APPEND<'a> {
    pub key: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_append(mut iter: Iter<Vec<u8>>) -> APPEND {
    let key = iter.next().unwrap();
    let value = iter.next().unwrap();
    APPEND { key, value }
}

#[derive(Debug)]
pub struct BITFIELD<'a> {
    pub key: &'a [u8],
    pub statements: Option<Vec<Operation<'a>>>,
    pub overflows: Option<Vec<Overflow>>,
}

#[derive(Debug)]
pub enum Operation<'a> {
    GET(Get<'a>),
    INCRBY(IncrBy<'a>),
    SET(Set<'a>),
}

#[derive(Debug)]
pub struct Get<'a> {
    pub _type: &'a [u8],
    pub offset: &'a [u8],
}

#[derive(Debug)]
pub struct IncrBy<'a> {
    pub _type: &'a [u8],
    pub offset: &'a [u8],
    pub increment: &'a [u8],
}

#[derive(Debug)]
pub struct Set<'a> {
    pub _type: &'a [u8],
    pub offset: &'a [u8],
    pub value: &'a [u8],
}

#[derive(Debug)]
pub enum Overflow {
    WRAP,
    SAT,
    FAIL,
}

pub(crate) fn parse_bitfield(mut iter: Iter<Vec<u8>>) -> BITFIELD {
    let key = iter.next().unwrap();

    let mut statements = Vec::new();
    let mut overflows = Vec::new();
    while let Some(next_arg) = iter.next() {
        let arg_upper = &String::from_utf8_lossy(next_arg).to_uppercase();
        if arg_upper == "GET" {
            let _type = iter.next().expect("bitfield 缺失get type");
            let offset = iter.next().expect("bitfield 缺失get offset");
            statements.push(Operation::GET(Get { _type, offset }));
        } else if arg_upper == "SET" {
            let _type = iter.next().unwrap();
            let offset = iter.next().expect("bitfield 缺失SET offset");
            let value = iter.next().expect("bitfield 缺失SET offset");
            statements.push(Operation::SET(Set {
                _type,
                offset,
                value,
            }));
        } else if arg_upper == "INCRBY" {
            let _type = iter.next().expect("bitfield 缺失INCR type");
            let offset = iter.next().expect("bitfield 缺失INCR offset");
            let increment = iter.next().expect("bitfield 缺失INCR offset");
            statements.push(Operation::INCRBY(IncrBy {
                _type,
                offset,
                increment,
            }));
        } else if arg_upper == "OVERFLOW" {
            let _type = String::from_utf8_lossy(iter.next().expect("bitfield 缺失OVERFLOW type"));
            let type_upper = &_type.to_uppercase();
            if type_upper == "FAIL" {
                overflows.push(Overflow::FAIL);
            } else if type_upper == "SAT" {
                overflows.push(Overflow::SAT);
            } else if type_upper == "WRAP" {
                overflows.push(Overflow::WRAP);
            }
        }
    }

    let _statements;
    if statements.is_empty() {
        _statements = None;
    } else {
        _statements = Some(statements);
    }
    let _overflows;
    if overflows.is_empty() {
        _overflows = None;
    } else {
        _overflows = Some(overflows);
    }
    BITFIELD {
        key,
        statements: _statements,
        overflows: _overflows,
    }
}

#[derive(Debug)]
pub struct BITOP<'a> {
    pub operation: Op,
    pub dest_key: &'a [u8],
    pub keys: Vec<&'a Vec<u8>>,
}

#[derive(Debug)]
pub enum Op {
    AND,
    OR,
    XOR,
    NOT,
}

pub(crate) fn parse_bitop(mut iter: Iter<Vec<u8>>) -> BITOP {
    let operation;
    let op = String::from_utf8_lossy(iter.next().unwrap()).to_uppercase();
    if &op == "AND" {
        operation = AND;
    } else if &op == "OR" {
        operation = OR;
    } else if &op == "XOR" {
        operation = XOR;
    } else if &op == "NOT" {
        operation = NOT;
    } else {
        panic!("bitop命令缺失operation")
    }
    let dest_key = iter.next().unwrap();

    let mut keys = Vec::new();
    while let Some(next_arg) = iter.next() {
        keys.push(next_arg);
    }
    if keys.is_empty() {
        panic!("bitop命令缺失input key")
    }
    BITOP {
        operation,
        dest_key,
        keys,
    }
}

#[derive(Debug)]
pub struct SET<'a> {
    pub key: &'a [u8],
    pub value: &'a [u8],
    pub expire: Option<(ExpireType, &'a Vec<u8>)>,
    pub exist_type: Option<ExistType>,
}

#[derive(Debug)]
pub enum ExpireType {
    // seconds -- Set the specified expire time, in seconds.
    EX,
    // milliseconds -- Set the specified expire time, in milliseconds.
    PX,
}

#[derive(Debug)]
pub enum ExistType {
    // Only set the key if it does not already exist.
    NX,
    // Only set the key if it already exist.
    XX,
}

pub(crate) fn parse_set(mut iter: Iter<Vec<u8>>) -> SET {
    let key = iter.next().unwrap();

    let value = iter.next().unwrap();

    let mut expire_time = None;
    let mut expire_type = None;
    let mut exist_type = None;
    let mut expire = None;

    for arg in iter {
        let arg_string = String::from_utf8_lossy(arg);
        let p_arg = &arg_string.to_uppercase();
        if p_arg == "EX" {
            expire_type = Some(ExpireType::EX);
        } else if p_arg == "PX" {
            expire_type = Some(ExpireType::PX);
        } else if p_arg == "NX" {
            exist_type = Some(ExistType::NX);
        } else if p_arg == "XX" {
            exist_type = Some(ExistType::XX);
        } else {
            // 读取过期时间
            expire_time = Some(arg);
        }
    }
    if expire_type.is_some() && expire_time.is_some() {
        expire = Some((expire_type.unwrap(), expire_time.unwrap()));
    }
    SET {
        key,
        value,
        exist_type,
        expire,
    }
}

#[derive(Debug)]
pub struct SETEX<'a> {
    pub key: &'a [u8],
    pub seconds: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_setex(mut iter: Iter<Vec<u8>>) -> SETEX {
    let key = iter.next().unwrap();
    let seconds = iter.next().unwrap();
    let value = iter.next().unwrap();
    SETEX {
        key,
        seconds,
        value,
    }
}

#[derive(Debug)]
pub struct SETNX<'a> {
    pub key: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_setnx(mut iter: Iter<Vec<u8>>) -> SETNX {
    let key = iter.next().unwrap();
    let value = iter.next().unwrap();
    SETNX { key, value }
}

#[derive(Debug)]
pub struct PSETEX<'a> {
    pub key: &'a [u8],
    pub milliseconds: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_psetex(mut iter: Iter<Vec<u8>>) -> PSETEX {
    let key = iter.next().unwrap();
    let milliseconds = iter.next().unwrap();
    let value = iter.next().unwrap();
    PSETEX {
        key,
        milliseconds,
        value,
    }
}

#[derive(Debug)]
pub struct SETRANGE<'a> {
    pub key: &'a [u8],
    pub offset: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_setrange(mut iter: Iter<Vec<u8>>) -> SETRANGE {
    let key = iter.next().unwrap();
    let offset = iter.next().unwrap();
    let value = iter.next().unwrap();
    SETRANGE { key, offset, value }
}

#[derive(Debug)]
pub struct DECR<'a> {
    pub key: &'a [u8],
}

pub(crate) fn parse_decr(mut iter: Iter<Vec<u8>>) -> DECR {
    let key = iter.next().unwrap();
    DECR { key }
}

#[derive(Debug)]
pub struct DECRBY<'a> {
    pub key: &'a [u8],
    pub decrement: &'a [u8],
}

pub(crate) fn parse_decrby(mut iter: Iter<Vec<u8>>) -> DECRBY {
    let key = iter.next().unwrap();
    let decrement = iter.next().unwrap();
    DECRBY { key, decrement }
}

#[derive(Debug)]
pub struct INCR<'a> {
    pub key: &'a [u8],
}

pub(crate) fn parse_incr(mut iter: Iter<Vec<u8>>) -> INCR {
    let key = iter.next().unwrap();
    INCR { key }
}

#[derive(Debug)]
pub struct INCRBY<'a> {
    pub key: &'a [u8],
    pub increment: &'a [u8],
}

pub(crate) fn parse_incrby(mut iter: Iter<Vec<u8>>) -> INCRBY {
    let key = iter.next().unwrap();
    let increment = iter.next().unwrap();
    INCRBY { key, increment }
}

#[derive(Debug)]
pub struct KeyValue<'a> {
    pub key: &'a [u8],
    pub value: &'a [u8],
}

#[derive(Debug)]
pub struct MSET<'a> {
    pub key_values: Vec<KeyValue<'a>>,
}

pub(crate) fn parse_mset(mut iter: Iter<Vec<u8>>) -> MSET {
    let mut key_values = Vec::new();
    while let Some(key) = iter.next() {
        if let Some(value) = iter.next() {
            key_values.push(KeyValue { key, value });
        }
    }
    if key_values.is_empty() {
        panic!("mset命令缺失key value");
    }
    MSET { key_values }
}

#[derive(Debug)]
pub struct MSETNX<'a> {
    pub key_values: Vec<KeyValue<'a>>,
}

pub(crate) fn parse_msetnx(mut iter: Iter<Vec<u8>>) -> MSETNX {
    let mut key_values = Vec::new();
    while let Some(key) = iter.next() {
        if let Some(value) = iter.next() {
            key_values.push(KeyValue { key, value });
        }
    }
    if key_values.is_empty() {
        panic!("msetnx命令缺失key value");
    }
    MSETNX { key_values }
}

#[derive(Debug)]
pub struct SETBIT<'a> {
    pub key: &'a [u8],
    pub offset: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_setbit(mut iter: Iter<Vec<u8>>) -> SETBIT {
    let key = iter.next().unwrap();
    let offset = iter.next().unwrap();
    let value = iter.next().unwrap();
    SETBIT { key, value, offset }
}

#[derive(Debug)]
pub struct GETSET<'a> {
    pub key: &'a [u8],
    pub value: &'a [u8],
}

pub(crate) fn parse_getset(mut iter: Iter<Vec<u8>>) -> GETSET {
    let key = iter.next().unwrap();
    let value = iter.next().unwrap();
    GETSET { key, value }
}