Skip to main content

ObjectCache

Struct ObjectCache 

Source
pub struct ObjectCache { /* private fields */ }
Expand description

A content-addressed store of fact sets on disk.

Implementations§

Source§

impl ObjectCache

Source

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.

Source

pub fn root(&self) -> &Path

The directory this cache stores objects under.

Source

pub fn contains(&self, blob_id: &str) -> bool

Whether a fact set is cached for blob_id.

Source

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.

Source

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.

Source

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 ordinary None, 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 mistaken retain is 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. put does create_dir_all and 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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.