1use once_cell::sync::OnceCell;
9use parking_lot::{Mutex, RwLock};
10use std::sync::Arc;
11
12use crate::CoreIdGenerator;
13use crate::IdGeneratorOptions;
14use crate::OptionError;
15
16pub struct IdInstance;
18
19impl IdInstance {
20 pub fn init(options: IdGeneratorOptions) -> Result<(), OptionError> {
22 IdInstance::get_instance().lock().init(options)
23 }
24
25 pub fn set_options(options: IdGeneratorOptions) -> Result<(), OptionError> {
27 IdInstance::get_instance().lock().set_options(options)
28 }
29
30 pub fn get_options() -> IdGeneratorOptions {
32 IdInstance::get_instance().lock().get_options()
33 }
34
35 pub fn next_id() -> i64 {
37 IdInstance::get_instance().lock().next_id()
38 }
39
40 fn get_instance() -> &'static Mutex<CoreIdGenerator> {
41 static INSTANCE: OnceCell<Mutex<CoreIdGenerator>> = OnceCell::new();
42 INSTANCE.get_or_init(|| Mutex::new(CoreIdGenerator::default()))
43 }
44}
45
46pub struct IdVecInstance;
48
49impl IdVecInstance {
50 pub fn init(mut options: Vec<IdGeneratorOptions>) -> Result<(), OptionError> {
54 if options.is_empty() {
55 return Err(OptionError::InvalidVecLen(0));
56 }
57 let mut instances = IdVecInstance::get_instance().write();
58 instances.clear();
59 for option in options.drain(..) {
60 let mut instance = CoreIdGenerator::default();
61 instance.init(option)?;
62 instances.push(Arc::new(Mutex::new(instance)));
63 }
64 Ok(())
65 }
66
67 pub fn set_options(index: usize, options: IdGeneratorOptions) -> Result<(), OptionError> {
69 let reader = {
70 let r = IdVecInstance::get_instance().read();
71 if index >= r.len() {
72 return Err(OptionError::IndexOutOfRange(index));
73 }
74 Arc::clone(&r[index])
75 };
76 reader.lock().set_options(options)?;
77 Ok(())
78 }
79
80 pub fn get_options(index: usize) -> Result<IdGeneratorOptions, OptionError> {
82 let reader = {
83 let r = IdVecInstance::get_instance().read();
84 if index >= r.len() {
85 return Err(OptionError::IndexOutOfRange(index));
86 }
87 Arc::clone(&r[index])
88 };
89 let options = reader.lock().get_options();
90 Ok(options)
91 }
92
93 pub fn next_id(index: usize) -> i64 {
95 let reader = {
98 let r = IdVecInstance::get_instance().read();
99 Arc::clone(&r[index])
100 };
101 let id = reader.lock().next_id();
102 id
103 }
104
105 fn get_instance() -> &'static RwLock<Vec<Arc<Mutex<CoreIdGenerator>>>> {
106 static INSTANCE: OnceCell<RwLock<Vec<Arc<Mutex<CoreIdGenerator>>>>> = OnceCell::new();
107 INSTANCE.get_or_init(|| RwLock::new(Vec::new()))
108 }
109}