1pub mod uuid {
2 use lazy_static::lazy_static;
3 use std::{ sync::Arc,thread };
4 use std::sync::{atomic::AtomicI64,atomic::AtomicI16,atomic::Ordering };
5 use chrono::{ Local,DateTime };
6
7 #[derive(Debug, Clone,Copy)]
8 pub enum IDMode {
9 SequenceId,
10 TimestampId,
11 }
12
13 lazy_static! {
14 static ref LARGEID:Arc<AtomicI64> = Arc::new(AtomicI64::new(1));
15 }
16
17 pub fn set_seed(seed:i64) {
19 LARGEID.store(seed,Ordering::Relaxed);
20 }
21
22 pub fn next_big_id() -> i64 {
24 let rvalue = LARGEID.load(Ordering::Relaxed);
25 LARGEID.fetch_add(1,Ordering::SeqCst);
26 rvalue
27 }
28
29 lazy_static! {
31 static ref TIMESTAPID:Arc<AtomicI16> = Arc::new(AtomicI16::new(1));
32 }
33
34 pub fn next_timestamp_id() -> i64 {
35 TIMESTAPID.fetch_add(1,Ordering::SeqCst);
36 let mut ids = TIMESTAPID.load(Ordering::Relaxed);
37 if ids >= 99 {
38 TIMESTAPID.store(1,Ordering::Relaxed);
39 ids = 1
40 }
41
42 (Local::now().timestamp() * 100) + (ids as i64)
43 }
44
45 #[test]
47 fn test_set_seed() {
48 set_seed(100);
49 let mut hv = vec![];
50 for i in 1..10 {
51 let h = thread::spawn(|| {
52 for i in 1..5 {
53 let next = next_big_id();
54 println!("{}",next);
55 }
56 });
57 hv.push(h);
58 }
59
60 for tv in hv {
61 tv.join();
62 }
63 }
64
65 #[test]
66 fn test_next_timestamp() {
67 let mut hv = vec![];
68 for i in 1..10 {
69 let h = thread::spawn(|| {
70 for i in 1..5 {
71 let next = next_timestamp_id();
72 println!("{}",next);
73 }
74 });
75 hv.push(h);
76 }
77
78 for tv in hv {
79 tv.join();
80 }
81 }
82}