1use std::{sync::{Arc, Mutex}, collections::BTreeMap};
2
3use dyn_clone::{clone_trait_object, DynClone};
4
5use crate::prelude::{types::LogString, StorageError};
6
7pub trait SiemComponentStateStorage: DynClone + Send {
8 fn get_value(&self, key: &str) -> Result<String, StorageError>;
10 fn set_value(
12 &mut self,
13 key: &str,
14 value: LogString,
15 replace: bool,
16 ) -> Result<(), StorageError>;
17
18 fn get_file(&self, filepath: &str) -> Result<Vec<u8>, StorageError>;
20
21 fn get_file_size(&self, filepath: &str) -> Result<u64, StorageError>;
23
24 fn get_file_range(
26 &self,
27 filepath: &str,
28 start: u64,
29 end: u64,
30 ) -> Result<Vec<u8>, StorageError>;
31
32 fn set_file(&mut self, filepath: &str, content: Vec<u8>) -> Result<(), StorageError>;
34
35 fn set_file_range(
37 &mut self,
38 filepath: &str,
39 content: Vec<u8>,
40 start: u64,
41 end: u64,
42 ) -> Result<(), StorageError>;
43
44 fn duplicate(&self) -> Box<dyn SiemComponentStateStorage>;
45}
46clone_trait_object!(SiemComponentStateStorage);
47
48#[derive(Clone)]
49pub struct DummyStateStorage {}
50
51impl SiemComponentStateStorage for DummyStateStorage {
52 fn get_value(&self, _key: &str) -> Result<String, StorageError> {
53 Err(StorageError::NotExists)
54 }
55
56 fn set_value(
57 &mut self,
58 _key: &str,
59 _value: LogString,
60 _replace: bool,
61 ) -> Result<(), StorageError> {
62 Ok(())
63 }
64
65 fn get_file(&self, _filepath: &str) -> Result<Vec<u8>, StorageError> {
66 Err(StorageError::NotExists)
67 }
68
69 fn get_file_size(&self, _filepath: &str) -> Result<u64, StorageError> {
70 Err(StorageError::NotExists)
71 }
72
73 fn get_file_range(
74 &self,
75 _filepath: &str,
76 _start: u64,
77 _end: u64,
78 ) -> Result<Vec<u8>, StorageError> {
79 Err(StorageError::NotExists)
80 }
81
82 fn set_file(&mut self, _filepath: &str, _content: Vec<u8>) -> Result<(), StorageError> {
83 Ok(())
84 }
85
86 fn set_file_range(
87 &mut self,
88 _filepath: &str,
89 _content: Vec<u8>,
90 _start: u64,
91 _end: u64,
92 ) -> Result<(), StorageError> {
93 Err(StorageError::NotExists)
94 }
95
96 fn duplicate(&self) -> Box<dyn SiemComponentStateStorage> {
97 Box::new(self.clone())
98 }
99}
100
101
102#[derive(Clone)]
103pub struct TestingStorage {
104 files : Arc<Mutex<BTreeMap<String, Vec<u8>>>>,
105 values : Arc<Mutex<BTreeMap<String, LogString>>>
106}
107
108impl TestingStorage {
109 pub fn new() -> Self {
110 Self {
111 files : Arc::new(Mutex::new(BTreeMap::new())),
112 values : Arc::new(Mutex::new(BTreeMap::new())),
113 }
114 }
115}
116
117impl SiemComponentStateStorage for TestingStorage {
118 fn get_value(&self, key: &str) -> Result<String, StorageError> {
119 let values = self.values.lock().unwrap();
120 values.get(key).map(|v| v.to_string()).ok_or(StorageError::NotExists)
121 }
122
123 fn set_value(
124 &mut self,
125 key: &str,
126 value: LogString,
127 replace: bool,
128 ) -> Result<(), StorageError> {
129 let mut values = self.values.lock().unwrap();
130 if !replace && values.contains_key(key) {
131 return Ok(())
132 }
133 values.insert(key.to_string(), value);
134 Ok(())
135 }
136
137 fn get_file(&self, filepath: &str) -> Result<Vec<u8>, StorageError> {
138 let files = self.files.lock().unwrap();
139 let file = files.get(filepath).ok_or(StorageError::NotExists)?;
140 Ok(file.clone())
141 }
142
143 fn get_file_size(&self, filepath: &str) -> Result<u64, StorageError> {
144 let files = self.files.lock().unwrap();
145 let file = files.get(filepath).ok_or(StorageError::NotExists)?;
146 Ok(file.len() as u64)
147 }
148
149 fn get_file_range(
150 &self,
151 filepath: &str,
152 start: u64,
153 end: u64,
154 ) -> Result<Vec<u8>, StorageError> {
155 let files = self.files.lock().unwrap();
156 let start = start as usize;
157 let end = end as usize;
158 let file = files.get(filepath).ok_or(StorageError::NotExists)?;
159 if start >= file.len() || end >= file.len() || start > end {
160 return Err(StorageError::ConnectionError)
161 }
162 Ok(file[start..end].to_vec())
163 }
164
165 fn set_file(&mut self, filepath: &str, content: Vec<u8>) -> Result<(), StorageError> {
166 let mut files = self.files.lock().unwrap();
167 files.insert(filepath.to_string(), content);
168 Ok(())
169 }
170
171 fn set_file_range(
172 &mut self,
173 filepath: &str,
174 content: Vec<u8>,
175 _start: u64,
176 _end: u64,
177 ) -> Result<(), StorageError> {
178 let mut files = self.files.lock().unwrap();
179 files.insert(filepath.to_string(), content);
180 Ok(())
181 }
182
183 fn duplicate(&self) -> Box<dyn SiemComponentStateStorage> {
184 Box::new(self.clone())
185 }
186}