Skip to content

Commit

Permalink
feat: add start_lazy function
Browse files Browse the repository at this point in the history
  • Loading branch information
peeeuzin committed Feb 20, 2024
1 parent 850cf4d commit 8b72085
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let mut dustdata = DustData::new(Default::default()).unwrap();
#[derive(Serialize, Deserialize, Clone, Debug)]
struct User {
name: String,
age: i32,
age: u32,
}

let collection = dustdata.collection::<User>("users");
Expand Down
10 changes: 10 additions & 0 deletions src/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ impl<T: Sync + Send + Clone + Debug + Serialize + 'static + DeserializeOwned> Co
Transaction::new()
}

pub fn start_lazy<F>(&self, f: F) -> Result<Transaction<T>>
where
F: FnOnce(&mut Transaction<T>),
{
let mut transaction = self.start();
f(&mut transaction);
self.commit(&mut transaction)?;
Ok(transaction)
}

/// Commits a transaction
pub fn commit(&self, transaction: &mut Transaction<T>) -> Result<()> {
if let TransactionStatus::Committed = transaction.status {
Expand Down
71 changes: 38 additions & 33 deletions tests/collection_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ pub fn test_config() -> dustdata::DustDataConfig {
#[test]
pub fn collection_insert_operation() {
let dustdata = DustData::new(test_config()).unwrap();
let collection = dustdata.collection::<String>("insert_and_get");
let collection = dustdata.collection::<String>("insert_collection");

let mut transaction = collection.start();
transaction.insert("key", "value".to_string());
collection.commit(&mut transaction).unwrap();
collection
.start_lazy(|t| {
t.insert("key", "value".to_string());
})
.unwrap();

let value = collection.get("key").unwrap().unwrap();

Expand All @@ -23,15 +25,19 @@ pub fn collection_insert_operation() {
#[test]
pub fn collection_update_operation() {
let dustdata = DustData::new(test_config()).unwrap();
let collection = dustdata.collection::<String>("update");
let collection = dustdata.collection::<String>("update_collection");

let mut transaction = collection.start();
transaction.insert("key", "value".to_string());
collection.commit(&mut transaction).unwrap();
collection
.start_lazy(|t| {
t.insert("key", "value".to_string());
})
.unwrap();

let mut transaction = collection.start();
transaction.update("key", "new_value".to_string());
collection.commit(&mut transaction).unwrap();
collection
.start_lazy(|t| {
t.update("key", "new_value".to_string());
})
.unwrap();

let value = collection.get("key").unwrap().unwrap();

Expand All @@ -41,15 +47,19 @@ pub fn collection_update_operation() {
#[test]
pub fn collection_delete_operation() {
let dustdata = DustData::new(test_config()).unwrap();
let collection = dustdata.collection::<String>("remove");
let collection = dustdata.collection::<String>("delete_collection");

let mut transaction = collection.start();
transaction.insert("key", "value".to_string());
collection.commit(&mut transaction).unwrap();
collection
.start_lazy(|t| {
t.insert("key", "value".to_string());
})
.unwrap();

let mut transaction = collection.start();
transaction.delete("key");
collection.commit(&mut transaction).unwrap();
collection
.start_lazy(|t| {
t.delete("key");
})
.unwrap();

let value = collection.get("key").unwrap();

Expand All @@ -61,22 +71,17 @@ pub fn collection_revert_operation() {
let dustdata = DustData::new(test_config()).unwrap();
let collection = dustdata.collection::<String>("revert_operation_collection");

let mut transaction = collection.start();
transaction.insert("key", "value".to_string());
collection.commit(&mut transaction).unwrap();
let mut rolledback_transaction = collection
.start_lazy(|t| {
t.insert("key", "value".to_string());
})
.unwrap();

let mut transaction = collection.start();
transaction.update("key", "new_value".to_string());
collection.commit(&mut transaction).unwrap();
collection
.rollback_transaction(&mut rolledback_transaction)
.unwrap();

let mut transaction = collection.start();
transaction.delete("key");
collection.commit(&mut transaction).unwrap();

let transaction = collection.rollback_transaction(&mut transaction).unwrap();

let value = collection.get("key").unwrap().unwrap();
let value = collection.get("key").unwrap();

assert_eq!(value, "new_value");
println!("{:?}", transaction);
assert!(value.is_none());
}

0 comments on commit 8b72085

Please sign in to comment.