Skip to main content

aft_store/
ids.rs

1//! Identities that appear in stored data.
2
3use std::fmt;
4
5/// A BLAKE3 digest truncated to its first 128 bits.
6///
7/// The store uses it for content keys and for every checksum. 128 bits keep
8/// accidental collisions out of reach for any repository, at half the size of
9/// the full digest in keys and checksum tables.
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Digest128([u8; 16]);
12
13impl Digest128 {
14    /// Length of a digest in bytes.
15    pub const LEN: usize = 16;
16
17    /// The digest of `bytes`.
18    #[must_use]
19    pub fn of(bytes: &[u8]) -> Self {
20        Self::from_hash(&blake3::hash(bytes))
21    }
22
23    /// The first 128 bits of a full BLAKE3 hash.
24    #[must_use]
25    pub fn from_hash(hash: &blake3::Hash) -> Self {
26        let mut bytes = [0; Self::LEN];
27        bytes.copy_from_slice(&hash.as_bytes()[..Self::LEN]);
28        Self(bytes)
29    }
30
31    /// A digest read back from storage.
32    #[must_use]
33    pub const fn from_bytes(bytes: [u8; Self::LEN]) -> Self {
34        Self(bytes)
35    }
36
37    #[must_use]
38    pub const fn as_bytes(&self) -> &[u8; Self::LEN] {
39        &self.0
40    }
41}
42
43impl fmt::Display for Digest128 {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        for byte in self.0 {
46            write!(f, "{byte:02x}")?;
47        }
48        Ok(())
49    }
50}
51
52impl fmt::Debug for Digest128 {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "Digest128({self})")
55    }
56}
57
58/// A repository family: every checkout, clone and fork of one repository on
59/// this machine. Members of a family share stored data.
60///
61/// The value is derived from the repository's root commits by the caller;
62/// this type only carries it.
63#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub struct FamilyId(Digest128);
65
66impl FamilyId {
67    #[must_use]
68    pub const fn from_digest(digest: Digest128) -> Self {
69        Self(digest)
70    }
71
72    #[must_use]
73    pub const fn digest(&self) -> Digest128 {
74        self.0
75    }
76}
77
78impl fmt::Display for FamilyId {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        self.0.fmt(f)
81    }
82}
83
84impl fmt::Debug for FamilyId {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        write!(f, "FamilyId({})", self.0)
87    }
88}
89
90/// The key of a file content: the digest of its bytes as read from the
91/// working tree. Units derived from a content are keyed by it.
92#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
93pub struct ContentKey(Digest128);
94
95impl ContentKey {
96    /// The key of `bytes`.
97    #[must_use]
98    pub fn of(bytes: &[u8]) -> Self {
99        Self(Digest128::of(bytes))
100    }
101
102    /// A key read back from storage.
103    #[must_use]
104    pub const fn from_digest(digest: Digest128) -> Self {
105        Self(digest)
106    }
107
108    /// A key for a file whose bytes are not read, such as one over the size
109    /// limit, derived from its path and stat evidence. It is hashed in its
110    /// own key-derivation context, so it never equals the key of real bytes,
111    /// and it changes whenever the evidence does.
112    #[must_use]
113    pub fn stand_in(path: &[u8], evidence: &[u8]) -> Self {
114        let mut hasher = blake3::Hasher::new_derive_key("aft-store 2026-11 unread file stand-in");
115        hasher.update(&u64::try_from(path.len()).unwrap_or(u64::MAX).to_le_bytes());
116        hasher.update(path);
117        hasher.update(evidence);
118        Self(Digest128::from_hash(&hasher.finalize()))
119    }
120
121    #[must_use]
122    pub const fn digest(&self) -> Digest128 {
123        self.0
124    }
125}
126
127impl fmt::Display for ContentKey {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        self.0.fmt(f)
130    }
131}
132
133impl fmt::Debug for ContentKey {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        write!(f, "ContentKey({})", self.0)
136    }
137}
138
139/// A segment's id within its family. Ids only increase and are never reused,
140/// so a name that once held a segment never holds different bytes.
141#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
142pub struct SegmentId(u64);
143
144impl SegmentId {
145    #[must_use]
146    pub const fn new(value: u64) -> Self {
147        Self(value)
148    }
149
150    #[must_use]
151    pub const fn get(self) -> u64 {
152        self.0
153    }
154}
155
156impl fmt::Display for SegmentId {
157    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158        write!(f, "{}", self.0)
159    }
160}
161
162/// One checkout of a family: a main checkout, a worktree or a clone. The value
163/// is derived from the checkout's root path and Git directory by the caller.
164#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
165pub struct CheckoutId(Digest128);
166
167impl CheckoutId {
168    #[must_use]
169    pub const fn from_digest(digest: Digest128) -> Self {
170        Self(digest)
171    }
172
173    #[must_use]
174    pub const fn digest(&self) -> Digest128 {
175        self.0
176    }
177}
178
179impl fmt::Display for CheckoutId {
180    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181        self.0.fmt(f)
182    }
183}
184
185impl fmt::Debug for CheckoutId {
186    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187        write!(f, "CheckoutId({})", self.0)
188    }
189}
190
191/// The number of one published manifest. Each publication's generation is one
192/// above the newest before it; a generation names one manifest forever.
193#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
194pub struct Generation(u64);
195
196impl Generation {
197    /// The generation of a family that has never published: no segments and
198    /// no checkouts. No manifest file carries it.
199    pub const EMPTY: Self = Self(0);
200
201    #[must_use]
202    pub const fn new(value: u64) -> Self {
203        Self(value)
204    }
205
206    #[must_use]
207    pub const fn get(self) -> u64 {
208        self.0
209    }
210
211    /// The generation published after this one.
212    #[must_use]
213    pub const fn next(self) -> Self {
214        Self(self.0 + 1)
215    }
216}
217
218impl fmt::Display for Generation {
219    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220        write!(f, "{}", self.0)
221    }
222}
223
224/// One run of a writing process. Recorded in every segment it writes, so
225/// files left behind by a crashed writer can be told apart from a live one's.
226#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
227pub struct WriterIncarnation(u64);
228
229impl WriterIncarnation {
230    #[must_use]
231    pub const fn new(value: u64) -> Self {
232        Self(value)
233    }
234
235    #[must_use]
236    pub const fn get(self) -> u64 {
237        self.0
238    }
239}
240
241#[cfg(test)]
242mod tests {
243    use super::*;
244
245    #[test]
246    fn digest_is_the_first_half_of_blake3() {
247        let full = blake3::hash(b"fn main() {}\n");
248        let digest = Digest128::of(b"fn main() {}\n");
249        assert_eq!(digest.as_bytes()[..], full.as_bytes()[..16]);
250        assert_eq!(digest.to_string(), full.to_hex()[..32]);
251    }
252
253    #[test]
254    fn content_keys_follow_bytes() {
255        assert_eq!(ContentKey::of(b"a"), ContentKey::of(b"a"));
256        assert_ne!(ContentKey::of(b"a"), ContentKey::of(b"a\n"));
257    }
258}