Skip to content

Commit

Permalink
implement removal of all data stores
Browse files Browse the repository at this point in the history
  • Loading branch information
Occuros committed Sep 4, 2024
1 parent 9c3112a commit 7ae4498
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ trait StoreImpl {
fn get<T: DeserializeOwned>(&self, key: &str) -> Result<T, Self::GetError>;
fn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<(), Self::SetError>;
fn remove(&mut self, key: &str) -> Result<(), Self::RemoveError>;
fn remove_and_get<T: DeserializeOwned>(&mut self, key: &str) -> Result<Option<T>, Self::RemoveError>;
fn remove_and_get<T: DeserializeOwned>(
&mut self,
key: &str,
) -> Result<Option<T>, Self::RemoveError>;
fn clear(&mut self) -> Result<(), Self::SetError>;
}

Expand All @@ -45,7 +48,7 @@ mod rocksdb_store;
use rocksdb_store::{self as backend};

// todo: Look into unifying these types?
pub use backend::{GetError, SetError, RemoveError};
pub use backend::{GetError, RemoveError, SetError};

enum Location<'a> {
PlatformDefault(&'a PlatformDefault),
Expand Down Expand Up @@ -128,12 +131,15 @@ impl PkvStore {
pub fn get<T: DeserializeOwned>(&self, key: impl AsRef<str>) -> Result<T, GetError> {
self.inner.get(key.as_ref())
}
/// Remove the value from the store
/// Remove the value from the store for the given key
/// returns the removed value if one existed
pub fn remove_and_get<T: DeserializeOwned>(&mut self, key: impl AsRef<str>) -> Result<Option<T>, RemoveError> {
pub fn remove_and_get<T: DeserializeOwned>(
&mut self,
key: impl AsRef<str>,
) -> Result<Option<T>, RemoveError> {
self.inner.remove_and_get(key.as_ref())
}

/// Remove the value from the store for the given key
pub fn remove(&mut self, key: impl AsRef<str>) -> Result<(), RemoveError> {
self.inner.remove(key.as_ref())
}
Expand Down Expand Up @@ -253,7 +259,7 @@ mod tests {
#[test]
fn remove() {
setup();
let mut store = PkvStore::new("BevyPkv", "test_set");
let mut store = PkvStore::new("BevyPkv", "test_remove");
let user = User {
name: "alice".to_string(),
age: 32,
Expand All @@ -266,7 +272,7 @@ mod tests {
#[test]
fn remove_and_get() {
setup();
let mut store = PkvStore::new("BevyPkv", "test_set");
let mut store = PkvStore::new("BevyPkv", "test_remove_and_get");
let user = User {
name: "alice".to_string(),
age: 32,
Expand All @@ -276,5 +282,4 @@ mod tests {
assert_eq!(user, removed_user);
assert_eq!(store.get::<User>("user").ok(), None);
}

}
12 changes: 7 additions & 5 deletions src/redb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@ impl StoreImpl for ReDbStore {
Ok(())
}

fn remove_and_get<T: DeserializeOwned>(&mut self, key: &str) -> Result<Option<T>, Self::RemoveError> {
fn remove_and_get<T: DeserializeOwned>(
&mut self,
key: &str,
) -> Result<Option<T>, Self::RemoveError> {
let value: Option<T>;
let write_txn = self.db.begin_write()?;
{
let mut table = write_txn.open_table(TABLE).unwrap();
value = match table.remove(key)? {
Some(kv) => {rmp_serde::from_slice(kv.value()).ok()}
None => None
value = match table.remove(key)? {
Some(kv) => rmp_serde::from_slice(kv.value()).ok(),
None => None,
};
}
write_txn.commit()?;
Expand All @@ -154,7 +157,6 @@ impl StoreImpl for ReDbStore {
{
let mut table = write_txn.open_table(TABLE).unwrap();
table.remove(key)?;

}
write_txn.commit()?;

Expand Down
31 changes: 30 additions & 1 deletion src/rocksdb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub enum GetError {
#[error("No value found for the given key")]
NotFound,
}

/// Errors that can occur during `PkvStore::set`
#[derive(thiserror::Error, Debug)]
pub enum SetError {
Expand All @@ -33,6 +32,20 @@ pub enum SetError {
MessagePack(#[from] rmp_serde::encode::Error),
}

/// Errors that can occur during `PkvStore::remove`
#[derive(thiserror::Error, Debug)]
pub enum RemoveError {
/// An internal error from the rocksdb crate
#[error("Rocksdb error")]
Rocksdb(#[from] rocksdb::Error),
/// Error when deserializing the value
#[error("MessagePack deserialization error")]
MessagePack(#[from] rmp_serde::decode::Error),
/// The value for the given key was not found
#[error("No value found for the given key")]
NotFound,
}

impl RocksDBStore {
pub(crate) fn new(location: Location) -> Self {
let mut options = rocksdb::Options::default();
Expand All @@ -49,6 +62,7 @@ impl RocksDBStore {
impl StoreImpl for RocksDBStore {
type GetError = GetError;
type SetError = SetError;
type RemoveError = RemoveError;

/// Serialize and store the value
fn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<(), Self::SetError> {
Expand Down Expand Up @@ -87,4 +101,19 @@ impl StoreImpl for RocksDBStore {

Ok(())
}

fn remove(&mut self, key: &str) -> Result<(), Self::RemoveError> {
self.db.delete(key)?;
Ok(())
}

fn remove_and_get<T: DeserializeOwned>(
&mut self,
key: &str,
) -> Result<Option<T>, Self::RemoveError> {
let bytes = self.db.get(key)?.ok_or(Self::RemoveError::NotFound)?;
let value = rmp_serde::from_slice(&bytes)?;
self.db.delete(key)?;
Ok(value)
}
}
30 changes: 30 additions & 0 deletions src/sled_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ pub enum SetError {
MessagePack(#[from] rmp_serde::encode::Error),
}

/// Errors that can occur during `PkvStore::remove`
#[derive(thiserror::Error, Debug)]
pub enum RemoveError {
/// An internal error from the sled crate
#[error("Sled error")]
Sled(#[from] sled::Error),
/// Error when deserializing the value
#[error("MessagePack deserialization error")]
MessagePack(#[from] rmp_serde::decode::Error),
/// The value for the given key was not found
#[error("No value found for the given key")]
NotFound,
}


impl SledStore {
pub(crate) fn new(location: Location) -> Self {
let db_path = location.get_path().join("bevy_pkv.sled");
Expand All @@ -44,6 +59,7 @@ impl SledStore {
impl StoreImpl for SledStore {
type GetError = GetError;
type SetError = SetError;
type RemoveError = RemoveError;

/// Serialize and store the value
fn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<(), Self::SetError> {
Expand Down Expand Up @@ -77,4 +93,18 @@ impl StoreImpl for SledStore {
self.db.flush()?;
Ok(())
}

fn remove(&mut self, key: &str) -> Result<(), Self::RemoveError> {
self.db.remove(key)?;
Ok(())
}

fn remove_and_get<T: DeserializeOwned>(
&mut self,
key: &str,
) -> Result<Option<T>, Self::RemoveError> {
let bytes = self.db.remove(key)?.ok_or(Self::RemoveError::NotFound)?;
let value = rmp_serde::from_slice(&bytes)?;
Ok(value)
}
}

0 comments on commit 7ae4498

Please sign in to comment.