feat: Introduce a custom tree for user custom settings

This commit is contained in:
Clément Renault 2019-05-15 12:01:08 +02:00
parent c5ba34d0b0
commit 830d2f28b9
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
5 changed files with 38 additions and 5 deletions

View File

@ -0,0 +1,13 @@
use std::sync::Arc;
use std::ops::Deref;
#[derive(Clone)]
pub struct CustomSettings(pub Arc<sled::Tree>);
impl Deref for CustomSettings {
type Target = sled::Tree;
fn deref(&self) -> &sled::Tree {
&self.0
}
}

View File

@ -12,7 +12,8 @@ use crate::ranked_map::RankedMap;
use crate::schema::Schema; use crate::schema::Schema;
use crate::serde::Deserializer; use crate::serde::Deserializer;
use super::{Error, RawIndex, DocumentsAddition, DocumentsDeletion}; use super::{Error, CustomSettings};
use super::{RawIndex, DocumentsAddition, DocumentsDeletion};
#[derive(Clone)] #[derive(Clone)]
pub struct Index(pub ArcSwap<InnerIndex>); pub struct Index(pub ArcSwap<InnerIndex>);
@ -69,6 +70,10 @@ impl Index {
self.0.lease().schema.clone() self.0.lease().schema.clone()
} }
pub fn custom_settings(&self) -> CustomSettings {
self.0.lease().raw.custom.clone()
}
pub fn documents_addition(&self) -> DocumentsAddition { pub fn documents_addition(&self) -> DocumentsAddition {
let ranked_map = self.0.lease().ranked_map.clone(); let ranked_map = self.0.lease().ranked_map.clone();
DocumentsAddition::new(self, ranked_map) DocumentsAddition::new(self, ranked_map)

View File

@ -5,6 +5,7 @@ use std::sync::{Arc, RwLock};
use crate::Schema; use crate::Schema;
mod custom_settings;
mod docs_words_index; mod docs_words_index;
mod documents_addition; mod documents_addition;
mod documents_deletion; mod documents_deletion;
@ -17,6 +18,7 @@ mod words_index;
pub use self::error::Error; pub use self::error::Error;
pub use self::index::Index; pub use self::index::Index;
pub use self::custom_settings::CustomSettings;
use self::docs_words_index::DocsWordsIndex; use self::docs_words_index::DocsWordsIndex;
use self::documents_addition::DocumentsAddition; use self::documents_addition::DocumentsAddition;
@ -96,7 +98,13 @@ impl Database {
DocumentsIndex(tree) DocumentsIndex(tree)
}; };
let raw_index = RawIndex { main, words, docs_words, documents }; let custom = {
let tree_name = format!("{}-custom", name);
let tree = self.inner.open_tree(tree_name)?;
CustomSettings(tree)
};
let raw_index = RawIndex { main, words, docs_words, documents, custom };
let index = Index::from_raw(raw_index)?; let index = Index::from_raw(raw_index)?;
vacant.insert(Arc::new(index)).clone() vacant.insert(Arc::new(index)).clone()
@ -145,11 +153,17 @@ impl Database {
DocumentsIndex(tree) DocumentsIndex(tree)
}; };
let custom = {
let tree_name = format!("{}-custom", name);
let tree = self.inner.open_tree(tree_name)?;
CustomSettings(tree)
};
let mut indexes = self.indexes()?.unwrap_or_else(HashSet::new); let mut indexes = self.indexes()?.unwrap_or_else(HashSet::new);
indexes.insert(name.to_string()); indexes.insert(name.to_string());
self.set_indexes(&indexes)?; self.set_indexes(&indexes)?;
let raw_index = RawIndex { main, words, docs_words, documents }; let raw_index = RawIndex { main, words, docs_words, documents, custom };
let index = Index::from_raw(raw_index)?; let index = Index::from_raw(raw_index)?;
vacant.insert(Arc::new(index)).clone() vacant.insert(Arc::new(index)).clone()

View File

@ -1,4 +1,4 @@
use super::{MainIndex, WordsIndex, DocsWordsIndex, DocumentsIndex}; use super::{MainIndex, WordsIndex, DocsWordsIndex, DocumentsIndex, CustomSettings};
#[derive(Clone)] #[derive(Clone)]
pub struct RawIndex { pub struct RawIndex {
@ -6,4 +6,5 @@ pub struct RawIndex {
pub words: WordsIndex, pub words: WordsIndex,
pub docs_words: DocsWordsIndex, pub docs_words: DocsWordsIndex,
pub documents: DocumentsIndex, pub documents: DocumentsIndex,
pub custom: CustomSettings,
} }

View File

@ -6,7 +6,7 @@ mod ranked_map;
mod serde; mod serde;
pub mod schema; pub mod schema;
pub use self::database::{Database, Index}; pub use self::database::{Database, Index, CustomSettings};
pub use self::number::Number; pub use self::number::Number;
pub use self::ranked_map::RankedMap; pub use self::ranked_map::RankedMap;
pub use self::schema::{Schema, SchemaAttr}; pub use self::schema::{Schema, SchemaAttr};