db/model/
tx.rs

1use crate::{
2	err::Error,
3	interface::{
4		kv::{Key, Val},
5		KeyValuePair,
6	},
7	util::now,
8};
9use async_trait::async_trait;
10use futures::lock::Mutex;
11use std::{pin::Pin, sync::Arc};
12
13pub type CF = Option<Vec<u8>>;
14
15/// # Distributed Database Transaction
16/// ## Atomically reference counter
17/// Shared references in Rust disallow mutation by default, and Arc is no exception: you cannot
18/// generally obtain a mutable reference to something inside an Arc. If you need to mutate
19/// through an Arc, use Mutex, RwLock, or one of the Atomic types.
20///
21/// because it tries to borrow arc as mutable. For it to happen, DerefMut would have
22/// to be implemented for Arc but it's not because Arc is not meant to be mutable.
23#[derive(Debug)]
24pub struct DBTransaction<D, T>
25where
26	D: 'static,
27	T: 'static,
28{
29	pub tx: Arc<Mutex<Option<T>>>,
30	pub ok: bool,
31	pub writable: bool,
32	pub readable: bool,
33	pub timestamp: i64,
34	pub _db: Pin<Arc<D>>,
35}
36
37impl<DBType, TxType> DBTransaction<DBType, TxType>
38where
39	DBType: 'static,
40	TxType: 'static,
41{
42	pub fn new(tx: TxType, db: Pin<Arc<DBType>>, w: bool) -> Result<Self, Error> {
43		Ok(DBTransaction {
44			tx: Arc::new(Mutex::new(Some(tx))),
45			ok: false,
46			writable: w,
47			readable: true,
48			timestamp: now(),
49			_db: db,
50		})
51	}
52}
53
54#[async_trait(?Send)]
55pub trait SimpleTransaction {
56	// Check if closed
57	fn closed(&self) -> bool;
58
59	// Cancel a transaction
60	async fn cancel(&mut self) -> Result<(), Error>;
61
62	// Count number of items
63	async fn count(&mut self, cf: CF) -> Result<usize, Error>;
64
65	// Commit a transaction
66	async fn commit(&mut self) -> Result<(), Error>;
67
68	// Check if a key exists
69	async fn exi<K: Into<Key> + Send>(&self, cf: CF, key: K) -> Result<bool, Error>;
70
71	/// Fetch a key from the database
72	async fn get<K: Into<Key> + Send>(&self, cf: CF, key: K) -> Result<Option<Val>, Error>;
73
74	/// Insert or update a key in the database
75	async fn set<K: Into<Key> + Send, V: Into<Key> + Send>(
76		&mut self,
77		cf: CF,
78		key: K,
79		val: V,
80	) -> Result<(), Error>;
81
82	/// Insert a key if it doesn't exist in the database
83	async fn put<K: Into<Key> + Send, V: Into<Key> + Send>(
84		&mut self,
85		cf: CF,
86		key: K,
87		val: V,
88	) -> Result<(), Error>;
89
90	/// Delete a key
91	async fn del<K: Into<Key> + Send>(&mut self, cf: CF, key: K) -> Result<(), Error>;
92
93	// OPTIONAL Fetch multiple keys from the database
94	async fn multi_get<K: Into<Key> + Send + AsRef<[u8]>>(
95		&self,
96		_cf: CF,
97		_keys: Vec<K>,
98	) -> Result<Vec<Option<Val>>, Error> {
99		todo!();
100	}
101
102	// Iterate elements in key value store
103	async fn iterate(&self, cf: CF) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
104
105	// Iterate elements with prefixx in key value store
106	async fn prefix_iterate<P: Into<Key> + Send>(
107		&self,
108		cf: CF,
109		prefix: P,
110	) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
111
112	// Iterate elements with prefixx in key value store
113	async fn suffix_iterate<S: Into<Key> + Send>(
114		&self,
115		cf: CF,
116		suffix: S,
117	) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
118}