Debian Codegraph @

src/mdb/lmdb_error.rs

raw
1 use std::error::Error as StdError;
2 use std::ffi::CStr;
3 use std::os::raw::c_char;
4 use std::{fmt, str};
5
6 use libc::c_int;
7 #[cfg(master3)]
8 use lmdb_master3_sys as ffi;
9 #[cfg(not(master3))]
10 use lmdb_master_sys as ffi;
11
12 /// An LMDB error kind.
13 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
14 pub enum Error {
15 /// A key/data pair already exists.
16 ///
17 /// May be also returned by append functions. Data passed
18 /// doesn't respect the database ordering.
19 KeyExist,
20 /// A key/data pair was not found (EOF).
21 NotFound,
22 /// Requested page not found - this usually indicates corruption.
23 PageNotFound,
24 /// Located page was wrong type.
25 Corrupted,
26 /// Update of meta page failed or environment had fatal error.
27 Panic,
28 /// Environment version mismatch.
29 VersionMismatch,
30 /// File is not a valid LMDB file.
31 Invalid,
32 /// Environment mapsize reached.
33 MapFull,
34 /// Environment maxdbs reached.
35 DbsFull,
36 /// Environment maxreaders reached.
37 ReadersFull,
38 /// Too many TLS keys in use - Windows only.
39 TlsFull,
40 /// Txn has too many dirty pages.
41 TxnFull,
42 /// Cursor stack too deep - internal error.
43 CursorFull,
44 /// Page has not enough space - internal error.
45 PageFull,
46 /// Database contents grew beyond environment mapsize.
47 MapResized,
48 /// Operation and DB incompatible, or DB type changed. This can mean:
49 /// - The operation expects an MDB_DUPSORT / MDB_DUPFIXED database.
50 /// - Opening a named DB when the unnamed DB has MDB_DUPSORT / MDB_INTEGERKEY.
51 /// - Accessing a data record as a database, or vice versa.
52 /// - The database was dropped and recreated with different flags.
53 Incompatible,
54 /// Invalid reuse of reader locktable slot.
55 BadRslot,
56 /// Transaction cannot recover - it must be aborted.
57 BadTxn,
58 /// Unsupported size of key/DB name/data, or wrong DUP_FIXED size.
59 ///
60 /// Common causes of this error:
61 /// - You tried to store a zero-length key
62 /// - You tried to store a key longer than the max allowed key (511 bytes by default)
63 /// - You are using [DUP_SORT](crate::DatabaseFlags::DUP_SORT) and trying to store a
64 /// value longer than the max allowed key size (511 bytes by default)
65 ///
66 /// In the last two cases you can enable the `longer-keys` feature to increase the max allowed key size.
67 BadValSize,
68 /// The specified DBI was changed unexpectedly.
69 BadDbi,
70 /// Unexpected problem - transaction should abort.
71 Problem,
72 /// Page checksum incorrect.
73 #[cfg(master3)]
74 BadChecksum,
75 /// Encryption/decryption failed.
76 #[cfg(master3)]
77 CryptoFail,
78 /// Environment encryption mismatch.
79 #[cfg(master3)]
80 EnvEncryption,
81 /// Other error.
82 Other(c_int),
83 }
84
85 impl Error {
86 /// Returns `true` if the given error is [`Error::NotFound`].
87 pub fn not_found(&self) -> bool {
88 *self == Error::NotFound
89 }
90
91 /// Converts a raw error code to an `Error`.
92 pub fn from_err_code(err_code: c_int) -> Error {
93 match err_code {
94 ffi::MDB_KEYEXIST => Error::KeyExist,
95 ffi::MDB_NOTFOUND => Error::NotFound,
96 ffi::MDB_PAGE_NOTFOUND => Error::PageNotFound,
97 ffi::MDB_CORRUPTED => Error::Corrupted,
98 ffi::MDB_PANIC => Error::Panic,
99 ffi::MDB_VERSION_MISMATCH => Error::VersionMismatch,
100 ffi::MDB_INVALID => Error::Invalid,
101 ffi::MDB_MAP_FULL => Error::MapFull,
102 ffi::MDB_DBS_FULL => Error::DbsFull,
103 ffi::MDB_READERS_FULL => Error::ReadersFull,
104 ffi::MDB_TLS_FULL => Error::TlsFull,
105 ffi::MDB_TXN_FULL => Error::TxnFull,
106 ffi::MDB_CURSOR_FULL => Error::CursorFull,
107 ffi::MDB_PAGE_FULL => Error::PageFull,
108 ffi::MDB_MAP_RESIZED => Error::MapResized,
109 ffi::MDB_INCOMPATIBLE => Error::Incompatible,
110 ffi::MDB_BAD_RSLOT => Error::BadRslot,
111 ffi::MDB_BAD_TXN => Error::BadTxn,
112 ffi::MDB_BAD_VALSIZE => Error::BadValSize,
113 ffi::MDB_BAD_DBI => Error::BadDbi,
114 ffi::MDB_PROBLEM => Error::Problem,
115 #[cfg(master3)]
116 ffi::MDB_BAD_CHECKSUM => Error::BadChecksum,
117 #[cfg(master3)]
118 ffi::MDB_CRYPTO_FAIL => Error::CryptoFail,
119 #[cfg(master3)]
120 ffi::MDB_ENV_ENCRYPTION => Error::EnvEncryption,
121 other => Error::Other(other),
122 }
123 }
124
125 /// Converts an `Error` to the raw error code.
126 #[allow(clippy::trivially_copy_pass_by_ref)]
127 pub fn to_err_code(&self) -> c_int {
128 match *self {
129 Error::KeyExist => ffi::MDB_KEYEXIST,
130 Error::NotFound => ffi::MDB_NOTFOUND,
131 Error::PageNotFound => ffi::MDB_PAGE_NOTFOUND,
132 Error::Corrupted => ffi::MDB_CORRUPTED,
133 Error::Panic => ffi::MDB_PANIC,
134 Error::VersionMismatch => ffi::MDB_VERSION_MISMATCH,
135 Error::Invalid => ffi::MDB_INVALID,
136 Error::MapFull => ffi::MDB_MAP_FULL,
137 Error::DbsFull => ffi::MDB_DBS_FULL,
138 Error::ReadersFull => ffi::MDB_READERS_FULL,
139 Error::TlsFull => ffi::MDB_TLS_FULL,
140 Error::TxnFull => ffi::MDB_TXN_FULL,
141 Error::CursorFull => ffi::MDB_CURSOR_FULL,
142 Error::PageFull => ffi::MDB_PAGE_FULL,
143 Error::MapResized => ffi::MDB_MAP_RESIZED,
144 Error::Incompatible => ffi::MDB_INCOMPATIBLE,
145 Error::BadRslot => ffi::MDB_BAD_RSLOT,
146 Error::BadTxn => ffi::MDB_BAD_TXN,
147 Error::BadValSize => ffi::MDB_BAD_VALSIZE,
148 Error::BadDbi => ffi::MDB_BAD_DBI,
149 Error::Problem => ffi::MDB_PROBLEM,
150 #[cfg(master3)]
151 Error::BadChecksum => ffi::MDB_BAD_CHECKSUM,
152 #[cfg(master3)]
153 Error::CryptoFail => ffi::MDB_CRYPTO_FAIL,
154 #[cfg(master3)]
155 Error::EnvEncryption => ffi::MDB_ENV_ENCRYPTION,
156 Error::Other(err_code) => err_code,
157 }
158 }
159 }
160
161 impl fmt::Display for Error {
162 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
163 let description = unsafe {
164 // This is safe since the error messages returned from mdb_strerror are static.
165 let err: *const c_char = ffi::mdb_strerror(self.to_err_code()) as *const c_char;
166 str::from_utf8_unchecked(CStr::from_ptr(err).to_bytes())
167 };
168
169 fmt.write_str(description)
170 }
171 }
172
173 impl StdError for Error {}
174
175 pub fn mdb_result(err_code: c_int) -> Result<(), Error> {
176 if err_code == ffi::MDB_SUCCESS {
177 Ok(())
178 } else {
179 Err(Error::from_err_code(err_code))
180 }
181 }
182
183 #[cfg(test)]
184 mod test {
185 use super::*;
186
187 #[test]
188 fn test_description() {
189 assert_eq!("Permission denied", Error::from_err_code(13).to_string());
190 assert_eq!("MDB_NOTFOUND: No matching key/data pair found", Error::NotFound.to_string());
191 }
192 }