2019-10-03 21:04:11 +08:00
|
|
|
mod docs_words;
|
2019-12-30 18:44:42 +08:00
|
|
|
mod prefix_documents_cache;
|
2019-12-31 01:01:27 +08:00
|
|
|
mod prefix_postings_lists_cache;
|
2019-10-03 17:49:13 +08:00
|
|
|
mod documents_fields;
|
2019-10-14 20:06:34 +08:00
|
|
|
mod documents_fields_counts;
|
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-12-30 18:44:42 +08:00
|
|
|
pub use self::prefix_documents_cache::PrefixDocumentsCache;
|
2019-12-31 01:01:27 +08:00
|
|
|
pub use self::prefix_postings_lists_cache::PrefixPostingsListsCache;
|
2019-10-18 19:05:28 +08:00
|
|
|
pub use self::documents_fields::{DocumentFieldsIter, DocumentsFields};
|
|
|
|
pub use self::documents_fields_counts::{
|
|
|
|
DocumentFieldsCountsIter, DocumentsFieldsCounts, DocumentsIdsIter,
|
|
|
|
};
|
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
|
|
|
|
2020-01-08 21:43:36 +08:00
|
|
|
use std::borrow::Cow;
|
2019-10-08 20:53:35 +08:00
|
|
|
use std::collections::HashSet;
|
2020-01-08 21:43:36 +08:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::{mem, ptr};
|
2019-10-14 20:06:34 +08:00
|
|
|
|
2019-10-21 18:05:53 +08:00
|
|
|
use heed::Result as ZResult;
|
2020-01-08 21:43:36 +08:00
|
|
|
use heed::{BytesEncode, BytesDecode};
|
2019-11-26 18:06:55 +08:00
|
|
|
use meilisearch_schema::{Schema, SchemaAttr};
|
2020-01-08 21:43:36 +08:00
|
|
|
use sdset::{Set, SetBuf};
|
2019-11-05 22:03:18 +08:00
|
|
|
use serde::de::{self, Deserialize};
|
2019-10-16 23:05:24 +08:00
|
|
|
use zerocopy::{AsBytes, FromBytes};
|
2019-10-14 20:06:34 +08:00
|
|
|
|
2019-10-10 21:17:13 +08:00
|
|
|
use crate::criterion::Criteria;
|
2019-11-26 23:12:06 +08:00
|
|
|
use crate::database::{MainT, UpdateT};
|
2020-01-08 21:17:38 +08:00
|
|
|
use crate::database::{UpdateEvent, UpdateEventsEmitter};
|
2019-10-08 20:53:35 +08:00
|
|
|
use crate::serde::Deserializer;
|
2020-01-08 21:43:36 +08:00
|
|
|
use crate::{query_builder::QueryBuilder, update, DocIndex, DocumentId, Error, MResult};
|
2019-10-07 22:16:04 +08:00
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
type BEU64 = zerocopy::U64<byteorder::BigEndian>;
|
|
|
|
type BEU16 = zerocopy::U16<byteorder::BigEndian>;
|
2019-10-14 20:06:34 +08:00
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
#[derive(Debug, Copy, Clone, AsBytes, FromBytes)]
|
2019-10-16 23:05:24 +08:00
|
|
|
#[repr(C)]
|
2019-10-18 19:05:28 +08:00
|
|
|
pub struct DocumentAttrKey {
|
|
|
|
docid: BEU64,
|
|
|
|
attr: BEU16,
|
|
|
|
}
|
2019-10-14 20:06:34 +08:00
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
impl DocumentAttrKey {
|
|
|
|
fn new(docid: DocumentId, attr: SchemaAttr) -> DocumentAttrKey {
|
2019-10-18 19:05:28 +08:00
|
|
|
DocumentAttrKey {
|
|
|
|
docid: BEU64::new(docid.0),
|
|
|
|
attr: BEU16::new(attr.0),
|
|
|
|
}
|
2019-10-16 23:05:24 +08:00
|
|
|
}
|
2019-10-14 20:06:34 +08:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:30:43 +08:00
|
|
|
#[derive(Default, Debug)]
|
2020-01-08 21:43:36 +08:00
|
|
|
pub struct Postings<'a> {
|
|
|
|
pub docids: Cow<'a, Set<DocumentId>>,
|
|
|
|
pub matches: Cow<'a, Set<DocIndex>>,
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:30:43 +08:00
|
|
|
pub struct PostingsCodec;
|
2020-01-08 21:43:36 +08:00
|
|
|
|
|
|
|
impl<'a> BytesEncode<'a> for PostingsCodec {
|
|
|
|
type EItem = Postings<'a>;
|
|
|
|
|
|
|
|
fn bytes_encode(item: &'a Self::EItem) -> Option<Cow<'a, [u8]>> {
|
|
|
|
let u64_size = mem::size_of::<u64>();
|
|
|
|
let docids_size = item.docids.len() * mem::size_of::<DocumentId>();
|
|
|
|
let matches_size = item.matches.len() * mem::size_of::<DocIndex>();
|
|
|
|
|
|
|
|
let mut buffer = Vec::with_capacity(u64_size + docids_size + matches_size);
|
|
|
|
|
|
|
|
let docids_len = item.docids.len();
|
|
|
|
buffer.extend_from_slice(&docids_len.to_be_bytes());
|
|
|
|
buffer.extend_from_slice(item.docids.as_bytes());
|
|
|
|
buffer.extend_from_slice(item.matches.as_bytes());
|
|
|
|
|
|
|
|
Some(Cow::Owned(buffer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn aligned_to(bytes: &[u8], align: usize) -> bool {
|
|
|
|
(bytes as *const _ as *const () as usize) % align == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes_to_set<'a, T: 'a>(bytes: &'a [u8]) -> Option<Cow<'a, Set<T>>>
|
|
|
|
where T: Clone + FromBytes
|
|
|
|
{
|
|
|
|
match zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes) {
|
|
|
|
Some(layout) => Some(Cow::Borrowed(Set::new_unchecked(layout.into_slice()))),
|
|
|
|
None => {
|
|
|
|
let len = bytes.len();
|
|
|
|
let elem_size = mem::size_of::<T>();
|
|
|
|
|
|
|
|
// ensure that it is the alignment that is wrong
|
|
|
|
// and the length is valid
|
|
|
|
if len % elem_size == 0 && !aligned_to(bytes, mem::align_of::<T>()) {
|
|
|
|
let elems = len / elem_size;
|
|
|
|
let mut vec = Vec::<T>::with_capacity(elems);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let dst = vec.as_mut_ptr() as *mut u8;
|
|
|
|
ptr::copy_nonoverlapping(bytes.as_ptr(), dst, len);
|
|
|
|
vec.set_len(elems);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(Cow::Owned(SetBuf::new_unchecked(vec)));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BytesDecode<'a> for PostingsCodec {
|
|
|
|
type DItem = Postings<'a>;
|
|
|
|
|
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
|
|
|
|
let u64_size = mem::size_of::<u64>();
|
|
|
|
let docid_size = mem::size_of::<DocumentId>();
|
|
|
|
|
|
|
|
let (len_bytes, bytes) = bytes.split_at(u64_size);
|
|
|
|
let docids_len = len_bytes.try_into().ok().map(u64::from_be_bytes)? as usize;
|
|
|
|
let docids_size = docids_len * docid_size;
|
|
|
|
|
|
|
|
let docids_bytes = &bytes[..docids_size];
|
|
|
|
let matches_bytes = &bytes[docids_size..];
|
|
|
|
|
|
|
|
let docids = from_bytes_to_set(docids_bytes)?;
|
|
|
|
let matches = from_bytes_to_set(matches_bytes)?;
|
|
|
|
|
|
|
|
Some(Postings { docids, matches })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-14 20:06:34 +08:00
|
|
|
fn documents_fields_counts_name(name: &str) -> String {
|
|
|
|
format!("store-{}-documents-fields-counts", 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-12-30 18:44:42 +08:00
|
|
|
fn prefix_documents_cache_name(name: &str) -> String {
|
2019-12-31 01:01:27 +08:00
|
|
|
format!("store-{}-prefix-documents-cache", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prefix_postings_lists_cache_name(name: &str) -> String {
|
|
|
|
format!("store-{}-prefix-postings-lists-cache", name)
|
2019-12-21 20:44:19 +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,
|
2019-10-14 20:06:34 +08:00
|
|
|
pub documents_fields_counts: DocumentsFieldsCounts,
|
2019-10-03 21:04:11 +08:00
|
|
|
pub synonyms: Synonyms,
|
|
|
|
pub docs_words: DocsWords,
|
2019-12-30 18:44:42 +08:00
|
|
|
pub prefix_documents_cache: PrefixDocumentsCache,
|
2019-12-31 01:01:27 +08:00
|
|
|
pub prefix_postings_lists_cache: PrefixPostingsListsCache,
|
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-11-22 20:22:35 +08:00
|
|
|
pub(crate) updates_notifier: UpdateEventsEmitter,
|
2019-10-03 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
impl Index {
|
2019-10-16 23:05:24 +08:00
|
|
|
pub fn document<T: de::DeserializeOwned>(
|
2019-10-08 20:53:35 +08:00
|
|
|
&self,
|
2019-11-26 23:12:06 +08:00
|
|
|
reader: &heed::RoTxn<MainT>,
|
2019-10-09 20:20:37 +08:00
|
|
|
attributes: Option<&HashSet<&str>>,
|
2019-10-08 20:53:35 +08:00
|
|
|
document_id: DocumentId,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> MResult<Option<T>> {
|
2019-10-08 20:53:35 +08:00
|
|
|
let schema = self.main.schema(reader)?;
|
|
|
|
let schema = schema.ok_or(Error::SchemaMissing)?;
|
|
|
|
|
2020-01-11 01:20:30 +08:00
|
|
|
// let attributes = attributes.map(|a| a.iter().filter_map(|name| schema.get_id(*name)).collect());
|
|
|
|
|
2019-10-09 20:20:37 +08:00
|
|
|
let attributes = match attributes {
|
2020-01-11 01:20:30 +08:00
|
|
|
Some(attributes) => Some(attributes.iter().filter_map(|name| schema.get_id(*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
|
|
|
};
|
|
|
|
|
2019-11-05 22:03:18 +08:00
|
|
|
Ok(Option::<T>::deserialize(&mut deserializer)?)
|
2019-10-08 20:53:35 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
pub fn document_attribute<T: de::DeserializeOwned>(
|
2019-10-09 20:20:37 +08:00
|
|
|
&self,
|
2019-11-26 23:12:06 +08:00
|
|
|
reader: &heed::RoTxn<MainT>,
|
2019-10-09 20:20:37 +08:00
|
|
|
document_id: DocumentId,
|
|
|
|
attribute: SchemaAttr,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> MResult<Option<T>> {
|
|
|
|
let bytes = self
|
|
|
|
.documents_fields
|
|
|
|
.document_attribute(reader, document_id, attribute)?;
|
2019-10-09 20:20:37 +08:00
|
|
|
match bytes {
|
2019-10-11 22:16:21 +08:00
|
|
|
Some(bytes) => Ok(Some(serde_json::from_slice(bytes)?)),
|
2019-10-09 20:20:37 +08:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 21:33:35 +08:00
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
pub fn customs_update(&self, writer: &mut heed::RwTxn<UpdateT>, customs: Vec<u8>) -> ZResult<u64> {
|
2019-11-06 17:49:13 +08:00
|
|
|
let _ = self.updates_notifier.send(UpdateEvent::NewUpdate);
|
2019-10-11 21:33:35 +08:00
|
|
|
update::push_customs_update(writer, self.updates, self.updates_results, customs)
|
2019-10-07 23:48:26 +08:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:17:38 +08:00
|
|
|
pub fn settings_update(&self, writer: &mut heed::RwTxn<UpdateT>, update: SettingsUpdate) -> ZResult<u64> {
|
|
|
|
let _ = self.updates_notifier.send(UpdateEvent::NewUpdate);
|
|
|
|
update::push_settings_update(writer, self.updates, self.updates_results, update)
|
|
|
|
}
|
|
|
|
|
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-11-05 22:23:41 +08:00
|
|
|
pub fn documents_partial_addition<D>(&self) -> update::DocumentsAddition<D> {
|
|
|
|
update::DocumentsAddition::new_partial(
|
|
|
|
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-11-26 23:12:06 +08:00
|
|
|
pub fn clear_all(&self, writer: &mut heed::RwTxn<UpdateT>) -> MResult<u64> {
|
2019-11-06 17:49:13 +08:00
|
|
|
let _ = self.updates_notifier.send(UpdateEvent::NewUpdate);
|
2019-10-23 22:32:11 +08:00
|
|
|
update::push_clear_all(writer, self.updates, self.updates_results)
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
pub fn current_update_id(&self, reader: &heed::RoTxn<UpdateT>) -> MResult<Option<u64>> {
|
|
|
|
match self.updates.last_update(reader)? {
|
2019-10-15 20:54:52 +08:00
|
|
|
Some((id, _)) => Ok(Some(id)),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
pub fn update_status(
|
2019-10-08 23:35:47 +08:00
|
|
|
&self,
|
2019-11-26 23:12:06 +08:00
|
|
|
reader: &heed::RoTxn<UpdateT>,
|
2019-10-08 23:35:47 +08:00
|
|
|
update_id: u64,
|
2019-11-13 01:00:47 +08:00
|
|
|
) -> MResult<Option<update::UpdateStatus>> {
|
2019-10-18 19:05:28 +08:00
|
|
|
update::update_status(reader, self.updates, self.updates_results, update_id)
|
2019-10-08 23:35:47 +08:00
|
|
|
}
|
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
pub fn all_updates_status(&self, reader: &heed::RoTxn<UpdateT>) -> MResult<Vec<update::UpdateStatus>> {
|
2019-10-31 18:13:37 +08:00
|
|
|
let mut updates = Vec::new();
|
|
|
|
let mut last_update_result_id = 0;
|
|
|
|
|
|
|
|
// retrieve all updates results
|
2019-11-26 23:12:06 +08:00
|
|
|
if let Some((last_id, _)) = self.updates_results.last_update(reader)? {
|
2019-10-31 18:13:37 +08:00
|
|
|
updates.reserve(last_id as usize);
|
|
|
|
|
|
|
|
for id in 0..=last_id {
|
2019-11-13 01:00:47 +08:00
|
|
|
if let Some(update) = self.update_status(reader, id)? {
|
|
|
|
updates.push(update);
|
|
|
|
last_update_result_id = id;
|
|
|
|
}
|
2019-10-31 18:13:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve all enqueued updates
|
2019-11-26 23:12:06 +08:00
|
|
|
if let Some((last_id, _)) = self.updates.last_update(reader)? {
|
2019-11-13 01:09:33 +08:00
|
|
|
for id in last_update_result_id + 1..=last_id {
|
2019-11-13 01:00:47 +08:00
|
|
|
if let Some(update) = self.update_status(reader, id)? {
|
|
|
|
updates.push(update);
|
|
|
|
}
|
2019-10-29 18:30:44 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 18:13:37 +08:00
|
|
|
|
|
|
|
Ok(updates)
|
2019-10-29 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 23:55:46 +08:00
|
|
|
pub fn query_builder(&self) -> QueryBuilder {
|
2019-10-15 00:48:32 +08:00
|
|
|
QueryBuilder::new(
|
|
|
|
self.main,
|
|
|
|
self.postings_lists,
|
|
|
|
self.documents_fields_counts,
|
|
|
|
self.synonyms,
|
2019-12-30 18:44:42 +08:00
|
|
|
self.prefix_documents_cache,
|
2019-12-31 19:53:40 +08:00
|
|
|
self.prefix_postings_lists_cache,
|
2019-10-15 00:48:32 +08:00
|
|
|
)
|
2019-10-07 23:55:46 +08:00
|
|
|
}
|
2019-10-10 21:17:13 +08:00
|
|
|
|
2019-10-17 20:45:21 +08:00
|
|
|
pub fn query_builder_with_criteria<'c, 'f, 'd>(
|
|
|
|
&self,
|
|
|
|
criteria: Criteria<'c>,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> QueryBuilder<'c, 'f, 'd> {
|
2019-10-15 00:48:32 +08:00
|
|
|
QueryBuilder::with_criteria(
|
|
|
|
self.main,
|
|
|
|
self.postings_lists,
|
|
|
|
self.documents_fields_counts,
|
|
|
|
self.synonyms,
|
2019-12-30 18:44:42 +08:00
|
|
|
self.prefix_documents_cache,
|
2019-12-31 19:53:40 +08:00
|
|
|
self.prefix_postings_lists_cache,
|
2019-10-15 00:48:32 +08:00
|
|
|
criteria,
|
|
|
|
)
|
2019-10-10 21:17:13 +08:00
|
|
|
}
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:16:04 +08:00
|
|
|
pub fn create(
|
2019-10-21 18:05:53 +08:00
|
|
|
env: &heed::Env,
|
2019-11-26 23:12:06 +08:00
|
|
|
update_env: &heed::Env,
|
2019-10-07 22:16:04 +08:00
|
|
|
name: &str,
|
2019-11-06 17:49:13 +08:00
|
|
|
updates_notifier: UpdateEventsEmitter,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> MResult<Index> {
|
2019-10-16 23:05:24 +08:00
|
|
|
// create all the store names
|
|
|
|
let main_name = main_name(name);
|
|
|
|
let postings_lists_name = postings_lists_name(name);
|
|
|
|
let documents_fields_name = documents_fields_name(name);
|
|
|
|
let documents_fields_counts_name = documents_fields_counts_name(name);
|
|
|
|
let synonyms_name = synonyms_name(name);
|
|
|
|
let docs_words_name = docs_words_name(name);
|
2019-12-30 18:44:42 +08:00
|
|
|
let prefix_documents_cache_name = prefix_documents_cache_name(name);
|
2019-12-31 01:01:27 +08:00
|
|
|
let prefix_postings_lists_cache_name = prefix_postings_lists_cache_name(name);
|
2019-10-16 23:05:24 +08:00
|
|
|
let updates_name = updates_name(name);
|
|
|
|
let updates_results_name = updates_results_name(name);
|
2019-10-07 22:16:04 +08:00
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
// open all the stores
|
2019-10-31 22:48:29 +08:00
|
|
|
let main = env.create_poly_database(Some(&main_name))?;
|
2019-10-16 23:05:24 +08:00
|
|
|
let postings_lists = env.create_database(Some(&postings_lists_name))?;
|
|
|
|
let documents_fields = env.create_database(Some(&documents_fields_name))?;
|
|
|
|
let documents_fields_counts = env.create_database(Some(&documents_fields_counts_name))?;
|
|
|
|
let synonyms = env.create_database(Some(&synonyms_name))?;
|
|
|
|
let docs_words = env.create_database(Some(&docs_words_name))?;
|
2019-12-30 18:44:42 +08:00
|
|
|
let prefix_documents_cache = env.create_database(Some(&prefix_documents_cache_name))?;
|
2019-12-31 01:01:27 +08:00
|
|
|
let prefix_postings_lists_cache = env.create_database(Some(&prefix_postings_lists_cache_name))?;
|
2019-11-26 23:12:06 +08:00
|
|
|
let updates = update_env.create_database(Some(&updates_name))?;
|
|
|
|
let updates_results = update_env.create_database(Some(&updates_results_name))?;
|
2019-10-16 23:05:24 +08:00
|
|
|
|
|
|
|
Ok(Index {
|
|
|
|
main: Main { main },
|
|
|
|
postings_lists: PostingsLists { postings_lists },
|
|
|
|
documents_fields: DocumentsFields { documents_fields },
|
2019-12-31 01:01:27 +08:00
|
|
|
documents_fields_counts: DocumentsFieldsCounts { documents_fields_counts },
|
2019-10-16 23:05:24 +08:00
|
|
|
synonyms: Synonyms { synonyms },
|
|
|
|
docs_words: DocsWords { docs_words },
|
2019-12-31 01:01:27 +08:00
|
|
|
prefix_postings_lists_cache: PrefixPostingsListsCache { prefix_postings_lists_cache },
|
2019-12-30 18:44:42 +08:00
|
|
|
prefix_documents_cache: PrefixDocumentsCache { prefix_documents_cache },
|
2019-10-16 23:05:24 +08:00
|
|
|
updates: Updates { updates },
|
|
|
|
updates_results: UpdatesResults { updates_results },
|
|
|
|
updates_notifier,
|
|
|
|
})
|
2019-10-03 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
pub fn open(
|
2019-10-21 18:05:53 +08:00
|
|
|
env: &heed::Env,
|
2019-11-26 23:12:06 +08:00
|
|
|
update_env: &heed::Env,
|
2019-10-03 17:49:13 +08:00
|
|
|
name: &str,
|
2019-11-06 17:49:13 +08:00
|
|
|
updates_notifier: UpdateEventsEmitter,
|
2019-10-18 19:05:28 +08:00
|
|
|
) -> MResult<Option<Index>> {
|
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-14 20:06:34 +08:00
|
|
|
let documents_fields_counts_name = documents_fields_counts_name(name);
|
2019-10-03 21:04:11 +08:00
|
|
|
let synonyms_name = synonyms_name(name);
|
|
|
|
let docs_words_name = docs_words_name(name);
|
2019-12-30 18:44:42 +08:00
|
|
|
let prefix_documents_cache_name = prefix_documents_cache_name(name);
|
2019-12-31 01:01:27 +08:00
|
|
|
let prefix_postings_lists_cache_name = prefix_postings_lists_cache_name(name);
|
2019-10-03 21:04:11 +08:00
|
|
|
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
|
2019-10-31 22:48:29 +08:00
|
|
|
let main = match env.open_poly_database(Some(&main_name))? {
|
2019-10-16 23:05:24 +08:00
|
|
|
Some(main) => main,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
let postings_lists = match env.open_database(Some(&postings_lists_name))? {
|
|
|
|
Some(postings_lists) => postings_lists,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
let documents_fields = match env.open_database(Some(&documents_fields_name))? {
|
|
|
|
Some(documents_fields) => documents_fields,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
let documents_fields_counts = match env.open_database(Some(&documents_fields_counts_name))? {
|
|
|
|
Some(documents_fields_counts) => documents_fields_counts,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
let synonyms = match env.open_database(Some(&synonyms_name))? {
|
|
|
|
Some(synonyms) => synonyms,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
let docs_words = match env.open_database(Some(&docs_words_name))? {
|
|
|
|
Some(docs_words) => docs_words,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-12-30 18:44:42 +08:00
|
|
|
let prefix_documents_cache = match env.open_database(Some(&prefix_documents_cache_name))? {
|
|
|
|
Some(prefix_documents_cache) => prefix_documents_cache,
|
2019-12-21 20:44:19 +08:00
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-12-31 01:01:27 +08:00
|
|
|
let prefix_postings_lists_cache = match env.open_database(Some(&prefix_postings_lists_cache_name))? {
|
|
|
|
Some(prefix_postings_lists_cache) => prefix_postings_lists_cache,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-11-26 23:12:06 +08:00
|
|
|
let updates = match update_env.open_database(Some(&updates_name))? {
|
2019-10-16 23:05:24 +08:00
|
|
|
Some(updates) => updates,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-11-26 23:12:06 +08:00
|
|
|
let updates_results = match update_env.open_database(Some(&updates_results_name))? {
|
2019-10-16 23:05:24 +08:00
|
|
|
Some(updates_results) => updates_results,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-10-16 23:05:24 +08:00
|
|
|
Ok(Some(Index {
|
2019-10-03 21:04:11 +08:00
|
|
|
main: Main { main },
|
|
|
|
postings_lists: PostingsLists { postings_lists },
|
|
|
|
documents_fields: DocumentsFields { documents_fields },
|
2019-12-31 01:01:27 +08:00
|
|
|
documents_fields_counts: DocumentsFieldsCounts { documents_fields_counts },
|
2019-10-03 21:04:11 +08:00
|
|
|
synonyms: Synonyms { synonyms },
|
|
|
|
docs_words: DocsWords { docs_words },
|
2019-12-30 18:44:42 +08:00
|
|
|
prefix_documents_cache: PrefixDocumentsCache { prefix_documents_cache },
|
2019-12-31 01:01:27 +08:00
|
|
|
prefix_postings_lists_cache: PrefixPostingsListsCache { prefix_postings_lists_cache },
|
2019-10-03 21:04:11 +08:00
|
|
|
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-16 23:05:24 +08:00
|
|
|
}))
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
2019-11-06 17:49:13 +08:00
|
|
|
|
2019-11-26 23:12:06 +08:00
|
|
|
pub fn clear(
|
|
|
|
writer: &mut heed::RwTxn<MainT>,
|
|
|
|
update_writer: &mut heed::RwTxn<UpdateT>,
|
|
|
|
index: &Index,
|
|
|
|
) -> MResult<()> {
|
2019-11-06 17:49:13 +08:00
|
|
|
// clear all the stores
|
|
|
|
index.main.clear(writer)?;
|
|
|
|
index.postings_lists.clear(writer)?;
|
|
|
|
index.documents_fields.clear(writer)?;
|
|
|
|
index.documents_fields_counts.clear(writer)?;
|
|
|
|
index.synonyms.clear(writer)?;
|
|
|
|
index.docs_words.clear(writer)?;
|
2019-12-30 18:44:42 +08:00
|
|
|
index.prefix_documents_cache.clear(writer)?;
|
2019-12-31 01:01:27 +08:00
|
|
|
index.prefix_postings_lists_cache.clear(writer)?;
|
2019-11-26 23:12:06 +08:00
|
|
|
index.updates.clear(update_writer)?;
|
|
|
|
index.updates_results.clear(update_writer)?;
|
2019-11-06 17:49:13 +08:00
|
|
|
Ok(())
|
|
|
|
}
|