pub struct Registry { /* private fields */ }Expand description
The in-memory model store, backed by <directory>/models.json. Every mutating
method acquires a short-held advisory file lock, reloads the on-disk state
under it, applies the change, and persists before returning — so two
instances on the same directory serialize their writes instead of one
silently clobbering the other. The lock is held only for the span of a
single mutation, never across the instance’s lifetime.
Implementations§
Source§impl Registry
impl Registry
Sourcepub fn open(directory: &Path) -> Result<Self, RegistryError>
pub fn open(directory: &Path) -> Result<Self, RegistryError>
Open the registry rooted at directory, loading models.json if present.
A missing store opens empty; a corrupt store is quarantined and reported
as RegistryError::CorruptStore; a store from a newer schema is left in
place and reported as RegistryError::FutureSchema. If the file holds
two records with the same id, the last one in file order wins.
Sourcepub fn refresh(&mut self) -> Result<bool, RegistryError>
pub fn refresh(&mut self) -> Result<bool, RegistryError>
Re-read the store, picking up what another process has committed since this one loaded it. Returns whether anything changed.
The in-memory view is otherwise only reloaded under a mutation, which is enough while one process owns the shelf. A pull worker registering what it fetched is another process, and a screen that never re-read would show a download as landed with the model nowhere on its shelf.
Sourcepub fn get(&self, id: &str) -> Option<&ModelRecord>
pub fn get(&self, id: &str) -> Option<&ModelRecord>
The record with id, if present.
Sourcepub fn generation(&self) -> u64
pub fn generation(&self) -> u64
A counter that advances every time Self::save persists a change.
Callers can cache work derived from the registry’s contents keyed on this
value and rebuild only when it moves.
Sourcepub fn list(&self) -> Vec<&ModelRecord>
pub fn list(&self) -> Vec<&ModelRecord>
Every record, sorted by display name (case-insensitive) then id.
Sourcepub fn register(&mut self, record: ModelRecord) -> Result<bool, RegistryError>
pub fn register(&mut self, record: ModelRecord) -> Result<bool, RegistryError>
Insert or replace record. Returns whether anything changed; an identical
record is a no-op that touches no disk.
Sourcepub fn register_all(
&mut self,
records: Vec<ModelRecord>,
) -> Result<usize, RegistryError>
pub fn register_all( &mut self, records: Vec<ModelRecord>, ) -> Result<usize, RegistryError>
Insert or replace many records, writing once. Returns how many input records differed from the store (counted per input record, so two inputs with the same id both count even though only the last survives).
Sourcepub fn unregister(
&mut self,
id: &str,
) -> Result<Option<ModelRecord>, RegistryError>
pub fn unregister( &mut self, id: &str, ) -> Result<Option<ModelRecord>, RegistryError>
Remove the record with id, returning it if it was present.
Sourcepub fn set_state_if_present(
&mut self,
id: &str,
state: ModelState,
) -> Result<bool, RegistryError>
pub fn set_state_if_present( &mut self, id: &str, state: ModelState, ) -> Result<bool, RegistryError>
Set the lifecycle state of id if it is present. Returns whether the
record was present; only a real state change touches disk.
Sourcepub fn update(
&mut self,
ids: &[String],
transform: impl Fn(&ModelRecord) -> Option<ModelRecord>,
) -> Result<Vec<ModelRecord>, RegistryError>
pub fn update( &mut self, ids: &[String], transform: impl Fn(&ModelRecord) -> Option<ModelRecord>, ) -> Result<Vec<ModelRecord>, RegistryError>
Apply transform to each present id. When it returns a record that differs
from the current one, the record is stored under its own id — if the
transform changed the id, the old key is removed so the record migrates
cleanly rather than leaving the map keyed by a stale id. Returns the changed
records; writes once if anything changed.