Debian Codegraph @

third_party/rust/rkv/src/backend/impl_lmdb/transaction.rs

raw
1 // Copyright 2018-2019 Mozilla
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 // this file except in compliance with the License. You may obtain a copy of the
5 // License at http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software distributed
7 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10
11 use lmdb::Transaction;
12
13 use super::{DatabaseImpl, ErrorImpl, RoCursorImpl, WriteFlagsImpl};
14 use crate::backend::traits::{
15 BackendRoCursorTransaction, BackendRoTransaction, BackendRwCursorTransaction,
16 BackendRwTransaction,
17 };
18
19 #[derive(Debug)]
20 pub struct RoTransactionImpl<'t>(pub(crate) lmdb::RoTransaction<'t>);
21
22 impl<'t> BackendRoTransaction for RoTransactionImpl<'t> {
23 type Database = DatabaseImpl;
24 type Error = ErrorImpl;
25
26 fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
27 self.0.get(db.0, &key).map_err(ErrorImpl::LmdbError)
28 }
29
30 fn abort(self) {
31 self.0.abort()
32 }
33 }
34
35 impl<'t> BackendRoCursorTransaction<'t> for RoTransactionImpl<'t> {
36 type RoCursor = RoCursorImpl<'t>;
37
38 fn open_ro_cursor(&'t self, db: &Self::Database) -> Result<Self::RoCursor, Self::Error> {
39 self.0
40 .open_ro_cursor(db.0)
41 .map(RoCursorImpl)
42 .map_err(ErrorImpl::LmdbError)
43 }
44 }
45
46 #[derive(Debug)]
47 pub struct RwTransactionImpl<'t>(pub(crate) lmdb::RwTransaction<'t>);
48
49 impl<'t> BackendRwTransaction for RwTransactionImpl<'t> {
50 type Database = DatabaseImpl;
51 type Error = ErrorImpl;
52 type Flags = WriteFlagsImpl;
53
54 fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
55 self.0.get(db.0, &key).map_err(ErrorImpl::LmdbError)
56 }
57
58 fn put(
59 &mut self,
60 db: &Self::Database,
61 key: &[u8],
62 value: &[u8],
63 flags: Self::Flags,
64 ) -> Result<(), Self::Error> {
65 self.0
66 .put(db.0, &key, &value, flags.0)
67 .map_err(ErrorImpl::LmdbError)
68 }
69
70 #[cfg(not(feature = "db-dup-sort"))]
71 fn del(&mut self, db: &Self::Database, key: &[u8]) -> Result<(), Self::Error> {
72 self.0.del(db.0, &key, None).map_err(ErrorImpl::LmdbError)
73 }
74
75 #[cfg(feature = "db-dup-sort")]
76 fn del(
77 &mut self,
78 db: &Self::Database,
79 key: &[u8],
80 value: Option<&[u8]>,
81 ) -> Result<(), Self::Error> {
82 self.0.del(db.0, &key, value).map_err(ErrorImpl::LmdbError)
83 }
84
85 fn clear_db(&mut self, db: &Self::Database) -> Result<(), Self::Error> {
86 self.0.clear_db(db.0).map_err(ErrorImpl::LmdbError)
87 }
88
89 fn commit(self) -> Result<(), Self::Error> {
90 self.0.commit().map_err(ErrorImpl::LmdbError)
91 }
92
93 fn abort(self) {
94 self.0.abort()
95 }
96 }
97
98 impl<'t> BackendRwCursorTransaction<'t> for RwTransactionImpl<'t> {
99 type RoCursor = RoCursorImpl<'t>;
100
101 fn open_ro_cursor(&'t self, db: &Self::Database) -> Result<Self::RoCursor, Self::Error> {
102 self.0
103 .open_ro_cursor(db.0)
104 .map(RoCursorImpl)
105 .map_err(ErrorImpl::LmdbError)
106 }
107 }