idgenerator/options.rs
1//! # IdGeneratorOptions
2//!
3//! IdGeneratorOptions will provide you a interface for setting generators' options
4
5/// Options for CoreIdGenerator
6///
7/// ## Parameters
8///
9/// - `method`: 1 means snowflake with shift.
10/// - `base_time`: base time of the snowflake algorithm, in milliseconds, can not exceed the current system time.
11/// - `worker_id`: should be decided externally, smaller than `2^worker_id_bit_len-1`.
12/// - `worker_id_bit_len`: the bit length of worker_id, default to 8, in range \[1, 15\]. **`worker_id_bit_len + seq_bit_len` should be less than 22**.
13/// - `seq_bit_len`: the bit length of sequence, default to 8, in range \[3, 21\].
14/// - `max_seq_num`: set the range of \[min_seq_num, 2^seq_bit_len-1\], default to 0 meaning `2^seq_bit_len-1`.
15/// - `min_seq_num`: default to 5, range \[5, max_seq_num\], reserved for manually value and time turned back.
16/// - `top_over_cost_count`: max shift count(included), default to 2000, recommended range is [500, 20000] (associated with computing ability).
17#[derive(Debug, Clone, PartialEq, Default)]
18pub struct IdGeneratorOptions {
19 /// Snowflake 1 for shift
20 pub method: Option<u8>,
21
22 /// base time (in milliseconds), can not exceed the current system time
23 pub base_time: Option<i64>,
24
25 /// should be decided externally, smaller than `2^worker_id_bit_len-1`
26 pub worker_id: Option<u32>,
27
28 /// `worker_id_bit_len + seq_bit_len` should be less than 22
29 /// default to 8, in range \[1, 15\]
30 pub worker_id_bit_len: Option<u8>,
31
32 /// default to 8, in range \[3, 21\]
33 pub seq_bit_len: Option<u8>,
34
35 /// set the range of \[min_seq_num, 2^seq_bit_len-1\], default to 0 meaning `2^seq_bit_len-1`
36 pub max_seq_num: Option<u32>,
37
38 /// default to 5, range [5, max_seq_num], reserved for manually value and time turned back
39 pub min_seq_num: Option<u32>,
40
41 /// max shift count(included), default to 2000, recommended range is [500, 20000] (associated with computing ability)
42 pub top_over_cost_count: Option<u32>,
43}
44
45impl IdGeneratorOptions {
46 pub fn new() -> Self {
47 IdGeneratorOptions {
48 method: None,
49 base_time: None,
50 worker_id: None,
51 worker_id_bit_len: None,
52 seq_bit_len: None,
53 max_seq_num: None,
54 min_seq_num: None,
55 top_over_cost_count: None,
56 }
57 }
58
59 pub fn method(mut self, method: u8) -> Self {
60 self.method = Some(method);
61 self
62 }
63
64 pub fn base_time(mut self, base_time: i64) -> Self {
65 self.base_time = Some(base_time);
66 self
67 }
68
69 pub fn worker_id(mut self, worker_id: u32) -> Self {
70 self.worker_id = Some(worker_id);
71 self
72 }
73
74 pub fn worker_id_bit_len(mut self, worker_id_bit_len: u8) -> Self {
75 self.worker_id_bit_len = Some(worker_id_bit_len);
76 self
77 }
78
79 pub fn seq_bit_len(mut self, seq_bit_len: u8) -> Self {
80 self.seq_bit_len = Some(seq_bit_len);
81 self
82 }
83
84 pub fn max_seq_num(mut self, max_seq_num: u32) -> Self {
85 self.max_seq_num = Some(max_seq_num);
86 self
87 }
88
89 pub fn min_seq_num(mut self, min_seq_num: u32) -> Self {
90 self.min_seq_num = Some(min_seq_num);
91 self
92 }
93
94 pub fn top_over_cost_count(mut self, top_over_cost_count: u32) -> Self {
95 self.top_over_cost_count = Some(top_over_cost_count);
96 self
97 }
98}