#[non_exhaustive]pub struct Interface<S: StorageAdaptor = MainStorage>(/* private fields */);Expand description
The primary interface for the storage system.
Implementations§
Source§impl<S: StorageAdaptor> Interface<S>
impl<S: StorageAdaptor> Interface<S>
Sourcepub fn add_child_to<D: Data>(
parent_id: Id,
child: &mut D,
) -> Result<bool, StorageError>
pub fn add_child_to<D: Data>( parent_id: Id, child: &mut D, ) -> Result<bool, StorageError>
Adds a child entity to a parent’s collection.
Updates Merkle hashes and generates sync actions automatically.
§Errors
SerializationErrorif child can’t be encodedIndexNotFoundif parent doesn’t exist
Sourcepub fn apply_action(action: Action) -> Result<(), StorageError>
pub fn apply_action(action: Action) -> Result<(), StorageError>
Applies a synchronization action from a remote node.
Handles Add/Update/Delete actions, creating missing ancestors if needed. Generates Compare action for hash verification after applying changes.
§Errors
DeserializationErrorif action data is invalidActionNotAllowedif Compare action is passed directly
Sourcepub fn children_of<D: Data>(parent_id: Id) -> Result<Vec<D>, StorageError>
pub fn children_of<D: Data>(parent_id: Id) -> Result<Vec<D>, StorageError>
Retrieves all children in a collection.
Returns deserialized child entities. Order is not guaranteed.
§Errors
IndexNotFoundif parent doesn’t existDeserializationErrorif child data is corrupt
Sourcepub fn child_info_for(parent_id: Id) -> Result<Vec<ChildInfo>, StorageError>
pub fn child_info_for(parent_id: Id) -> Result<Vec<ChildInfo>, StorageError>
Retrieves child metadata without deserializing full data.
Returns IDs, hashes, and timestamps only. More efficient than children_of().
§Errors
Returns error if index lookup fails.
Sourcepub fn compare_trees(
foreign_entity_data: Option<Vec<u8>>,
foreign_index_data: ComparisonData,
) -> Result<(Vec<Action>, Vec<Action>), StorageError>
pub fn compare_trees( foreign_entity_data: Option<Vec<u8>>, foreign_index_data: ComparisonData, ) -> Result<(Vec<Action>, Vec<Action>), StorageError>
Compares local and remote entity trees, generating sync actions.
Compares Merkle hashes recursively, producing action lists for both sides.
Returns (local_actions, remote_actions) to bring trees into sync.
§Errors
Returns error if index lookup or hash comparison fails.
Sourcepub fn compare_affective(
data: Option<Vec<u8>>,
comparison_data: ComparisonData,
) -> Result<(), StorageError>
pub fn compare_affective( data: Option<Vec<u8>>, comparison_data: ComparisonData, ) -> Result<(), StorageError>
Compares entities and automatically applies sync actions locally.
Convenience wrapper around compare_trees() that applies
local actions immediately and pushes remote actions to sync queue.
§Errors
Returns error if comparison or action application fails.
Sourcepub fn find_by_id<D: Data>(id: Id) -> Result<Option<D>, StorageError>
pub fn find_by_id<D: Data>(id: Id) -> Result<Option<D>, StorageError>
Finds and deserializes an entity by its unique ID.
Filters out tombstoned (deleted) entities automatically.
§Errors
DeserializationErrorif stored data is corruptIndexNotFoundif entity exists but has no index
Sourcepub fn find_by_id_raw(id: Id) -> Option<Vec<u8>>
pub fn find_by_id_raw(id: Id) -> Option<Vec<u8>>
Finds an entity by ID, returning raw bytes without deserialization.
Note: This does NOT filter deleted entities. Use find_by_id for automatic
tombstone filtering.
Sourcepub fn get(id: Id) -> Result<Vec<u8>, StorageError>
pub fn get(id: Id) -> Result<Vec<u8>, StorageError>
Gets raw entity data by ID.
This is a simple alias for find_by_id_raw for convenience in tests.
§Errors
Returns IndexNotFound if entity doesn’t exist.
Sourcepub fn generate_comparison_data(
id: Option<Id>,
) -> Result<ComparisonData, StorageError>
pub fn generate_comparison_data( id: Option<Id>, ) -> Result<ComparisonData, StorageError>
Generates comparison metadata for tree synchronization.
Includes hashes, ancestors, children info. Used by compare_trees().
§Errors
Returns error if index lookup fails.
Sourcepub fn has_children(parent_id: Id) -> Result<bool, StorageError>
pub fn has_children(parent_id: Id) -> Result<bool, StorageError>
Sourcepub fn parent_of<D: Data>(child_id: Id) -> Result<Option<D>, StorageError>
pub fn parent_of<D: Data>(child_id: Id) -> Result<Option<D>, StorageError>
Retrieves the parent entity of a child.
§Errors
Returns error if index lookup or deserialization fails.
Sourcepub fn remove_child_from(
parent_id: Id,
child_id: Id,
) -> Result<bool, StorageError>
pub fn remove_child_from( parent_id: Id, child_id: Id, ) -> Result<bool, StorageError>
Removes a child from a collection.
Deletes the child entity and generates sync actions automatically.
§Errors
Returns error if parent or child doesn’t exist.
Sourcepub fn commit_root<D: Data>(root: Option<D>) -> Result<(), StorageError>
pub fn commit_root<D: Data>(root: Option<D>) -> Result<(), StorageError>
Saves the root entity and commits sync actions.
Should be called at the end of each transaction. Call once per execution.
§Errors
UnexpectedIdif root ID doesn’t matchSerializationErrorif encoding fails
Sourcepub fn save<D: Data>(entity: &mut D) -> Result<bool, StorageError>
pub fn save<D: Data>(entity: &mut D) -> Result<bool, StorageError>
Saves an entity to storage, updating if it exists.
Only saves if entity is dirty. Returns false if not saved due to:
- Entity not dirty
- Existing record is newer (last-write-wins guard)
Automatically:
- Calculates Merkle hashes
- Updates timestamps
- Generates sync actions
- Propagates hash changes up ancestor chain
Note: Use add_child_to() for new children,
then save() for subsequent updates.
§Errors
SerializationErrorif encoding failsCannotCreateOrphanif entity has no parent and isn’t root