pub struct ObjectCache { /* private fields */ }Expand description
A content-addressed store of fact sets on disk.
Implementations§
Source§impl ObjectCache
impl ObjectCache
Sourcepub fn open(root: impl Into<PathBuf>) -> Result<Self, CacheError>
pub fn open(root: impl Into<PathBuf>) -> Result<Self, CacheError>
Open (creating if absent) a cache rooted at root.
§Errors
Returns CacheError::Io if the root directory cannot be created.
Sourcepub fn get(&self, blob_id: &str) -> Result<Option<FactSet>, CacheError>
pub fn get(&self, blob_id: &str) -> Result<Option<FactSet>, CacheError>
Load the cached fact set for blob_id, if present.
§Errors
Returns CacheError::Io on read failure or CacheError::Json if the
entry cannot be decoded.
Sourcepub fn put(&self, blob_id: &str, facts: &FactSet) -> Result<(), CacheError>
pub fn put(&self, blob_id: &str, facts: &FactSet) -> Result<(), CacheError>
Store facts under blob_id, replacing any existing entry. The write is
atomic (write-to-temp then rename) so a crash never leaves a torn entry.
§Errors
Returns CacheError::Io on write failure or CacheError::Json if
facts cannot be encoded.
Sourcepub fn sweep(
&self,
retain: &dyn Fn(&str) -> bool,
) -> Result<ObjectSweep, CacheError>
pub fn sweep( &self, retain: &dyn Fn(&str) -> bool, ) -> Result<ObjectSweep, CacheError>
Delete every entry whose key retain rejects, returning what the pass did.
This module deliberately does not know what a key means. It derives
none and interprets none — the caller derives the key (see the module
doc), so the caller is the only thing entitled to say which keys are still
reachable. retain receives the whole key, reassembled from the shard
directory and the file stem, so a policy that reads any part of it reads
the same string Self::put was given.
The pass is safe to run while other processes are using the same cache — which is not optional, because the root lives under the common git dir and every worktree shares it:
- Entries are whole files written by atomic rename, and this deletes whole
files, so no reader can observe a torn one. A reader that had already
opened a deleted entry keeps reading it (POSIX); a reader that had not
gets
Self::get’s ordinaryNone, which is a cache miss — and a miss costs a re-extraction, never a wrong answer, because the cache is derived. That is the whole reason a mistakenretainis survivable. - Nothing that is not an entry is touched, so a concurrent
put’s temp file survives to be renamed. - Shard directories are not removed, even when emptied.
putdoescreate_dir_alland then writes; removing the directory in between would fail an unrelated process’s write to reclaim four kilobytes.
§Errors
Returns CacheError::Io if the root or a shard cannot be listed — an
unreadable cache is reported, never silently swept as empty. Per-entry
delete failures are counted in ObjectSweep::failed instead, so one
stuck file does not abandon the rest.