| 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 std::{ |
| 12 |
fs, |
| 13 |
path::{Path, PathBuf}, |
| 14 |
}; |
| 15 |
|
| 16 |
use lmdb::Error as LmdbError; |
| 17 |
|
| 18 |
use super::{ |
| 19 |
DatabaseFlagsImpl, DatabaseImpl, EnvironmentFlagsImpl, ErrorImpl, InfoImpl, RoTransactionImpl, |
| 20 |
RwTransactionImpl, StatImpl, |
| 21 |
}; |
| 22 |
use crate::backend::common::RecoveryStrategy; |
| 23 |
use crate::backend::traits::{ |
| 24 |
BackendEnvironment, BackendEnvironmentBuilder, BackendInfo, BackendIter, BackendRoCursor, |
| 25 |
BackendRoCursorTransaction, BackendStat, |
| 26 |
}; |
| 27 |
|
| 28 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 29 |
pub struct EnvironmentBuilderImpl { |
| 30 |
builder: lmdb::EnvironmentBuilder, |
| 31 |
env_path_type: EnvironmentPathType, |
| 32 |
env_lock_type: EnvironmentLockType, |
| 33 |
env_db_type: EnvironmentDefaultDbType, |
| 34 |
make_dir_if_needed: bool, |
| 35 |
} |
| 36 |
|
| 37 |
impl<'b> BackendEnvironmentBuilder<'b> for EnvironmentBuilderImpl { |
| 38 |
type Environment = EnvironmentImpl; |
| 39 |
type Error = ErrorImpl; |
| 40 |
type Flags = EnvironmentFlagsImpl; |
| 41 |
|
| 42 |
fn new() -> EnvironmentBuilderImpl { |
| 43 |
EnvironmentBuilderImpl { |
| 44 |
builder: lmdb::Environment::new(), |
| 45 |
env_path_type: EnvironmentPathType::SubDir, |
| 46 |
env_lock_type: EnvironmentLockType::Lockfile, |
| 47 |
env_db_type: EnvironmentDefaultDbType::SingleDatabase, |
| 48 |
make_dir_if_needed: false, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
fn set_flags<T>(&mut self, flags: T) -> &mut Self |
| 53 |
where |
| 54 |
T: Into<Self::Flags>, |
| 55 |
{ |
| 56 |
let flags = flags.into(); |
| 57 |
if flags.0 == lmdb::EnvironmentFlags::NO_SUB_DIR { |
| 58 |
self.env_path_type = EnvironmentPathType::NoSubDir; |
| 59 |
} |
| 60 |
if flags.0 == lmdb::EnvironmentFlags::NO_LOCK { |
| 61 |
self.env_lock_type = EnvironmentLockType::NoLockfile; |
| 62 |
} |
| 63 |
self.builder.set_flags(flags.0); |
| 64 |
self |
| 65 |
} |
| 66 |
|
| 67 |
fn set_max_readers(&mut self, max_readers: u32) -> &mut Self { |
| 68 |
self.builder.set_max_readers(max_readers); |
| 69 |
self |
| 70 |
} |
| 71 |
|
| 72 |
fn set_max_dbs(&mut self, max_dbs: u32) -> &mut Self { |
| 73 |
if max_dbs > 0 { |
| 74 |
self.env_db_type = EnvironmentDefaultDbType::MultipleNamedDatabases |
| 75 |
} |
| 76 |
self.builder.set_max_dbs(max_dbs); |
| 77 |
self |
| 78 |
} |
| 79 |
|
| 80 |
fn set_map_size(&mut self, size: usize) -> &mut Self { |
| 81 |
self.builder.set_map_size(size); |
| 82 |
self |
| 83 |
} |
| 84 |
|
| 85 |
fn set_make_dir_if_needed(&mut self, make_dir_if_needed: bool) -> &mut Self { |
| 86 |
self.make_dir_if_needed = make_dir_if_needed; |
| 87 |
self |
| 88 |
} |
| 89 |
|
| 90 |
/// **UNIMPLEMENTED.** Will panic at runtime. |
| 91 |
fn set_corruption_recovery_strategy(&mut self, _strategy: RecoveryStrategy) -> &mut Self { |
| 92 |
// Unfortunately, when opening a database, LMDB doesn't handle all the ways it could have |
| 93 |
// been corrupted. Prefer using the `SafeMode` backend if this is important. |
| 94 |
unimplemented!(); |
| 95 |
} |
| 96 |
|
| 97 |
fn open(&self, path: &Path) -> Result<Self::Environment, Self::Error> { |
| 98 |
match self.env_path_type { |
| 99 |
EnvironmentPathType::NoSubDir => { |
| 100 |
if !path.is_file() { |
| 101 |
return Err(ErrorImpl::UnsuitableEnvironmentPath(path.into())); |
| 102 |
} |
| 103 |
} |
| 104 |
EnvironmentPathType::SubDir => { |
| 105 |
if !path.is_dir() { |
| 106 |
if !self.make_dir_if_needed { |
| 107 |
return Err(ErrorImpl::UnsuitableEnvironmentPath(path.into())); |
| 108 |
} |
| 109 |
fs::create_dir_all(path)?; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
self.builder |
| 115 |
.open(path) |
| 116 |
.map_err(ErrorImpl::LmdbError) |
| 117 |
.and_then(|lmdbenv| { |
| 118 |
EnvironmentImpl::new( |
| 119 |
path, |
| 120 |
self.env_path_type, |
| 121 |
self.env_lock_type, |
| 122 |
self.env_db_type, |
| 123 |
lmdbenv, |
| 124 |
) |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 130 |
pub enum EnvironmentPathType { |
| 131 |
SubDir, |
| 132 |
NoSubDir, |
| 133 |
} |
| 134 |
|
| 135 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 136 |
pub enum EnvironmentLockType { |
| 137 |
Lockfile, |
| 138 |
NoLockfile, |
| 139 |
} |
| 140 |
|
| 141 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 142 |
pub enum EnvironmentDefaultDbType { |
| 143 |
SingleDatabase, |
| 144 |
MultipleNamedDatabases, |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Debug)] |
| 148 |
pub struct EnvironmentImpl { |
| 149 |
path: PathBuf, |
| 150 |
env_path_type: EnvironmentPathType, |
| 151 |
env_lock_type: EnvironmentLockType, |
| 152 |
env_db_type: EnvironmentDefaultDbType, |
| 153 |
lmdbenv: lmdb::Environment, |
| 154 |
} |
| 155 |
|
| 156 |
impl EnvironmentImpl { |
| 157 |
pub(crate) fn new( |
| 158 |
path: &Path, |
| 159 |
env_path_type: EnvironmentPathType, |
| 160 |
env_lock_type: EnvironmentLockType, |
| 161 |
env_db_type: EnvironmentDefaultDbType, |
| 162 |
lmdbenv: lmdb::Environment, |
| 163 |
) -> Result<EnvironmentImpl, ErrorImpl> { |
| 164 |
Ok(EnvironmentImpl { |
| 165 |
path: path.to_path_buf(), |
| 166 |
env_path_type, |
| 167 |
env_lock_type, |
| 168 |
env_db_type, |
| 169 |
lmdbenv, |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
impl<'e> BackendEnvironment<'e> for EnvironmentImpl { |
| 175 |
type Database = DatabaseImpl; |
| 176 |
type Error = ErrorImpl; |
| 177 |
type Flags = DatabaseFlagsImpl; |
| 178 |
type Info = InfoImpl; |
| 179 |
type RoTransaction = RoTransactionImpl<'e>; |
| 180 |
type RwTransaction = RwTransactionImpl<'e>; |
| 181 |
type Stat = StatImpl; |
| 182 |
|
| 183 |
fn get_dbs(&self) -> Result<Vec<Option<String>>, Self::Error> { |
| 184 |
if self.env_db_type == EnvironmentDefaultDbType::SingleDatabase { |
| 185 |
return Ok(vec![None]); |
| 186 |
} |
| 187 |
let db = self |
| 188 |
.lmdbenv |
| 189 |
.open_db(None) |
| 190 |
.map(DatabaseImpl) |
| 191 |
.map_err(ErrorImpl::LmdbError)?; |
| 192 |
let reader = self.begin_ro_txn()?; |
| 193 |
let cursor = reader.open_ro_cursor(&db)?; |
| 194 |
let mut iter = cursor.into_iter(); |
| 195 |
let mut store = vec![]; |
| 196 |
while let Some(result) = iter.next() { |
| 197 |
let (key, _) = result?; |
| 198 |
let name = String::from_utf8(key.to_owned()) |
| 199 |
.map_err(|_| ErrorImpl::LmdbError(lmdb::Error::Corrupted))?; |
| 200 |
store.push(Some(name)); |
| 201 |
} |
| 202 |
Ok(store) |
| 203 |
} |
| 204 |
|
| 205 |
fn open_db(&self, name: Option<&str>) -> Result<Self::Database, Self::Error> { |
| 206 |
self.lmdbenv |
| 207 |
.open_db(name) |
| 208 |
.map(DatabaseImpl) |
| 209 |
.map_err(ErrorImpl::LmdbError) |
| 210 |
} |
| 211 |
|
| 212 |
fn create_db( |
| 213 |
&self, |
| 214 |
name: Option<&str>, |
| 215 |
flags: Self::Flags, |
| 216 |
) -> Result<Self::Database, Self::Error> { |
| 217 |
self.lmdbenv |
| 218 |
.create_db(name, flags.0) |
| 219 |
.map(DatabaseImpl) |
| 220 |
.map_err(ErrorImpl::LmdbError) |
| 221 |
} |
| 222 |
|
| 223 |
fn begin_ro_txn(&'e self) -> Result<Self::RoTransaction, Self::Error> { |
| 224 |
self.lmdbenv |
| 225 |
.begin_ro_txn() |
| 226 |
.map(RoTransactionImpl) |
| 227 |
.map_err(ErrorImpl::LmdbError) |
| 228 |
} |
| 229 |
|
| 230 |
fn begin_rw_txn(&'e self) -> Result<Self::RwTransaction, Self::Error> { |
| 231 |
self.lmdbenv |
| 232 |
.begin_rw_txn() |
| 233 |
.map(RwTransactionImpl) |
| 234 |
.map_err(ErrorImpl::LmdbError) |
| 235 |
} |
| 236 |
|
| 237 |
fn sync(&self, force: bool) -> Result<(), Self::Error> { |
| 238 |
self.lmdbenv.sync(force).map_err(ErrorImpl::LmdbError) |
| 239 |
} |
| 240 |
|
| 241 |
fn stat(&self) -> Result<Self::Stat, Self::Error> { |
| 242 |
self.lmdbenv |
| 243 |
.stat() |
| 244 |
.map(StatImpl) |
| 245 |
.map_err(ErrorImpl::LmdbError) |
| 246 |
} |
| 247 |
|
| 248 |
fn info(&self) -> Result<Self::Info, Self::Error> { |
| 249 |
self.lmdbenv |
| 250 |
.info() |
| 251 |
.map(InfoImpl) |
| 252 |
.map_err(ErrorImpl::LmdbError) |
| 253 |
} |
| 254 |
|
| 255 |
fn freelist(&self) -> Result<usize, Self::Error> { |
| 256 |
self.lmdbenv.freelist().map_err(ErrorImpl::LmdbError) |
| 257 |
} |
| 258 |
|
| 259 |
fn load_ratio(&self) -> Result<Option<f32>, Self::Error> { |
| 260 |
let stat = self.stat()?; |
| 261 |
let info = self.info()?; |
| 262 |
let freelist = self.freelist()?; |
| 263 |
|
| 264 |
let last_pgno = info.last_pgno() + 1; // pgno is 0 based. |
| 265 |
let total_pgs = info.map_size() / stat.page_size(); |
| 266 |
if freelist > last_pgno { |
| 267 |
return Err(ErrorImpl::LmdbError(LmdbError::Corrupted)); |
| 268 |
} |
| 269 |
let used_pgs = last_pgno - freelist; |
| 270 |
Ok(Some(used_pgs as f32 / total_pgs as f32)) |
| 271 |
} |
| 272 |
|
| 273 |
fn set_map_size(&self, size: usize) -> Result<(), Self::Error> { |
| 274 |
self.lmdbenv |
| 275 |
.set_map_size(size) |
| 276 |
.map_err(ErrorImpl::LmdbError) |
| 277 |
} |
| 278 |
|
| 279 |
fn get_files_on_disk(&self) -> Vec<PathBuf> { |
| 280 |
let mut store = vec![]; |
| 281 |
|
| 282 |
if self.env_path_type == EnvironmentPathType::NoSubDir { |
| 283 |
// The option NO_SUB_DIR could change the default directory layout; therefore this should |
| 284 |
// probably return the path used to create environment, along with the custom lockfile |
| 285 |
// when available. |
| 286 |
unimplemented!(); |
| 287 |
} |
| 288 |
|
| 289 |
let mut db_filename = self.path.clone(); |
| 290 |
db_filename.push("data.mdb"); |
| 291 |
store.push(db_filename); |
| 292 |
|
| 293 |
if self.env_lock_type == EnvironmentLockType::Lockfile { |
| 294 |
let mut lock_filename = self.path.clone(); |
| 295 |
lock_filename.push("lock.mdb"); |
| 296 |
store.push(lock_filename); |
| 297 |
} |
| 298 |
|
| 299 |
store |
| 300 |
} |
| 301 |
} |