Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Added encoding for paths #7

Merged
merged 2 commits into from
Jun 22, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ path = "test/test.rs"
[dependencies]
libc = "0.2"
rocksdb-sys = { path = "rocksdb-sys", version = "0.3.0" }
local-encoding = "0.2.0"
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
//
extern crate rocksdb_sys;
extern crate local_encoding;

Choose a reason for hiding this comment

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

style: newline between links and pub exports


pub use rocksdb_sys::rocksdb_ffi as rocksdb_ffi;
pub use rocksdb_ffi::{DBCompactionStyle, DBComparator, new_bloom_filter};
pub use rocksdb::{DB, DBIterator, DBVector, Direction, IteratorMode, Writable,
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ mod tests {
opts.set_compaction_style(DBUniversalCompaction);
opts.set_max_background_compactions(4);
opts.set_max_background_flushes(4);
opts.set_filter_deletes(false);
blockopts.set_block_size(524288);
opts.set_block_based_table_factory(blockopts);
opts.set_disable_auto_compactions(true);
Expand Down
74 changes: 70 additions & 4 deletions src/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use self::libc::size_t;
use rocksdb_ffi::{self, DBCFHandle, error_message};
use rocksdb_options::{Options, WriteOptions};

use local_encoding::{Encoding, Encoder};

pub struct DB {
inner: rocksdb_ffi::DBInstance,
cfs: BTreeMap<String, DBCFHandle>,
Expand Down Expand Up @@ -276,7 +278,16 @@ impl DB {
if cfs.len() != cf_opts.len() {
return Err(format!("Mismatching number of CF options"));
}
let cpath = match CString::new(path.as_bytes()) {
let encoded_path = match Encoding::ANSI.to_bytes(path) {

Choose a reason for hiding this comment

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

idiomatic rust tip: you can use map_err and the ? operator to condense this to
let encoded_path = Encoding::ANSI.to_bytes(path).map_err(|_| "error msg".to_string())?

Ok(c) => c,
Err(_) => {
return Err("Failed to encode path to codepage when opening \
rocksdb"
.to_string())
}
};

let cpath = match CString::new(encoded_path) {
Ok(c) => c,
Err(_) => {
return Err("Failed to convert path to CString when opening \
Expand Down Expand Up @@ -378,7 +389,24 @@ impl DB {
}

pub fn destroy(opts: &Options, path: &str) -> Result<(), String> {
let cpath = CString::new(path.as_bytes()).unwrap();
let encoded_path = match Encoding::ANSI.to_bytes(path) {
Ok(c) => c,
Err(_) => {
return Err("Failed to encode path to codepage when destroying \
rocksdb"
.to_string())
}
};

let cpath = match CString::new(encoded_path) {
Ok(c) => c,
Err(_) => {
return Err("Failed to convert path to CString when destroying \
rocksdb"
.to_string())
}
};

let cpath_ptr = cpath.as_ptr();

let mut err: *const i8 = 0 as *const i8;
Expand All @@ -395,7 +423,24 @@ impl DB {
}

pub fn repair(opts: &Options, path: &str) -> Result<(), String> {
let cpath = CString::new(path.as_bytes()).unwrap();
let encoded_path = match Encoding::ANSI.to_bytes(path) {
Ok(c) => c,
Err(_) => {
return Err("Failed to encode path to codepage when repairing \
rocksdb"
.to_string())
}
};

let cpath = match CString::new(encoded_path) {
Ok(c) => c,
Err(_) => {
return Err("Failed to convert path to CString when repairing \
rocksdb"
.to_string())
}
};

let cpath_ptr = cpath.as_ptr();

let mut err: *const i8 = 0 as *const i8;
Expand Down Expand Up @@ -518,7 +563,16 @@ impl DB {
name: &str,
opts: &Options)
-> Result<Column, String> {
let cname = match CString::new(name.as_bytes()) {
let encoded_name = match Encoding::ANSI.to_bytes(name) {
Ok(c) => c,
Err(_) => {
return Err("Failed to encode path to codepage when opening \
rocksdb"
.to_string())
}
};

let cname = match CString::new(encoded_name) {
Ok(c) => c,
Err(_) => {
return Err("Failed to convert path to CString when opening \
Expand Down Expand Up @@ -1024,6 +1078,18 @@ fn iterator_test() {
assert!(DB::destroy(&opts, path).is_ok());
}

#[test]
fn non_unicode_path_test() {
let path = "путь_не_юникод/_rust_rocksdb_unicode_test";
{
let db = DB::open_default(path).unwrap();
assert!(db.put(b"my key", b"my value").is_ok());
assert!(db.delete(b"my key").is_ok());
}
let opts = Options::new();
assert!(DB::destroy(&opts, path).is_ok());
}

#[test]
fn snapshot_test() {
let path = "_rust_rocksdb_snapshottest";
Expand Down