From c94bc59d7e70991ace9b4371381b277d2f2c42e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 5 Nov 2020 13:34:15 +0100 Subject: [PATCH] Introduce a function to transform an obk into a JSON --- src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index edeee2563..173d96737 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,9 @@ use std::borrow::Cow; use std::collections::HashMap; use std::hash::BuildHasherDefault; +use anyhow::Context; use fxhash::{FxHasher32, FxHasher64}; +use serde_json::{Map, Value}; pub use self::criterion::{Criterion, default_criteria}; pub use self::fields_ids_map::FieldsIdsMap; @@ -38,3 +40,21 @@ pub type Attribute = u32; pub type Position = u32; type MergeFn = for<'a> fn(&[u8], &[Cow<'a, [u8]>]) -> anyhow::Result>; + +/// Transform a raw obkv store into a JSON Object. +pub fn obkv_to_json( + displayed_fields: &[u8], + fields_ids_map: &FieldsIdsMap, + obkv: obkv::KvReader, +) -> anyhow::Result> +{ + displayed_fields.iter() + .copied() + .flat_map(|id| obkv.get(id).map(|value| (id, value))) + .map(|(id, value)| { + let name = fields_ids_map.name(id).context("unknown obkv field id")?; + let value = serde_json::from_slice(value)?; + Ok((name.to_owned(), value)) + }) + .collect() +}