db/model/
tx.rs

1use crate::{
2	err::Error,
3	interface::{
4		kv::{Key, Val},
5		KeyValuePair,
6	},
7	util::now,
8	TagBucket,
9};
10use async_trait::async_trait;
11use futures::lock::Mutex;
12use std::{pin::Pin, sync::Arc};
13
14pub type CF = Option<Vec<u8>>;
15
16/// # Distributed Database Transaction
17/// ## Atomically reference counter
18/// Shared references in Rust disallow mutation by default, and Arc is no exception: you cannot
19/// generally obtain a mutable reference to something inside an Arc. If you need to mutate
20/// through an Arc, use Mutex, RwLock, or one of the Atomic types.
21///
22/// because it tries to borrow arc as mutable. For it to happen, DerefMut would have
23/// to be implemented for Arc but it's not because Arc is not meant to be mutable.
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, tags: TagBucket) -> 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, key: K, tags: TagBucket) -> Result<bool, Error>;
70
71	/// Fetch a key from the database
72	async fn get<K: Into<Key> + Send>(&self, key: K, tags: TagBucket)
73		-> Result<Option<Val>, Error>;
74
75	/// Insert or update a key in the database
76	async fn set<K: Into<Key> + Send, V: Into<Key> + Send>(
77		&mut self,
78		key: K,
79		val: V,
80		tags: TagBucket,
81	) -> Result<(), Error>;
82
83	/// Insert a key if it doesn't exist in the database
84	async fn put<K: Into<Key> + Send, V: Into<Key> + Send>(
85		&mut self,
86		key: K,
87		val: V,
88		tags: TagBucket,
89	) -> Result<(), Error>;
90
91	/// Delete a key
92	async fn del<K: Into<Key> + Send>(&mut self, key: K, tags: TagBucket) -> Result<(), Error>;
93
94	// Iterate elements in key value store
95	async fn iterate(&self, tags: TagBucket) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
96
97	// Iterate elements with prefixx in key value store
98	async fn prefix_iterate<P: Into<Key> + Send>(
99		&self,
100		prefix: P,
101		tags: TagBucket,
102	) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
103
104	// Iterate elements with prefixx in key value store
105	async fn suffix_iterate<S: Into<Key> + Send>(
106		&self,
107		suffix: S,
108		tags: TagBucket,
109	) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
110}