wedb_embed 0.1.7

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
Documentation
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use std::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

use super::meta::{NextStreamEntryIdStrategy, StreamId};

/// Stream trim strategy (aligned with Apache Kvrocks StreamTrimStrategy).
/// Stream 裁剪策略(对标 Apache Kvrocks StreamTrimStrategy)
#[derive(Debug, Clone, Copy, PartialEq, Eq, bitcode::Encode, bitcode::Decode, Default)]
#[repr(u8)]
pub enum StreamTrimStrategy {
  #[default]
  None = 0,
  MaxLen = 1,
  MinId = 2,
}

/// Stream trim configuration (aligned with Apache Kvrocks StreamTrimOpt).
/// Stream 裁剪配置(对标 Apache Kvrocks StreamTrimOpt)
#[derive(Debug, Clone, Copy, PartialEq, Eq, bitcode::Encode, bitcode::Decode, Default)]
pub struct StreamTrim {
  pub strategy: StreamTrimStrategy,
  pub max_len: u64,
  pub min_id: StreamId,
  pub limit: Option<usize>,
}

/// XTRIM command options enumeration.
/// XTRIM 选项枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XTrim {
  MaxLen(u64),
  MinId(StreamId),
  Limit(usize),
}

impl StreamTrim {
  pub fn none() -> Self {
    Self::default()
  }

  pub fn maxlen(max_len: u64) -> Self {
    Self {
      strategy: StreamTrimStrategy::MaxLen,
      max_len,
      min_id: StreamId::min(),
      limit: None,
    }
  }

  pub fn minid(min_id: StreamId) -> Self {
    Self {
      strategy: StreamTrimStrategy::MinId,
      max_len: 0,
      min_id,
      limit: None,
    }
  }

  pub fn with_limit(mut self, limit: usize) -> Self {
    self.limit = Some(limit);
    self
  }

  pub fn from_options(options: impl IntoIterator<Item = XTrim>) -> Self {
    let mut trim = Self::default();
    for opt in options {
      match opt {
        XTrim::MaxLen(len) => {
          trim.strategy = StreamTrimStrategy::MaxLen;
          trim.max_len = len;
        }
        XTrim::MinId(id) => {
          trim.strategy = StreamTrimStrategy::MinId;
          trim.min_id = id;
        }
        XTrim::Limit(l) => trim.limit = Some(l),
      }
    }
    trim
  }
}

/// XADD command options enumeration.
/// XADD 选项枚举
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XAdd {
  Id(StreamId),
  Strategy(NextStreamEntryIdStrategy),
  Trim(StreamTrim),
  NoMkStream,
}

/// XADD command configuration options aligned with Apache Kvrocks StreamAddOpt.
/// XADD 配置选项(对标 Apache Kvrocks StreamAddOpt)
#[derive(Debug, Clone, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
pub struct StreamAdd {
  pub trim_options: StreamTrim,
  pub next_id_strategy: NextStreamEntryIdStrategy,
  pub nomkstream: bool,
}

impl Default for StreamAdd {
  fn default() -> Self {
    Self {
      trim_options: StreamTrim::none(),
      next_id_strategy: NextStreamEntryIdStrategy::Auto,
      nomkstream: false,
    }
  }
}

impl StreamAdd {
  pub fn auto() -> Self {
    Self::default()
  }

  pub fn with_id(id: StreamId) -> Self {
    Self {
      trim_options: StreamTrim::none(),
      next_id_strategy: NextStreamEntryIdStrategy::FullySpecified(id),
      nomkstream: false,
    }
  }

  pub fn with_strategy(strategy: NextStreamEntryIdStrategy) -> Self {
    Self {
      trim_options: StreamTrim::none(),
      next_id_strategy: strategy,
      nomkstream: false,
    }
  }

  pub fn with_trim(mut self, trim_options: StreamTrim) -> Self {
    self.trim_options = trim_options;
    self
  }

  pub fn nomkstream(mut self, nomkstream: bool) -> Self {
    self.nomkstream = nomkstream;
    self
  }

  pub fn from_options(options: impl IntoIterator<Item = XAdd>) -> Self {
    let mut add = Self::default();
    for opt in options {
      match opt {
        XAdd::Id(id) => add.next_id_strategy = NextStreamEntryIdStrategy::FullySpecified(id),
        XAdd::Strategy(s) => add.next_id_strategy = s,
        XAdd::Trim(t) => add.trim_options = t,
        XAdd::NoMkStream => add.nomkstream = true,
      }
    }
    add
  }
}

impl From<Option<StreamId>> for StreamAdd {
  fn from(opt: Option<StreamId>) -> Self {
    match opt {
      Some(id) => Self::with_id(id),
      None => Self::auto(),
    }
  }
}

impl From<()> for StreamAdd {
  fn from(_: ()) -> Self {
    Self::auto()
  }
}

impl From<&StreamAdd> for StreamAdd {
  fn from(opt: &StreamAdd) -> Self {
    opt.clone()
  }
}

impl From<StreamId> for StreamAdd {
  fn from(id: StreamId) -> Self {
    Self::with_id(id)
  }
}

/// XRANGE command options enumeration.
/// XRANGE 选项枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XRange {
  Count(usize),
  Rev,
  ExcludeStart,
  ExcludeEnd,
}

/// XRANGE / XREVRANGE configuration structure (aligned with Kvrocks StreamRangeOpt).
/// XRANGE / XREVRANGE 配置选项(对标 Apache Kvrocks StreamRangeOpt)
#[derive(Debug, Clone, Copy, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
pub struct StreamRange {
  pub start: StreamId,
  pub end: StreamId,
  pub count: Option<usize>,
  pub reverse: bool,
  pub exclude_start: bool,
  pub exclude_end: bool,
}

impl Default for StreamRange {
  fn default() -> Self {
    Self {
      start: StreamId::min(),
      end: StreamId::max(),
      count: None,
      reverse: false,
      exclude_start: false,
      exclude_end: false,
    }
  }
}

impl From<(StreamId, StreamId)> for StreamRange {
  fn from((start, end): (StreamId, StreamId)) -> Self {
    Self::new(start, end)
  }
}

impl From<(StreamId, StreamId, Option<usize>)> for StreamRange {
  fn from((start, end, count): (StreamId, StreamId, Option<usize>)) -> Self {
    let mut opt = Self::new(start, end);
    opt.count = count;
    opt
  }
}

impl From<(StreamId, StreamId, usize)> for StreamRange {
  fn from((start, end, count): (StreamId, StreamId, usize)) -> Self {
    Self::new(start, end).with_count(count)
  }
}

impl From<Range<StreamId>> for StreamRange {
  #[inline]
  fn from(r: Range<StreamId>) -> Self {
    let mut s = Self::new(r.start, r.end);
    s.exclude_end = true;
    s
  }
}

impl From<RangeInclusive<StreamId>> for StreamRange {
  #[inline]
  fn from(r: RangeInclusive<StreamId>) -> Self {
    Self::new(*r.start(), *r.end())
  }
}

impl From<RangeFrom<StreamId>> for StreamRange {
  #[inline]
  fn from(r: RangeFrom<StreamId>) -> Self {
    Self::new(r.start, StreamId::max())
  }
}

impl From<RangeTo<StreamId>> for StreamRange {
  #[inline]
  fn from(r: RangeTo<StreamId>) -> Self {
    let mut s = Self::new(StreamId::min(), r.end);
    s.exclude_end = true;
    s
  }
}

impl From<RangeToInclusive<StreamId>> for StreamRange {
  #[inline]
  fn from(r: RangeToInclusive<StreamId>) -> Self {
    Self::new(StreamId::min(), r.end)
  }
}

impl From<RangeFull> for StreamRange {
  #[inline]
  fn from(_: RangeFull) -> Self {
    Self::default()
  }
}

impl From<(Bound<StreamId>, Bound<StreamId>)> for StreamRange {
  #[inline]
  fn from((start, end): (Bound<StreamId>, Bound<StreamId>)) -> Self {
    let (s_id, excl_s) = match start {
      Bound::Included(id) => (id, false),
      Bound::Excluded(id) => (id, true),
      Bound::Unbounded => (StreamId::min(), false),
    };
    let (e_id, excl_e) = match end {
      Bound::Included(id) => (id, false),
      Bound::Excluded(id) => (id, true),
      Bound::Unbounded => (StreamId::max(), false),
    };
    Self {
      start: s_id,
      end: e_id,
      count: None,
      reverse: false,
      exclude_start: excl_s,
      exclude_end: excl_e,
    }
  }
}

impl From<&StreamRange> for StreamRange {
  fn from(opt: &StreamRange) -> Self {
    *opt
  }
}

impl StreamRange {
  pub fn new(start: StreamId, end: StreamId) -> Self {
    Self {
      start,
      end,
      count: None,
      reverse: false,
      exclude_start: false,
      exclude_end: false,
    }
  }

  pub fn reverse(start: StreamId, end: StreamId) -> Self {
    Self {
      start,
      end,
      count: None,
      reverse: true,
      exclude_start: false,
      exclude_end: false,
    }
  }

  pub fn with_count(mut self, count: usize) -> Self {
    self.count = Some(count);
    self
  }

  pub fn exclude_start(mut self, exclude: bool) -> Self {
    self.exclude_start = exclude;
    self
  }

  pub fn exclude_end(mut self, exclude: bool) -> Self {
    self.exclude_end = exclude;
    self
  }

  pub fn from_options(
    start: StreamId,
    end: StreamId,
    options: impl IntoIterator<Item = XRange>,
  ) -> Self {
    let mut range = Self::new(start, end);
    for opt in options {
      match opt {
        XRange::Count(c) => range.count = Some(c),
        XRange::Rev => range.reverse = true,
        XRange::ExcludeStart => range.exclude_start = true,
        XRange::ExcludeEnd => range.exclude_end = true,
      }
    }
    range
  }
}

/// XLEN command options aligned with Apache Kvrocks StreamLenOpt.
/// XLEN 配置选项(对标 Apache Kvrocks StreamLenOpt)
#[derive(Debug, Clone, Copy, PartialEq, Eq, bitcode::Encode, bitcode::Decode, Default)]
pub struct StreamLen {
  pub entry_id: StreamId,
  pub with_entry_id: bool,
  pub to_first: bool,
}

/// XGROUP CREATE command configuration options aligned with Apache Kvrocks StreamXGroupCreate.
/// XGROUP CREATE 配置选项(对标 Apache Kvrocks StreamXGroupCreate)
#[derive(Debug, Clone, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
pub struct StreamXGroupCreate {
  pub mkstream: bool,
  pub entries_read: Option<i64>,
  pub last_id: String,
}

impl Default for StreamXGroupCreate {
  fn default() -> Self {
    Self {
      mkstream: false,
      entries_read: None,
      last_id: "$".to_string(),
    }
  }
}

impl StreamXGroupCreate {
  pub fn new(last_id: impl Into<String>) -> Self {
    Self {
      mkstream: false,
      entries_read: None,
      last_id: last_id.into(),
    }
  }

  pub fn mkstream(mut self, mkstream: bool) -> Self {
    self.mkstream = mkstream;
    self
  }

  pub fn entries_read(mut self, entries_read: i64) -> Self {
    self.entries_read = Some(entries_read);
    self
  }
}

/// XCLAIM command configuration options aligned with Apache Kvrocks StreamClaimOpt.
/// XCLAIM 配置选项(对标 Apache Kvrocks StreamClaimOpt)
#[derive(Debug, Clone, PartialEq, Eq, bitcode::Encode, bitcode::Decode, Default)]
pub struct StreamClaim {
  pub idle_time_ms: u64,
  pub with_time: bool,
  pub last_delivery_time_ms: u64,
  pub with_retry_count: bool,
  pub last_delivery_count: u64,
  pub force: bool,
  pub just_id: bool,
  pub last_delivered_id: Option<StreamId>,
}

impl StreamClaim {
  pub fn new(idle_time_ms: u64) -> Self {
    Self {
      idle_time_ms,
      ..Default::default()
    }
  }

  pub fn with_time(mut self, last_delivery_time_ms: u64) -> Self {
    self.with_time = true;
    self.last_delivery_time_ms = last_delivery_time_ms;
    self
  }

  pub fn with_retry_count(mut self, last_delivery_count: u64) -> Self {
    self.with_retry_count = true;
    self.last_delivery_count = last_delivery_count;
    self
  }

  pub fn force(mut self, force: bool) -> Self {
    self.force = force;
    self
  }

  pub fn just_id(mut self, just_id: bool) -> Self {
    self.just_id = just_id;
    self
  }

  pub fn with_last_id(mut self, last_delivered_id: StreamId) -> Self {
    self.last_delivered_id = Some(last_delivered_id);
    self
  }
}

/// XAUTOCLAIM command configuration options aligned with Apache Kvrocks StreamAutoClaimOpt.
/// XAUTOCLAIM 配置选项(对标 Apache Kvrocks StreamAutoClaimOpt)
#[derive(Debug, Clone, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
pub struct StreamAutoClaim {
  pub min_idle_time_ms: u64,
  pub start_id: StreamId,
  pub count: usize,
  pub attempts_factors: usize,
  pub just_id: bool,
  pub exclude_start: bool,
}

impl Default for StreamAutoClaim {
  fn default() -> Self {
    Self {
      min_idle_time_ms: 0,
      start_id: StreamId::min(),
      count: 100,
      attempts_factors: 10,
      just_id: false,
      exclude_start: false,
    }
  }
}

impl StreamAutoClaim {
  pub fn new(min_idle_time_ms: u64, start_id: StreamId) -> Self {
    Self {
      min_idle_time_ms,
      start_id,
      count: 100,
      attempts_factors: 10,
      just_id: false,
      exclude_start: false,
    }
  }

  pub fn count(mut self, count: usize) -> Self {
    self.count = count;
    self
  }

  pub fn just_id(mut self, just_id: bool) -> Self {
    self.just_id = just_id;
    self
  }

  pub fn exclude_start(mut self, exclude: bool) -> Self {
    self.exclude_start = exclude;
    self
  }
}

/// XPENDING command configuration options aligned with Apache Kvrocks StreamPendingOpt.
/// XPENDING 配置选项(对标 Apache Kvrocks StreamPendingOpt)
#[derive(Debug, Clone, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
pub struct StreamPending {
  pub idle_time: u64,
  pub with_time: bool,
  pub start_id: StreamId,
  pub end_id: StreamId,
  pub exclude_start: bool,
  pub exclude_end: bool,
  pub count: Option<usize>,
  pub consumer: Option<String>,
}

impl Default for StreamPending {
  fn default() -> Self {
    Self {
      idle_time: 0,
      with_time: false,
      start_id: StreamId::min(),
      end_id: StreamId::max(),
      exclude_start: false,
      exclude_end: false,
      count: None,
      consumer: None,
    }
  }
}

impl StreamPending {
  pub fn summary() -> Self {
    Self::default()
  }

  pub fn range(start_id: StreamId, end_id: StreamId, count: usize) -> Self {
    Self {
      idle_time: 0,
      with_time: false,
      start_id,
      end_id,
      exclude_start: false,
      exclude_end: false,
      count: Some(count),
      consumer: None,
    }
  }

  pub fn idle(mut self, idle_time: u64) -> Self {
    self.with_time = true;
    self.idle_time = idle_time;
    self
  }

  pub fn consumer(mut self, consumer: impl Into<String>) -> Self {
    self.consumer = Some(consumer.into());
    self
  }

  pub fn exclude_start(mut self, exclude: bool) -> Self {
    self.exclude_start = exclude;
    self
  }

  pub fn exclude_end(mut self, exclude: bool) -> Self {
    self.exclude_end = exclude;
    self
  }
}

/// XREAD command options enumeration.
/// XREAD 选项枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XRead {
  Count(usize),
  Block(u64),
  NoAck,
}

/// XGROUP CREATE command options enumeration.
/// XGROUP CREATE 选项枚举
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XGroupCreate {
  MkStream,
  EntriesRead(i64),
}

/// XCLAIM command options enumeration.
/// XCLAIM 选项枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XClaim {
  Idle(u64),
  Time(u64),
  RetryCount(u64),
  Force,
  JustId,
  LastId(StreamId),
}

/// XAUTOCLAIM command options enumeration.
/// XAUTOCLAIM 选项枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XAutoClaim {
  Count(usize),
  JustId,
  ExcludeStart,
}

/// XPENDING command options enumeration.
/// XPENDING 选项枚举
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XPending {
  Idle(u64),
  Consumer(String),
  ExcludeStart,
  ExcludeEnd,
  Count(usize),
}

/// XREAD command configuration options.
/// XREAD 配置选项
#[derive(Debug, Clone, PartialEq, Eq, bitcode::Encode, bitcode::Decode, Default)]
pub struct StreamRead {
  pub count: Option<usize>,
  pub block: Option<u64>,
  pub noack: bool,
}