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
use std::{any::Any, sync::Arc};
use lru::LruCache;
use parking_lot::Mutex;
use crate::ArcBytes;
#[derive(Clone, Debug)]
#[must_use]
pub struct ChunkCache {
max_block_length: usize,
cache: Arc<Mutex<LruCache<ChunkKey, CacheEntry>>>,
}
pub trait AnySendSync: Any + Send + Sync {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
impl<T> AnySendSync for T
where
T: Any + Send + Sync,
{
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
#[derive(Hash, Eq, PartialEq, Debug)]
pub struct ChunkKey {
position: u64,
file_id: u64,
}
impl ChunkCache {
pub fn new(capacity: usize, max_chunk_length: usize) -> Self {
Self {
max_block_length: max_chunk_length,
cache: Arc::new(Mutex::new(LruCache::new(capacity))),
}
}
pub fn insert(&self, file_id: u64, position: u64, buffer: ArcBytes<'static>) {
if buffer.len() <= self.max_block_length {
let mut cache = self.cache.lock();
cache.put(ChunkKey { position, file_id }, CacheEntry::ArcBytes(buffer));
}
}
pub fn replace_with_decoded<T: AnySendSync + 'static>(
&self,
file_id: u64,
position: u64,
value: T,
) {
let mut cache = self.cache.lock();
cache.put(
ChunkKey { position, file_id },
CacheEntry::Decoded(Arc::new(value)),
);
}
#[must_use]
pub fn get(&self, file_id: u64, position: u64) -> Option<CacheEntry> {
let mut cache = self.cache.lock();
cache.get(&ChunkKey { position, file_id }).cloned()
}
}
#[derive(Clone)]
pub enum CacheEntry {
ArcBytes(ArcBytes<'static>),
Decoded(Arc<dyn AnySendSync>),
}