2019-10-03 21:04:11 +08:00
|
|
|
mod docs_words;
|
2019-10-03 17:49:13 +08:00
|
|
|
mod documents_fields;
|
2019-10-03 21:04:11 +08:00
|
|
|
mod main;
|
|
|
|
mod postings_lists;
|
2019-10-02 23:34:32 +08:00
|
|
|
mod synonyms;
|
2019-10-03 21:04:11 +08:00
|
|
|
mod updates;
|
2019-10-03 22:13:09 +08:00
|
|
|
mod updates_results;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
pub use self::docs_words::DocsWords;
|
2019-10-03 17:49:13 +08:00
|
|
|
pub use self::documents_fields::{DocumentsFields, DocumentFieldsIter};
|
2019-10-03 21:04:11 +08:00
|
|
|
pub use self::main::Main;
|
|
|
|
pub use self::postings_lists::PostingsLists;
|
2019-10-02 23:34:32 +08:00
|
|
|
pub use self::synonyms::Synonyms;
|
2019-10-03 21:04:11 +08:00
|
|
|
pub use self::updates::Updates;
|
2019-10-03 22:13:09 +08:00
|
|
|
pub use self::updates_results::UpdatesResults;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-10-08 20:53:35 +08:00
|
|
|
use std::collections::HashSet;
|
2019-10-09 20:20:37 +08:00
|
|
|
use meilidb_schema::{Schema, SchemaAttr};
|
2019-10-08 20:53:35 +08:00
|
|
|
use serde::de;
|
2019-10-10 21:17:13 +08:00
|
|
|
use crate::criterion::Criteria;
|
2019-10-08 20:53:35 +08:00
|
|
|
use crate::serde::Deserializer;
|
2019-10-10 21:17:13 +08:00
|
|
|
use crate::{update, query_builder::QueryBuilder, DocumentId, MResult, Error};
|
2019-10-07 22:16:04 +08:00
|
|
|
|
2019-10-02 23:34:32 +08:00
|
|
|
fn aligned_to(bytes: &[u8], align: usize) -> bool {
|
|
|
|
(bytes as *const _ as *const () as usize) % align == 0
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:56:55 +08:00
|
|
|
fn main_name(name: &str) -> String {
|
|
|
|
format!("store-{}", name)
|
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
fn postings_lists_name(name: &str) -> String {
|
2019-10-07 16:56:55 +08:00
|
|
|
format!("store-{}-postings-lists", name)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn documents_fields_name(name: &str) -> String {
|
2019-10-07 16:56:55 +08:00
|
|
|
format!("store-{}-documents-fields", name)
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn synonyms_name(name: &str) -> String {
|
2019-10-07 16:56:55 +08:00
|
|
|
format!("store-{}-synonyms", name)
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
fn docs_words_name(name: &str) -> String {
|
2019-10-07 16:56:55 +08:00
|
|
|
format!("store-{}-docs-words", name)
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
fn updates_name(name: &str) -> String {
|
2019-10-07 16:56:55 +08:00
|
|
|
format!("store-{}-updates", name)
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:09 +08:00
|
|
|
fn updates_results_name(name: &str) -> String {
|
2019-10-07 16:56:55 +08:00
|
|
|
format!("store-{}-updates-results", name)
|
2019-10-03 22:13:09 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
#[derive(Clone)]
|
2019-10-03 21:04:11 +08:00
|
|
|
pub struct Index {
|
|
|
|
pub main: Main,
|
|
|
|
pub postings_lists: PostingsLists,
|
|
|
|
pub documents_fields: DocumentsFields,
|
|
|
|
pub synonyms: Synonyms,
|
|
|
|
pub docs_words: DocsWords,
|
2019-10-07 22:16:04 +08:00
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
pub updates: Updates,
|
2019-10-03 22:13:09 +08:00
|
|
|
pub updates_results: UpdatesResults,
|
2019-10-07 22:16:04 +08:00
|
|
|
updates_notifier: crossbeam_channel::Sender<()>,
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
impl Index {
|
2019-10-10 19:09:29 +08:00
|
|
|
pub fn document<R: rkv::Readable, T: de::DeserializeOwned>(
|
2019-10-08 20:53:35 +08:00
|
|
|
&self,
|
|
|
|
reader: &R,
|
2019-10-09 20:20:37 +08:00
|
|
|
attributes: Option<&HashSet<&str>>,
|
2019-10-08 20:53:35 +08:00
|
|
|
document_id: DocumentId,
|
|
|
|
) -> MResult<Option<T>>
|
|
|
|
{
|
|
|
|
let schema = self.main.schema(reader)?;
|
|
|
|
let schema = schema.ok_or(Error::SchemaMissing)?;
|
|
|
|
|
2019-10-09 20:20:37 +08:00
|
|
|
let attributes = match attributes {
|
|
|
|
Some(attributes) => attributes.into_iter().map(|name| schema.attribute(name)).collect(),
|
2019-10-08 20:53:35 +08:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut deserializer = Deserializer {
|
|
|
|
document_id,
|
|
|
|
reader,
|
|
|
|
documents_fields: self.documents_fields,
|
|
|
|
schema: &schema,
|
2019-10-09 20:20:37 +08:00
|
|
|
attributes: attributes.as_ref(),
|
2019-10-08 20:53:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: currently we return an error if all document fields are missing,
|
|
|
|
// returning None would have been better
|
|
|
|
Ok(T::deserialize(&mut deserializer).map(Some)?)
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:20:37 +08:00
|
|
|
pub fn document_attribute<T: de::DeserializeOwned, R: rkv::Readable>(
|
|
|
|
&self,
|
|
|
|
reader: &R,
|
|
|
|
document_id: DocumentId,
|
|
|
|
attribute: SchemaAttr,
|
|
|
|
) -> MResult<Option<T>>
|
|
|
|
{
|
|
|
|
let bytes = self.documents_fields.document_attribute(reader, document_id, attribute)?;
|
|
|
|
match bytes {
|
|
|
|
Some(bytes) => Ok(Some(rmp_serde::from_read_ref(bytes)?)),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:29:47 +08:00
|
|
|
pub fn schema_update(&self, writer: &mut rkv::Writer, schema: Schema) -> MResult<()> {
|
2019-10-07 23:48:26 +08:00
|
|
|
let _ = self.updates_notifier.send(());
|
2019-10-11 17:29:47 +08:00
|
|
|
update::push_schema_update(writer, self.updates, self.updates_results, schema)?;
|
2019-10-07 23:48:26 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn documents_addition<D>(&self) -> update::DocumentsAddition<D> {
|
|
|
|
update::DocumentsAddition::new(
|
|
|
|
self.updates,
|
|
|
|
self.updates_results,
|
|
|
|
self.updates_notifier.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:14:04 +08:00
|
|
|
pub fn documents_deletion(&self) -> update::DocumentsDeletion {
|
2019-10-07 22:16:04 +08:00
|
|
|
update::DocumentsDeletion::new(
|
|
|
|
self.updates,
|
|
|
|
self.updates_results,
|
|
|
|
self.updates_notifier.clone(),
|
|
|
|
)
|
|
|
|
}
|
2019-10-07 23:55:46 +08:00
|
|
|
|
2019-10-08 23:18:22 +08:00
|
|
|
pub fn synonyms_addition(&self) -> update::SynonymsAddition {
|
|
|
|
update::SynonymsAddition::new(
|
|
|
|
self.updates,
|
|
|
|
self.updates_results,
|
|
|
|
self.updates_notifier.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn synonyms_deletion(&self) -> update::SynonymsDeletion {
|
|
|
|
update::SynonymsDeletion::new(
|
|
|
|
self.updates,
|
|
|
|
self.updates_results,
|
|
|
|
self.updates_notifier.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-08 23:35:47 +08:00
|
|
|
pub fn update_status<T: rkv::Readable>(
|
|
|
|
&self,
|
|
|
|
reader: &T,
|
|
|
|
update_id: u64,
|
|
|
|
) -> MResult<update::UpdateStatus>
|
|
|
|
{
|
|
|
|
update::update_status(
|
|
|
|
reader,
|
|
|
|
self.updates,
|
|
|
|
self.updates_results,
|
|
|
|
update_id,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-07 23:55:46 +08:00
|
|
|
pub fn query_builder(&self) -> QueryBuilder {
|
|
|
|
QueryBuilder::new(self.main, self.postings_lists, self.synonyms)
|
|
|
|
}
|
2019-10-10 21:17:13 +08:00
|
|
|
|
|
|
|
pub fn query_builder_with_criteria<'c>(&self, criteria: Criteria<'c>) -> QueryBuilder<'c> {
|
|
|
|
QueryBuilder::with_criteria(self.main, self.postings_lists, self.synonyms, criteria)
|
|
|
|
}
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn create(
|
|
|
|
env: &rkv::Rkv,
|
|
|
|
name: &str,
|
|
|
|
updates_notifier: crossbeam_channel::Sender<()>,
|
|
|
|
) -> Result<Index, rkv::StoreError>
|
|
|
|
{
|
|
|
|
open_options(env, name, rkv::StoreOptions::create(), updates_notifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open(
|
|
|
|
env: &rkv::Rkv,
|
|
|
|
name: &str,
|
|
|
|
updates_notifier: crossbeam_channel::Sender<()>,
|
|
|
|
) -> Result<Index, rkv::StoreError>
|
|
|
|
{
|
2019-10-03 17:49:13 +08:00
|
|
|
let mut options = rkv::StoreOptions::default();
|
|
|
|
options.create = false;
|
2019-10-07 22:16:04 +08:00
|
|
|
open_options(env, name, options, updates_notifier)
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn open_options(
|
|
|
|
env: &rkv::Rkv,
|
|
|
|
name: &str,
|
|
|
|
options: rkv::StoreOptions,
|
2019-10-07 22:16:04 +08:00
|
|
|
updates_notifier: crossbeam_channel::Sender<()>,
|
2019-10-03 21:04:11 +08:00
|
|
|
) -> Result<Index, rkv::StoreError>
|
2019-10-03 17:49:13 +08:00
|
|
|
{
|
2019-10-07 16:56:55 +08:00
|
|
|
// create all the store names
|
|
|
|
let main_name = main_name(name);
|
2019-10-03 21:04:11 +08:00
|
|
|
let postings_lists_name = postings_lists_name(name);
|
2019-10-03 17:49:13 +08:00
|
|
|
let documents_fields_name = documents_fields_name(name);
|
2019-10-03 21:04:11 +08:00
|
|
|
let synonyms_name = synonyms_name(name);
|
|
|
|
let docs_words_name = docs_words_name(name);
|
|
|
|
let updates_name = updates_name(name);
|
2019-10-03 22:13:09 +08:00
|
|
|
let updates_results_name = updates_results_name(name);
|
2019-10-03 17:49:13 +08:00
|
|
|
|
2019-10-07 16:56:55 +08:00
|
|
|
// open all the stores
|
|
|
|
let main = env.open_single(main_name.as_str(), options)?;
|
2019-10-03 21:04:11 +08:00
|
|
|
let postings_lists = env.open_single(postings_lists_name.as_str(), options)?;
|
2019-10-03 17:49:13 +08:00
|
|
|
let documents_fields = env.open_single(documents_fields_name.as_str(), options)?;
|
2019-10-03 21:04:11 +08:00
|
|
|
let synonyms = env.open_single(synonyms_name.as_str(), options)?;
|
|
|
|
let docs_words = env.open_single(docs_words_name.as_str(), options)?;
|
|
|
|
let updates = env.open_single(updates_name.as_str(), options)?;
|
2019-10-03 22:13:09 +08:00
|
|
|
let updates_results = env.open_single(updates_results_name.as_str(), options)?;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-10-03 21:04:11 +08:00
|
|
|
Ok(Index {
|
|
|
|
main: Main { main },
|
|
|
|
postings_lists: PostingsLists { postings_lists },
|
|
|
|
documents_fields: DocumentsFields { documents_fields },
|
|
|
|
synonyms: Synonyms { synonyms },
|
|
|
|
docs_words: DocsWords { docs_words },
|
|
|
|
updates: Updates { updates },
|
2019-10-03 22:13:09 +08:00
|
|
|
updates_results: UpdatesResults { updates_results },
|
2019-10-07 22:16:04 +08:00
|
|
|
updates_notifier,
|
2019-10-03 21:04:11 +08:00
|
|
|
})
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|