fix: Use incremental ID
This commit is contained in:
@@ -32,9 +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 and return the assigned ID.
|
||||
/// This method does not modify the original model.
|
||||
fn set(&self, value: &V) -> Result<u32, 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>>
|
||||
@@ -46,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
|
||||
@@ -59,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> {
|
||||
@@ -72,3 +90,21 @@ impl<E> From<bincode::error::EncodeError> for Error<E> {
|
||||
Error::Encode(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user