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
use std::collections::HashMap;
use std::io;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use futures::stream::{FuturesUnordered, StreamExt};
mod dir;
mod file;
pub use dir::{DirEntry, DirLock, DirReadGuard, DirWriteGuard};
pub use file::{FileLoad, FileLock, FileReadGuard, FileWriteGuard};
type LFU = freqache::LFUCache<PathBuf>;
struct Inner<FE> {
files: HashMap<PathBuf, FileLock<FE>>,
size: usize,
}
impl<FE> Inner<FE> {
fn new() -> Self {
Self {
size: 0,
files: HashMap::new(),
}
}
}
pub struct Cache<FE> {
capacity: usize,
lfu: LFU,
inner: Mutex<Inner<FE>>,
}
impl<FE> Cache<FE> {
fn insert(&self, path: PathBuf, file: FileLock<FE>, file_size: usize) {
let mut state = self.inner.lock().expect("file cache state");
self.lfu.insert(path.clone());
if state.files.insert(path.clone(), file).is_none() {
state.size += file_size;
}
}
fn resize(&self, old_size: usize, new_size: usize) {
let mut state = self.inner.lock().expect("file cache state");
if new_size > old_size {
state.size += new_size - old_size;
} else if new_size < old_size {
state.size -= old_size - new_size;
}
}
}
impl<FE: FileLoad + Send + Sync + 'static> Cache<FE> {
pub fn new(capacity: usize, cleanup_interval: Duration) -> Arc<Self> {
let cache = Arc::new(Self {
lfu: LFU::new(),
inner: Mutex::new(Inner::new()),
capacity,
});
spawn_cleanup_thread(cache.clone(), cleanup_interval);
cache
}
pub async fn load(self: Arc<Self>, path: PathBuf) -> Result<DirLock<FE>, io::Error> {
{
let lock = self.inner.lock().expect("file cache state");
for file_path in lock.files.keys() {
if file_path.starts_with(&path) {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"called Cache::load on a directory that's already loaded: {:?}",
path
),
));
}
}
}
DirLock::load(self, path).await
}
}
fn spawn_cleanup_thread<FE: FileLoad + Send + Sync + 'static>(
cache: Arc<Cache<FE>>,
interval: Duration,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
loop {
loop {
if cache.inner.lock().expect("file cache state").size > cache.capacity {
break;
} else {
tokio::time::sleep(interval).await;
}
}
let mut evictions = {
let state = cache.inner.lock().expect("file cache state");
let mut over = state.size as i64 - cache.capacity as i64;
let evictions = FuturesUnordered::new();
let mut lfu = cache.lfu.iter();
while let Some(path) = lfu.next() {
if over <= 0 {
break;
}
if let Some(file) = state.files.get(&path).cloned() {
if let Some((size, eviction)) = file.evict() {
over -= size as i64;
evictions.push(eviction);
}
}
}
evictions
};
while let Some(result) = evictions.next().await {
match result {
Ok(()) => {},
Err(cause) => panic!("failed to evict file from cache: {}", cause),
}
}
tokio::time::sleep(interval).await
}
})
}