1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::{
backend::{
BackendDatabase,
BackendRoCursor,
BackendRoCursorTransaction,
BackendRoTransaction,
BackendRwCursorTransaction,
BackendRwTransaction,
},
error::StoreError,
helpers::read_transform,
value::Value,
};
pub struct Reader<T>(T);
pub struct Writer<T>(T);
pub trait Readable<'r> {
type Database: BackendDatabase;
type RoCursor: BackendRoCursor<'r>;
fn get<K>(&'r self, db: &Self::Database, k: &K) -> Result<Option<Value<'r>>, StoreError>
where
K: AsRef<[u8]>;
fn open_ro_cursor(&'r self, db: &Self::Database) -> Result<Self::RoCursor, StoreError>;
}
impl<'r, T> Readable<'r> for Reader<T>
where
T: BackendRoCursorTransaction<'r>,
{
type Database = T::Database;
type RoCursor = T::RoCursor;
fn get<K>(&'r self, db: &T::Database, k: &K) -> Result<Option<Value<'r>>, StoreError>
where
K: AsRef<[u8]>,
{
let bytes = self.0.get(db, k.as_ref()).map_err(|e| e.into());
match read_transform(bytes).map(Some) {
Err(StoreError::KeyValuePairNotFound) => Ok(None),
result => result,
}
}
fn open_ro_cursor(&'r self, db: &T::Database) -> Result<T::RoCursor, StoreError> {
self.0.open_ro_cursor(db).map_err(|e| e.into())
}
}
impl<T> Reader<T> {
pub(crate) fn new(txn: T) -> Reader<T> {
Reader(txn)
}
}
impl<T> Reader<T>
where
T: BackendRoTransaction,
{
pub fn abort(self) {
self.0.abort();
}
}
impl<'r, T> Readable<'r> for Writer<T>
where
T: BackendRwCursorTransaction<'r>,
{
type Database = T::Database;
type RoCursor = T::RoCursor;
fn get<K>(&'r self, db: &T::Database, k: &K) -> Result<Option<Value<'r>>, StoreError>
where
K: AsRef<[u8]>,
{
let bytes = self.0.get(db, k.as_ref()).map_err(|e| e.into());
match read_transform(bytes).map(Some) {
Err(StoreError::KeyValuePairNotFound) => Ok(None),
result => result,
}
}
fn open_ro_cursor(&'r self, db: &T::Database) -> Result<T::RoCursor, StoreError> {
self.0.open_ro_cursor(db).map_err(|e| e.into())
}
}
impl<T> Writer<T> {
pub(crate) fn new(txn: T) -> Writer<T> {
Writer(txn)
}
}
impl<T> Writer<T>
where
T: BackendRwTransaction,
{
pub fn commit(self) -> Result<(), StoreError> {
self.0.commit().map_err(|e| e.into())
}
pub fn abort(self) {
self.0.abort();
}
pub(crate) fn put<K>(&mut self, db: &T::Database, k: &K, v: &Value, flags: T::Flags) -> Result<(), StoreError>
where
K: AsRef<[u8]>,
{
self.0.put(db, k.as_ref(), &v.to_bytes()?, flags).map_err(|e| e.into())
}
#[cfg(not(feature = "db-dup-sort"))]
pub(crate) fn delete<K>(&mut self, db: &T::Database, k: &K) -> Result<(), StoreError>
where
K: AsRef<[u8]>,
{
self.0.del(db, k.as_ref()).map_err(|e| e.into())
}
#[cfg(feature = "db-dup-sort")]
pub(crate) fn delete<K>(&mut self, db: &T::Database, k: &K, v: Option<&[u8]>) -> Result<(), StoreError>
where
K: AsRef<[u8]>,
{
self.0.del(db, k.as_ref(), v).map_err(|e| e.into())
}
pub(crate) fn clear(&mut self, db: &T::Database) -> Result<(), StoreError> {
self.0.clear_db(db).map_err(|e| e.into())
}
}