feat: Introduce synonyms addition using the update system

This commit is contained in:
Clément Renault 2019-08-22 11:08:54 +02:00 committed by Clément Renault
parent 0db3e6c58c
commit 451c0a6d03
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
3 changed files with 50 additions and 12 deletions

View File

@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{HashSet, BTreeMap};
use std::convert::TryInto;
use std::sync::Arc;
use std::thread;
@ -26,7 +26,7 @@ use self::words_index::WordsIndex;
use super::{
DocumentsAddition, FinalDocumentsAddition,
DocumentsDeletion, FinalDocumentsDeletion,
SynonymsAddition,
SynonymsAddition, FinalSynonymsAddition,
SynonymsDeletion,
};
@ -48,7 +48,7 @@ fn event_is_set(event: &sled::Event) -> bool {
enum UpdateOwned {
DocumentsAddition(Vec<serde_json::Value>),
DocumentsDeletion(Vec<DocumentId>),
SynonymsAddition( () /*SynonymsAddition*/),
SynonymsAddition(BTreeMap<String, Vec<String>>),
SynonymsDeletion( () /*SynonymsDeletion*/),
}
@ -56,7 +56,7 @@ enum UpdateOwned {
enum Update<D: serde::Serialize> {
DocumentsAddition(Vec<D>),
DocumentsDeletion(Vec<DocumentId>),
SynonymsAddition( () /*SynonymsAddition*/),
SynonymsAddition(BTreeMap<String, Vec<String>>),
SynonymsDeletion( () /*SynonymsDeletion*/),
}
@ -91,8 +91,9 @@ fn spawn_update_system(index: Index) -> thread::JoinHandle<()> {
deletion.extend(documents);
deletion.finalize()?;
},
UpdateOwned::SynonymsAddition(_) => {
// ...
UpdateOwned::SynonymsAddition(synonyms) => {
let addition = FinalSynonymsAddition::from_map(&index, synonyms);
addition.finalize()?;
},
UpdateOwned::SynonymsDeletion(_) => {
// ...
@ -313,8 +314,12 @@ impl Index {
self.raw_push_update(update)
}
pub(crate) fn push_synonyms_addition(&self, addition: SynonymsAddition) -> Result<u64, Error> {
let update = bincode::serialize(&())?;
pub(crate) fn push_synonyms_addition(
&self,
addition: BTreeMap<String, Vec<String>>,
) -> Result<u64, Error>
{
let update = bincode::serialize(&addition)?;
self.raw_push_update(update)
}

View File

@ -17,7 +17,7 @@ pub use self::index::{Index, CustomSettingsIndex};
use self::documents_addition::{DocumentsAddition, FinalDocumentsAddition};
use self::documents_deletion::{DocumentsDeletion, FinalDocumentsDeletion};
use self::synonyms_addition::SynonymsAddition;
use self::synonyms_addition::{SynonymsAddition, FinalSynonymsAddition};
use self::synonyms_deletion::SynonymsDeletion;
fn load_indexes(tree: &sled::Tree) -> Result<HashSet<String>, Error> {

View File

@ -9,13 +9,46 @@ use super::{Error, Index};
use super::index::Cache;
pub struct SynonymsAddition<'a> {
inner: &'a Index,
index: &'a Index,
synonyms: BTreeMap<String, Vec<String>>,
}
impl<'a> SynonymsAddition<'a> {
pub fn new(inner: &'a Index) -> SynonymsAddition<'a> {
SynonymsAddition { inner, synonyms: BTreeMap::new() }
pub fn new(index: &'a Index) -> SynonymsAddition<'a> {
SynonymsAddition { index, synonyms: BTreeMap::new() }
}
pub fn add_synonym<S, T, I>(&mut self, synonym: S, alternatives: I)
where S: AsRef<str>,
T: AsRef<str>,
I: Iterator<Item=T>,
{
let synonym = normalize_str(synonym.as_ref());
let alternatives = alternatives.map(|s| s.as_ref().to_lowercase());
self.synonyms.entry(synonym).or_insert_with(Vec::new).extend(alternatives);
}
pub fn finalize(self) -> Result<u64, Error> {
self.index.push_synonyms_addition(self.synonyms)
}
}
pub struct FinalSynonymsAddition<'a> {
inner: &'a Index,
synonyms: BTreeMap<String, Vec<String>>,
}
impl<'a> FinalSynonymsAddition<'a> {
pub fn new(inner: &'a Index) -> FinalSynonymsAddition<'a> {
FinalSynonymsAddition { inner, synonyms: BTreeMap::new() }
}
pub fn from_map(
inner: &'a Index,
synonyms: BTreeMap<String, Vec<String>>,
) -> FinalSynonymsAddition<'a>
{
FinalSynonymsAddition { inner, synonyms }
}
pub fn add_synonym<S, T, I>(&mut self, synonym: S, alternatives: I)