From 03709910fd2da62ce261a6c7b4a7b4e6677eafba Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 18 Sep 2019 11:41:29 +0200 Subject: [PATCH] feat: add typed index custom settings for common uses --- .../database/index/custom_settings_index.rs | 77 +++++++++++++++++++ meilidb-data/src/database/index/mod.rs | 2 +- meilidb-data/src/database/mod.rs | 2 +- meilidb-data/src/lib.rs | 2 +- 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/meilidb-data/src/database/index/custom_settings_index.rs b/meilidb-data/src/database/index/custom_settings_index.rs index 0fd0aade9..3404eff5b 100644 --- a/meilidb-data/src/database/index/custom_settings_index.rs +++ b/meilidb-data/src/database/index/custom_settings_index.rs @@ -1,4 +1,25 @@ +use serde::de::DeserializeOwned; +use serde::{Serialize, Deserialize}; +use std::collections::{HashMap, HashSet}; use std::ops::Deref; +use super::Error; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum RankingOrdering { + Asc, + Dsc +} + +pub type StopWords = HashSet; +pub type RankingOrder = Vec; +pub type DistinctField = String; +pub type RankingRules = HashMap; + +const STOP_WORDS_KEY: &str = "stop-words"; +const RANKING_ORDER_KEY: &str = "ranking-order"; +const DISTINCT_FIELD_KEY: &str = "distinct-field"; +const RANKING_RULES_KEY: &str = "ranking-rules"; #[derive(Clone)] pub struct CustomSettingsIndex(pub(crate) crate::CfTree); @@ -10,3 +31,59 @@ impl Deref for CustomSettingsIndex { &self.0 } } + +impl CustomSettingsIndex { + fn get(&self, key: K) -> Result, Error> + where K: AsRef<[u8]>, + T: DeserializeOwned, + { + let setting = self.0.get(key)?; + let raw = match setting { + Some(raw) => raw, + None => return Ok(None) + }; + + Ok(Some(bincode::deserialize(&raw)?)) + } + + fn set(&self, key: K, data: &T) -> Result<(), Error> + where K: AsRef<[u8]>, + T: Serialize, + { + let raw = bincode::serialize(data)?; + self.0.insert(key, &raw)?; + Ok(()) + } + + pub fn get_stop_words(&self) -> Result, Error> { + self.get(STOP_WORDS_KEY) + } + + pub fn get_ranking_order(&self) -> Result, Error> { + self.get(RANKING_ORDER_KEY) + } + + pub fn get_distinct_field(&self) -> Result, Error> { + self.get(DISTINCT_FIELD_KEY) + } + + pub fn get_ranking_rules(&self) -> Result, Error> { + self.get(RANKING_RULES_KEY) + } + + pub fn set_stop_words(&self, value: &StopWords) -> Result<(), Error> { + self.set(STOP_WORDS_KEY, value) + } + + pub fn set_ranking_order(&self, value: &RankingOrder) -> Result<(), Error> { + self.set(RANKING_ORDER_KEY, value) + } + + pub fn set_distinct_field(&self, value: &DistinctField) -> Result<(), Error> { + self.set(DISTINCT_FIELD_KEY, value) + } + + pub fn set_ranking_rules(&self, value: &RankingRules) -> Result<(), Error> { + self.set(RANKING_RULES_KEY, value) + } +} diff --git a/meilidb-data/src/database/index/mod.rs b/meilidb-data/src/database/index/mod.rs index 1bcfaec95..5ecfedafa 100644 --- a/meilidb-data/src/database/index/mod.rs +++ b/meilidb-data/src/database/index/mod.rs @@ -17,7 +17,7 @@ use crate::CfTree; use crate::ranked_map::RankedMap; use crate::serde::{Deserializer, DeserializerError}; -pub use self::custom_settings_index::CustomSettingsIndex; +pub use self::custom_settings_index::{CustomSettingsIndex, RankingOrdering, StopWords, RankingOrder, DistinctField, RankingRules}; pub use self::common_index::CommonIndex; use self::docs_words_index::DocsWordsIndex; use self::documents_index::DocumentsIndex; diff --git a/meilidb-data/src/database/mod.rs b/meilidb-data/src/database/mod.rs index 1975dd3e8..b1391ac49 100644 --- a/meilidb-data/src/database/mod.rs +++ b/meilidb-data/src/database/mod.rs @@ -12,7 +12,7 @@ mod update; use crate::CfTree; pub use self::error::Error; -pub use self::index::{Index, CustomSettingsIndex, CommonIndex}; +pub use self::index::{Index, CustomSettingsIndex, CommonIndex, RankingOrdering, StopWords, RankingOrder, DistinctField, RankingRules}; pub use self::update::DocumentsAddition; pub use self::update::DocumentsDeletion; diff --git a/meilidb-data/src/lib.rs b/meilidb-data/src/lib.rs index 9124dcc77..168311755 100644 --- a/meilidb-data/src/lib.rs +++ b/meilidb-data/src/lib.rs @@ -7,7 +7,7 @@ mod ranked_map; mod serde; pub use self::cf_tree::{CfTree, CfIter}; -pub use self::database::{Database, Index, CustomSettingsIndex}; +pub use self::database::{Database, Index, CustomSettingsIndex, RankingOrdering, StopWords, RankingOrder, DistinctField, RankingRules}; pub use self::number::Number; pub use self::ranked_map::RankedMap; pub use self::serde::{compute_document_id, extract_document_id, value_to_string};