Merge branch 'development_add_incremental_mode_to_heromodels'

This commit is contained in:
timurgordon
2025-05-22 18:24:11 +03:00
34 changed files with 2399 additions and 433 deletions

View File

@@ -32,8 +32,16 @@ where
/// Get an object from its ID. This does not use an index lookup
fn get_by_id(&self, id: u32) -> Result<Option<V>, Error<Self::Error>>;
/// Store an item in the DB.
fn set(&self, value: &V) -> Result<(), Error<Self::Error>>;
/// Store an item in the DB and return the assigned ID and the updated model.
///
/// # Important Notes
/// - This method does not modify the original model passed as an argument.
/// - For new models (with ID 0), an ID will be auto-generated by OurDB.
/// - The returned model will have the correct ID and should be used instead of the original model.
/// - The original model should not be used after calling this method, as it may have
/// an inconsistent state compared to what's in the database.
/// - ID 0 is reserved for new models and should not be used for existing models.
fn set(&self, value: &V) -> Result<(u32, V), Error<Self::Error>>;
/// Delete all items from the db with a given index.
fn delete<I, Q>(&self, key: &Q) -> Result<(), Error<Self::Error>>
@@ -45,8 +53,11 @@ where
/// Delete an object with a given ID
fn delete_by_id(&self, id: u32) -> Result<(), Error<Self::Error>>;
/// Get all objects from the colelction
/// Get all objects from the collection
fn get_all(&self) -> Result<Vec<V>, Error<Self::Error>>;
/// Begin a transaction for this collection
fn begin_transaction(&self) -> Result<Box<dyn Transaction<Error = Self::Error>>, Error<Self::Error>>;
}
/// Errors returned by the DB implementation
@@ -58,6 +69,14 @@ pub enum Error<E> {
Decode(bincode::error::DecodeError),
/// Error encoding a model for storage
Encode(bincode::error::EncodeError),
/// Invalid ID used (e.g., using ID 0 for an existing model)
InvalidId(String),
/// ID mismatch (e.g., expected ID 5 but got ID 6)
IdMismatch(String),
/// ID collision (e.g., trying to create a model with an ID that already exists)
IdCollision(String),
/// Type error (e.g., trying to get a model of the wrong type)
TypeError,
}
impl<E> From<bincode::error::DecodeError> for Error<E> {
@@ -80,4 +99,20 @@ impl<E: std::fmt::Debug + std::fmt::Display> std::fmt::Display for Error<E> {
Error::Encode(e) => write!(f, "Failed to encode model: {}", e),
}
}
/// A transaction that can be committed or rolled back
pub trait Transaction {
/// Error type for transaction operations
type Error: std::fmt::Debug;
/// Begin a transaction
fn begin(&self) -> Result<(), Error<Self::Error>>;
/// Commit a transaction
fn commit(&self) -> Result<(), Error<Self::Error>>;
/// Roll back a transaction
fn rollback(&self) -> Result<(), Error<Self::Error>>;
/// Check if a transaction is active
fn is_active(&self) -> bool;
}