1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#[cfg(test)]
use std::ffi::{CStr, CString};
#[cfg(test)]
use std::mem;
#[cfg(test)]
use std::iter;
#[cfg(test)]
use std::ptr;

#[allow(non_upper_case_globals)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
mod c;

pub use c::*;


pub fn version() -> String {
    unsafe {
        format!("{}.{}.{}",
                rocks_version_major(),
                rocks_version_minor(),
                rocks_version_patch())
    }
}

#[test]
fn test_db_list_cf_names() {
    unsafe {
        let opt = c::rocks_options_create();
        let mut status = mem::uninitialized::<c::rocks_status_t>();
        let dbname = CString::new("./data.test").unwrap();

        let mut lencfs = 0_usize;
        let cnames = c::rocks_db_list_column_families(opt, dbname.as_ptr(), &mut lencfs, &mut status);
        if status.code != 0 {
            println!("status => {:?}", CStr::from_ptr(status.state));
        }
        assert!(status.code == 0);

        println!("len => {:?}", lencfs);
        let mut cfnames: Vec<String> = vec![];
        for i in 0..lencfs {
            cfnames.push(CStr::from_ptr(*cnames.offset(i as isize)).to_str().unwrap().to_owned());
        }
        println!("cf => {:?}", cfnames);
        assert!(cfnames.contains(&"default".to_owned()));



        c::rocks_db_list_column_families_destroy(cnames, lencfs);
        c::rocks_options_destroy(opt);
    }
}

#[test]
fn test_smoke() {
    unsafe {
        // let opt = c::rocks_options_create();
        let cfopt = c::rocks_cfoptions_create();
        let dbopt = c::rocks_dboptions_create();

        c::rocks_cfoptions_optimize_for_point_lookup(cfopt, 512); 
        // 
        let mut status = mem::uninitialized::<c::rocks_status_t>();
        let dbname = CString::new("./data.test.default").unwrap();

        c::rocks_dboptions_set_create_if_missing(dbopt, 1);

        let opt = c::rocks_options_create_from_db_cf_options(dbopt, cfopt);
        println!("opt => {:?}", opt);
        assert!(!opt.is_null());

        assert!(!opt.is_null());


        println!("going to open db");
        let db = c::rocks_db_open(opt, dbname.as_ptr(), &mut status);
        println!("db => {:?}", db);
        println!("code => {:?}", status.code);

        assert!(status.code == 0, "status => {:?}", CStr::from_ptr(status.state));

        let wopt = c::rocks_writeoptions_create();

        for i in 0..1000 {
            let key = format!("test3-key-{}", i);
            let val = format!("rocksdb-value-{}", i*10);
            let value: String = iter::repeat(val)
                .take(100)
                .collect::<Vec<_>>()
                .concat();
            c::rocks_db_put(db, wopt,
                            key.as_bytes().as_ptr() as _, key.len(),
                            value.as_bytes().as_ptr() as _, value.len(),
                            &mut status);
            if status.code != 0 {
                println!("status => {:?}", CStr::from_ptr(status.state));
            }
        }

        c::rocks_db_close(db);
        c::rocks_writeoptions_destroy(wopt);
        c::rocks_options_destroy(opt);
    }
}