From 37578ed74f526014a04d57fef878921310553de7 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Tue, 12 Feb 2019 17:20:47 +0100 Subject: [PATCH] feat: store config into database --- src/database/config.rs | 29 +++++++++++++++++++++++++++++ src/database/mod.rs | 30 ++++++++++++++++++++++++++++++ src/database/view.rs | 7 +++++-- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/database/config.rs diff --git a/src/database/config.rs b/src/database/config.rs new file mode 100644 index 000000000..2150566a8 --- /dev/null +++ b/src/database/config.rs @@ -0,0 +1,29 @@ +use std::collections::{HashSet, HashMap}; + +use serde_derive::{Serialize, Deserialize}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum RankingOrdering { + Asc, + Dsc +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Config { + stop_words: Option>, + ranking_order: Option>, + distinct_field: Option, + ranking_rules: Option>, +} + + +impl Config { + pub(crate) fn default() -> Config { + Config { + stop_words: None, + ranking_order: None, + distinct_field: None, + ranking_rules: None, + } + } +} diff --git a/src/database/mod.rs b/src/database/mod.rs index 5d526dcd7..8a032080e 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -23,6 +23,7 @@ use crate::DocumentId; use self::update::{ReadIndexEvent, ReadRankedMapEvent}; +pub use self::config::Config; pub use self::document_key::{DocumentKey, DocumentKeyAttr}; pub use self::view::{DatabaseView, DocumentIter}; pub use self::update::Update; @@ -31,12 +32,15 @@ pub use self::schema::Schema; pub use self::index::Index; pub use self::number::{Number, ParseNumberError}; + pub type RankedMap = HashMap<(DocumentId, SchemaAttr), Number>; const DATA_INDEX: &[u8] = b"data-index"; const DATA_RANKED_MAP: &[u8] = b"data-ranked-map"; const DATA_SCHEMA: &[u8] = b"data-schema"; +const CONFIG: &[u8] = b"config"; +pub mod config; pub mod schema; pub(crate) mod index; mod number; @@ -104,6 +108,15 @@ where D: Deref, } } +fn retrieve_config(snapshot: &Snapshot) -> Result> +where D: Deref, +{ + match snapshot.get(CONFIG)? { + Some(vector) => Ok(bincode::deserialize(&*vector)?), + None => Ok(Config::default()), + } +} + fn merge_indexes(existing: Option<&[u8]>, operands: &mut MergeOperands) -> Vec { use self::update::ReadIndexEvent::{self, *}; use self::update::WriteIndexEvent; @@ -265,6 +278,17 @@ impl DatabaseIndex { fn view(&self) -> Arc>> { self.view.load() } + + fn update_config(&self, config: Config) -> Result>>, Box>{ + let data = bincode::serialize(&config)?; + self.db.put(CONFIG, &data)?; + + let snapshot = Snapshot::new(self.db.clone()); + let view = Arc::new(DatabaseView::new(snapshot)?); + self.view.set(view.clone()); + + Ok(view) + } } impl Drop for DatabaseIndex { @@ -369,6 +393,12 @@ impl Database { Ok(index_guard.val().view()) } + pub fn update_config(&self, index: &str, config: Config) -> Result>>, Box>{ + let index_guard = self.indexes.get(index).ok_or("Index not found")?; + + Ok(index_guard.val().update_config(config)?) + } + } #[cfg(test)] diff --git a/src/database/view.rs b/src/database/view.rs index 74e4ef002..3cee6db4e 100644 --- a/src/database/view.rs +++ b/src/database/view.rs @@ -7,13 +7,14 @@ use rocksdb::rocksdb_options::{ReadOptions, EnvOptions, ColumnFamilyOptions}; use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey, SstFileWriter}; use serde::de::DeserializeOwned; -use crate::database::{retrieve_data_schema, retrieve_data_index, retrieve_data_ranked_map}; +use crate::database::{retrieve_data_schema, retrieve_data_index, retrieve_data_ranked_map, retrieve_config}; use crate::database::serde::deserializer::Deserializer; use crate::database::{DocumentKey, DocumentKeyAttr}; use crate::rank::{QueryBuilder, FilterFunc}; use crate::database::schema::Schema; use crate::database::index::Index; use crate::database::RankedMap; +use crate::database::Config; use crate::DocumentId; pub struct DatabaseView @@ -23,6 +24,7 @@ where D: Deref index: Index, ranked_map: RankedMap, schema: Schema, + config: Config, } impl DatabaseView @@ -32,7 +34,8 @@ where D: Deref let schema = retrieve_data_schema(&snapshot)?; let index = retrieve_data_index(&snapshot)?; let ranked_map = retrieve_data_ranked_map(&snapshot)?; - Ok(DatabaseView { snapshot, index, ranked_map, schema }) + let config = retrieve_config(&snapshot)?; + Ok(DatabaseView { snapshot, index, ranked_map, schema, config }) } pub fn schema(&self) -> &Schema {