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
use crate::cache::Cache;
use crate::error::DBError;
use crate::filesystem::{fs_delete, fs_load, fs_save};
use crate::GenericDatabase;
use std::fs::read;
use hashbrown::HashMap;
use rmp_serde::{decode, encode};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
pub struct CachedDB {
location: String,
cache: Cache,
}
impl GenericDatabase for CachedDB {
fn location(&self) -> &str {
&self.location
}
fn exists(&self, key: &str) -> bool {
if self.cache.limit.is_some() {
let mut p = PathBuf::new();
p.push(&self.location);
p.push(key);
p.exists()
} else {
self.cache.content.contains_key(key)
}
}
fn save<T>(&mut self, key: &str, value: &T) -> Result<(), DBError>
where
for<'de> T: Deserialize<'de> + Serialize + Clone,
{
let mut path = PathBuf::new();
path.push(&self.location());
path.push(key);
fs_save(&path, &value)?;
let k = key.to_owned();
self.cache.add_tracker(k.clone());
if !self.cache.full {
match self.cache.limit {
Some(limit) => {
if self.cache.count.len() == limit {
self.cache.full = true
}
}
None => (),
}
self.cache.content.insert(
k.clone(),
match encode::to_vec(value) {
Ok(v) => v,
Err(_) => return Err(DBError::save(&format!("Unable to serialize {}", k))),
},
);
}
Ok(())
}
fn load<T>(&mut self, key: &str) -> Result<T, DBError>
where
for<'de> T: Deserialize<'de> + Serialize + Clone,
{
if self.cache.should_resync() {
self.cache.timer = 0;
self.cache.full = true;
self.resync();
}
self.cache.timer += 1;
self.cache.increase_use(key);
match self.cache.content.get(key) {
None => {
let mut path = PathBuf::new();
path.push(&self.location());
path.push(key);
match fs_load::<T>(&path) {
Ok(v) => Ok(v),
Err(e) => Err(e),
}
}
Some(v) => Ok(match decode::from_slice(v) {
Ok(v) => v,
Err(e) => return Err(DBError::load(&format!("Unable to decode {} ({})", key, e))),
}),
}
}
fn delete(&mut self, key: &str) {
self.cache.content.remove(key);
self.cache.del_tracker(key);
let mut path = PathBuf::new();
path.push(&self.location());
path.push(key);
fs_delete(&path);
}
}
impl CachedDB {
pub fn resync(&mut self) {
let mut pairs = Vec::with_capacity(self.cache.count.len());
let mut i: usize = 0;
for (key, value) in &self.cache.count {
pairs.push((key, value));
i += 1;
}
pairs.sort_by(|(_, a_v), (_, b_v)| b_v.cmp(a_v));
let mut should_exist: HashMap<&str, ()> = HashMap::new();
for (c, (k, _)) in pairs.iter().enumerate() {
if c > i {
break;
} else if self.cache.limit.is_some() {
if c >= self.cache.limit.unwrap() {
break;
};
}
should_exist.insert(k.clone(), ());
let value_from_fs = if self.cache.content.contains_key(*k) {
continue;
} else {
let mut path = PathBuf::new();
path.push(&self.location);
path.push(*k);
read(path)
.map_err(|e| eprintln!("sfsdb: File and Cache mismatch ({}): {}", *k, e))
.unwrap_or_default()
};
self.cache
.content
.insert(k.clone().to_owned(), value_from_fs);
}
let mut in_cache: Vec<String> = Vec::new();
for key in self.cache.content.keys() {
in_cache.push(key.clone());
}
for key in in_cache {
if !should_exist.contains_key(key.as_str()) {
self.cache.content.remove(&key);
}
}
}
pub fn new(location: &str, cache_limit: Option<usize>, resync_every: u16) -> Self {
CachedDB {
location: String::from(location),
cache: Cache::new(cache_limit, resync_every),
}
}
}