Struct initiative_core::storage::repository::Repository
source · pub struct Repository {
data_store: Box<dyn DataStore>,
data_store_enabled: bool,
recent: VecDeque<Thing>,
redo_change: Option<Change>,
undo_history: VecDeque<Change>,
}
Fields§
§data_store: Box<dyn DataStore>
§data_store_enabled: bool
§recent: VecDeque<Thing>
§redo_change: Option<Change>
§undo_history: VecDeque<Change>
Implementations§
source§impl Repository
impl Repository
pub fn new(data_store: impl DataStore + 'static) -> Self
sourcepub async fn init(&mut self)
pub async fn init(&mut self)
The data store will not necessarily be available at construct, so we need to check if it’s healthy or discard it and fall back on a memory data store instead.
sourcepub async fn get_by_change(&self, change: &Change) -> Result<Record, Error>
pub async fn get_by_change(&self, change: &Change) -> Result<Record, Error>
Get the record associated with a given change, if available.
sourcepub async fn load_relations(
&self,
thing: &Thing,
) -> Result<ThingRelations, Error>
pub async fn load_relations( &self, thing: &Thing, ) -> Result<ThingRelations, Error>
Load child and grandchild relations associated with a Thing (eg. location).
sourcepub async fn get_by_name_start(
&self,
name: &str,
limit: Option<usize>,
) -> Result<Vec<Record>, Error>
pub async fn get_by_name_start( &self, name: &str, limit: Option<usize>, ) -> Result<Vec<Record>, Error>
Get all saved and recent Things beginning with a given (case-insensitive) string, up to an optional limit.
sourcepub async fn journal(&self) -> Result<Vec<Thing>, Error>
pub async fn journal(&self) -> Result<Vec<Thing>, Error>
Get all Things contained in the journal. This could get heavy, so should not be used lightly.
sourcepub async fn get_by_name(&self, name: &str) -> Result<Record, Error>
pub async fn get_by_name(&self, name: &str) -> Result<Record, Error>
Get the Thing from saved or recent with a given name. (There should be only one.)
sourcepub async fn get_by_uuid(&self, uuid: &Uuid) -> Result<Record, Error>
pub async fn get_by_uuid(&self, uuid: &Uuid) -> Result<Record, Error>
Get the Thing from saved or recent with a given UUID. (There should be only one.)
sourcepub async fn modify(
&mut self,
change: Change,
) -> Result<Option<Record>, (Change, Error)>
pub async fn modify( &mut self, change: Change, ) -> Result<Option<Record>, (Change, Error)>
Apply a given Change, returning the affected Thing (with modifications applied) on success, or a tuple of the Change and Error message on failure.
sourcepub async fn undo(&mut self) -> Option<Result<Option<Record>, Error>>
pub async fn undo(&mut self) -> Option<Result<Option<Record>, Error>>
Undo the most recent Change. Returns None if the undo history is empty; otherwise returns the Result of the modify() operation.
sourcepub fn undo_history(&self) -> impl Iterator<Item = &Change>
pub fn undo_history(&self) -> impl Iterator<Item = &Change>
Get an iterator over the Changes currently queued up in the undo history, from newest to oldest.
sourcepub async fn redo(&mut self) -> Option<Result<Option<Record>, Error>>
pub async fn redo(&mut self) -> Option<Result<Option<Record>, Error>>
Redo the most recently undid Change. Returns None if no such change exists; otherwise returns the result of the modify() operation. This differs from undo() in that only one Change is stored in history at a time.
sourcepub fn get_redo(&self) -> Option<&Change>
pub fn get_redo(&self) -> Option<&Change>
Get the Change currently queued up for redo(), if any.
sourcepub async fn modify_without_undo(
&mut self,
change: Change,
) -> Result<Change, (Change, Error)>
pub async fn modify_without_undo( &mut self, change: Change, ) -> Result<Change, (Change, Error)>
Apply a Change to the Repository without adding the Change to the undo history. Returns the reverse operation on success (what would be otherwise inserted into the undo history), or a tuple of the failed Change and error message on failure.
sourcepub async fn get_key_value(&self, key: &KeyValue) -> Result<KeyValue, Error>
pub async fn get_key_value(&self, key: &KeyValue) -> Result<KeyValue, Error>
Get a value from the key-value store.
sourcepub fn data_store_enabled(&self) -> bool
pub fn data_store_enabled(&self) -> bool
Is the data store currently enabled? Returns false if init() has not yet been called.
sourceasync fn set_key_value(
&mut self,
key_value: &KeyValue,
) -> Result<KeyValue, Error>
async fn set_key_value( &mut self, key_value: &KeyValue, ) -> Result<KeyValue, Error>
Set a value in the key-value store.
Publicly this is done using modify() with Change::SetKeyValue.
sourcefn push_recent(&mut self, thing: Thing)
fn push_recent(&mut self, thing: Thing)
Add a Thing to the recent list.
sourcefn take_recent<F>(&mut self, f: F) -> Option<Thing>
fn take_recent<F>(&mut self, f: F) -> Option<Thing>
Remove the latest Thing in the recent list, returning it if one is present.
sourceasync fn create_thing(
&mut self,
thing_data: ThingData,
uuid: Option<Uuid>,
) -> Result<Uuid, (ThingData, Error)>
async fn create_thing( &mut self, thing_data: ThingData, uuid: Option<Uuid>, ) -> Result<Uuid, (ThingData, Error)>
Create a Thing, pushing it onto the recent list.
Publicly this is invoked using modify() with Change::Create.
sourceasync fn create_and_save_thing(
&mut self,
thing_data: ThingData,
uuid: Option<Uuid>,
) -> Result<Thing, (ThingData, Error)>
async fn create_and_save_thing( &mut self, thing_data: ThingData, uuid: Option<Uuid>, ) -> Result<Thing, (ThingData, Error)>
Create a Thing and save it directly to the journal.
Publicly this is invoked using modify() with Change::CreateAndSave.
sourceasync fn delete_thing_by_uuid(
&mut self,
uuid: &Uuid,
) -> Result<Record, (Option<Record>, Error)>
async fn delete_thing_by_uuid( &mut self, uuid: &Uuid, ) -> Result<Record, (Option<Record>, Error)>
Delete a Thing from recent or journal by its UUID.
Publicly this is invoked using modify() with Change::Delete.
sourceasync fn save_thing_by_name(&mut self, name: &String) -> Result<Thing, Error>
async fn save_thing_by_name(&mut self, name: &String) -> Result<Thing, Error>
Transfer a Thing from recent to the journal, referenced by its name. Returns the Thing transferred, or an error on failure.
Publicly this is invoked using modify() with Change::Save { uuid: None, .. }
sourceasync fn save_thing_by_uuid(&mut self, uuid: &Uuid) -> Result<Thing, Error>
async fn save_thing_by_uuid(&mut self, uuid: &Uuid) -> Result<Thing, Error>
Transfer a Thing from recent to the journal, referenced by its UUID. Returns the Thing transferred, or an error on failure.
Publicly this is invoked using modify() with Change::Save { uuid: Some(_), .. }
sourceasync fn save_thing(&mut self, thing: &Thing) -> Result<(), Error>
async fn save_thing(&mut self, thing: &Thing) -> Result<(), Error>
Write a Thing to the data store.
sourceasync fn unsave_thing_by_uuid(
&mut self,
uuid: &Uuid,
) -> Result<String, (Option<String>, Error)>
async fn unsave_thing_by_uuid( &mut self, uuid: &Uuid, ) -> Result<String, (Option<String>, Error)>
Remove a Thing from the data store and add it to the recent list instead. Returns the name of the Thing transferred on success, or a tuple of the optional name and an error on failure. This is asymmetric with save_thing_by_* because writing to the recent list takes ownership while writing to the data store accepts a reference, so returning a Thing here would require an unnecessary clone() call when all we really want is the name of the Thing we just unsaved.
Publicly this is invoked using modify() with Change::Unsave.
sourceasync fn edit_thing_by_name(
&mut self,
name: &String,
diff: ThingData,
) -> Result<(Record, String), (Option<Record>, ThingData, Error)>
async fn edit_thing_by_name( &mut self, name: &String, diff: ThingData, ) -> Result<(Record, String), (Option<Record>, ThingData, Error)>
Apply a diff to a Thing matched by name. See edit_thing() for details.
Publicly this is invoked using modify() with Change::Edit { uuid: None, .. }.
sourceasync fn edit_thing_by_uuid(
&mut self,
uuid: &Uuid,
diff: ThingData,
) -> Result<(Record, String), (Option<Record>, ThingData, Error)>
async fn edit_thing_by_uuid( &mut self, uuid: &Uuid, diff: ThingData, ) -> Result<(Record, String), (Option<Record>, ThingData, Error)>
Apply a diff to a Thing matched by UUID. See edit_thing() for details.
Publicly this is invoked using modify() with Change::Edit { uuid: Some(_), .. }.
sourceasync fn edit_thing(
&mut self,
record: Record,
diff: ThingData,
) -> Result<(Record, String), (Record, ThingData, Error)>
async fn edit_thing( &mut self, record: Record, diff: ThingData, ) -> Result<(Record, String), (Record, ThingData, Error)>
Apply a diff to a given Record. Returns a tuple consisting of a Record containing the modified fields and the matched Thing’s actual name on success, or a tuple consisting of an optional Record of the matched Thing, the attempted diff, and an error message on failure. Note that the successful response includes only the old values of any modified fields, so re-applying the diff will revert the Thing back to its original state.
Supports the edit_thing_by_* functions.