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
16pub 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, tags: TagBucket) -> Result<usize, Error>;
64
65 async fn commit(&mut self) -> Result<(), Error>;
67
68 async fn exi<K: Into<Key> + Send>(&self, key: K, tags: TagBucket) -> Result<bool, Error>;
70
71 async fn get<K: Into<Key> + Send>(&self, key: K, tags: TagBucket)
73 -> Result<Option<Val>, Error>;
74
75 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 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 async fn del<K: Into<Key> + Send>(&mut self, key: K, tags: TagBucket) -> Result<(), Error>;
93
94 async fn iterate(&self, tags: TagBucket) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
96
97 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 async fn suffix_iterate<S: Into<Key> + Send>(
106 &self,
107 suffix: S,
108 tags: TagBucket,
109 ) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
110}