Skip to main content

zccache_artifact/
lib.rs

1//! Disk artifact cache for zccache.
2//!
3//! Provides content-addressed storage for compilation artifacts, backed by
4//! the filesystem with an in-memory `ArtifactIndex` snapshotted to a bincode
5//! blob (`index.bin`) for metadata and eviction.
6
7#![allow(clippy::missing_errors_doc)] // TODO: add error docs
8
9pub mod kv;
10mod rust_plan;
11mod store;
12
13pub use kv::{
14    is_valid_namespace, Key, KvError, KvResult, KvStore, INLINE_THRESHOLD, MAX_VALUE_BYTES,
15};
16pub use rust_plan::{
17    restore_rust_plan_local, rust_plan_bundle_dir, rust_plan_cache_key, save_rust_plan_local,
18    RustArtifactBundleManifest, RustArtifactClass, RustArtifactPlanV1, RustBundledArtifact,
19    RustPlanArtifactEffectiveness, RustPlanCompatibility, RustPlanError, RustPlanInputs,
20    RustPlanMode, RustPlanOperation, RustPlanPackages, RustPlanSkippedSample, RustPlanSummary,
21    RustToolchainIdentity, RUST_ARTIFACT_CACHE_SCHEMA_VERSION, RUST_ARTIFACT_PLAN_SCHEMA_VERSION,
22};
23pub use store::{ArtifactIndex, ArtifactStore};
24
25use std::path::Path;
26use zccache_core::NormalizedPath;
27
28/// Configuration for the artifact store.
29#[derive(Debug, Clone)]
30pub struct ArtifactStoreConfig {
31    /// Root directory for artifact storage.
32    pub cache_dir: NormalizedPath,
33    /// Maximum total cache size in bytes.
34    pub max_size: u64,
35}
36
37/// The artifact store manages cached compilation outputs on disk.
38///
39/// Artifacts are stored in a content-addressed directory layout:
40/// `{cache_dir}/artifacts/{hash[0..2]}/{hash[2..4]}/{hash}`
41///
42/// A bincode blob at `{cache_dir}/index.bin` tracks metadata and access
43/// times for eviction; see `store.rs` for the in-memory + flush design.
44pub struct ArtifactStoreLegacy {
45    config: ArtifactStoreConfig,
46}
47
48impl ArtifactStoreLegacy {
49    /// Open or create an artifact store at the given configuration.
50    ///
51    /// Creates the cache directory if it does not exist.
52    pub fn open(config: ArtifactStoreConfig) -> zccache_core::Result<Self> {
53        std::fs::create_dir_all(&config.cache_dir)?;
54        Ok(Self { config })
55    }
56
57    /// Returns the path where an artifact with the given key would be stored.
58    #[must_use]
59    pub fn artifact_path(&self, key: &zccache_hash::ContentHash) -> NormalizedPath {
60        let shards = key.shard_prefix(2, 1);
61        self.config
62            .cache_dir
63            .join("artifacts")
64            .join(&shards[0])
65            .join(&shards[1])
66            .join(key.to_hex())
67    }
68
69    /// Check if an artifact exists for the given cache key.
70    #[must_use]
71    pub fn contains(&self, key: &zccache_hash::ContentHash) -> bool {
72        self.artifact_path(key).exists()
73    }
74
75    /// Returns the configured cache directory.
76    #[must_use]
77    pub fn cache_dir(&self) -> &Path {
78        &self.config.cache_dir
79    }
80}