forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodhtcache.rs
162 lines (138 loc) · 3.99 KB
/
odhtcache.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
// blatantly copied from https://github.com/race604/dedup/blob/master/src/cache.rs
use std::collections::HashSet;
use log::debug;
use odht::{Config, FxHashFn, HashTableOwned};
struct ExtDedupConfig;
const CHUNK_SIZE: usize = 127;
impl Config for ExtDedupConfig {
type EncodedKey = [u8; CHUNK_SIZE + 1];
type EncodedValue = [u8; 1];
type H = FxHashFn;
type Key = [u8; CHUNK_SIZE + 1];
type Value = bool;
#[inline]
fn encode_key(k: &Self::Key) -> Self::EncodedKey {
*k
}
#[inline]
fn encode_value(v: &Self::Value) -> Self::EncodedValue {
[*v as u8; 1]
}
#[inline]
fn decode_key(k: &Self::EncodedKey) -> Self::Key {
*k
}
#[inline]
fn decode_value(v: &Self::EncodedValue) -> Self::Value {
v[0] == 1
}
}
pub struct ExtDedupCache {
memo: HashSet<String>,
disk: Option<HashTableOwned<ExtDedupConfig>>,
memo_limit: u64,
memo_size: u64,
}
impl ExtDedupCache {
pub fn new(memo_limit: u64) -> Self {
Self {
memo: HashSet::new(),
disk: None,
memo_limit: if memo_limit == 0 {
u64::MAX
} else {
memo_limit
},
memo_size: 0,
}
}
#[inline]
pub fn insert(&mut self, item: &str) -> bool {
if self.memo_size >= self.memo_limit {
self.dump_to_disk();
}
let mut res = self.memo.insert(item.to_owned());
if res {
self.memo_size += item.len() as u64;
if self.disk.is_some() {
res = self.insert_on_disk(item);
// debug!("Insert on disk: {res}");
}
}
res
}
#[inline]
pub fn contains(&self, item: &str) -> bool {
if self.memo.contains(item) {
return true;
}
return if let Some(ref disk) = self.disk {
ExtDedupCache::item_to_keys(item).all(|key| disk.contains_key(&key))
} else {
false
};
}
fn insert_on_disk(&mut self, item: &str) -> bool {
let disk = self.disk.get_or_insert_with(|| {
debug!("Create new disk cache");
HashTableOwned::<ExtDedupConfig>::with_capacity(1_000_000, 95)
});
let mut res = false;
for key in ExtDedupCache::item_to_keys(item) {
res = disk.insert(&key, &true).is_none() || res;
}
res
}
fn item_to_keys(item: &str) -> impl Iterator<Item = [u8; CHUNK_SIZE + 1]> + '_ {
let res = item
.as_bytes()
.chunks(CHUNK_SIZE)
.enumerate()
.map(|(i, chunk)| {
let mut key = [0_u8; CHUNK_SIZE + 1];
key[CHUNK_SIZE] = i as u8;
key[..chunk.len()].copy_from_slice(chunk);
key
});
res
}
fn dump_to_disk(&mut self) {
// debug!("Memory cache is full, dump to disk");
let keys = self.memo.drain().collect::<Vec<_>>();
for key in keys {
self.insert_on_disk(&key);
}
self.memo_size = 0;
}
}
#[cfg(test)]
mod tests {
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use super::*;
#[test]
fn test_basic_cache() {
let mut cache = ExtDedupCache::new(0);
assert!(cache.insert("hello"));
assert!(cache.insert("world"));
assert!(cache.contains("hello"));
assert!(cache.contains("world"));
assert!(!cache.contains("other"));
}
#[test]
fn test_limit_memory() {
let mut cache = ExtDedupCache::new(1024);
for _ in 0..100 {
cache.insert(&rand_string(32));
}
assert!(cache.memo.len() < 100);
assert!(cache.disk.is_some());
assert!(cache.disk.unwrap().len() > 0);
}
fn rand_string(len: usize) -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect()
}
}