Commit 9c41fa6b authored by Cholerae Hu's avatar Cholerae Hu Committed by zhangjinpeng1987

SstFileWriter: get the ownership of Options (#82)

parent 425a023c
...@@ -1346,25 +1346,31 @@ impl Drop for BackupEngine { ...@@ -1346,25 +1346,31 @@ impl Drop for BackupEngine {
/// All keys in files generated by SstFileWriter will have sequence number = 0 /// All keys in files generated by SstFileWriter will have sequence number = 0
pub struct SstFileWriter { pub struct SstFileWriter {
inner: *mut crocksdb_ffi::SstFileWriter, inner: *mut crocksdb_ffi::SstFileWriter,
_env_opt: EnvOptions,
_opt: Options,
} }
unsafe impl Send for SstFileWriter {} unsafe impl Send for SstFileWriter {}
impl SstFileWriter { impl SstFileWriter {
pub fn new(env_opt: &EnvOptions, opt: &Options) -> SstFileWriter { pub fn new(env_opt: EnvOptions, opt: Options) -> SstFileWriter {
unsafe { unsafe {
SstFileWriter { SstFileWriter {
inner: crocksdb_ffi::crocksdb_sstfilewriter_create(env_opt.inner, opt.inner), inner: crocksdb_ffi::crocksdb_sstfilewriter_create(env_opt.inner, opt.inner),
_env_opt: env_opt,
_opt: opt,
} }
} }
} }
pub fn new_cf(env_opt: &EnvOptions, opt: &Options, cf: &CFHandle) -> SstFileWriter { pub fn new_cf(env_opt: EnvOptions, opt: Options, cf: &CFHandle) -> SstFileWriter {
unsafe { unsafe {
SstFileWriter { SstFileWriter {
inner: crocksdb_ffi::crocksdb_sstfilewriter_create_cf(env_opt.inner, inner: crocksdb_ffi::crocksdb_sstfilewriter_create_cf(env_opt.inner,
opt.inner, opt.inner,
cf.inner), cf.inner),
_env_opt: env_opt,
_opt: opt,
} }
} }
} }
...@@ -1408,7 +1414,6 @@ impl Drop for SstFileWriter { ...@@ -1408,7 +1414,6 @@ impl Drop for SstFileWriter {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use rocksdb_options::*;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::str; use std::str;
......
...@@ -16,13 +16,13 @@ use rocksdb::*; ...@@ -16,13 +16,13 @@ use rocksdb::*;
use std::fs; use std::fs;
use tempdir::TempDir; use tempdir::TempDir;
fn gen_sst(opt: &Options, cf: Option<&CFHandle>, path: &str, data: &[(&[u8], &[u8])]) { fn gen_sst(opt: Options, cf: Option<&CFHandle>, path: &str, data: &[(&[u8], &[u8])]) {
let _ = fs::remove_file(path); let _ = fs::remove_file(path);
let env_opt = EnvOptions::new(); let env_opt = EnvOptions::new();
let mut writer = if cf.is_some() { let mut writer = if cf.is_some() {
SstFileWriter::new_cf(&env_opt, opt, cf.unwrap()) SstFileWriter::new_cf(env_opt, opt, cf.unwrap())
} else { } else {
SstFileWriter::new(&env_opt, opt) SstFileWriter::new(env_opt, opt)
}; };
writer.open(path).unwrap(); writer.open(path).unwrap();
for &(k, v) in data { for &(k, v) in data {
...@@ -48,7 +48,7 @@ fn test_ingest_external_file() { ...@@ -48,7 +48,7 @@ fn test_ingest_external_file() {
let test_sstfile_str = test_sstfile.to_str().unwrap(); let test_sstfile_str = test_sstfile.to_str().unwrap();
let default_options = db.get_options(); let default_options = db.get_options();
gen_sst(&default_options, gen_sst(default_options,
Some(db.cf_handle("default").unwrap()), Some(db.cf_handle("default").unwrap()),
test_sstfile_str, test_sstfile_str,
&[(b"k1", b"v1"), (b"k2", b"v2")]); &[(b"k1", b"v1"), (b"k2", b"v2")]);
...@@ -60,7 +60,7 @@ fn test_ingest_external_file() { ...@@ -60,7 +60,7 @@ fn test_ingest_external_file() {
assert_eq!(db.get(b"k1").unwrap().unwrap(), b"v1"); assert_eq!(db.get(b"k1").unwrap().unwrap(), b"v1");
assert_eq!(db.get(b"k2").unwrap().unwrap(), b"v2"); assert_eq!(db.get(b"k2").unwrap().unwrap(), b"v2");
gen_sst(&cf_opts, gen_sst(cf_opts,
None, None,
test_sstfile_str, test_sstfile_str,
&[(b"k1", b"v3"), (b"k2", b"v4")]); &[(b"k1", b"v3"), (b"k2", b"v4")]);
...@@ -71,7 +71,8 @@ fn test_ingest_external_file() { ...@@ -71,7 +71,8 @@ fn test_ingest_external_file() {
let snap = db.snapshot(); let snap = db.snapshot();
gen_sst(&default_options, let opt = Options::new();
gen_sst(opt,
None, None,
test_sstfile_str, test_sstfile_str,
&[(b"k2", b"v5"), (b"k3", b"v6")]); &[(b"k2", b"v5"), (b"k3", b"v6")]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment