rapiddb/traits/
i_database.rs

1/// IDatabase trait abstracts the underlying Database implementation
2///
3/// ## Examples
4/// ```no_run
5/// use crate::rapiddb::traits::IDatabase;
6///
7/// let db = std::sync::Arc::new(
8///   std::sync::RwLock::new(
9///     rapiddb::db::MMAVDatabase::new()
10///   )
11/// );
12///
13/// let value = b"{\"key\": \"value\"}";
14/// db.write().unwrap().post("test-0", value);
15/// assert_eq!(db.write().unwrap().get_latest("test-0"), value);
16/// ```
17pub trait IDatabase: Send + Sync {
18  /// Checks if the sensor with `id` exists in the Database
19  ///
20  /// ## Examples
21  /// ```no_run
22  /// use crate::rapiddb::traits::IDatabase;
23  ///
24  /// let db = std::sync::Arc::new(
25  ///   std::sync::RwLock::new(
26  ///     rapiddb::db::MMAVDatabase::new()
27  ///   )
28  /// );
29  ///
30  /// let id = "test-0";
31  ///
32  /// if db.read().unwrap().contains(id) {
33  ///     println!("{id} exists in the Database");
34  /// }
35  /// ```
36  fn contains(&self, id: &str) -> bool;
37
38  /// Get the record with `rec_id` from the sensor with `id` in the
39  /// Database
40  ///
41  /// May load data from disk, if it is not in-memory,
42  /// as such it is mutable, even though a get normaly
43  /// is immutable.
44  ///
45  /// ## Examples
46  /// ```no_run
47  /// use crate::rapiddb::traits::IDatabase;
48  ///
49  /// let db = std::sync::Arc::new(
50  ///   std::sync::RwLock::new(
51  ///     rapiddb::db::MMAVDatabase::new()
52  ///   )
53  /// );
54  ///
55  /// db.write().unwrap().get("test-0", 0);
56  /// ```
57  fn get(&mut self, id: &str, rec_id: usize) -> Vec<u8>;
58
59  /// Post a record with `value` to the sensor with `id` in the Database
60  ///
61  /// ## Examples
62  /// ```no_run
63  /// use crate::rapiddb::traits::IDatabase;
64  ///
65  /// let db = std::sync::Arc::new(
66  ///   std::sync::RwLock::new(
67  ///     rapiddb::db::MMAVDatabase::new()
68  ///   )
69  /// );
70  ///
71  /// db.write().unwrap().post(
72  ///   "test-0",
73  ///   b"{\"key\": \"value\"}"
74  /// );
75  /// ```
76  fn post(&mut self, id: &str, value: &[u8]);
77
78  /// Get metadata from the sensor with `id` in the Database
79  ///
80  /// ## Examples
81  /// ```no_run
82  /// use crate::rapiddb::traits::IDatabase;
83  ///
84  /// let db = std::sync::Arc::new(
85  ///   std::sync::RwLock::new(
86  ///     rapiddb::db::MMAVDatabase::new()
87  ///   )
88  /// );
89  ///
90  /// db.write().unwrap().get_meta("test-0");
91  /// ```
92  fn get_meta(&mut self, id: &str) -> Vec<u8>;
93
94  /// Post metadata with `value` to the sensor with `id` in the Database
95  ///
96  /// ## Examples
97  /// ```no_run
98  /// use crate::rapiddb::traits::IDatabase;
99  ///
100  /// let db = std::sync::Arc::new(
101  ///   std::sync::RwLock::new(
102  ///     rapiddb::db::MMAVDatabase::new()
103  ///   )
104  /// );
105  ///
106  /// db.write().unwrap().post_meta(
107  ///   "test-0",
108  ///   b"{\"key\": \"value\"}".to_vec()
109  /// );
110  /// ```
111  fn post_meta(&mut self, id: &str, value: Vec<u8>);
112
113  /// Get aggregates from the sensor with `id` in the Database
114  ///
115  /// ## Examples
116  /// ```no_run
117  /// use crate::rapiddb::traits::IDatabase;
118  ///
119  /// let db = std::sync::Arc::new(
120  ///   std::sync::RwLock::new(
121  ///     rapiddb::db::MMAVDatabase::new()
122  ///   )
123  /// );
124  ///
125  /// db.read().unwrap().get_aggregates("test-0");
126  /// ```
127  fn get_aggregates(&self, id: &str) -> Vec<u8>;
128
129  /// Get the latest record from the sensor with `id` in the Database
130  ///
131  /// ## Examples
132  /// ```no_run
133  /// use crate::rapiddb::traits::IDatabase;
134  ///
135  /// let db = std::sync::Arc::new(
136  ///   std::sync::RwLock::new(
137  ///     rapiddb::db::MMAVDatabase::new()
138  ///   )
139  /// );
140  ///
141  /// db.write().unwrap().get_latest("test-0");
142  /// ```
143  // fn get_latest(&mut self, id: &str) -> Vec<u8>;
144  fn get_latest(&mut self, id: &str) -> Vec<u8>;
145
146  /// Get the latest `limit` number of records from the sensor with `id`
147  /// in the Database
148  ///
149  /// May load data from disk, if it is not in-memory,
150  /// as such it is mutable, even though a get latest limit normaly is
151  /// immutable.
152  ///
153  /// ## Examples
154  /// ```no_run
155  /// use crate::rapiddb::traits::IDatabase;
156  ///
157  /// let db = std::sync::Arc::new(
158  ///   std::sync::RwLock::new(
159  ///     rapiddb::db::MMAVDatabase::new()
160  ///   )
161  /// );
162  ///
163  /// db.write().unwrap().get_latest_with_limit("test-0", 10);
164  /// ```
165  fn get_latest_with_limit(&mut self, id: &str, limit: usize) -> Vec<Vec<u8>>;
166
167  /// Get a range from `start` to `end` of records from the sensor with
168  /// `id` in the Database
169  ///
170  /// May load data from disk, if it is not in-memory,
171  /// as such it is mutable, even though a range scan normaly is
172  /// immutable.
173  ///
174  /// ## Examples
175  /// ```no_run
176  /// use crate::rapiddb::traits::IDatabase;
177  ///
178  /// let db = std::sync::Arc::new(
179  ///   std::sync::RwLock::new(
180  ///     rapiddb::db::MMAVDatabase::new()
181  ///   )
182  /// );
183  ///
184  /// db.write().unwrap().get_range("test-0", 0, 10);
185  /// ```
186  fn get_range(&mut self, id: &str, start: usize, end: usize) -> Vec<Vec<u8>>;
187
188  /// Get metadata from all sensors in the Database
189  ///
190  /// ## Examples
191  /// ```no_run
192  /// use crate::rapiddb::traits::IDatabase;
193  ///
194  /// let db = std::sync::Arc::new(
195  ///   std::sync::RwLock::new(
196  ///     rapiddb::db::MMAVDatabase::new()
197  ///   )
198  /// );
199  ///
200  /// db.write().unwrap().get_all_meta();
201  /// ```
202  fn get_all_meta(&mut self) -> std::collections::HashMap<&str, Vec<u8>>;
203
204  /// Get aggregates from all sensors in the Database
205  ///
206  /// ## Examples
207  /// ```no_run
208  /// use crate::rapiddb::traits::IDatabase;
209  ///
210  /// let db = std::sync::Arc::new(
211  ///   std::sync::RwLock::new(
212  ///     rapiddb::db::MMAVDatabase::new()
213  ///   )
214  /// );
215  ///
216  /// db.read().unwrap().get_all_aggregates();
217  /// ```
218  fn get_all_aggregates(&self) -> std::collections::HashMap<&str, Vec<u8>>;
219
220  /// Get the latest record from all sensors in the Database
221  ///
222  /// ## Examples
223  /// ```no_run
224  /// use crate::rapiddb::traits::IDatabase;
225  ///
226  /// let db = std::sync::Arc::new(
227  ///   std::sync::RwLock::new(
228  ///     rapiddb::db::MMAVDatabase::new()
229  ///   )
230  /// );
231  ///
232  /// db.write().unwrap().get_all_latest();
233  /// ```
234  fn get_all_latest(&mut self) -> std::collections::HashMap<&str, Vec<u8>>;
235
236  /// Get the latest `limit` number of records from all sensors in the
237  /// Database
238  ///
239  /// May load data from disk, if it is not in-memory,
240  /// as such it is mutable, even though a get latest limit normaly is
241  /// immutable.
242  ///
243  /// ## Examples
244  /// ```no_run
245  /// use crate::rapiddb::traits::IDatabase;
246  ///
247  /// let db = std::sync::Arc::new(
248  ///   std::sync::RwLock::new(
249  ///     rapiddb::db::MMAVDatabase::new()
250  ///   )
251  /// );
252  ///
253  /// db.write().unwrap().get_all_latest_with_limit(10);
254  /// ```
255  fn get_all_latest_with_limit(
256    &mut self,
257    limit: usize,
258  ) -> std::collections::HashMap<&str, Vec<Vec<u8>>>;
259}