cellular_raza_core/storage/
sled_database.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use super::concepts::StorageError;
use super::concepts::{StorageInterfaceLoad, StorageInterfaceOpen, StorageInterfaceStore};

use serde::{Deserialize, Serialize};

use std::collections::{BTreeMap, HashMap};
use std::marker::PhantomData;

/// Use the [sled] database to save results to an embedded database.
// TODO use custom field for config [](https://docs.rs/sled/latest/sled/struct.Config.html) to let the user control these parameters
#[derive(Clone)]
pub struct SledStorageInterface<Id, Element, const TEMP: bool = false> {
    db: sled::Db,
    // TODO use this buffer
    // buffer: StorageBuffer<Id, Element>,
    id_phantom: PhantomData<Id>,
    element_phantom: PhantomData<Element>,
    bincode_config: bincode::config::Configuration,
}

impl<Id: core::fmt::Debug, Element: core::fmt::Debug, const TEMP: bool> core::fmt::Debug
    for SledStorageInterface<Id, Element, TEMP>
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        #[allow(unused)]
        #[derive(Debug)]
        struct DbDebug<Id, Element, const TEMP: bool> {
            db: sled::Db,
            id_phantom: PhantomData<Id>,
            element_phantom: PhantomData<Element>,
        }
        let SledStorageInterface {
            db,
            id_phantom,
            element_phantom,
            bincode_config: _,
        } = self;
        let db_debug = DbDebug::<Id, Element, TEMP> {
            db: db.clone(),
            id_phantom: *id_phantom,
            element_phantom: *element_phantom,
        };
        core::fmt::Debug::fmt(&db_debug, f)
    }
}

impl<Id, Element, const TEMP: bool> SledStorageInterface<Id, Element, TEMP> {
    /// Transform a u64 value to an iteration key which can be given to a sled tree.
    fn iteration_to_key(iteration: u64) -> [u8; 8] {
        iteration.to_le_bytes()
    }

    /// Transform the key given by the tree to the corresponding iteartion u64 value
    fn key_to_iteration(
        key: &sled::IVec,
        bincode_config: bincode::config::Configuration,
    ) -> Result<u64, StorageError> {
        let iteration = bincode::serde::decode_from_slice(key, bincode_config)?.0;
        Ok(iteration)
    }

    /// Get the correct tree of the iteration or create if not currently present.
    fn open_or_create_tree(&self, iteration: u64) -> Result<sled::Tree, StorageError> {
        let tree_key = Self::iteration_to_key(iteration);
        let tree = self.db.open_tree(&tree_key)?;
        Ok(tree)
    }

    fn open_tree(&self, iteration: u64) -> Result<Option<sled::Tree>, StorageError> {
        let tree_key = Self::iteration_to_key(iteration);
        if !self.db.tree_names().contains(&sled::IVec::from(&tree_key)) {
            Ok(None)
        } else {
            let tree = self.db.open_tree(tree_key)?;
            Ok(Some(tree))
        }
    }
}

impl<Id, Element, const TEMP: bool> StorageInterfaceOpen
    for SledStorageInterface<Id, Element, TEMP>
{
    fn open_or_create(
        location: &std::path::Path,
        _storage_instance: u64,
    ) -> Result<Self, StorageError> {
        let config = sled::Config::default()
            .mode(sled::Mode::HighThroughput)
            .cache_capacity(1024 * 1024 * 1024 * 5) // 5gb
            .path(&location)
            .temporary(TEMP)
            .use_compression(false);

        let db = config.open()?;
        let bincode_config = bincode::config::standard();

        Ok(SledStorageInterface {
            db,
            id_phantom: PhantomData,
            element_phantom: PhantomData,
            bincode_config,
        })
    }

    fn clone_to_new_instance(&self, _storage_instance: u64) -> Self {
        Self {
            db: self.db.clone(),
            bincode_config: self.bincode_config.clone(),
            id_phantom: PhantomData,
            element_phantom: PhantomData,
        }
    }
}

impl<Id, Element, const TEMP: bool> StorageInterfaceStore<Id, Element>
    for SledStorageInterface<Id, Element, TEMP>
{
    fn store_single_element(
        &mut self,
        iteration: u64,
        identifier: &Id,
        element: &Element,
    ) -> Result<(), StorageError>
    where
        Id: Serialize,
        Element: Serialize,
    {
        let tree = self.open_or_create_tree(iteration)?;

        // Serialize the identifier and the element
        let identifier_serialized =
            bincode::serde::encode_to_vec(&identifier, self.bincode_config)?;
        let element_serialized = bincode::serde::encode_to_vec(&element, self.bincode_config)?;
        match tree.insert(identifier_serialized, element_serialized)? {
            None => Ok(()),
            Some(_) => Err(StorageError::InitError(format!(
                "Element already present at iteration {}",
                iteration
            ))),
        }?;
        Ok(())
    }

    fn store_batch_elements<'a, I>(
        &'a mut self,
        iteration: u64,
        identifiers_elements: I,
    ) -> Result<(), StorageError>
    where
        Id: 'a + Serialize,
        Element: 'a + Serialize,
        I: Clone + IntoIterator<Item = (&'a Id, &'a Element)>,
    {
        let tree = self.open_or_create_tree(iteration)?;
        let mut batch = sled::Batch::default();
        for (identifier, element) in identifiers_elements.into_iter() {
            let identifier_serialized =
                bincode::serde::encode_to_vec(&identifier, self.bincode_config)?;
            let element_serialized = bincode::serde::encode_to_vec(&element, self.bincode_config)?;
            batch.insert(identifier_serialized, element_serialized)
        }
        tree.apply_batch(batch)?;
        Ok(())
    }
}

impl<Id, Element, const TEMP: bool> StorageInterfaceLoad<Id, Element>
    for SledStorageInterface<Id, Element, TEMP>
{
    fn load_single_element(
        &self,
        iteration: u64,
        identifier: &Id,
    ) -> Result<Option<Element>, StorageError>
    where
        Id: Serialize + for<'a> Deserialize<'a>,
        Element: for<'a> Deserialize<'a>,
    {
        let tree = match self.open_tree(iteration)? {
            Some(tree) => tree,
            None => return Ok(None),
        };
        let identifier_serialized = bincode::serde::encode_to_vec(identifier, self.bincode_config)?;
        match tree.get(&identifier_serialized)? {
            Some(element_serialized) => {
                let element: Element =
                    bincode::serde::decode_from_slice(&element_serialized, self.bincode_config)?.0;
                Ok(Some(element))
            }
            None => Ok(None),
        }
    }

    fn load_element_history(&self, identifier: &Id) -> Result<HashMap<u64, Element>, StorageError>
    where
        Id: Serialize,
        Element: for<'a> Deserialize<'a>,
    {
        // Keep track if the element has not been found in a database.
        // If so we can either get the current minimal iteration or maximal depending on where it was found else.
        let mut minimal_iteration = None;
        let mut maximal_iteration = None;
        let mut success_iteration = None;

        // Save results in this hashmap
        let mut accumulator = HashMap::new();
        // Serialize the identifier
        let identifier_serialized = bincode::serde::encode_to_vec(identifier, self.bincode_config)?;
        for iteration_serialized in self.db.tree_names() {
            // If we are above the maximal or below the minimal iteration, we skip checking
            let iteration: u64 =
                bincode::serde::decode_from_slice(&iteration_serialized, self.bincode_config)?.0;
            match minimal_iteration {
                None => (),
                Some(min_iter) => {
                    if iteration < min_iter {
                        continue;
                    }
                }
            }
            match maximal_iteration {
                None => (),
                Some(max_iter) => {
                    if max_iter < iteration {
                        continue;
                    }
                }
            }
            // Get the tree for a random iteration
            let tree = self.db.open_tree(iteration_serialized)?;
            match tree.get(&identifier_serialized)? {
                // We found and element insert it
                Some(element_serialized) => {
                    let element: Element = bincode::serde::decode_from_slice(
                        &element_serialized,
                        self.bincode_config,
                    )?
                    .0;
                    accumulator.insert(iteration, element);
                    success_iteration = Some(iteration);
                }
                // We did not find an element. Thus update the helper variables atop.
                None => match (minimal_iteration, maximal_iteration, success_iteration) {
                    (None, None, Some(suc_iter)) => {
                        if iteration > suc_iter {
                            maximal_iteration = Some(iteration);
                        }
                        if iteration < suc_iter {
                            minimal_iteration = Some(iteration);
                        }
                    }
                    (Some(min_iter), None, Some(suc_iter)) => {
                        if iteration > suc_iter {
                            maximal_iteration = Some(iteration);
                        }
                        if iteration < suc_iter && iteration > min_iter {
                            minimal_iteration = Some(iteration);
                        }
                    }
                    (None, Some(max_iter), Some(suc_iter)) => {
                        if iteration > suc_iter && iteration < max_iter {
                            maximal_iteration = Some(iteration);
                        }
                        if iteration < suc_iter {
                            minimal_iteration = Some(iteration);
                        }
                    }
                    (Some(min_iter), Some(max_iter), Some(suc_iter)) => {
                        if iteration > suc_iter && iteration < max_iter {
                            maximal_iteration = Some(iteration);
                        }
                        if iteration < suc_iter && iteration > min_iter {
                            minimal_iteration = Some(iteration);
                        }
                    }
                    (_, _, None) => (),
                },
            };
        }
        Ok(accumulator)
    }

    fn load_all_elements_at_iteration(
        &self,
        iteration: u64,
    ) -> Result<HashMap<Id, Element>, StorageError>
    where
        Id: std::hash::Hash + std::cmp::Eq + for<'a> Deserialize<'a>,
        Element: for<'a> Deserialize<'a>,
    {
        let tree = match self.open_tree(iteration)? {
            Some(tree) => tree,
            None => return Ok(HashMap::new()),
        };
        tree.iter()
            .map(|entry_result| {
                let (identifier_serialized, element_serialized) = entry_result?;
                let identifier: Id =
                    bincode::serde::decode_from_slice(&identifier_serialized, self.bincode_config)?
                        .0;
                let element: Element =
                    bincode::serde::decode_from_slice(&element_serialized, self.bincode_config)?.0;
                Ok((identifier, element))
            })
            .collect::<Result<HashMap<Id, Element>, StorageError>>()
    }

    fn load_all_elements(&self) -> Result<BTreeMap<u64, HashMap<Id, Element>>, StorageError>
    where
        Id: std::hash::Hash + std::cmp::Eq + for<'a> Deserialize<'a>,
        Element: for<'a> Deserialize<'a>,
    {
        self.db
            .tree_names()
            .iter()
            .map(|tree_name_serialized| {
                let tree = self.db.open_tree(tree_name_serialized)?;
                let iteration = Self::key_to_iteration(tree_name_serialized, self.bincode_config)?;
                let identifier_to_element = tree
                    .iter()
                    .map(|entry_result| {
                        let (identifier_serialized, element_serialized) = entry_result?;
                        let identifier: Id = bincode::serde::decode_from_slice(
                            &identifier_serialized,
                            self.bincode_config,
                        )?
                        .0;
                        let element: Element = bincode::serde::decode_from_slice(
                            &element_serialized,
                            self.bincode_config,
                        )?
                        .0;
                        Ok((identifier, element))
                    })
                    .collect::<Result<HashMap<Id, Element>, StorageError>>()?;
                Ok((iteration, identifier_to_element))
            })
            .collect::<Result<BTreeMap<u64, HashMap<Id, Element>>, StorageError>>()
    }

    fn get_all_iterations(&self) -> Result<Vec<u64>, StorageError> {
        let iterations = self
            .db
            .tree_names()
            .iter()
            // TODO this should not be here! Fix it properly (I asked on sled discord)
            .filter(|key| {
                **key
                    != sled::IVec::from(&[
                        95, 95, 115, 108, 101, 100, 95, 95, 100, 101, 102, 97, 117, 108, 116,
                    ])
            })
            .map(|tree_name_serialized| Self::key_to_iteration(tree_name_serialized, self.bincode_config))
            .collect::<Result<Vec<_>, StorageError>>()?;

        Ok(iterations)
    }
}