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
use super::storage;
use std::path;
use std::sync::{Arc, Mutex};
use storage::lsm;
#[derive(Clone)]
pub struct LsmConfig {
pub flush_threshold: usize,
}
#[derive(Clone)]
pub struct DustDataConfig {
pub cache_size: usize,
pub path: String,
pub lsm_config: LsmConfig,
}
pub struct DustData {
pub config: DustDataConfig,
pub cache: Arc<Mutex<super::cache::Cache>>,
pub lsm: storage::lsm::Lsm,
}
impl DustData {
pub fn new(configuration: DustDataConfig) -> Self {
let path = path::Path::new(&configuration.path);
let cache = super::cache::Cache::new_app_cache(configuration.cache_size);
let lsm = storage::lsm::Lsm::new(lsm::LsmConfig {
flush_threshold: configuration.lsm_config.flush_threshold,
sstable_path: path.to_str().unwrap().to_string(),
});
Self {
cache,
lsm,
config: configuration,
}
}
pub fn get(&mut self, key: &str) -> Option<bson::Document> {
let cache = self.cache.lock().unwrap();
let document = cache.get(key);
if let Some(document) = document {
return Some(document.result.as_document().unwrap().clone());
}
let document = self.lsm.get(key);
if document.is_some() {
self.cache.lock().unwrap().add(
key.to_string(),
bson::Bson::Document(document.as_ref().unwrap().clone()),
);
}
document
}
pub fn insert(&mut self, key: &str, document: bson::Document) -> Result<(), &str> {
self.lsm.insert(key, document)
}
pub fn delete(&mut self, key: &str) -> Result<(), &str> {
self.lsm.delete(key)
}
pub fn update(&mut self, key: &str, document: bson::Document) -> Result<(), &str> {
self.lsm.update(key, document)
}
}