lmdb/
lib.rs

1//! Idiomatic and safe APIs for interacting with the
2//! [Lightning Memory-mapped Database (LMDB)](https://symas.com/lmdb).
3
4#![deny(missing_docs)]
5#![doc(html_root_url = "https://docs.rs/lmdb-rkv/0.14.0")]
6
7extern crate byteorder;
8extern crate libc;
9extern crate lmdb_sys as ffi;
10
11#[cfg(test)]
12extern crate tempdir;
13#[macro_use]
14extern crate bitflags;
15
16pub use cursor::{
17    Cursor,
18    Iter,
19    IterDup,
20    RoCursor,
21    RwCursor,
22};
23pub use database::Database;
24pub use environment::{
25    Environment,
26    EnvironmentBuilder,
27    Info,
28    Stat,
29};
30pub use error::{
31    Error,
32    Result,
33};
34pub use flags::*;
35pub use transaction::{
36    InactiveTransaction,
37    RoTransaction,
38    RwTransaction,
39    Transaction,
40};
41
42macro_rules! lmdb_try {
43    ($expr:expr) => {{
44        match $expr {
45            ::ffi::MDB_SUCCESS => (),
46            err_code => return Err(::Error::from_err_code(err_code)),
47        }
48    }};
49}
50
51macro_rules! lmdb_try_with_cleanup {
52    ($expr:expr, $cleanup:expr) => {{
53        match $expr {
54            ::ffi::MDB_SUCCESS => (),
55            err_code => {
56                let _ = $cleanup;
57                return Err(::Error::from_err_code(err_code));
58            },
59        }
60    }};
61}
62
63mod cursor;
64mod database;
65mod environment;
66mod error;
67mod flags;
68mod transaction;
69
70#[cfg(test)]
71mod test_utils {
72
73    use byteorder::{
74        ByteOrder,
75        LittleEndian,
76    };
77    use tempdir::TempDir;
78
79    use super::*;
80
81    /// Regression test for https://github.com/danburkert/lmdb-rs/issues/21.
82    /// This test reliably segfaults when run against lmbdb compiled with opt level -O3 and newer
83    /// GCC compilers.
84    #[test]
85    fn issue_21_regression() {
86        const HEIGHT_KEY: [u8; 1] = [0];
87
88        let dir = TempDir::new("test").unwrap();
89
90        let env = {
91            let mut builder = Environment::new();
92            builder.set_max_dbs(2);
93            builder.set_map_size(1_000_000);
94            builder.open(dir.path()).expect("open lmdb env")
95        };
96        let index = env.create_db(None, DatabaseFlags::DUP_SORT).expect("open index db");
97
98        for height in 0..1000 {
99            let mut value = [0u8; 8];
100            LittleEndian::write_u64(&mut value, height);
101            let mut tx = env.begin_rw_txn().expect("begin_rw_txn");
102            tx.put(index, &HEIGHT_KEY, &value, WriteFlags::empty()).expect("tx.put");
103            tx.commit().expect("tx.commit")
104        }
105    }
106}