Skip to content

Commit ef662e3

Browse files
committed
feat: add default transaction functions
1 parent 2018449 commit ef662e3

5 files changed

Lines changed: 83 additions & 108 deletions

File tree

src/collection/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum TransactionStatus {
2424

2525
pub const COLLECTION_LOCK_FILE: &str = ".lock";
2626

27-
#[derive(Clone)]
27+
/// Collection is a key-value store
2828
pub struct Collection<T: ValueTrait> {
2929
btree: Arc<Mutex<BTree<String, T>>>,
3030
xlog: Arc<Mutex<XLog<T>>>,
@@ -54,7 +54,7 @@ impl<T: ValueTrait> Collection<T> {
5454
})
5555
}
5656

57-
/// Starts a new transaction
57+
/// Starts a new branch transaction.
5858
pub fn branch_start(&self) -> Result<impl Transaction<T> + '_> {
5959
let dustdata_config = dustdata_config();
6060
let base_path = dustdata_config.data_path.join(self.name.clone());

src/collection/strategies/branch.rs

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::fs;
22

33
use crate::{
4-
btree::{spec::BTreePair, BTree, BTreeIterator, ValueTrait},
5-
collection::{
6-
xact::{Transaction, TransactionOperations},
7-
xlog::XLogOperation,
8-
},
4+
btree::{BTree, ValueTrait},
5+
collection::xact::{Transaction, TransactionOperations},
96
dustdata_config,
10-
error::{Error, Result},
7+
error::Error,
118
};
129

1310
pub struct BranchTransaction<T: ValueTrait> {
@@ -18,50 +15,6 @@ pub struct BranchTransaction<T: ValueTrait> {
1815
}
1916

2017
impl<T: ValueTrait> Transaction<T> for BranchTransaction<T> {
21-
fn get(&mut self, key: &str) -> Result<Option<T>> {
22-
self.btree.get(&key.to_string())
23-
}
24-
25-
fn insert(&mut self, key: &str, value: T) -> Result<()> {
26-
self.xact_op.write(XLogOperation::Insert {
27-
key: key.to_string(),
28-
value: value.clone(),
29-
})?;
30-
31-
self.btree.insert(key.to_string(), value)
32-
}
33-
34-
fn delete(&mut self, key: &str) -> Result<()> {
35-
self.xact_op.write(XLogOperation::Delete {
36-
key: key.to_string(),
37-
})?;
38-
39-
self.btree.delete(&key.to_string())
40-
}
41-
42-
fn update(&mut self, key: &str, new_value: T) -> Result<()> {
43-
self.xact_op.write(XLogOperation::Update {
44-
key: key.to_string(),
45-
new_value: new_value.clone(),
46-
})?;
47-
48-
self.btree.delete(&key.to_string())?;
49-
self.btree.insert(key.to_string(), new_value)?;
50-
51-
Ok(())
52-
}
53-
54-
fn iter(&mut self) -> BTreeIterator<'_, String, T> {
55-
self.btree.iter()
56-
}
57-
58-
fn find_by_pattern<'a>(
59-
&'a mut self,
60-
pattern: &'a str,
61-
) -> Box<(dyn Iterator<Item = BTreePair<String, T>> + 'a)> {
62-
Box::new(self.btree.find_pattern(pattern))
63-
}
64-
6518
fn rollback(self) {}
6619

6720
fn xid(&self) -> u64 {
@@ -71,6 +24,10 @@ impl<T: ValueTrait> Transaction<T> for BranchTransaction<T> {
7124
fn log(&mut self) -> &mut TransactionOperations<T> {
7225
&mut self.xact_op
7326
}
27+
28+
fn btree(&mut self) -> &mut BTree<String, T> {
29+
&mut self.btree
30+
}
7431
}
7532

7633
impl<T: ValueTrait> Drop for BranchTransaction<T> {

src/collection/strategies/lock.rs

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::{fs, sync::MutexGuard};
22

33
use crate::{
4-
btree::{spec::BTreePair, BTree, BTreeIterator, ValueTrait},
5-
collection::{
6-
xact::{Transaction, TransactionOperations},
7-
xlog::XLogOperation,
8-
},
4+
btree::{BTree, ValueTrait},
5+
collection::xact::{Transaction, TransactionOperations},
96
dustdata_config,
10-
error::{Error, Result},
7+
error::Error,
118
};
129

1310
pub struct LockTransaction<'lock, T: ValueTrait> {
@@ -19,50 +16,6 @@ pub struct LockTransaction<'lock, T: ValueTrait> {
1916
}
2017

2118
impl<'lock, T: ValueTrait> Transaction<T> for LockTransaction<'lock, T> {
22-
fn get(&mut self, key: &str) -> Result<Option<T>> {
23-
self.btree.get(&key.to_string())
24-
}
25-
26-
fn insert(&mut self, key: &str, value: T) -> Result<()> {
27-
self.xact_op.write(XLogOperation::Insert {
28-
key: key.to_string(),
29-
value: value.clone(),
30-
})?;
31-
32-
self.btree.insert(key.to_string(), value)
33-
}
34-
35-
fn delete(&mut self, key: &str) -> Result<()> {
36-
self.xact_op.write(XLogOperation::Delete {
37-
key: key.to_string(),
38-
})?;
39-
40-
self.btree.delete(&key.to_string())
41-
}
42-
43-
fn update(&mut self, key: &str, new_value: T) -> Result<()> {
44-
self.xact_op.write(XLogOperation::Update {
45-
key: key.to_string(),
46-
new_value: new_value.clone(),
47-
})?;
48-
49-
self.btree.delete(&key.to_string())?;
50-
self.btree.insert(key.to_string(), new_value)?;
51-
52-
Ok(())
53-
}
54-
55-
fn iter(&mut self) -> BTreeIterator<'_, String, T> {
56-
self.btree.iter()
57-
}
58-
59-
fn find_by_pattern<'a>(
60-
&'a mut self,
61-
pattern: &'a str,
62-
) -> Box<(dyn Iterator<Item = BTreePair<String, T>> + 'a)> {
63-
Box::new(self.btree.find_pattern(pattern))
64-
}
65-
6619
fn rollback(self) {}
6720

6821
fn xid(&self) -> u64 {
@@ -72,6 +25,10 @@ impl<'lock, T: ValueTrait> Transaction<T> for LockTransaction<'lock, T> {
7225
fn log(&mut self) -> &mut TransactionOperations<T> {
7326
&mut self.xact_op
7427
}
28+
29+
fn btree(&mut self) -> &mut BTree<String, T> {
30+
&mut self.btree
31+
}
7532
}
7633

7734
impl<'a, T: ValueTrait> Drop for LockTransaction<'a, T> {

src/collection/xact.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,57 @@ impl<T: ValueTrait> TransactionOperations<T> {
4242
}
4343

4444
pub trait Transaction<T: ValueTrait> {
45-
fn get(&mut self, key: &str) -> Result<Option<T>>;
46-
fn insert(&mut self, key: &str, value: T) -> Result<()>;
47-
fn delete(&mut self, key: &str) -> Result<()>;
48-
fn update(&mut self, key: &str, new_value: T) -> Result<()>;
49-
fn iter(&mut self) -> BTreeIterator<'_, String, T>;
45+
fn get(&mut self, key: &str) -> Result<Option<T>> {
46+
self.btree().get(&key.to_string())
47+
}
48+
49+
fn insert(&mut self, key: &str, value: T) -> Result<()> {
50+
self.log().write(XLogOperation::Insert {
51+
key: key.to_string(),
52+
value: value.clone(),
53+
})?;
54+
55+
self.btree().insert(key.to_string(), value)
56+
}
57+
58+
fn delete(&mut self, key: &str) -> Result<()> {
59+
self.log().write(XLogOperation::Delete {
60+
key: key.to_string(),
61+
})?;
62+
63+
self.btree().delete(&key.to_string())
64+
}
65+
66+
fn update(&mut self, key: &str, new_value: T) -> Result<()> {
67+
self.log().write(XLogOperation::Update {
68+
key: key.to_string(),
69+
new_value: new_value.clone(),
70+
})?;
71+
72+
self.btree().delete(&key.to_string())?;
73+
self.btree().insert(key.to_string(), new_value)?;
74+
75+
Ok(())
76+
}
77+
78+
fn iter(&mut self) -> BTreeIterator<'_, String, T> {
79+
self.btree().iter()
80+
}
81+
5082
fn find_by_pattern<'a>(
5183
&'a mut self,
5284
pattern: &'a str,
53-
) -> Box<dyn Iterator<Item = BTreePair<String, T>> + 'a>;
85+
) -> Box<(dyn Iterator<Item = BTreePair<String, T>> + 'a)>
86+
where
87+
T: 'a,
88+
{
89+
Box::new(self.btree().find_pattern(pattern))
90+
}
91+
5492
fn rollback(self);
5593
fn xid(&self) -> u64;
5694
fn log(&mut self) -> &mut TransactionOperations<T>;
95+
fn btree(&mut self) -> &mut BTree<String, T>;
5796
}
5897

5998
pub struct TransactionBuilder;

src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,35 @@ impl DustData {
105105
Ok(Self)
106106
}
107107

108+
/// Creates a new collection.
109+
/// ## Arguments
110+
/// * `name` - The name of the collection.
111+
/// ## Example
112+
/// ```rust
113+
/// use dustdata::DustData;
114+
///
115+
/// let dustdata = DustData::new().unwrap();
116+
///
117+
/// let collection = dustdata.create_collection::<User>("users").unwrap();
118+
/// ```
108119
pub fn collection<T>(&self, name: &str) -> Result<Arc<collection::Collection<T>>>
109120
where
110121
T: Sync + Send + Clone + Debug + Serialize + DeserializeOwned + 'static + Ord,
111122
{
112123
Ok(Arc::new(collection::Collection::new(name)?))
113124
}
114125

126+
/// Drops a collection.
127+
/// ## Arguments
128+
/// * `name` - The name of the collection.
129+
/// ## Example
130+
/// ```rust
131+
/// use dustdata::DustData;
132+
///
133+
/// let dustdata = DustData::new().unwrap();
134+
///
135+
/// dustdata.drop_collection("users").unwrap();
136+
/// ```
115137
pub fn drop_collection(&self, name: &str) -> Result<()> {
116138
let dustdata_config = dustdata_config();
117139

0 commit comments

Comments
 (0)