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

allow keys with internal nul chars in rust bindings #220

Merged
merged 9 commits into from
May 13, 2021
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
4 changes: 2 additions & 2 deletions keyvi/bin/keyvi_c/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ char* keyvi_dictionary_get_statistics(const keyvi_dictionary* dict) {
return std_2_c_string(dict->obj_->GetStatistics());
}

keyvi_match* keyvi_dictionary_get(const keyvi_dictionary* dict, const char* key) {
return new keyvi_match(dict->obj_->operator[](key));
keyvi_match* keyvi_dictionary_get(const keyvi_dictionary* dict, const char* key, const size_t key_len) {
return new keyvi_match(dict->obj_->operator[](std::string(key, key_len)));
}

keyvi_match_iterator* keyvi_dictionary_get_all_items(const keyvi_dictionary* dict) {
Expand Down
2 changes: 1 addition & 1 deletion keyvi/include/keyvi/c_api/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ size_t keyvi_dictionary_get_size(const struct keyvi_dictionary*);

char* keyvi_dictionary_get_statistics(const struct keyvi_dictionary*);

struct keyvi_match* keyvi_dictionary_get(const struct keyvi_dictionary*, const char*);
struct keyvi_match* keyvi_dictionary_get(const struct keyvi_dictionary*, const char*, const size_t);

struct keyvi_match_iterator* keyvi_dictionary_get_all_items(const struct keyvi_dictionary*);

Expand Down
5 changes: 3 additions & 2 deletions rust/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ impl Dictionary {
}

pub fn get(&self, key: &str) -> KeyviMatch {
let key_c = CString::new(key).unwrap();
let match_ptr = unsafe { root::keyvi_dictionary_get(self.dict, key_c.as_ptr()) };
let match_ptr = unsafe {
root::keyvi_dictionary_get(self.dict, key.as_ptr() as *const i8, key.len() as u64)
};
KeyviMatch::new(match_ptr)
}

Expand Down
Binary file modified rust/test_data/test.kv
Binary file not shown.
21 changes: 14 additions & 7 deletions rust/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod tests {
#[test]
fn dictionary_size() {
let dict = dictionary::Dictionary::new("test_data/test.kv").unwrap();
assert_eq!(dict.size(), 3);
assert_eq!(dict.size(), 5);
}

#[test]
Expand Down Expand Up @@ -77,18 +77,25 @@ mod tests {

#[test]
fn match_msgpacked_value_non_existing_key() {
let m = dictionary::Dictionary::new("test_data/test.kv")
.unwrap()
.get("non-existing-key");
let d = dictionary::Dictionary::new("test_data/test.kv").unwrap();
let m = d.get("non-existing-key");
assert!(m.get_value_as_string().is_empty());

let m = d.get("non-existing-key-with-\0-in-middle");
assert!(m.get_value_as_string().is_empty());
}

#[test]
fn match_value() {
let m = dictionary::Dictionary::new("test_data/test.kv")
.unwrap()
.get("a");
let d = dictionary::Dictionary::new("test_data/test.kv").unwrap();
let m = d.get("a");
assert_eq!(m.get_value_as_string(), "[12,13]");

let m = d.get("d\0");
assert_eq!(m.get_value_as_string(), "[1,2]");

let m = d.get("e\0f");
assert_eq!(m.get_value_as_string(), "[3,4]");
}

#[test]
Expand Down