diff --git a/Cargo.toml b/Cargo.toml index 7d19433..3c0d8da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ bitflags = "*" [dev-dependencies] rand = "*" +tempdir = "*" diff --git a/src/cursor.rs b/src/cursor.rs index a5db632..3261640 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -290,12 +290,13 @@ impl <'txn> Iterator for IterDup<'txn> { #[cfg(test)] mod test { - use std::{fs, ptr}; + use std::ptr; use test::{Bencher, black_box}; - use ffi::*; + use tempdir::TempDir; use environment::*; + use ffi::*; use flags::*; use super::*; use test_utils::*; @@ -303,7 +304,7 @@ mod test { #[test] fn test_get() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -313,27 +314,27 @@ mod test { txn.put(db, b"key3", b"val3", WriteFlags::empty()).unwrap(); let cursor = txn.open_ro_cursor(db).unwrap(); - assert_eq!((Some(b"key1"), b"val1"), + assert_eq!((Some(&b"key1"[..]), &b"val1"[..]), cursor.get(None, None, MDB_FIRST).unwrap()); - assert_eq!((Some(b"key1"), b"val1"), + assert_eq!((Some(&b"key1"[..]), &b"val1"[..]), cursor.get(None, None, MDB_GET_CURRENT).unwrap()); - assert_eq!((Some(b"key2"), b"val2"), + assert_eq!((Some(&b"key2"[..]), &b"val2"[..]), cursor.get(None, None, MDB_NEXT).unwrap()); - assert_eq!((Some(b"key1"), b"val1"), + assert_eq!((Some(&b"key1"[..]), &b"val1"[..]), cursor.get(None, None, MDB_PREV).unwrap()); - assert_eq!((Some(b"key3"), b"val3"), + assert_eq!((Some(&b"key3"[..]), &b"val3"[..]), cursor.get(None, None, MDB_LAST).unwrap()); - assert_eq!((None, b"val2"), + assert_eq!((None, &b"val2"[..]), cursor.get(Some(b"key2"), None, MDB_SET).unwrap()); - assert_eq!((Some(b"key3"), b"val3"), - cursor.get(Some(b"key3"), None, MDB_SET_KEY).unwrap()); - assert_eq!((Some(b"key3"), b"val3"), - cursor.get(Some(b"key2\0"), None, MDB_SET_RANGE).unwrap()); + assert_eq!((Some(&b"key3"[..]), &b"val3"[..]), + cursor.get(Some(&b"key3"[..]), None, MDB_SET_KEY).unwrap()); + assert_eq!((Some(&b"key3"[..]), &b"val3"[..]), + cursor.get(Some(&b"key2\0"[..]), None, MDB_SET_RANGE).unwrap()); } #[test] fn test_get_dup() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.create_db(None, DUP_SORT).unwrap(); @@ -346,40 +347,40 @@ mod test { txn.put(db, b"key2", b"val3", WriteFlags::empty()).unwrap(); let cursor = txn.open_ro_cursor(db).unwrap(); - assert_eq!((Some(b"key1"), b"val1"), + assert_eq!((Some(&b"key1"[..]), &b"val1"[..]), cursor.get(None, None, MDB_FIRST).unwrap()); - assert_eq!((None, b"val1"), + assert_eq!((None, &b"val1"[..]), cursor.get(None, None, MDB_FIRST_DUP).unwrap()); - assert_eq!((Some(b"key1"), b"val1"), + assert_eq!((Some(&b"key1"[..]), &b"val1"[..]), cursor.get(None, None, MDB_GET_CURRENT).unwrap()); - assert_eq!((Some(b"key2"), b"val1"), + assert_eq!((Some(&b"key2"[..]), &b"val1"[..]), cursor.get(None, None, MDB_NEXT_NODUP).unwrap()); - assert_eq!((Some(b"key2"), b"val2"), + assert_eq!((Some(&b"key2"[..]), &b"val2"[..]), cursor.get(None, None, MDB_NEXT_DUP).unwrap()); - assert_eq!((Some(b"key2"), b"val3"), + assert_eq!((Some(&b"key2"[..]), &b"val3"[..]), cursor.get(None, None, MDB_NEXT_DUP).unwrap()); assert!(cursor.get(None, None, MDB_NEXT_DUP).is_err()); - assert_eq!((Some(b"key2"), b"val2"), + assert_eq!((Some(&b"key2"[..]), &b"val2"[..]), cursor.get(None, None, MDB_PREV_DUP).unwrap()); - assert_eq!((None, b"val3"), + assert_eq!((None, &b"val3"[..]), cursor.get(None, None, MDB_LAST_DUP).unwrap()); - assert_eq!((Some(b"key1"), b"val3"), + assert_eq!((Some(&b"key1"[..]), &b"val3"[..]), cursor.get(None, None, MDB_PREV_NODUP).unwrap()); - assert_eq!((None, b"val1"), - cursor.get(Some(b"key1"), None, MDB_SET).unwrap()); - assert_eq!((Some(b"key2"), b"val1"), - cursor.get(Some(b"key2"), None, MDB_SET_KEY).unwrap()); - assert_eq!((Some(b"key2"), b"val1"), - cursor.get(Some(b"key1\0"), None, MDB_SET_RANGE).unwrap()); - assert_eq!((None, b"val3"), - cursor.get(Some(b"key1"), Some(b"val3"), MDB_GET_BOTH).unwrap()); - assert_eq!((None, b"val1"), - cursor.get(Some(b"key2"), Some(b"val"), MDB_GET_BOTH_RANGE).unwrap()); + assert_eq!((None, &b"val1"[..]), + cursor.get(Some(&b"key1"[..]), None, MDB_SET).unwrap()); + assert_eq!((Some(&b"key2"[..]), &b"val1"[..]), + cursor.get(Some(&b"key2"[..]), None, MDB_SET_KEY).unwrap()); + assert_eq!((Some(&b"key2"[..]), &b"val1"[..]), + cursor.get(Some(&b"key1\0"[..]), None, MDB_SET_RANGE).unwrap()); + assert_eq!((None, &b"val3"[..]), + cursor.get(Some(&b"key1"[..]), Some(&b"val3"[..]), MDB_GET_BOTH).unwrap()); + assert_eq!((None, &b"val1"[..]), + cursor.get(Some(&b"key2"[..]), Some(&b"val"[..]), MDB_GET_BOTH_RANGE).unwrap()); } #[test] fn test_get_dupfixed() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.create_db(None, DUP_SORT | DUP_FIXED).unwrap(); @@ -392,22 +393,22 @@ mod test { txn.put(db, b"key2", b"val6", WriteFlags::empty()).unwrap(); let cursor = txn.open_ro_cursor(db).unwrap(); - assert_eq!((Some(b"key1"), b"val1"), + assert_eq!((Some(&b"key1"[..]), &b"val1"[..]), cursor.get(None, None, MDB_FIRST).unwrap()); - assert_eq!((None, b"val1val2val3"), + assert_eq!((None, &b"val1val2val3"[..]), cursor.get(None, None, MDB_GET_MULTIPLE).unwrap()); assert!(cursor.get(None, None, MDB_NEXT_MULTIPLE).is_err()); } #[test] fn test_iter() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); - let items = vec!((b"key1", b"val1"), - (b"key2", b"val2"), - (b"key3", b"val3")); + let items: Vec<(&[u8], &[u8])> = vec!((b"key1", b"val1"), + (b"key2", b"val2"), + (b"key3", b"val3")); { let mut txn = env.begin_rw_txn().unwrap(); @@ -433,19 +434,19 @@ mod test { #[test] fn test_iter_dup() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.create_db(None, DUP_SORT).unwrap(); - let items = vec!((b"a", b"1"), - (b"a", b"2"), - (b"a", b"3"), - (b"b", b"1"), - (b"b", b"2"), - (b"b", b"3"), - (b"c", b"1"), - (b"c", b"2"), - (b"c", b"3")); + let items: Vec<(&[u8], &[u8])> = vec!((b"a", b"1"), + (b"a", b"2"), + (b"a", b"3"), + (b"b", b"1"), + (b"b", b"2"), + (b"b", b"3"), + (b"c", b"1"), + (b"c", b"2"), + (b"c", b"3")); { let mut txn = env.begin_rw_txn().unwrap(); @@ -480,7 +481,7 @@ mod test { #[test] fn test_put_del() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -491,11 +492,11 @@ mod test { cursor.put(b"key2", b"val2", WriteFlags::empty()).unwrap(); cursor.put(b"key3", b"val3", WriteFlags::empty()).unwrap(); - assert_eq!((Some(b"key3"), b"val3"), + assert_eq!((Some(&b"key3"[..]), &b"val3"[..]), cursor.get(None, None, MDB_GET_CURRENT).unwrap()); cursor.del(WriteFlags::empty()).unwrap(); - assert_eq!((Some(b"key2"), b"val2"), + assert_eq!((Some(&b"key2"[..]), &b"val2"[..]), cursor.get(None, None, MDB_LAST).unwrap()); } diff --git a/src/environment.rs b/src/environment.rs index 65fc79e..368422a 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,6 +1,6 @@ use libc::{c_uint, size_t, mode_t}; use std::ffi::{AsOsStr, CString}; -use std::os::unix::OsStrExt; +use std::os::unix::ffi::OsStrExt; use std::path::Path; use std::ptr; use std::sync::Mutex; @@ -252,7 +252,7 @@ impl EnvironmentBuilder { #[cfg(test)] mod test { - use std::fs; + use tempdir::TempDir; use flags::*; @@ -260,7 +260,7 @@ mod test { #[test] fn test_open() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); // opening non-existent env with read-only should fail assert!(Environment::new().set_flags(READ_ONLY) @@ -278,7 +278,7 @@ mod test { #[test] fn test_begin_txn() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); { // writable environment let env = Environment::new().open(dir.path()).unwrap(); @@ -299,7 +299,7 @@ mod test { #[test] fn test_open_db() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().set_max_dbs(1) .open(dir.path()) .unwrap(); @@ -310,7 +310,7 @@ mod test { #[test] fn test_create_db() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().set_max_dbs(11) .open(dir.path()) .unwrap(); @@ -321,7 +321,7 @@ mod test { #[test] fn test_close_database() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let mut env = Environment::new().set_max_dbs(10) .open(dir.path()) .unwrap(); @@ -333,7 +333,7 @@ mod test { #[test] fn test_sync() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); { let env = Environment::new().open(dir.path()).unwrap(); assert!(env.sync(true).is_ok()); diff --git a/src/lib.rs b/src/lib.rs index e98289d..1948282 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,12 +2,13 @@ //! [Symas Lightning Memory-Mapped Database (LMDB)](http://symas.com/mdb/). #![feature(core, libc, optin_builtin_traits, std_misc, unsafe_destructor)] -#![cfg_attr(test, feature(tempdir, test))] +#![cfg_attr(test, feature(test))] extern crate libc; extern crate "lmdb-sys" as ffi; #[cfg(test)] extern crate rand; +#[cfg(test)] extern crate tempdir; #[cfg(test)] extern crate test; #[macro_use] extern crate bitflags; @@ -60,7 +61,7 @@ mod transaction; #[cfg(test)] mod test_utils { - use std::fs; + use tempdir::TempDir; use super::*; @@ -72,14 +73,14 @@ mod test_utils { format!("data{}", n) } - pub fn setup_bench_db<'a>(num_rows: u32) -> (fs::TempDir, Environment) { - let dir = fs::TempDir::new("test").unwrap(); + pub fn setup_bench_db<'a>(num_rows: u32) -> (TempDir, Environment) { + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); { let db = env.open_db(None).unwrap(); let mut txn = env.begin_rw_txn().unwrap(); - for i in range(0, num_rows) { + for i in 0..num_rows { txn.put(db, get_key(i).as_bytes(), get_data(i).as_bytes(), diff --git a/src/transaction.rs b/src/transaction.rs index f05fdaf..d5b8d9c 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -359,23 +359,24 @@ impl <'env> Transaction<'env> for RwTransaction<'env> { #[cfg(test)] mod test { + use std::ptr; use rand::{Rng, XorShiftRng}; use std::io::Write; use std::sync::{Arc, Barrier, Future}; - use std::{fs, ptr}; use test::{Bencher, black_box}; - use ffi::*; + use tempdir::TempDir; use environment::*; use error::*; + use ffi::*; use flags::*; use super::*; use test_utils::*; #[test] fn test_put_get_del() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -397,7 +398,7 @@ mod test { #[test] fn test_reserve() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -418,7 +419,7 @@ mod test { #[test] fn test_inactive_txn() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -436,7 +437,7 @@ mod test { #[test] fn test_nested_txn() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -456,7 +457,7 @@ mod test { #[test] fn test_clear_db() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().open(dir.path()).unwrap(); let db = env.open_db(None).unwrap(); @@ -479,7 +480,7 @@ mod test { #[test] fn test_drop_db() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Environment::new().set_max_dbs(2) .open(dir.path()).unwrap(); let db = env.create_db(Some("test"), DatabaseFlags::empty()).unwrap(); @@ -500,7 +501,7 @@ mod test { #[test] fn test_concurrent_readers_single_writer() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env: Arc = Arc::new(Environment::new().open(dir.path()).unwrap()); let n = 10usize; // Number of concurrent readers @@ -510,7 +511,7 @@ mod test { let key = b"key"; let val = b"val"; - for _ in range(0, n) { + for _ in 0..n { let reader_env = env.clone(); let reader_barrier = barrier.clone(); @@ -542,7 +543,7 @@ mod test { #[test] fn test_concurrent_writers() { - let dir = fs::TempDir::new("test").unwrap(); + let dir = TempDir::new("test").unwrap(); let env = Arc::new(Environment::new().open(dir.path()).unwrap()); let n = 10usize; // Number of concurrent writers @@ -551,7 +552,7 @@ mod test { let key = "key"; let val = "val"; - for i in range(0, n) { + for i in 0..n { let writer_env = env.clone(); futures.push(Future::spawn(move|| { @@ -570,7 +571,7 @@ mod test { let db = env.open_db(None).unwrap(); let txn = env.begin_ro_txn().unwrap(); - for i in range(0, n) { + for i in 0..n { assert_eq!( format!("{}{}", val, i).as_bytes(), txn.get(db, format!("{}{}", key, i).as_bytes()).unwrap()); @@ -584,8 +585,7 @@ mod test { let db = env.open_db(None).unwrap(); let txn = env.begin_ro_txn().unwrap(); - let mut keys: Vec = range(0, n).map(|n| get_key(n)) - .collect::>(); + let mut keys: Vec = (0..n).map(|n| get_key(n)).collect(); XorShiftRng::new_unseeded().shuffle(keys.as_mut_slice()); b.iter(|| { @@ -604,8 +604,7 @@ mod test { let db = env.open_db(None).unwrap(); let _txn = env.begin_ro_txn().unwrap(); - let mut keys: Vec = range(0, n).map(|n| get_key(n)) - .collect::>(); + let mut keys: Vec = (0..n).map(|n| get_key(n)).collect(); XorShiftRng::new_unseeded().shuffle(keys.as_mut_slice()); let dbi = db.dbi(); @@ -634,8 +633,7 @@ mod test { let (_dir, env) = setup_bench_db(0); let db = env.open_db(None).unwrap(); - let mut items: Vec<(String, String)> = range(0, n).map(|n| (get_key(n), get_data(n))) - .collect::>(); + let mut items: Vec<(String, String)> = (0..n).map(|n| (get_key(n), get_data(n))).collect(); XorShiftRng::new_unseeded().shuffle(items.as_mut_slice()); b.iter(|| { @@ -653,8 +651,7 @@ mod test { let (_dir, _env) = setup_bench_db(0); let db = _env.open_db(None).unwrap(); - let mut items: Vec<(String, String)> = range(0, n).map(|n| (get_key(n), get_data(n))) - .collect::>(); + let mut items: Vec<(String, String)> = (0..n).map(|n| (get_key(n), get_data(n))).collect(); XorShiftRng::new_unseeded().shuffle(items.as_mut_slice()); let dbi = db.dbi();