OXYGENHUB
# 5259a3af8ee6 oxygen / crates / oxy-core / src / repo / gc.rs
4.9 KiB · Rust · last changed Pack merging above 16 packs; modern CLI presentation (colour, spinners, --plain, NO_COLOR); oxy prompt with manifest-based dirty check and docs/prompt.md 1 month ago source blame history raw
1
//! Garbage collection (spec §21): pack loose objects, then delete objects
2
//! nothing reachable refers to.
3
//!
4
//! Roots: the current view, the views of every operation newer than
5
//! `core.op-retention`, reference attestations, and the policy. Operation
6
//! objects are always kept so the log stays walkable, but an operation older
7
//! than the retention window no longer keeps its states alive, so undoing
8
//! that far back may fail once its objects are gone.
9
10
use super::*;
11
use crate::store::{PackReport, PruneReport};
12
use std::collections::HashSet;
13
14
#[derive(Debug, Clone, Default, PartialEq, Eq)]
15
pub struct GcReport {
16
    pub packed: PackReport,
17
    pub pruned: PruneReport,
18
    pub reachable: usize,
19
    /// Packs folded into one because there were more than `MAX_PACKS`.
20
    pub merged_packs: usize,
21
}
22
23
impl Repo {
24
    /// Pack loose objects and prune unreachable ones older than the grace
25
    /// period (`now` ignores the grace period).
26
    pub fn gc(&self, now: bool) -> Result<GcReport> {
27
        let packed = self.store.pack_loose()?;
28
        let keep = self.reachable()?;
29
        let grace = if now { std::time::Duration::ZERO } else { std::time::Duration::from_secs(self.config.gc_grace_days * 86_400) };
30
        let pruned = self.store.prune(&keep, grace)?;
31
        let merged_packs = self.store.merge_packs()?;
32
        Ok(GcReport { packed, pruned, reachable: keep.len(), merged_packs })
33
    }
34
35
    /// Every object that must survive garbage collection.
36
    pub fn reachable(&self) -> Result<HashSet<ObjectRef>> {
37
        let mut keep: HashSet<ObjectRef> = HashSet::new();
38
        let mut states: Vec<ObjectRef> = Vec::new();
39
        let mut changes: Vec<ChangeHash> = Vec::new();
40
        let mut attests: Vec<ObjectRef> = Vec::new();
41
        let mut policies: Vec<ObjectRef> = Vec::new();
42
43
        // Operations: always kept; views within retention are roots.
44
        let cutoff = Hlc::now().wall_ms.saturating_sub(self.config.op_retention_days * 86_400_000);
45
        for (i, (r, op)) in self.op_log()?.into_iter().enumerate() {
46
            keep.insert(r);
47
            if i == 0 || op.hlc.wall_ms >= cutoff {
48
                states.extend(op.view.bookmarks.values().copied());
49
                states.extend(op.view.remotes.values().flat_map(|b| b.values().copied()));
50
                changes.extend(op.view.workspaces.values().copied());
51
                policies.extend(op.view.policy);
52
            }
53
        }
54
        // Checkpoints: the object, its payload blob, and the blob's chunks.
55
        for r in self.checkpoint_refs()? {
56
            if let Ok(bytes) = self.store.get_typed(&r, ObjectType::Checkpoint) {
57
                keep.insert(r);
58
                let cp = crate::objects::Checkpoint::decode(&bytes)?;
59
                keep.insert(cp.payload);
60
                if let Ok(b) = self.store.get_typed(&cp.payload, ObjectType::Blob) {
61
                    keep.extend(Blob::decode(&b)?.chunks);
62
                }
63
            }
64
        }
65
        // Reference attestations.
66
        for name in self.view()?.bookmarks.keys() {
67
            if let Some((r, _)) = self.latest_ref_attest(name)? {
68
                attests.push(r);
69
            }
70
        }
71
        for st in states {
72
            if keep.insert(st) {
73
                changes.extend(self.store.get_state(&st)?.heads);
74
            }
75
        }
76
        for p in policies {
77
            if keep.insert(p) {
78
                let policy = crate::policy::Policy::decode(&self.store.get_typed(&p, ObjectType::Policy)?)?;
79
                for id in &policy.identities {
80
                    keep.insert(id.hash());
81
                }
82
                for sig in self.store.signatures(&p)? {
83
                    keep.insert(sig.key);
84
                }
85
            }
86
        }
87
        // Changes: their closure, identities, blobs, chunks, seals.
88
        let mut stack = changes;
89
        while let Some(h) = stack.pop() {
90
            if !keep.insert(h) {
91
                continue;
92
            }
93
            let c = self.store.get_change(&h)?;
94
            stack.extend(c.deps.iter().copied());
95
            if !c.author.is_zero() {
96
                keep.insert(c.author);
97
            }
98
            for sig in self.store.signatures(&h)? {
99
                keep.insert(sig.key);
100
            }
101
            for e in &c.edits {
102
                if let Edit::Replace { old, new, .. } = e {
103
                    for b in [old, new].into_iter().flatten() {
104
                        if keep.insert(*b) {
105
                            if let Ok(bytes) = self.store.get_typed(b, ObjectType::Blob) {
106
                                keep.extend(Blob::decode(&bytes)?.chunks);
107
                            }
108
                        }
109
                    }
110
                }
111
            }
112
            attests.extend(self.store.seals_for(&h)?);
113
        }
114
        for a in attests {
115
            if keep.insert(a) {
116
                for sig in self.store.signatures(&a)? {
117
                    keep.insert(sig.key);
118
                }
119
            }
120
        }
121
        Ok(keep)
122
    }
123
}