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
use super::{BlockInfo, CacheTracker};
use std::{
fmt::Debug,
sync::{Arc, Mutex},
};
pub trait Spawner: Send {
fn spawn_blocking(&self, f: impl FnOnce() + Send + 'static);
}
pub struct AsyncCacheTracker<S, T> {
spawner: S,
inner: Arc<Mutex<T>>,
}
impl<S, T> Debug for AsyncCacheTracker<S, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AsyncCacheTracker").finish()
}
}
impl<S: Spawner, T: CacheTracker> AsyncCacheTracker<S, T> {
pub fn new(spawner: S, inner: T) -> Self {
Self {
spawner,
inner: Arc::new(Mutex::new(inner)),
}
}
}
impl<S, T> CacheTracker for AsyncCacheTracker<S, T>
where
S: Spawner,
T: CacheTracker + Send + 'static,
{
fn blocks_accessed(&mut self, blocks: Vec<BlockInfo>) {
let inner = self.inner.clone();
self.spawner.spawn_blocking(move || {
inner.lock().unwrap().blocks_accessed(blocks);
});
}
fn blocks_written(&mut self, blocks: Vec<BlockInfo>) {
let inner = self.inner.clone();
self.spawner.spawn_blocking(move || {
inner.lock().unwrap().blocks_written(blocks);
});
}
fn delete_ids(&mut self, ids: &[i64]) {
self.inner.lock().unwrap().delete_ids(ids);
}
fn retain_ids(&mut self, ids: &[i64]) {
self.inner.lock().unwrap().retain_ids(ids);
}
fn sort_ids(&self, ids: &mut [i64]) {
self.inner.lock().unwrap().sort_ids(ids);
}
}