casq_core 0.3.0

A minimal content-addressed file store using BLAKE3. (library)
Documentation
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Garbage collection.

use crate::error::Result;
use crate::hash::Hash;
use crate::object::ObjectType;
use crate::store::Store;
use std::collections::HashSet;
use std::fs;

/// Statistics from a garbage collection run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GcStats {
    /// Number of objects deleted.
    pub objects_deleted: usize,
    /// Bytes freed.
    pub bytes_freed: u64,
}

/// Information about an orphaned tree root.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrphanRoot {
    /// Hash of the orphaned tree.
    pub hash: Hash,
    /// Number of entries in the tree.
    pub entry_count: usize,
    /// Approximate size in bytes (on-disk size).
    pub approx_size: u64,
}

type EntryInfo = (Hash, usize, u64);
type GcResult = (Vec<EntryInfo>, HashSet<Hash>);

impl Store {
    /// Run garbage collection.
    ///
    /// Walks from all refs to mark reachable objects, then deletes unreachable objects.
    /// If `dry_run` is true, reports what would be deleted without actually deleting.
    pub fn gc(&self, dry_run: bool) -> Result<GcStats> {
        // Mark phase: collect all reachable objects
        let reachable = self.mark_reachable()?;

        // Sweep phase: delete unreachable objects
        self.sweep(&reachable, dry_run)
    }

    /// Mark phase: traverse from all refs and collect reachable objects.
    pub(crate) fn mark_reachable(&self) -> Result<HashSet<Hash>> {
        let mut reachable = HashSet::new();

        // Get all refs
        let refs = self.refs().list()?;

        // Traverse from each ref
        for (_name, hash) in refs {
            self.mark_object(&hash, &mut reachable)?;
        }

        Ok(reachable)
    }

    /// Recursively mark an object and its children as reachable.
    fn mark_object(&self, hash: &Hash, reachable: &mut HashSet<Hash>) -> Result<()> {
        // Already visited
        if reachable.contains(hash) {
            return Ok(());
        }

        // Check if object exists
        let obj_path = self.object_path(hash);
        if !obj_path.exists() {
            // Object referenced by ref but doesn't exist - skip
            return Ok(());
        }

        // Mark as reachable
        reachable.insert(*hash);

        // If it's a tree, recursively mark children
        let header = self.read_object_header(&obj_path)?;
        if header.object_type == ObjectType::Tree {
            let tree = self.get_tree(hash)?;
            for entry in tree {
                self.mark_object(&entry.hash, reachable)?;
            }
        }

        Ok(())
    }

    /// Sweep phase: delete unreachable objects.
    fn sweep(&self, reachable: &HashSet<Hash>, dry_run: bool) -> Result<GcStats> {
        let mut stats = GcStats {
            objects_deleted: 0,
            bytes_freed: 0,
        };

        let objects_dir = self.root().join("objects").join(self.algorithm().as_str());
        if !objects_dir.exists() {
            return Ok(stats);
        }

        // Walk all shard directories
        for shard_entry in fs::read_dir(&objects_dir)? {
            let shard_entry = shard_entry?;
            let shard_path = shard_entry.path();

            if !shard_path.is_dir() {
                continue;
            }

            // Walk all objects in shard
            for obj_entry in fs::read_dir(&shard_path)? {
                let obj_entry = obj_entry?;
                let obj_path = obj_entry.path();

                if !obj_path.is_file() {
                    continue;
                }

                // Parse hash from path
                let prefix = shard_path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("");
                let suffix = obj_path.file_name().and_then(|n| n.to_str()).unwrap_or("");

                let hash_str = format!("{}{}", prefix, suffix);
                if let Ok(hash) = Hash::from_hex(&hash_str) {
                    // If not reachable, delete it
                    if !reachable.contains(&hash) {
                        let metadata = fs::metadata(&obj_path)?;
                        stats.bytes_freed += metadata.len();
                        stats.objects_deleted += 1;

                        if !dry_run {
                            fs::remove_file(&obj_path)?;
                        }
                    }
                }
            }

            // Remove empty shard directories (only if not dry run)
            if !dry_run
                && let Ok(mut entries) = fs::read_dir(&shard_path)
                && entries.next().is_none()
            {
                let _ = fs::remove_dir(&shard_path);
            }
        }

        Ok(stats)
    }

    /// Find orphaned tree roots.
    ///
    /// Returns a list of unreferenced trees that are not referenced by other
    /// unreferenced objects. These are "root" trees that were added without refs.
    pub fn find_orphan_roots(&self) -> Result<Vec<OrphanRoot>> {
        // Mark phase: collect all reachable objects
        let reachable = self.mark_reachable()?;

        // Scan unreachable objects
        let (unreachable_trees, child_refs) = self.scan_unreachable(&reachable)?;

        // Filter for orphan roots (trees not referenced by other unreachable objects)
        self.filter_orphan_roots(unreachable_trees, &child_refs)
    }

    /// Scan all objects and identify unreachable trees and their child references.
    ///
    /// Returns:
    /// - Vec of (hash, entry_count, size) for unreachable tree objects
    /// - HashSet of all hashes referenced by unreachable trees
    fn scan_unreachable(&self, reachable: &HashSet<Hash>) -> Result<GcResult> {
        let mut unreachable_trees = Vec::new();
        let mut child_refs = HashSet::new();

        let objects_dir = self.root().join("objects").join(self.algorithm().as_str());
        if !objects_dir.exists() {
            return Ok((unreachable_trees, child_refs));
        }

        // Walk all shard directories
        for shard_entry in fs::read_dir(&objects_dir)? {
            let shard_entry = shard_entry?;
            let shard_path = shard_entry.path();

            if !shard_path.is_dir() {
                continue;
            }

            // Walk all objects in shard
            for obj_entry in fs::read_dir(&shard_path)? {
                let obj_entry = obj_entry?;
                let obj_path = obj_entry.path();

                if !obj_path.is_file() {
                    continue;
                }

                // Parse hash from path
                let prefix = shard_path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("");
                let suffix = obj_path.file_name().and_then(|n| n.to_str()).unwrap_or("");

                let hash_str = format!("{}{}", prefix, suffix);
                if let Ok(hash) = Hash::from_hex(&hash_str) {
                    // Skip reachable objects
                    if reachable.contains(&hash) {
                        continue;
                    }

                    // Check if it's a tree
                    if let Ok(header) = self.read_object_header(&obj_path)
                        && header.object_type == ObjectType::Tree
                    {
                        // Get size
                        let size = fs::metadata(&obj_path)?.len();

                        // Get tree entries to count them and collect child refs
                        if let Ok(entries) = self.get_tree(&hash) {
                            let entry_count = entries.len();

                            // Collect all child hashes from this unreachable tree
                            for entry in &entries {
                                child_refs.insert(entry.hash);
                            }

                            unreachable_trees.push((hash, entry_count, size));
                        }
                    }
                }
            }
        }

        Ok((unreachable_trees, child_refs))
    }

    /// Filter unreachable trees to find orphan roots.
    ///
    /// An orphan root is an unreachable tree that is NOT referenced by any other
    /// unreachable tree (i.e., it's a top-level tree that was added without a ref).
    fn filter_orphan_roots(
        &self,
        unreachable_trees: Vec<(Hash, usize, u64)>,
        child_refs: &HashSet<Hash>,
    ) -> Result<Vec<OrphanRoot>> {
        let mut orphan_roots = Vec::new();

        for (hash, entry_count, approx_size) in unreachable_trees {
            // If this tree is not referenced by any other unreachable tree,
            // it's an orphan root
            if !child_refs.contains(&hash) {
                orphan_roots.push(OrphanRoot {
                    hash,
                    entry_count,
                    approx_size,
                });
            }
        }

        Ok(orphan_roots)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hash::Algorithm;
    use tempfile::TempDir;

    #[test]
    fn test_gc_empty_store() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        let stats = store.gc(false).unwrap();
        assert_eq!(stats.objects_deleted, 0);
        assert_eq!(stats.bytes_freed, 0);
    }

    #[test]
    fn test_gc_with_ref() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create a blob and reference it
        let hash = store.put_blob(b"test data".as_ref()).unwrap();
        store.refs().add("myref", &hash).unwrap();

        // GC should not delete referenced object
        let stats = store.gc(false).unwrap();
        assert_eq!(stats.objects_deleted, 0);
    }

    #[test]
    fn test_gc_unreferenced_blob() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create a blob without referencing it
        let hash = store.put_blob(b"orphan data".as_ref()).unwrap();

        // Verify object exists
        assert!(store.object_path(&hash).exists());

        // GC should delete unreferenced object
        let stats = store.gc(false).unwrap();
        assert_eq!(stats.objects_deleted, 1);
        assert!(stats.bytes_freed > 0);

        // Verify object was deleted
        assert!(!store.object_path(&hash).exists());
    }

    #[test]
    fn test_gc_dry_run() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create orphan blob
        let hash = store.put_blob(b"orphan".as_ref()).unwrap();

        // Dry run should report but not delete
        let stats = store.gc(true).unwrap();
        assert_eq!(stats.objects_deleted, 1);
        assert!(stats.bytes_freed > 0);

        // Object should still exist
        assert!(store.object_path(&hash).exists());

        // Actual GC should delete
        let stats2 = store.gc(false).unwrap();
        assert_eq!(stats2.objects_deleted, 1);
        assert!(!store.object_path(&hash).exists());
    }

    #[test]
    fn test_gc_tree_reachability() {
        use crate::tree::{EntryType, TreeEntry, file_modes};

        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create blobs
        let blob1 = store.put_blob(b"file1".as_ref()).unwrap();
        let blob2 = store.put_blob(b"file2".as_ref()).unwrap();
        let orphan = store.put_blob(b"orphan".as_ref()).unwrap();

        // Create tree referencing blob1 and blob2
        let entries = vec![
            TreeEntry::new(
                EntryType::Blob,
                file_modes::REGULAR,
                blob1,
                "file1".to_string(),
            )
            .unwrap(),
            TreeEntry::new(
                EntryType::Blob,
                file_modes::REGULAR,
                blob2,
                "file2".to_string(),
            )
            .unwrap(),
        ];
        let tree = store.put_tree(entries).unwrap();

        // Reference the tree
        store.refs().add("mytree", &tree).unwrap();

        // GC should only delete orphan
        let stats = store.gc(false).unwrap();
        assert_eq!(stats.objects_deleted, 1);

        // Verify tree and its blobs still exist
        assert!(store.object_path(&tree).exists());
        assert!(store.object_path(&blob1).exists());
        assert!(store.object_path(&blob2).exists());
        assert!(!store.object_path(&orphan).exists());
    }

    #[test]
    fn test_gc_after_ref_removed() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create and reference a blob
        let hash = store.put_blob(b"data".as_ref()).unwrap();
        store.refs().add("ref1", &hash).unwrap();

        // GC should not delete
        let stats = store.gc(false).unwrap();
        assert_eq!(stats.objects_deleted, 0);

        // Remove ref
        store.refs().remove("ref1").unwrap();

        // GC should now delete
        let stats = store.gc(false).unwrap();
        assert_eq!(stats.objects_deleted, 1);
        assert!(!store.object_path(&hash).exists());
    }

    #[test]
    fn test_find_orphans_empty_store() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        let orphans = store.find_orphan_roots().unwrap();
        assert_eq!(orphans.len(), 0);
    }

    #[test]
    fn test_find_orphans_unreferenced_tree() {
        use crate::tree::{EntryType, TreeEntry, file_modes};

        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create a blob
        let blob = store.put_blob(b"file content".as_ref()).unwrap();

        // Create a tree without referencing it
        let entries = vec![
            TreeEntry::new(
                EntryType::Blob,
                file_modes::REGULAR,
                blob,
                "file.txt".to_string(),
            )
            .unwrap(),
        ];
        let tree_hash = store.put_tree(entries).unwrap();

        // Should find the orphaned tree
        let orphans = store.find_orphan_roots().unwrap();
        assert_eq!(orphans.len(), 1);
        assert_eq!(orphans[0].hash, tree_hash);
        assert_eq!(orphans[0].entry_count, 1);
        assert!(orphans[0].approx_size > 0);
    }

    #[test]
    fn test_find_orphans_referenced_tree() {
        use crate::tree::{EntryType, TreeEntry, file_modes};

        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create a blob
        let blob = store.put_blob(b"file content".as_ref()).unwrap();

        // Create a tree and reference it
        let entries = vec![
            TreeEntry::new(
                EntryType::Blob,
                file_modes::REGULAR,
                blob,
                "file.txt".to_string(),
            )
            .unwrap(),
        ];
        let tree_hash = store.put_tree(entries).unwrap();
        store.refs().add("mytree", &tree_hash).unwrap();

        // Should NOT find any orphans (tree is referenced)
        let orphans = store.find_orphan_roots().unwrap();
        assert_eq!(orphans.len(), 0);
    }

    #[test]
    fn test_find_orphans_nested_unreferenced_trees() {
        use crate::tree::{EntryType, TreeEntry, file_modes};

        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create a blob
        let blob = store.put_blob(b"nested file".as_ref()).unwrap();

        // Create a subtree
        let subtree_entries = vec![
            TreeEntry::new(
                EntryType::Blob,
                file_modes::REGULAR,
                blob,
                "nested.txt".to_string(),
            )
            .unwrap(),
        ];
        let subtree_hash = store.put_tree(subtree_entries).unwrap();

        // Create a parent tree referencing the subtree
        let parent_entries = vec![
            TreeEntry::new(
                EntryType::Tree,
                file_modes::DIRECTORY,
                subtree_hash,
                "subdir".to_string(),
            )
            .unwrap(),
        ];
        let parent_hash = store.put_tree(parent_entries).unwrap();

        // Neither tree is referenced, but subtree is referenced by parent
        // So only parent should be an orphan root
        let orphans = store.find_orphan_roots().unwrap();
        assert_eq!(orphans.len(), 1);
        assert_eq!(orphans[0].hash, parent_hash);
    }

    #[test]
    fn test_find_orphans_blobs_not_reported() {
        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create orphaned blobs
        store.put_blob(b"orphan1".as_ref()).unwrap();
        store.put_blob(b"orphan2".as_ref()).unwrap();

        // Should find no orphans (only trees are reported)
        let orphans = store.find_orphan_roots().unwrap();
        assert_eq!(orphans.len(), 0);
    }

    #[test]
    fn test_find_orphans_multiple_orphan_roots() {
        use crate::tree::{EntryType, TreeEntry, file_modes};

        let temp_dir = TempDir::new().unwrap();
        let store = Store::init(temp_dir.path(), Algorithm::Blake3).unwrap();

        // Create first orphan tree
        let blob1 = store.put_blob(b"file1".as_ref()).unwrap();
        let tree1 = store
            .put_tree(vec![
                TreeEntry::new(
                    EntryType::Blob,
                    file_modes::REGULAR,
                    blob1,
                    "file1.txt".to_string(),
                )
                .unwrap(),
            ])
            .unwrap();

        // Create second orphan tree
        let blob2 = store.put_blob(b"file2".as_ref()).unwrap();
        let tree2 = store
            .put_tree(vec![
                TreeEntry::new(
                    EntryType::Blob,
                    file_modes::REGULAR,
                    blob2,
                    "file2.txt".to_string(),
                )
                .unwrap(),
            ])
            .unwrap();

        // Should find both orphan roots
        let orphans = store.find_orphan_roots().unwrap();
        assert_eq!(orphans.len(), 2);

        let orphan_hashes: Vec<_> = orphans.iter().map(|o| o.hash).collect();
        assert!(orphan_hashes.contains(&tree1));
        assert!(orphan_hashes.contains(&tree2));
    }
}