Renaming DBName to DBPath and implementing AsRef<Path> on DBPath...

master
Jordan Terrell 6 years ago
parent fc7ed1e20b
commit 0a1eff1833
  1. 28
      tests/test_column_family.rs
  2. 10
      tests/test_iterator.rs
  3. 6
      tests/test_multithreaded.rs
  4. 22
      tests/test_raw_iterator.rs
  5. 6
      tests/test_rocksdb_options.rs
  6. 6
      tests/test_slice_transform.rs
  7. 25
      tests/util/mod.rs

@ -16,18 +16,18 @@ extern crate rocksdb;
mod util; mod util;
use rocksdb::{DB, MergeOperands, Options, ColumnFamilyDescriptor}; use rocksdb::{DB, MergeOperands, Options, ColumnFamilyDescriptor};
use util::DBName; use util::DBPath;
#[test] #[test]
pub fn test_column_family() { pub fn test_column_family() {
let n = DBName::new("_rust_rocksdb_cftest"); let n = DBPath::new("_rust_rocksdb_cftest");
// should be able to create column families // should be able to create column families
{ {
let mut opts = Options::default(); let mut opts = Options::default();
opts.create_if_missing(true); opts.create_if_missing(true);
opts.set_merge_operator("test operator", test_provided_merge, None); opts.set_merge_operator("test operator", test_provided_merge, None);
let mut db = DB::open(&opts, &n.name).unwrap(); let mut db = DB::open(&opts, &n).unwrap();
let opts = Options::default(); let opts = Options::default();
match db.create_cf("cf1", &opts) { match db.create_cf("cf1", &opts) {
Ok(_db) => println!("cf1 created successfully"), Ok(_db) => println!("cf1 created successfully"),
@ -41,7 +41,7 @@ pub fn test_column_family() {
{ {
let mut opts = Options::default(); let mut opts = Options::default();
opts.set_merge_operator("test operator", test_provided_merge, None); opts.set_merge_operator("test operator", test_provided_merge, None);
match DB::open(&opts, &n.name) { match DB::open(&opts, &n) {
Ok(_db) => { Ok(_db) => {
panic!("should not have opened DB successfully without \ panic!("should not have opened DB successfully without \
specifying column specifying column
@ -59,7 +59,7 @@ pub fn test_column_family() {
{ {
let mut opts = Options::default(); let mut opts = Options::default();
opts.set_merge_operator("test operator", test_provided_merge, None); opts.set_merge_operator("test operator", test_provided_merge, None);
match DB::open_cf(&opts, &n.name, &["cf1"]) { match DB::open_cf(&opts, &n, &["cf1"]) {
Ok(_db) => println!("successfully opened db with column family"), Ok(_db) => println!("successfully opened db with column family"),
Err(e) => panic!("failed to open db with column family: {}", e), Err(e) => panic!("failed to open db with column family: {}", e),
} }
@ -68,7 +68,7 @@ pub fn test_column_family() {
// should be able to list a cf // should be able to list a cf
{ {
let opts = Options::default(); let opts = Options::default();
let vec = DB::list_cf(&opts, &n.name); let vec = DB::list_cf(&opts, &n);
match vec { match vec {
Ok(vec) => assert_eq!(vec, vec!["default", "cf1"]), Ok(vec) => assert_eq!(vec, vec!["default", "cf1"]),
Err(e) => panic!("failed to drop column family: {}", e), Err(e) => panic!("failed to drop column family: {}", e),
@ -83,7 +83,7 @@ pub fn test_column_family() {
} }
// should b able to drop a cf // should b able to drop a cf
{ {
let mut db = DB::open_cf(&Options::default(), &n.name, &["cf1"]).unwrap(); let mut db = DB::open_cf(&Options::default(), &n, &["cf1"]).unwrap();
match db.drop_cf("cf1") { match db.drop_cf("cf1") {
Ok(_) => println!("cf1 successfully dropped."), Ok(_) => println!("cf1 successfully dropped."),
Err(e) => panic!("failed to drop column family: {}", e), Err(e) => panic!("failed to drop column family: {}", e),
@ -93,7 +93,7 @@ pub fn test_column_family() {
#[test] #[test]
fn test_create_missing_column_family() { fn test_create_missing_column_family() {
let n = DBName::new("_rust_rocksdb_missing_cftest"); let n = DBPath::new("_rust_rocksdb_missing_cftest");
// should be able to create new column families when opening a new database // should be able to create new column families when opening a new database
{ {
@ -101,7 +101,7 @@ fn test_create_missing_column_family() {
opts.create_if_missing(true); opts.create_if_missing(true);
opts.create_missing_column_families(true); opts.create_missing_column_families(true);
match DB::open_cf(&opts, &n.name, &["cf1"]) { match DB::open_cf(&opts, &n, &["cf1"]) {
Ok(_db) => println!("successfully created new column family"), Ok(_db) => println!("successfully created new column family"),
Err(e) => panic!("failed to create new column family: {}", e), Err(e) => panic!("failed to create new column family: {}", e),
} }
@ -111,12 +111,12 @@ fn test_create_missing_column_family() {
#[test] #[test]
#[ignore] #[ignore]
fn test_merge_operator() { fn test_merge_operator() {
let n = DBName::new("_rust_rocksdb_cftest_merge"); let n = DBPath::new("_rust_rocksdb_cftest_merge");
// TODO should be able to write, read, merge, batch, and iterate over a cf // TODO should be able to write, read, merge, batch, and iterate over a cf
{ {
let mut opts = Options::default(); let mut opts = Options::default();
opts.set_merge_operator("test operator", test_provided_merge, None); opts.set_merge_operator("test operator", test_provided_merge, None);
let db = match DB::open_cf(&opts, &n.name, &["cf1"]) { let db = match DB::open_cf(&opts, &n, &["cf1"]) {
Ok(db) => { Ok(db) => {
println!("successfully opened db with column family"); println!("successfully opened db with column family");
db db
@ -178,7 +178,7 @@ fn test_provided_merge(_: &[u8],
#[test] #[test]
pub fn test_column_family_with_options() { pub fn test_column_family_with_options() {
let n = DBName::new("_rust_rocksdb_cf_with_optionstest"); let n = DBPath::new("_rust_rocksdb_cf_with_optionstest");
{ {
let mut cfopts = Options::default(); let mut cfopts = Options::default();
cfopts.set_max_write_buffer_number(16); cfopts.set_max_write_buffer_number(16);
@ -189,7 +189,7 @@ pub fn test_column_family_with_options() {
opts.create_missing_column_families(true); opts.create_missing_column_families(true);
let cfs = vec![cf_descriptor]; let cfs = vec![cf_descriptor];
match DB::open_cf_descriptors(&opts, &n.name, cfs) { match DB::open_cf_descriptors(&opts, &n, cfs) {
Ok(_db) => println!("created db with column family descriptors succesfully"), Ok(_db) => println!("created db with column family descriptors succesfully"),
Err(e) => { Err(e) => {
panic!("could not create new database with column family descriptors: {}", e); panic!("could not create new database with column family descriptors: {}", e);
@ -205,7 +205,7 @@ pub fn test_column_family_with_options() {
let opts = Options::default(); let opts = Options::default();
let cfs = vec![cf_descriptor]; let cfs = vec![cf_descriptor];
match DB::open_cf_descriptors(&opts, &n.name, cfs) { match DB::open_cf_descriptors(&opts, &n, cfs) {
Ok(_db) => println!("succesfully re-opened database with column family descriptors"), Ok(_db) => println!("succesfully re-opened database with column family descriptors"),
Err(e) => { Err(e) => {
panic!("unable to re-open database with column family descriptors: {}", e); panic!("unable to re-open database with column family descriptors: {}", e);

@ -16,7 +16,7 @@ extern crate rocksdb;
mod util; mod util;
use rocksdb::{DB, Direction, IteratorMode, MemtableFactory, Options}; use rocksdb::{DB, Direction, IteratorMode, MemtableFactory, Options};
use util::DBName; use util::DBPath;
fn cba(input: &Box<[u8]>) -> Box<[u8]> { fn cba(input: &Box<[u8]>) -> Box<[u8]> {
input.iter().cloned().collect::<Vec<_>>().into_boxed_slice() input.iter().cloned().collect::<Vec<_>>().into_boxed_slice()
@ -24,7 +24,7 @@ fn cba(input: &Box<[u8]>) -> Box<[u8]> {
#[test] #[test]
pub fn test_iterator() { pub fn test_iterator() {
let n = DBName::new("_rust_rocksdb_iteratortest"); let n = DBPath::new("_rust_rocksdb_iteratortest");
{ {
let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice(); let k1: Box<[u8]> = b"k1".to_vec().into_boxed_slice();
let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice(); let k2: Box<[u8]> = b"k2".to_vec().into_boxed_slice();
@ -34,7 +34,7 @@ pub fn test_iterator() {
let v2: Box<[u8]> = b"v2222".to_vec().into_boxed_slice(); let v2: Box<[u8]> = b"v2222".to_vec().into_boxed_slice();
let v3: Box<[u8]> = b"v3333".to_vec().into_boxed_slice(); let v3: Box<[u8]> = b"v3333".to_vec().into_boxed_slice();
let v4: Box<[u8]> = b"v4444".to_vec().into_boxed_slice(); let v4: Box<[u8]> = b"v4444".to_vec().into_boxed_slice();
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
let p = db.put(&*k1, &*v1); let p = db.put(&*k1, &*v1);
assert!(p.is_ok()); assert!(p.is_ok());
let p = db.put(&*k2, &*v2); let p = db.put(&*k2, &*v2);
@ -161,7 +161,7 @@ fn key(k: &[u8]) -> Box<[u8]> { k.to_vec().into_boxed_slice() }
#[test] #[test]
pub fn test_prefix_iterator() { pub fn test_prefix_iterator() {
let n = DBName::new("_rust_rocksdb_prefixiteratortest"); let n = DBPath::new("_rust_rocksdb_prefixiteratortest");
{ {
let a1: Box<[u8]> = key(b"aaa1"); let a1: Box<[u8]> = key(b"aaa1");
let a2: Box<[u8]> = key(b"aaa2"); let a2: Box<[u8]> = key(b"aaa2");
@ -174,7 +174,7 @@ pub fn test_prefix_iterator() {
opts.create_if_missing(true); opts.create_if_missing(true);
opts.set_prefix_extractor(prefix_extractor); opts.set_prefix_extractor(prefix_extractor);
let db = DB::open(&opts, &n.name).unwrap(); let db = DB::open(&opts, &n).unwrap();
assert!(db.put(&*a1, &*a1).is_ok()); assert!(db.put(&*a1, &*a1).is_ok());
assert!(db.put(&*a2, &*a2).is_ok()); assert!(db.put(&*a2, &*a2).is_ok());

@ -18,16 +18,16 @@ mod util;
use rocksdb::DB; use rocksdb::DB;
use std::thread; use std::thread;
use std::sync::Arc; use std::sync::Arc;
use util::DBName; use util::DBPath;
const N: usize = 100_000; const N: usize = 100_000;
#[test] #[test]
pub fn test_multithreaded() { pub fn test_multithreaded() {
let n = DBName::new("_rust_rocksdb_multithreadtest"); let n = DBPath::new("_rust_rocksdb_multithreadtest");
{ {
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
let db = Arc::new(db); let db = Arc::new(db);
db.put(b"key", b"value1").unwrap(); db.put(b"key", b"value1").unwrap();

@ -16,13 +16,13 @@ extern crate rocksdb;
mod util; mod util;
use rocksdb::DB; use rocksdb::DB;
use util::DBName; use util::DBPath;
#[test] #[test]
pub fn test_forwards_iteration() { pub fn test_forwards_iteration() {
let n = DBName::new("forwards_iteration"); let n = DBPath::new("forwards_iteration");
{ {
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
db.put(b"k1", b"v1").unwrap(); db.put(b"k1", b"v1").unwrap();
db.put(b"k2", b"v2").unwrap(); db.put(b"k2", b"v2").unwrap();
db.put(b"k3", b"v3").unwrap(); db.put(b"k3", b"v3").unwrap();
@ -54,9 +54,9 @@ pub fn test_forwards_iteration() {
#[test] #[test]
pub fn test_seek_last() { pub fn test_seek_last() {
let n = DBName::new("backwards_iteration"); let n = DBPath::new("backwards_iteration");
{ {
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
db.put(b"k1", b"v1").unwrap(); db.put(b"k1", b"v1").unwrap();
db.put(b"k2", b"v2").unwrap(); db.put(b"k2", b"v2").unwrap();
db.put(b"k3", b"v3").unwrap(); db.put(b"k3", b"v3").unwrap();
@ -88,9 +88,9 @@ pub fn test_seek_last() {
#[test] #[test]
pub fn test_seek() { pub fn test_seek() {
let n = DBName::new("seek"); let n = DBPath::new("seek");
{ {
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
db.put(b"k1", b"v1").unwrap(); db.put(b"k1", b"v1").unwrap();
db.put(b"k2", b"v2").unwrap(); db.put(b"k2", b"v2").unwrap();
db.put(b"k4", b"v4").unwrap(); db.put(b"k4", b"v4").unwrap();
@ -114,9 +114,9 @@ pub fn test_seek() {
#[test] #[test]
pub fn test_seek_to_nonexistant() { pub fn test_seek_to_nonexistant() {
let n = DBName::new("seek_to_nonexistant"); let n = DBPath::new("seek_to_nonexistant");
{ {
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
db.put(b"k1", b"v1").unwrap(); db.put(b"k1", b"v1").unwrap();
db.put(b"k3", b"v3").unwrap(); db.put(b"k3", b"v3").unwrap();
db.put(b"k4", b"v4").unwrap(); db.put(b"k4", b"v4").unwrap();
@ -132,9 +132,9 @@ pub fn test_seek_to_nonexistant() {
#[test] #[test]
pub fn test_seek_for_prev() { pub fn test_seek_for_prev() {
let n = DBName::new("seek_for_prev"); let n = DBPath::new("seek_for_prev");
{ {
let db = DB::open_default(&n.name).unwrap(); let db = DB::open_default(&n).unwrap();
db.put(b"k1", b"v1").unwrap(); db.put(b"k1", b"v1").unwrap();
db.put(b"k2", b"v2").unwrap(); db.put(b"k2", b"v2").unwrap();
db.put(b"k4", b"v4").unwrap(); db.put(b"k4", b"v4").unwrap();

@ -16,15 +16,15 @@ extern crate rocksdb;
mod util; mod util;
use rocksdb::{DB, Options}; use rocksdb::{DB, Options};
use util::DBName; use util::DBPath;
#[test] #[test]
fn test_set_num_levels() { fn test_set_num_levels() {
let n = DBName::new("_rust_rocksdb_test_set_num_levels"); let n = DBPath::new("_rust_rocksdb_test_set_num_levels");
{ {
let mut opts = Options::default(); let mut opts = Options::default();
opts.create_if_missing(true); opts.create_if_missing(true);
opts.set_num_levels(2); opts.set_num_levels(2);
let _db = DB::open(&opts, &n.name).unwrap(); let _db = DB::open(&opts, n).unwrap();
} }
} }

@ -2,11 +2,11 @@ extern crate rocksdb;
mod util; mod util;
use rocksdb::{DB, Options, SliceTransform}; use rocksdb::{DB, Options, SliceTransform};
use util::DBName; use util::DBPath;
#[test] #[test]
pub fn test_slice_transform() { pub fn test_slice_transform() {
let n = DBName::new("_rust_rocksdb_slicetransform_test"); let n = DBPath::new("_rust_rocksdb_slicetransform_test");
{ {
let a1: Box<[u8]> = key(b"aaa1"); let a1: Box<[u8]> = key(b"aaa1");
let a2: Box<[u8]> = key(b"aaa2"); let a2: Box<[u8]> = key(b"aaa2");
@ -23,7 +23,7 @@ pub fn test_slice_transform() {
opts.create_if_missing(true); opts.create_if_missing(true);
opts.set_prefix_extractor(prefix_extractor); opts.set_prefix_extractor(prefix_extractor);
let db = DB::open(&opts, &n.name).unwrap(); let db = DB::open(&opts, &n).unwrap();
assert!(db.put(&*a1, &*a1).is_ok()); assert!(db.put(&*a1, &*a1).is_ok());
assert!(db.put(&*a2, &*a2).is_ok()); assert!(db.put(&*a2, &*a2).is_ok());

@ -1,34 +1,41 @@
extern crate rocksdb; extern crate rocksdb;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::path::{PathBuf, Path};
use rocksdb::{DB, Options}; use rocksdb::{DB, Options};
// Ensures that DB::Destroy is called for this database when DBName is dropped. // Ensures that DB::Destroy is called for this database when DBPath is dropped.
pub struct DBName { pub struct DBPath {
pub name: String, path: PathBuf
} }
impl DBName { impl DBPath {
// Suffixes the given `prefix` with a timestamp to ensure that subsequent test runs don't reuse // Suffixes the given `prefix` with a timestamp to ensure that subsequent test runs don't reuse
// an old database in case of panics prior to Drop being called. // an old database in case of panics prior to Drop being called.
pub fn new(prefix: &str) -> DBName { pub fn new(prefix: &str) -> DBPath {
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let name = format!( let path = format!(
"{}.{}.{}", "{}.{}.{}",
prefix, prefix,
current_time.as_secs(), current_time.as_secs(),
current_time.subsec_nanos() current_time.subsec_nanos()
); );
DBName { name } DBPath { path: PathBuf::from(path) }
} }
} }
impl Drop for DBName { impl Drop for DBPath {
fn drop(&mut self) { fn drop(&mut self) {
let opts = Options::default(); let opts = Options::default();
DB::destroy(&opts, &self.name).unwrap(); DB::destroy(&opts, &self.path).unwrap();
}
}
impl AsRef<Path> for DBPath {
fn as_ref(&self) -> &Path {
&self.path
} }
} }

Loading…
Cancel
Save