Skip to main content

git_internal/internal/pack/
waitlist.rs

1//! Temporary storage for delta objects while their base object is still decoding, keyed by both pack
2//! offset and object hash.
3
4use dashmap::DashMap;
5
6use crate::{hash::ObjectHash, internal::pack::cache_object::CacheObject};
7
8/// Waitlist for Delta objects while the Base object is not ready.
9/// Easier and faster than Channels.
10#[derive(Default, Debug)]
11pub struct Waitlist {
12    //TODO Memory Control!
13    pub map_offset: DashMap<usize, Vec<CacheObject>>,
14    pub map_ref: DashMap<ObjectHash, Vec<CacheObject>>,
15}
16
17impl Waitlist {
18    /// Create a new, empty Waitlist.
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    /// Insert an object into the waitlist by its pack offset or object hash.
24    pub fn insert_offset(&self, offset: usize, obj: CacheObject) {
25        self.map_offset.entry(offset).or_default().push(obj);
26    }
27
28    /// Insert an object into the waitlist by its object hash.
29    pub fn insert_ref(&self, hash: ObjectHash, obj: CacheObject) {
30        self.map_ref.entry(hash).or_default().push(obj);
31    }
32
33    /// Take objects out (get & remove)
34    /// <br> Return Vec::new() if None
35    pub fn take(&self, offset: usize, hash: ObjectHash) -> Vec<CacheObject> {
36        let mut res = Vec::new();
37        if let Some((_, vec)) = self.map_offset.remove(&offset) {
38            res.extend(vec);
39        }
40        if let Some((_, vec)) = self.map_ref.remove(&hash) {
41            res.extend(vec);
42        }
43        res
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::internal::{object::types::ObjectType, pack::cache_object::CacheObjectInfo};
51
52    /// Helper to build a base CacheObject with given size and hash.
53    fn make_test_obj(offset: usize) -> CacheObject {
54        CacheObject {
55            info: CacheObjectInfo::BaseObject(ObjectType::Blob, ObjectHash::default()),
56            offset,
57            crc32: 0,
58            data_decompressed: vec![],
59            mem_recorder: None,
60            is_delta_in_pack: false,
61        }
62    }
63
64    /// Test inserting and taking objects by offset.
65    #[test]
66    fn test_waitlist_offset() {
67        let waitlist = Waitlist::new();
68        let obj1 = make_test_obj(10);
69        let obj2 = make_test_obj(20);
70
71        waitlist.insert_offset(100, obj1);
72        waitlist.insert_offset(100, obj2);
73
74        let res = waitlist.take(100, ObjectHash::default());
75        assert_eq!(res.len(), 2);
76        assert_eq!(res[0].offset, 10);
77        assert_eq!(res[1].offset, 20);
78
79        let res_empty = waitlist.take(100, ObjectHash::default());
80        assert!(res_empty.is_empty());
81    }
82
83    /// Test inserting and taking objects by object hash.
84    #[test]
85    fn test_waitlist_ref() {
86        let waitlist = Waitlist::new();
87        let hash = ObjectHash::new(b"test_hash");
88        let obj = make_test_obj(30);
89
90        waitlist.insert_ref(hash, obj);
91
92        let res = waitlist.take(0, hash);
93        assert_eq!(res.len(), 1);
94        assert_eq!(res[0].offset, 30);
95
96        let res_empty = waitlist.take(0, hash);
97        assert!(res_empty.is_empty());
98    }
99
100    /// Test inserting and taking objects by both offset and object hash.
101    #[test]
102    fn test_waitlist_mixed() {
103        let waitlist = Waitlist::new();
104        let hash = ObjectHash::new(b"test_hash");
105        let offset = 200;
106
107        let obj1 = make_test_obj(1);
108        let obj2 = make_test_obj(2);
109
110        waitlist.insert_offset(offset, obj1);
111        waitlist.insert_ref(hash, obj2);
112
113        // Take using both keys, should retrieve both lists
114        let res = waitlist.take(offset, hash);
115        assert_eq!(res.len(), 2);
116
117        // Verify we got both objects
118        assert!(res.iter().any(|o| o.offset == 1));
119        assert!(res.iter().any(|o| o.offset == 2));
120
121        // Verify maps are empty
122        assert!(waitlist.map_offset.is_empty());
123        assert!(waitlist.map_ref.is_empty());
124    }
125}