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
pub trait DecodeEntry {
    fn put(&mut self, offset: u64, data: &[u8], kind: git_object::Kind, compressed_size: usize);
    fn get(&mut self, offset: u64, out: &mut Vec<u8>) -> Option<(git_object::Kind, usize)>;
}

pub struct DecodeEntryNoop;

impl DecodeEntry for DecodeEntryNoop {
    fn put(&mut self, _offset: u64, _data: &[u8], _kind: git_object::Kind, _compressed_size: usize) {}
    fn get(&mut self, _offset: u64, _out: &mut Vec<u8>) -> Option<(git_object::Kind, usize)> {
        None
    }
}

struct LRUEntry {
    offset: u64,
    data: Vec<u8>,
    kind: git_object::Kind,
    compressed_size: usize,
}

#[derive(Default)]
pub struct DecodeEntryLRU(uluru::LRUCache<[uluru::Entry<LRUEntry>; 64]>);

impl DecodeEntry for DecodeEntryLRU {
    fn put(&mut self, offset: u64, data: &[u8], kind: git_object::Kind, compressed_size: usize) {
        self.0.insert(LRUEntry {
            offset,
            data: Vec::from(data),
            kind,
            compressed_size,
        })
    }

    fn get(&mut self, offset: u64, out: &mut Vec<u8>) -> Option<(git_object::Kind, usize)> {
        self.0.lookup(|e: &mut LRUEntry| {
            if e.offset == offset {
                out.resize(e.data.len(), 0);
                out.copy_from_slice(&e.data);
                Some((e.kind, e.compressed_size))
            } else {
                None
            }
        })
    }
}