Debian Codegraph @

third_party/rust/rkv/src/backend/impl_safe/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 #![allow(dead_code)] // TODO: Get rid of unused struct members
11
12 use std::{collections::HashMap, sync::Arc};
13
14 use super::{
15 snapshot::Snapshot, DatabaseImpl, EnvironmentImpl, ErrorImpl, RoCursorImpl, WriteFlagsImpl,
16 };
17 use crate::backend::traits::{
18 BackendRoCursorTransaction, BackendRoTransaction, BackendRwCursorTransaction,
19 BackendRwTransaction,
20 };
21
22 #[derive(Debug)]
23 pub struct RoTransactionImpl<'t> {
24 env: &'t EnvironmentImpl,
25 snapshots: HashMap<DatabaseImpl, Snapshot>,
26 idx: Arc<()>,
27 }
28
29 impl<'t> RoTransactionImpl<'t> {
30 pub(crate) fn new(
31 env: &'t EnvironmentImpl,
32 idx: Arc<()>,
33 ) -> Result<RoTransactionImpl<'t>, ErrorImpl> {
34 let snapshots = env
35 .dbs()?
36 .arena
37 .iter()
38 .map(|(id, db)| (DatabaseImpl(id), db.snapshot()))
39 .collect();
40 Ok(RoTransactionImpl {
41 env,
42 snapshots,
43 idx,
44 })
45 }
46 }
47
48 impl<'t> BackendRoTransaction for RoTransactionImpl<'t> {
49 type Database = DatabaseImpl;
50 type Error = ErrorImpl;
51
52 fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
53 let snapshot = self.snapshots.get(db).ok_or(ErrorImpl::DbIsForeignError)?;
54 snapshot.get(key).ok_or(ErrorImpl::KeyValuePairNotFound)
55 }
56
57 fn abort(self) {
58 // noop
59 }
60 }
61
62 impl<'t> BackendRoCursorTransaction<'t> for RoTransactionImpl<'t> {
63 type RoCursor = RoCursorImpl<'t>;
64
65 fn open_ro_cursor(&'t self, db: &Self::Database) -> Result<Self::RoCursor, Self::Error> {
66 let snapshot = self.snapshots.get(db).ok_or(ErrorImpl::DbIsForeignError)?;
67 Ok(RoCursorImpl(snapshot))
68 }
69 }
70
71 #[derive(Debug)]
72 pub struct RwTransactionImpl<'t> {
73 env: &'t EnvironmentImpl,
74 snapshots: HashMap<DatabaseImpl, Snapshot>,
75 idx: Arc<()>,
76 }
77
78 impl<'t> RwTransactionImpl<'t> {
79 pub(crate) fn new(
80 env: &'t EnvironmentImpl,
81 idx: Arc<()>,
82 ) -> Result<RwTransactionImpl<'t>, ErrorImpl> {
83 let snapshots = env
84 .dbs()?
85 .arena
86 .iter()
87 .map(|(id, db)| (DatabaseImpl(id), db.snapshot()))
88 .collect();
89 Ok(RwTransactionImpl {
90 env,
91 snapshots,
92 idx,
93 })
94 }
95 }
96
97 impl<'t> BackendRwTransaction for RwTransactionImpl<'t> {
98 type Database = DatabaseImpl;
99 type Error = ErrorImpl;
100 type Flags = WriteFlagsImpl;
101
102 fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
103 let snapshot = self.snapshots.get(db).ok_or(ErrorImpl::DbIsForeignError)?;
104 snapshot.get(key).ok_or(ErrorImpl::KeyValuePairNotFound)
105 }
106
107 #[cfg(not(feature = "db-dup-sort"))]
108 fn put(
109 &mut self,
110 db: &Self::Database,
111 key: &[u8],
112 value: &[u8],
113 _flags: Self::Flags,
114 ) -> Result<(), Self::Error> {
115 let snapshot = self
116 .snapshots
117 .get_mut(db)
118 .ok_or_else(|| ErrorImpl::DbIsForeignError)?;
119 snapshot.put(key, value);
120 Ok(())
121 }
122
123 #[cfg(feature = "db-dup-sort")]
124 fn put(
125 &mut self,
126 db: &Self::Database,
127 key: &[u8],
128 value: &[u8],
129 _flags: Self::Flags,
130 ) -> Result<(), Self::Error> {
131 use super::DatabaseFlagsImpl;
132 let snapshot = self
133 .snapshots
134 .get_mut(db)
135 .ok_or(ErrorImpl::DbIsForeignError)?;
136 if snapshot.flags().contains(DatabaseFlagsImpl::DUP_SORT) {
137 snapshot.put_dup(key, value);
138 } else {
139 snapshot.put(key, value);
140 }
141 Ok(())
142 }
143
144 #[cfg(not(feature = "db-dup-sort"))]
145 fn del(&mut self, db: &Self::Database, key: &[u8]) -> Result<(), Self::Error> {
146 let snapshot = self
147 .snapshots
148 .get_mut(db)
149 .ok_or_else(|| ErrorImpl::DbIsForeignError)?;
150 let deleted = snapshot.del(key);
151 Ok(deleted.ok_or_else(|| ErrorImpl::KeyValuePairNotFound)?)
152 }
153
154 #[cfg(feature = "db-dup-sort")]
155 fn del(
156 &mut self,
157 db: &Self::Database,
158 key: &[u8],
159 value: Option<&[u8]>,
160 ) -> Result<(), Self::Error> {
161 use super::DatabaseFlagsImpl;
162 let snapshot = self
163 .snapshots
164 .get_mut(db)
165 .ok_or(ErrorImpl::DbIsForeignError)?;
166 let deleted = match (value, snapshot.flags()) {
167 (Some(value), flags) if flags.contains(DatabaseFlagsImpl::DUP_SORT) => {
168 snapshot.del_exact(key, value)
169 }
170 _ => snapshot.del(key),
171 };
172 deleted.ok_or(ErrorImpl::KeyValuePairNotFound)
173 }
174
175 fn clear_db(&mut self, db: &Self::Database) -> Result<(), Self::Error> {
176 let snapshot = self
177 .snapshots
178 .get_mut(db)
179 .ok_or(ErrorImpl::DbIsForeignError)?;
180 snapshot.clear();
181 Ok(())
182 }
183
184 fn commit(self) -> Result<(), Self::Error> {
185 let mut dbs = self.env.dbs_mut()?;
186
187 for (id, snapshot) in self.snapshots {
188 let db = dbs.arena.get_mut(id.0).ok_or(ErrorImpl::DbIsForeignError)?;
189 db.replace(snapshot);
190 }
191
192 drop(dbs);
193 self.env.write_to_disk()
194 }
195
196 fn abort(self) {
197 // noop
198 }
199 }
200
201 impl<'t> BackendRwCursorTransaction<'t> for RwTransactionImpl<'t> {
202 type RoCursor = RoCursorImpl<'t>;
203
204 fn open_ro_cursor(&'t self, db: &Self::Database) -> Result<Self::RoCursor, Self::Error> {
205 let snapshot = self.snapshots.get(db).ok_or(ErrorImpl::DbIsForeignError)?;
206 Ok(RoCursorImpl(snapshot))
207 }
208 }