Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FileExists and DeleteFile to Env #174

Merged
merged 1 commit into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete it via Env? Why not just use fs::remove_file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because file can be any kind of types, for example memory sst file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a link to the explanation of memory SST file? Does it have a path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In RocksDB, Env is an interface to abstract some platform-dependent APIs.
For example, I can provide a MemEnv, which maintains a virtual file system in memory.
Then I can create a SstFileWriter with the MemEnv to generate SST file in memory.

Ref: https://github.com/facebook/rocksdb/blob/master/env/mock_env.h

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information.

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());
}