| 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 |
fmt::{Debug, Display}, |
| 13 |
path::{Path, PathBuf}, |
| 14 |
}; |
| 15 |
|
| 16 |
use crate::{ |
| 17 |
backend::common::{DatabaseFlags, EnvironmentFlags, RecoveryStrategy, WriteFlags}, |
| 18 |
error::StoreError, |
| 19 |
}; |
| 20 |
|
| 21 |
pub trait BackendError: Debug + Display + Into<StoreError> {} |
| 22 |
|
| 23 |
pub trait BackendDatabase: Debug + Eq + PartialEq + Copy + Clone {} |
| 24 |
|
| 25 |
pub trait BackendFlags: Debug + Eq + PartialEq + Copy + Clone + Default { |
| 26 |
fn empty() -> Self; |
| 27 |
} |
| 28 |
|
| 29 |
pub trait BackendEnvironmentFlags: BackendFlags { |
| 30 |
fn set(&mut self, flag: EnvironmentFlags, value: bool); |
| 31 |
} |
| 32 |
|
| 33 |
pub trait BackendDatabaseFlags: BackendFlags { |
| 34 |
fn set(&mut self, flag: DatabaseFlags, value: bool); |
| 35 |
} |
| 36 |
|
| 37 |
pub trait BackendWriteFlags: BackendFlags { |
| 38 |
fn set(&mut self, flag: WriteFlags, value: bool); |
| 39 |
} |
| 40 |
|
| 41 |
pub trait BackendStat { |
| 42 |
fn page_size(&self) -> usize; |
| 43 |
|
| 44 |
fn depth(&self) -> usize; |
| 45 |
|
| 46 |
fn branch_pages(&self) -> usize; |
| 47 |
|
| 48 |
fn leaf_pages(&self) -> usize; |
| 49 |
|
| 50 |
fn overflow_pages(&self) -> usize; |
| 51 |
|
| 52 |
fn entries(&self) -> usize; |
| 53 |
} |
| 54 |
|
| 55 |
pub trait BackendInfo { |
| 56 |
fn map_size(&self) -> usize; |
| 57 |
|
| 58 |
fn last_pgno(&self) -> usize; |
| 59 |
|
| 60 |
fn last_txnid(&self) -> usize; |
| 61 |
|
| 62 |
fn max_readers(&self) -> usize; |
| 63 |
|
| 64 |
fn num_readers(&self) -> usize; |
| 65 |
} |
| 66 |
|
| 67 |
pub trait BackendEnvironmentBuilder<'b>: Debug + Eq + PartialEq + Copy + Clone { |
| 68 |
type Error: BackendError; |
| 69 |
type Environment: BackendEnvironment<'b>; |
| 70 |
type Flags: BackendEnvironmentFlags; |
| 71 |
|
| 72 |
fn new() -> Self; |
| 73 |
|
| 74 |
fn set_flags<T>(&mut self, flags: T) -> &mut Self |
| 75 |
where |
| 76 |
T: Into<Self::Flags>; |
| 77 |
|
| 78 |
fn set_max_dbs(&mut self, max_dbs: u32) -> &mut Self; |
| 79 |
|
| 80 |
fn set_max_readers(&mut self, max_readers: u32) -> &mut Self; |
| 81 |
|
| 82 |
fn set_map_size(&mut self, size: usize) -> &mut Self; |
| 83 |
|
| 84 |
fn set_make_dir_if_needed(&mut self, make_dir_if_needed: bool) -> &mut Self; |
| 85 |
|
| 86 |
/// Set the corruption recovery strategy. See [`RecoveryStrategy`] for details. |
| 87 |
fn set_corruption_recovery_strategy(&mut self, strategy: RecoveryStrategy) -> &mut Self; |
| 88 |
|
| 89 |
fn open(&self, path: &Path) -> Result<Self::Environment, Self::Error>; |
| 90 |
} |
| 91 |
|
| 92 |
pub trait BackendEnvironment<'e>: Debug { |
| 93 |
type Error: BackendError; |
| 94 |
type Database: BackendDatabase; |
| 95 |
type Flags: BackendDatabaseFlags; |
| 96 |
type Stat: BackendStat; |
| 97 |
type Info: BackendInfo; |
| 98 |
type RoTransaction: BackendRoCursorTransaction<'e, Database = Self::Database>; |
| 99 |
type RwTransaction: BackendRwCursorTransaction<'e, Database = Self::Database>; |
| 100 |
|
| 101 |
fn get_dbs(&self) -> Result<Vec<Option<String>>, Self::Error>; |
| 102 |
|
| 103 |
fn open_db(&self, name: Option<&str>) -> Result<Self::Database, Self::Error>; |
| 104 |
|
| 105 |
fn create_db( |
| 106 |
&self, |
| 107 |
name: Option<&str>, |
| 108 |
flags: Self::Flags, |
| 109 |
) -> Result<Self::Database, Self::Error>; |
| 110 |
|
| 111 |
fn begin_ro_txn(&'e self) -> Result<Self::RoTransaction, Self::Error>; |
| 112 |
|
| 113 |
fn begin_rw_txn(&'e self) -> Result<Self::RwTransaction, Self::Error>; |
| 114 |
|
| 115 |
fn sync(&self, force: bool) -> Result<(), Self::Error>; |
| 116 |
|
| 117 |
fn stat(&self) -> Result<Self::Stat, Self::Error>; |
| 118 |
|
| 119 |
fn info(&self) -> Result<Self::Info, Self::Error>; |
| 120 |
|
| 121 |
fn freelist(&self) -> Result<usize, Self::Error>; |
| 122 |
|
| 123 |
fn load_ratio(&self) -> Result<Option<f32>, Self::Error>; |
| 124 |
|
| 125 |
fn set_map_size(&self, size: usize) -> Result<(), Self::Error>; |
| 126 |
|
| 127 |
fn get_files_on_disk(&self) -> Vec<PathBuf>; |
| 128 |
} |
| 129 |
|
| 130 |
pub trait BackendRoTransaction: Debug { |
| 131 |
type Error: BackendError; |
| 132 |
type Database: BackendDatabase; |
| 133 |
|
| 134 |
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error>; |
| 135 |
|
| 136 |
fn abort(self); |
| 137 |
} |
| 138 |
|
| 139 |
pub trait BackendRwTransaction: Debug { |
| 140 |
type Error: BackendError; |
| 141 |
type Database: BackendDatabase; |
| 142 |
type Flags: BackendWriteFlags; |
| 143 |
|
| 144 |
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error>; |
| 145 |
|
| 146 |
fn put( |
| 147 |
&mut self, |
| 148 |
db: &Self::Database, |
| 149 |
key: &[u8], |
| 150 |
value: &[u8], |
| 151 |
flags: Self::Flags, |
| 152 |
) -> Result<(), Self::Error>; |
| 153 |
|
| 154 |
#[cfg(not(feature = "db-dup-sort"))] |
| 155 |
fn del(&mut self, db: &Self::Database, key: &[u8]) -> Result<(), Self::Error>; |
| 156 |
|
| 157 |
#[cfg(feature = "db-dup-sort")] |
| 158 |
fn del( |
| 159 |
&mut self, |
| 160 |
db: &Self::Database, |
| 161 |
key: &[u8], |
| 162 |
value: Option<&[u8]>, |
| 163 |
) -> Result<(), Self::Error>; |
| 164 |
|
| 165 |
fn clear_db(&mut self, db: &Self::Database) -> Result<(), Self::Error>; |
| 166 |
|
| 167 |
fn commit(self) -> Result<(), Self::Error>; |
| 168 |
|
| 169 |
fn abort(self); |
| 170 |
} |
| 171 |
|
| 172 |
pub trait BackendRoCursorTransaction<'t>: BackendRoTransaction { |
| 173 |
type RoCursor: BackendRoCursor<'t>; |
| 174 |
|
| 175 |
fn open_ro_cursor(&'t self, db: &Self::Database) -> Result<Self::RoCursor, Self::Error>; |
| 176 |
} |
| 177 |
|
| 178 |
pub trait BackendRwCursorTransaction<'t>: BackendRwTransaction { |
| 179 |
type RoCursor: BackendRoCursor<'t>; |
| 180 |
|
| 181 |
fn open_ro_cursor(&'t self, db: &Self::Database) -> Result<Self::RoCursor, Self::Error>; |
| 182 |
} |
| 183 |
|
| 184 |
pub trait BackendRoCursor<'c>: Debug { |
| 185 |
type Iter: BackendIter<'c>; |
| 186 |
|
| 187 |
fn into_iter(self) -> Self::Iter; |
| 188 |
|
| 189 |
fn into_iter_from<K>(self, key: K) -> Self::Iter |
| 190 |
where |
| 191 |
K: AsRef<[u8]> + 'c; |
| 192 |
|
| 193 |
fn into_iter_dup_of<K>(self, key: K) -> Self::Iter |
| 194 |
where |
| 195 |
K: AsRef<[u8]> + 'c; |
| 196 |
} |
| 197 |
|
| 198 |
pub trait BackendIter<'i> { |
| 199 |
type Error: BackendError; |
| 200 |
|
| 201 |
#[allow(clippy::type_complexity)] |
| 202 |
fn next(&mut self) -> Option<Result<(&'i [u8], &'i [u8]), Self::Error>>; |
| 203 |
} |