diff --git a/src/database/config.rs b/src/database/config.rs new file mode 100644 index 000000000..491cdba93 --- /dev/null +++ b/src/database/config.rs @@ -0,0 +1,46 @@ +use std::collections::{HashSet, HashMap}; +use serde_derive::{Serialize, Deserialize}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum RankingOrdering { + Asc, + Dsc +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AccessToken { + pub read_key: String, + pub write_key: String, + pub admin_key: String, +} + + +#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Config { + pub stop_words: Option>, + pub ranking_order: Option>, + pub distinct_field: Option, + pub ranking_rules: Option>, + pub access_token: Option, +} + +impl Config { + pub fn update_with(&mut self, new: Config) { + if let Some(stop_words) = new.stop_words { + self.stop_words = Some(stop_words); + }; + if let Some(ranking_order) = new.ranking_order { + self.ranking_order = Some(ranking_order); + }; + if let Some(distinct_field) = new.distinct_field { + self.distinct_field = Some(distinct_field); + }; + if let Some(ranking_rules) = new.ranking_rules { + self.ranking_rules = Some(ranking_rules); + }; + if let Some(access_token) = new.access_token { + self.access_token = Some(access_token); + }; + } +} diff --git a/src/database/mod.rs b/src/database/mod.rs index 5d526dcd7..7a2982fb5 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,21 @@ impl DatabaseIndex { fn view(&self) -> Arc>> { self.view.load() } + + fn get_config(&self) -> Config { + self.view().config().clone() + } + + 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.store(view.clone()); + + Ok(view) + } } impl Drop for DatabaseIndex { @@ -369,6 +397,18 @@ impl Database { Ok(index_guard.val().view()) } + pub fn get_config(&self, index: &str) -> Result> { + let index_guard = self.indexes.get(index).ok_or("Index not found")?; + + Ok(index_guard.val().get_config()) + } + + 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..81d14c3d5 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 { @@ -55,6 +58,10 @@ where D: Deref &self.snapshot } + pub fn config(&self) -> &Config { + &self.config + } + pub fn get(&self, key: &[u8]) -> Result, Box> { Ok(self.snapshot.get(key)?) }