spin_factor_key_value/
runtime_config.rs1pub mod spin;
2
3use std::{collections::HashMap, sync::Arc, time::Duration};
4
5use crate::StoreManager;
6
7#[derive(Default, Clone)]
9pub struct RuntimeConfig {
10 store_managers: HashMap<String, Arc<dyn StoreManager>>,
12 max_concurrent_operations: Option<usize>,
14 wait_timeout: Option<Duration>,
16}
17
18impl RuntimeConfig {
19 pub fn add_store_manager(&mut self, label: String, store_manager: Arc<dyn StoreManager>) {
23 self.store_managers.insert(label, store_manager);
24 }
25
26 pub fn has_store_manager(&self, label: &str) -> bool {
28 self.store_managers.contains_key(label)
29 }
30
31 pub fn get_store_manager(&self, label: &str) -> Option<Arc<dyn StoreManager>> {
33 self.store_managers.get(label).cloned()
34 }
35
36 pub fn set_max_concurrent_operations(&mut self, max: Option<usize>) {
38 self.max_concurrent_operations = max;
39 }
40
41 pub fn set_wait_timeout(&mut self, wait_timeout: Option<Duration>) {
43 self.wait_timeout = wait_timeout;
44 }
45
46 pub fn max_concurrent_operations(&self) -> Option<usize> {
48 self.max_concurrent_operations
49 }
50
51 pub fn wait_timeout(&self) -> Option<Duration> {
53 self.wait_timeout
54 }
55}
56
57impl IntoIterator for RuntimeConfig {
58 type Item = (String, Arc<dyn StoreManager>);
59 type IntoIter = std::collections::hash_map::IntoIter<String, Arc<dyn StoreManager>>;
60
61 fn into_iter(self) -> Self::IntoIter {
62 self.store_managers.into_iter()
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::RuntimeConfig;
69 use std::time::Duration;
70
71 #[test]
72 fn runtime_config_can_set_operation_limits() {
73 let mut config = RuntimeConfig::default();
74 assert_eq!(config.max_concurrent_operations(), None);
75 assert_eq!(config.wait_timeout(), None);
76
77 config.set_max_concurrent_operations(Some(7));
78 config.set_wait_timeout(Some(Duration::from_millis(250)));
79
80 assert_eq!(config.max_concurrent_operations(), Some(7));
81 assert_eq!(config.wait_timeout(), Some(Duration::from_millis(250)));
82 }
83}