Skip to content

Commit

Permalink
Add FileExists and DeleteFile to Env (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
huachaohuang committed Dec 11, 2017
1 parent 20d3999 commit e6492cb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions librocksdb_sys/crocksdb/c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2877,6 +2877,14 @@ void crocksdb_env_join_all_threads(crocksdb_env_t* env) {
env->rep->WaitForJoin();
}

void crocksdb_env_file_exists(crocksdb_env_t* env, const char* path, char** errptr) {
SaveError(errptr, env->rep->FileExists(path));
}

void crocksdb_env_delete_file(crocksdb_env_t* env, const char* path, char** errptr) {
SaveError(errptr, env->rep->DeleteFile(path));
}

void crocksdb_env_destroy(crocksdb_env_t* env) {
if (!env->is_default) delete env->rep;
delete env;
Expand Down
4 changes: 4 additions & 0 deletions librocksdb_sys/crocksdb/crocksdb/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,10 @@ extern C_ROCKSDB_LIBRARY_API void
crocksdb_env_set_high_priority_background_threads(crocksdb_env_t* env, int n);
extern C_ROCKSDB_LIBRARY_API void crocksdb_env_join_all_threads(
crocksdb_env_t* env);
extern C_ROCKSDB_LIBRARY_API void crocksdb_env_file_exists(
crocksdb_env_t* env, const char* path, char** errptr);
extern C_ROCKSDB_LIBRARY_API void crocksdb_env_delete_file(
crocksdb_env_t* env, const char* path, char** errptr);
extern C_ROCKSDB_LIBRARY_API void crocksdb_env_destroy(crocksdb_env_t*);

extern C_ROCKSDB_LIBRARY_API crocksdb_envoptions_t* crocksdb_envoptions_create();
Expand Down
2 changes: 2 additions & 0 deletions librocksdb_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,8 @@ extern "C" {
// Env
pub fn crocksdb_create_default_env() -> *mut DBEnv;
pub fn crocksdb_create_mem_env() -> *mut DBEnv;
pub fn crocksdb_env_file_exists(env: *mut DBEnv, path: *const c_char, err: *mut *mut c_char);
pub fn crocksdb_env_delete_file(env: *mut DBEnv, path: *const c_char, err: *mut *mut c_char);
pub fn crocksdb_env_destroy(env: *mut DBEnv);

// EnvOptions
Expand Down
16 changes: 16 additions & 0 deletions src/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,22 @@ impl Env {
Ok(SequentialFile::new(file))
}
}

pub fn file_exists(&self, path: &str) -> Result<(), String> {
unsafe {
let file_path = CString::new(path).unwrap();
ffi_try!(crocksdb_env_file_exists(self.inner, file_path.as_ptr()));
Ok(())
}
}

pub fn delete_file(&self, path: &str) -> Result<(), String> {
unsafe {
let file_path = CString::new(path).unwrap();
ffi_try!(crocksdb_env_delete_file(self.inner, file_path.as_ptr()));
Ok(())
}
}
}

impl Drop for Env {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_ingest_external_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,8 @@ fn test_mem_sst_file_writer() {
(b"k3", Some(b"v3")),
],
);

assert!(env.file_exists(mem_sst_str).is_ok());
assert!(env.delete_file(mem_sst_str).is_ok());
assert!(env.file_exists(mem_sst_str).is_err());
}

0 comments on commit e6492cb

Please sign in to comment.