wresp 0.1.3

RESP (REdis Serialization Protocol) response writer and serializer
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
//! RESP 协议通用命令选项与词法解析(对标 Garnet.common 与 Garnet.server:RespEnums)
//!
//! 集中统一定义 Redis 命令通用选项枚举,提供零分配、无分支/最少分支的快速词法解析。

use bitflags::bitflags;

/// 比较字节切片是否相等(忽略 ASCII 大小写)
///
/// 对标 Garnet CmdStrings.EqualsUpperCaseSpanIgnoringCase
#[inline]
pub fn equals_ignore_case(a: &[u8], b: &[u8]) -> bool {
  a.eq_ignore_ascii_case(b)
}

bitflags! {
  /// ZADD 修饰选项(对标 Garnet.common/Objects/SortedSet:SortedSetAddOption)
  #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
  pub struct SortedSetAddOption: u8 {
    /// 无选项
    const NONE = 0;
    const None = 0;
    /// 仅更新已存在元素
    const XX = 1;
    const Xx = 1;
    /// 仅新增不存在元素
    const NX = 1 << 1;
    const Nx = 1 << 1;
    /// 新分值小于当前分值才更新
    const LT = 1 << 2;
    const Lt = 1 << 2;
    /// 新分值大于当前分值才更新
    const GT = 1 << 3;
    const Gt = 1 << 3;
    /// 返回值改为"新增+变更"总数
    const CH = 1 << 4;
    const Ch = 1 << 4;
    /// ZADD 退化为 ZINCRBY,仅允许单对 score-element
    const INCR = 1 << 5;
    const Incr = 1 << 5;
  }
}

/// 解析 ZADD 选项词元(XX/NX/LT/GT/CH/INCR)
#[inline]
pub fn try_get_sorted_set_add_option(v: &[u8]) -> Option<SortedSetAddOption> {
  match v.len() {
    2 => {
      let b0 = v[0].to_ascii_uppercase();
      let b1 = v[1].to_ascii_uppercase();
      match (b0, b1) {
        (b'X', b'X') => Some(SortedSetAddOption::XX),
        (b'N', b'X') => Some(SortedSetAddOption::NX),
        (b'L', b'T') => Some(SortedSetAddOption::LT),
        (b'G', b'T') => Some(SortedSetAddOption::GT),
        (b'C', b'H') => Some(SortedSetAddOption::CH),
        _ => None,
      }
    }
    4 => {
      let b0 = v[0].to_ascii_uppercase();
      let b1 = v[1].to_ascii_uppercase();
      let b2 = v[2].to_ascii_uppercase();
      let b3 = v[3].to_ascii_uppercase();
      if (b0, b1, b2, b3) == (b'I', b'N', b'C', b'R') {
        Some(SortedSetAddOption::INCR)
      } else {
        None
      }
    }
    _ => None,
  }
}

bitflags! {
  /// 过期条件选项(NX/XX/GT/LT;对标 Garnet.server:ExpireOption)
  #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
  pub struct ExpireOption: u8 {
    /// 无条件
    const NONE = 0;
    const None = 0;
    /// 仅当无既有过期时设置
    const NX = 1 << 0;
    const Nx = 1 << 0;
    /// 仅当有既有过期时设置
    const XX = 1 << 1;
    const Xx = 1 << 1;
    /// 仅当新过期晚于当前时设置
    const GT = 1 << 2;
    const Gt = 1 << 2;
    /// 仅当新过期早于当前时设置
    const LT = 1 << 3;
    const Lt = 1 << 3;
    /// 既有且更大
    const XXGT = Self::XX.bits() | Self::GT.bits();
    /// 既有且更小
    const XXLT = Self::XX.bits() | Self::LT.bits();
  }
}

/// 解析过期选项词元(NX/XX/GT/LT)
#[inline]
pub fn try_get_expire_option(v: &[u8]) -> Option<ExpireOption> {
  if v.len() != 2 {
    return None;
  }
  let b0 = v[0].to_ascii_uppercase();
  let b1 = v[1].to_ascii_uppercase();
  match (b0, b1) {
    (b'N', b'X') => Some(ExpireOption::NX),
    (b'X', b'X') => Some(ExpireOption::XX),
    (b'G', b'T') => Some(ExpireOption::GT),
    (b'L', b'T') => Some(ExpireOption::LT),
    _ => None,
  }
}

/// 从单个 token 解析过期选项(等价于 [`try_get_expire_option`])
#[inline]
pub fn expire_option_from_token(arg: &[u8]) -> Option<ExpireOption> {
  try_get_expire_option(arg)
}

/// 复合过期时间戳与条件选项(对标 Garnet libs/server/ExpirationWithOption.cs)
///
/// 低 4 位存储 [`ExpireOption`],高 60 位存储粗粒度 .NET Ticks(1600ns 分辨率)。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ExpirationWithOption {
  word: i64,
}

impl ExpirationWithOption {
  /// libs/server/ExpirationWithOption.cs:ExpirationWithOption(long, ExpireOption)
  #[inline]
  pub fn new(expiration_time_in_ticks: i64, expire_option: ExpireOption) -> Self {
    Self {
      word: ((expiration_time_in_ticks >> 4) << 4) | (expire_option.bits() as i64 & 0xF),
    }
  }

  /// 由 (word_head, word_tail) 两个 i32 拼装(C# RespServerSession 传参形态)
  #[inline]
  pub fn from_word_head_tail(word_head: i32, word_tail: i32) -> Self {
    Self {
      word: ((((word_head as u32) as u64) << 32) | (word_tail as u32 as u64)) as i64,
    }
  }

  /// libs/server/ExpirationWithOption.cs:ExpirationTimeInTicks
  #[inline]
  pub fn expiration_time_in_ticks(&self) -> i64 {
    (self.word >> 4) << 4
  }

  /// libs/server/ExpirationWithOption.cs:ExpireOption
  #[inline]
  pub fn expire_option(&self) -> ExpireOption {
    ExpireOption::from_bits_truncate((self.word & 0xF) as u8)
  }

  /// libs/server/ExpirationWithOption.cs:Word
  #[inline]
  pub fn word(&self) -> i64 {
    self.word
  }

  /// libs/server/ExpirationWithOption.cs:WordHead
  #[inline]
  pub fn word_head(&self) -> i32 {
    ((self.word >> 32) & 0xFFFF_FFFF) as i32
  }
}

/// SET/EXPIRE 过期形式(对标 Garnet.server:ExpirationOption)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ExpirationOption {
  ///  #[default]
  None,
  /// 秒相对
  Ex,
  /// 毫秒相对
  Px,
  /// 秒绝对
  Exat,
  /// 毫秒绝对
  Pxat,
  /// 保留 TTL
  Keepttl,
}

/// 解析过期形式(EX/PX/EXAT/PXAT/KEEPTTL)
#[inline]
pub fn try_get_expiration_option(token: &[u8]) -> Option<ExpirationOption> {
  match token.len() {
    2 => {
      let b0 = token[0].to_ascii_uppercase();
      let b1 = token[1].to_ascii_uppercase();
      match (b0, b1) {
        (b'E', b'X') => Some(ExpirationOption::Ex),
        (b'P', b'X') => Some(ExpirationOption::Px),
        _ => None,
      }
    }
    4 => {
      let b0 = token[0].to_ascii_uppercase();
      let b1 = token[1].to_ascii_uppercase();
      let b2 = token[2].to_ascii_uppercase();
      let b3 = token[3].to_ascii_uppercase();
      match (b0, b1, b2, b3) {
        (b'E', b'X', b'A', b'T') => Some(ExpirationOption::Exat),
        (b'P', b'X', b'A', b'T') => Some(ExpirationOption::Pxat),
        _ => None,
      }
    }
    7 => {
      if token.eq_ignore_ascii_case(b"KEEPTTL") {
        Some(ExpirationOption::Keepttl)
      } else {
        None
      }
    }
    _ => None,
  }
}

/// 从 token 解析过期形式(等价于 [`try_get_expiration_option`])
#[inline]
pub fn expiration_option_from_token(token: &[u8]) -> Option<ExpirationOption> {
  try_get_expiration_option(token)
}

/// 存在性约束选项(对标 Garnet.server:ExistOptions)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ExistOptions {
  #[default]
  None,
  Nx,
  Xx,
}

/// 解析存在性约束(NX/XX)
#[inline]
pub fn try_get_exist_options(token: &[u8]) -> Option<ExistOptions> {
  if token.len() != 2 {
    return None;
  }
  let b0 = token[0].to_ascii_uppercase();
  let b1 = token[1].to_ascii_uppercase();
  match (b0, b1) {
    (b'N', b'X') => Some(ExistOptions::Nx),
    (b'X', b'X') => Some(ExistOptions::Xx),
    _ => None,
  }
}

/// 有序集合聚合类型(对标 Garnet.server:SortedSetAggregateType)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SortedSetAggregateType {
  #[default]
  Sum,
  Min,
  Max,
}

impl SortedSetAggregateType {
  /// 对两分值执行聚合运算(SUM / MIN / MAX)
  #[inline(always)]
  pub fn apply(self, a: f64, b: f64) -> f64 {
    match self {
      Self::Sum => a + b,
      Self::Min => a.min(b),
      Self::Max => a.max(b),
    }
  }
}

/// 解析有序集合聚合方式(SUM/MIN/MAX)
#[inline]
pub fn try_get_sorted_set_aggregate_type(token: &[u8]) -> Option<SortedSetAggregateType> {
  if token.len() != 3 {
    return None;
  }
  let b0 = token[0].to_ascii_uppercase();
  let b1 = token[1].to_ascii_uppercase();
  let b2 = token[2].to_ascii_uppercase();
  match (b0, b1, b2) {
    (b'S', b'U', b'M') => Some(SortedSetAggregateType::Sum),
    (b'M', b'I', b'N') => Some(SortedSetAggregateType::Min),
    (b'M', b'A', b'X') => Some(SortedSetAggregateType::Max),
    _ => None,
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_equals_ignore_case() {
    assert!(equals_ignore_case(b"hello", b"HELLO"));
    assert!(equals_ignore_case(b"ZADD", b"zadd"));
    assert!(!equals_ignore_case(b"ZADD", b"zadd1"));
    assert!(!equals_ignore_case(b"", b"a"));
  }

  #[test]
  fn test_sorted_set_add_options_parse() {
    for (raw, expected) in [
      (b"xx" as &[u8], SortedSetAddOption::XX),
      (b"XX", SortedSetAddOption::XX),
      (b"nx", SortedSetAddOption::NX),
      (b"NX", SortedSetAddOption::NX),
      (b"lt", SortedSetAddOption::LT),
      (b"LT", SortedSetAddOption::LT),
      (b"gt", SortedSetAddOption::GT),
      (b"GT", SortedSetAddOption::GT),
      (b"ch", SortedSetAddOption::CH),
      (b"CH", SortedSetAddOption::CH),
      (b"incr", SortedSetAddOption::INCR),
      (b"INCR", SortedSetAddOption::INCR),
    ] {
      assert_eq!(try_get_sorted_set_add_option(raw), Some(expected));
    }
    for bad in [b"" as &[u8], b"x", b"xxx", b"inc", b"incr1", b"zz"] {
      assert_eq!(try_get_sorted_set_add_option(bad), None);
    }
  }

  #[test]
  fn test_expire_options_parse() {
    for (raw, expected) in [
      (b"nx" as &[u8], ExpireOption::NX),
      (b"NX", ExpireOption::NX),
      (b"xx", ExpireOption::XX),
      (b"XX", ExpireOption::XX),
      (b"gt", ExpireOption::GT),
      (b"GT", ExpireOption::GT),
      (b"lt", ExpireOption::LT),
      (b"LT", ExpireOption::LT),
    ] {
      assert_eq!(try_get_expire_option(raw), Some(expected));
      assert_eq!(expire_option_from_token(raw), Some(expected));
    }
    for bad in [b"" as &[u8], b"n", b"nxx", b"zz", b"none"] {
      assert_eq!(try_get_expire_option(bad), None);
      assert_eq!(expire_option_from_token(bad), None);
    }
  }

  #[test]
  fn test_expiration_options_parse() {
    for (raw, expected) in [
      (b"ex" as &[u8], ExpirationOption::Ex),
      (b"EX", ExpirationOption::Ex),
      (b"px", ExpirationOption::Px),
      (b"PX", ExpirationOption::Px),
      (b"exat", ExpirationOption::Exat),
      (b"EXAT", ExpirationOption::Exat),
      (b"pxat", ExpirationOption::Pxat),
      (b"PXAT", ExpirationOption::Pxat),
      (b"keepttl", ExpirationOption::Keepttl),
      (b"KEEPTTL", ExpirationOption::Keepttl),
    ] {
      assert_eq!(try_get_expiration_option(raw), Some(expected));
      assert_eq!(expiration_option_from_token(raw), Some(expected));
    }
    for bad in [b"" as &[u8], b"e", b"exx", b"pxa", b"keeptt", b"keepttll"] {
      assert_eq!(try_get_expiration_option(bad), None);
      assert_eq!(expiration_option_from_token(bad), None);
    }
  }

  #[test]
  fn test_exist_options_parse() {
    assert_eq!(try_get_exist_options(b"nx"), Some(ExistOptions::Nx));
    assert_eq!(try_get_exist_options(b"NX"), Some(ExistOptions::Nx));
    assert_eq!(try_get_exist_options(b"xx"), Some(ExistOptions::Xx));
    assert_eq!(try_get_exist_options(b"XX"), Some(ExistOptions::Xx));
    assert_eq!(try_get_exist_options(b"none"), None);
    assert_eq!(try_get_exist_options(b""), None);
  }

  #[test]
  fn test_sorted_set_aggregate_type_parse() {
    assert_eq!(
      try_get_sorted_set_aggregate_type(b"sum"),
      Some(SortedSetAggregateType::Sum)
    );
    assert_eq!(
      try_get_sorted_set_aggregate_type(b"SUM"),
      Some(SortedSetAggregateType::Sum)
    );
    assert_eq!(
      try_get_sorted_set_aggregate_type(b"min"),
      Some(SortedSetAggregateType::Min)
    );
    assert_eq!(
      try_get_sorted_set_aggregate_type(b"MAX"),
      Some(SortedSetAggregateType::Max)
    );
    assert_eq!(try_get_sorted_set_aggregate_type(b"avg"), None);
    assert_eq!(try_get_sorted_set_aggregate_type(b"summ"), None);
    assert_eq!(try_get_sorted_set_aggregate_type(b""), None);
  }

  #[test]
  fn test_sorted_set_aggregate_apply() {
    assert_eq!(SortedSetAggregateType::Sum.apply(2.5, 3.5), 6.0);
    assert_eq!(SortedSetAggregateType::Min.apply(2.5, 3.5), 2.5);
    assert_eq!(SortedSetAggregateType::Max.apply(2.5, 3.5), 3.5);
  }

  #[test]
  fn test_expiration_with_option() {
    let ticks = 1_000_000_000i64;
    let opt = ExpireOption::GT;
    let e = ExpirationWithOption::new(ticks, opt);
    assert_eq!(e.expire_option(), ExpireOption::GT);
    assert_eq!(e.expiration_time_in_ticks(), (ticks >> 4) << 4);

    let reconstructed =
      ExpirationWithOption::from_word_head_tail(e.word_head(), (e.word() & 0xFFFF_FFFF) as i32);
    assert_eq!(e, reconstructed);
    assert_eq!(e.word(), reconstructed.word());
  }
}