mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 02:27:40 +08:00
feat: store config into database
This commit is contained in:
parent
f5992ce822
commit
37578ed74f
29
src/database/config.rs
Normal file
29
src/database/config.rs
Normal file
@ -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<HashSet<String>>,
|
||||||
|
ranking_order: Option<Vec<String>>,
|
||||||
|
distinct_field: Option<String>,
|
||||||
|
ranking_rules: Option<HashMap<String, RankingOrdering>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub(crate) fn default() -> Config {
|
||||||
|
Config {
|
||||||
|
stop_words: None,
|
||||||
|
ranking_order: None,
|
||||||
|
distinct_field: None,
|
||||||
|
ranking_rules: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,6 +23,7 @@ use crate::DocumentId;
|
|||||||
|
|
||||||
use self::update::{ReadIndexEvent, ReadRankedMapEvent};
|
use self::update::{ReadIndexEvent, ReadRankedMapEvent};
|
||||||
|
|
||||||
|
pub use self::config::Config;
|
||||||
pub use self::document_key::{DocumentKey, DocumentKeyAttr};
|
pub use self::document_key::{DocumentKey, DocumentKeyAttr};
|
||||||
pub use self::view::{DatabaseView, DocumentIter};
|
pub use self::view::{DatabaseView, DocumentIter};
|
||||||
pub use self::update::Update;
|
pub use self::update::Update;
|
||||||
@ -31,12 +32,15 @@ pub use self::schema::Schema;
|
|||||||
pub use self::index::Index;
|
pub use self::index::Index;
|
||||||
pub use self::number::{Number, ParseNumberError};
|
pub use self::number::{Number, ParseNumberError};
|
||||||
|
|
||||||
|
|
||||||
pub type RankedMap = HashMap<(DocumentId, SchemaAttr), Number>;
|
pub type RankedMap = HashMap<(DocumentId, SchemaAttr), Number>;
|
||||||
|
|
||||||
const DATA_INDEX: &[u8] = b"data-index";
|
const DATA_INDEX: &[u8] = b"data-index";
|
||||||
const DATA_RANKED_MAP: &[u8] = b"data-ranked-map";
|
const DATA_RANKED_MAP: &[u8] = b"data-ranked-map";
|
||||||
const DATA_SCHEMA: &[u8] = b"data-schema";
|
const DATA_SCHEMA: &[u8] = b"data-schema";
|
||||||
|
const CONFIG: &[u8] = b"config";
|
||||||
|
|
||||||
|
pub mod config;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub(crate) mod index;
|
pub(crate) mod index;
|
||||||
mod number;
|
mod number;
|
||||||
@ -104,6 +108,15 @@ where D: Deref<Target=DB>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn retrieve_config<D>(snapshot: &Snapshot<D>) -> Result<Config, Box<Error>>
|
||||||
|
where D: Deref<Target=DB>,
|
||||||
|
{
|
||||||
|
match snapshot.get(CONFIG)? {
|
||||||
|
Some(vector) => Ok(bincode::deserialize(&*vector)?),
|
||||||
|
None => Ok(Config::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn merge_indexes(existing: Option<&[u8]>, operands: &mut MergeOperands) -> Vec<u8> {
|
fn merge_indexes(existing: Option<&[u8]>, operands: &mut MergeOperands) -> Vec<u8> {
|
||||||
use self::update::ReadIndexEvent::{self, *};
|
use self::update::ReadIndexEvent::{self, *};
|
||||||
use self::update::WriteIndexEvent;
|
use self::update::WriteIndexEvent;
|
||||||
@ -265,6 +278,17 @@ impl DatabaseIndex {
|
|||||||
fn view(&self) -> Arc<DatabaseView<Arc<DB>>> {
|
fn view(&self) -> Arc<DatabaseView<Arc<DB>>> {
|
||||||
self.view.load()
|
self.view.load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_config(&self, config: Config) -> Result<Arc<DatabaseView<Arc<DB>>>, Box<Error>>{
|
||||||
|
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 {
|
impl Drop for DatabaseIndex {
|
||||||
@ -369,6 +393,12 @@ impl Database {
|
|||||||
Ok(index_guard.val().view())
|
Ok(index_guard.val().view())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_config(&self, index: &str, config: Config) -> Result<Arc<DatabaseView<Arc<DB>>>, Box<Error>>{
|
||||||
|
let index_guard = self.indexes.get(index).ok_or("Index not found")?;
|
||||||
|
|
||||||
|
Ok(index_guard.val().update_config(config)?)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -7,13 +7,14 @@ use rocksdb::rocksdb_options::{ReadOptions, EnvOptions, ColumnFamilyOptions};
|
|||||||
use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey, SstFileWriter};
|
use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey, SstFileWriter};
|
||||||
use serde::de::DeserializeOwned;
|
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::serde::deserializer::Deserializer;
|
||||||
use crate::database::{DocumentKey, DocumentKeyAttr};
|
use crate::database::{DocumentKey, DocumentKeyAttr};
|
||||||
use crate::rank::{QueryBuilder, FilterFunc};
|
use crate::rank::{QueryBuilder, FilterFunc};
|
||||||
use crate::database::schema::Schema;
|
use crate::database::schema::Schema;
|
||||||
use crate::database::index::Index;
|
use crate::database::index::Index;
|
||||||
use crate::database::RankedMap;
|
use crate::database::RankedMap;
|
||||||
|
use crate::database::Config;
|
||||||
use crate::DocumentId;
|
use crate::DocumentId;
|
||||||
|
|
||||||
pub struct DatabaseView<D>
|
pub struct DatabaseView<D>
|
||||||
@ -23,6 +24,7 @@ where D: Deref<Target=DB>
|
|||||||
index: Index,
|
index: Index,
|
||||||
ranked_map: RankedMap,
|
ranked_map: RankedMap,
|
||||||
schema: Schema,
|
schema: Schema,
|
||||||
|
config: Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D> DatabaseView<D>
|
impl<D> DatabaseView<D>
|
||||||
@ -32,7 +34,8 @@ where D: Deref<Target=DB>
|
|||||||
let schema = retrieve_data_schema(&snapshot)?;
|
let schema = retrieve_data_schema(&snapshot)?;
|
||||||
let index = retrieve_data_index(&snapshot)?;
|
let index = retrieve_data_index(&snapshot)?;
|
||||||
let ranked_map = retrieve_data_ranked_map(&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 {
|
pub fn schema(&self) -> &Schema {
|
||||||
|
Loading…
Reference in New Issue
Block a user