Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Add all_obkv_to_json function #707

Merged
merged 2 commits into from
Nov 28, 2022
Merged
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
28 changes: 28 additions & 0 deletions milli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ pub fn obkv_to_json(
.collect()
}

/// Transform every field of a raw obkv store into a JSON Object.
pub fn all_obkv_to_json(obkv: obkv::KvReaderU16, fields_ids_map: &FieldsIdsMap) -> Result<Object> {
let all_keys = obkv.iter().map(|(k, _v)| k).collect::<Vec<_>>();
obkv_to_json(all_keys.as_slice(), fields_ids_map, obkv)
}

/// Transform a JSON value into a string that can be indexed.
pub fn json_to_string(value: &Value) -> Option<String> {
fn inner(value: &Value, output: &mut String) -> bool {
Expand Down Expand Up @@ -285,4 +291,26 @@ mod tests {
assert_eq!(0x12345678, absolute_from_relative_position(0x1234, 0x5678));
assert_eq!(0xFFFFFFFF, absolute_from_relative_position(0xFFFF, 0xFFFF));
}

#[test]
fn test_all_obkv_to_json() {
let mut fields_ids_map = FieldsIdsMap::new();
let id1 = fields_ids_map.insert("field1").unwrap();
let id2 = fields_ids_map.insert("field2").unwrap();

let mut writer = obkv::KvWriterU16::memory();
writer.insert(id1, b"1234").unwrap();
writer.insert(id2, b"4321").unwrap();
let contents = writer.into_inner().unwrap();
let obkv = obkv::KvReaderU16::new(&contents);

let expected = json!({
"field1": 1234,
"field2": 4321,
});
let expected = expected.as_object().unwrap();
let actual = all_obkv_to_json(obkv, &fields_ids_map).unwrap();

assert_eq!(&actual, expected);
}
}