Skip to content

Commit

Permalink
allow keys with internal nul chars in rust bindings (#220)
Browse files Browse the repository at this point in the history
* return empty match instead of NulError panic

* bump version

* no newline for formatting

* remove version bumnp

* rust fmt

* modify to allow internal nulchars

* cpp formatting

* fix cpplint errors

* no need for keyvi_bytes intermediate
  • Loading branch information
gmossessian authored May 13, 2021
1 parent b29d0b3 commit b6dcf45
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
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

0 comments on commit b6dcf45

Please sign in to comment.