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#[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 fn closed(&self) -> bool;
58
59 async fn cancel(&mut self) -> Result<(), Error>;
61
62 async fn count(&mut self, cf: CF) -> Result<usize, Error>;
64
65 async fn commit(&mut self) -> Result<(), Error>;
67
68 async fn exi<K: Into<Key> + Send>(&self, cf: CF, key: K) -> Result<bool, Error>;
70
71 async fn get<K: Into<Key> + Send>(&self, cf: CF, key: K) -> Result<Option<Val>, Error>;
73
74 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 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 async fn del<K: Into<Key> + Send>(&mut self, cf: CF, key: K) -> Result<(), Error>;
92
93 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 async fn iterate(&self, cf: CF) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
104
105 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 async fn suffix_iterate<S: Into<Key> + Send>(
114 &self,
115 cf: CF,
116 suffix: S,
117 ) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
118}