Skip to main content

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/monero-lmdb-rkv/0.1.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::{Cursor, Iter, IterDup, RoCursor, RwCursor};
17pub use database::Database;
18pub use environment::{Environment, EnvironmentBuilder, Info, Stat};
19pub use error::{Error, Result};
20pub use flags::*;
21pub use transaction::{InactiveTransaction, RoTransaction, RwTransaction, Transaction};
22
23macro_rules! lmdb_try {
24    ($expr:expr) => {{
25        match $expr {
26            ::ffi::MDB_SUCCESS => (),
27            err_code => return Err(::Error::from_err_code(err_code)),
28        }
29    }};
30}
31
32macro_rules! lmdb_try_with_cleanup {
33    ($expr:expr, $cleanup:expr) => {{
34        match $expr {
35            ::ffi::MDB_SUCCESS => (),
36            err_code => {
37                let _ = $cleanup;
38                return Err(::Error::from_err_code(err_code));
39            },
40        }
41    }};
42}
43
44mod cursor;
45mod database;
46mod environment;
47mod error;
48mod flags;
49mod transaction;
50
51#[cfg(test)]
52mod test_utils {
53
54    use byteorder::{ByteOrder, LittleEndian};
55    use tempdir::TempDir;
56
57    use super::*;
58
59    /// Regression test for https://github.com/danburkert/lmdb-rs/issues/21.
60    /// This test reliably segfaults when run against lmbdb compiled with opt level -O3 and newer
61    /// GCC compilers.
62    #[test]
63    fn issue_21_regression() {
64        const HEIGHT_KEY: [u8; 1] = [0];
65
66        let dir = TempDir::new("test").unwrap();
67
68        let env = {
69            let mut builder = Environment::new();
70            builder.set_max_dbs(2);
71            builder.set_map_size(1_000_000);
72            builder.open(dir.path()).expect("open lmdb env")
73        };
74        let index = env.create_db(None, DatabaseFlags::DUP_SORT).expect("open index db");
75
76        for height in 0..1000 {
77            let mut value = [0u8; 8];
78            LittleEndian::write_u64(&mut value, height);
79            let mut tx = env.begin_rw_txn().expect("begin_rw_txn");
80            tx.put(index, &HEIGHT_KEY, &value, WriteFlags::empty()).expect("tx.put");
81            tx.commit().expect("tx.commit")
82        }
83    }
84}