MeiliSearch/meilisearch-core/src/store/main.rs

327 lines
13 KiB
Rust
Raw Normal View History

2020-05-05 22:28:46 +02:00
use std::borrow::Cow;
2020-01-13 19:10:58 +01:00
use std::sync::Arc;
2020-01-18 16:12:02 +01:00
use std::collections::HashMap;
2020-01-13 19:10:58 +01:00
2019-11-20 11:24:08 +01:00
use chrono::{DateTime, Utc};
use heed::Result as ZResult;
use heed::types::{ByteSlice, OwnedType, SerdeBincode, Str};
2020-05-05 22:19:34 +02:00
use meilisearch_schema::{FieldId, Schema};
use meilisearch_types::DocumentId;
2020-05-05 22:19:34 +02:00
use sdset::Set;
2020-01-13 19:10:58 +01:00
use crate::database::MainT;
use crate::RankedMap;
use crate::settings::RankingRule;
use super::{CowSet, DocumentsIds};
2019-10-03 15:04:11 +02:00
2020-05-05 22:19:34 +02:00
const ATTRIBUTES_FOR_FACETING: &str = "attributes-for-faceting";
const CREATED_AT_KEY: &str = "created-at";
2020-01-29 18:30:21 +01:00
const CUSTOMS_KEY: &str = "customs";
const DISTINCT_ATTRIBUTE_KEY: &str = "distinct-attribute";
2019-11-20 17:28:46 +01:00
const FIELDS_FREQUENCY_KEY: &str = "fields-frequency";
const INTERNAL_IDS_KEY: &str = "internal-ids";
2019-11-20 17:28:46 +01:00
const NAME_KEY: &str = "name";
2019-10-03 16:13:09 +02:00
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
2019-10-18 13:05:28 +02:00
const RANKED_MAP_KEY: &str = "ranked-map";
const RANKING_RULES_KEY: &str = "ranking-rules";
2019-10-18 13:05:28 +02:00
const SCHEMA_KEY: &str = "schema";
const STOP_WORDS_KEY: &str = "stop-words";
const SYNONYMS_KEY: &str = "synonyms";
2019-11-20 17:28:46 +01:00
const UPDATED_AT_KEY: &str = "updated-at";
const USER_IDS_KEY: &str = "user-ids";
2019-10-18 13:05:28 +02:00
const WORDS_KEY: &str = "words";
2019-10-03 16:13:09 +02:00
pub type FreqsMap = HashMap<String, usize>;
type SerdeFreqsMap = SerdeBincode<FreqsMap>;
type SerdeDatetime = SerdeBincode<DateTime<Utc>>;
2019-10-03 15:04:11 +02:00
#[derive(Copy, Clone)]
pub struct Main {
2019-10-21 12:05:53 +02:00
pub(crate) main: heed::PolyDatabase,
2019-10-03 15:04:11 +02:00
}
impl Main {
pub fn clear(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> {
2019-11-06 10:49:13 +01:00
self.main.clear(writer)
}
pub fn put_name(self, writer: &mut heed::RwTxn<MainT>, name: &str) -> ZResult<()> {
self.main.put::<_, Str, Str>(writer, NAME_KEY, name)
2019-11-20 11:24:08 +01:00
}
pub fn name(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<String>> {
2019-11-20 11:24:08 +01:00
Ok(self
.main
.get::<_, Str, Str>(reader, NAME_KEY)?
2019-11-20 11:24:08 +01:00
.map(|name| name.to_owned()))
}
pub fn put_created_at(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> {
2019-11-20 11:24:08 +01:00
self.main
.put::<_, Str, SerdeDatetime>(writer, CREATED_AT_KEY, &Utc::now())
}
pub fn created_at(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<DateTime<Utc>>> {
self.main.get::<_, Str, SerdeDatetime>(reader, CREATED_AT_KEY)
}
2019-11-20 14:12:12 +01:00
pub fn put_updated_at(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<()> {
2019-11-20 11:24:08 +01:00
self.main
.put::<_, Str, SerdeDatetime>(writer, UPDATED_AT_KEY, &Utc::now())
}
pub fn updated_at(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<DateTime<Utc>>> {
self.main.get::<_, Str, SerdeDatetime>(reader, UPDATED_AT_KEY)
2019-11-19 16:18:01 +01:00
}
pub fn put_internal_ids(self, writer: &mut heed::RwTxn<MainT>, ids: &sdset::Set<DocumentId>) -> ZResult<()> {
self.main.put::<_, Str, DocumentsIds>(writer, INTERNAL_IDS_KEY, ids)
}
pub fn internal_ids<'txn>(self, reader: &'txn heed::RoTxn<MainT>) -> ZResult<Cow<'txn, sdset::Set<DocumentId>>> {
match self.main.get::<_, Str, DocumentsIds>(reader, INTERNAL_IDS_KEY)? {
Some(ids) => Ok(ids),
None => Ok(Cow::default()),
}
}
2020-05-19 11:45:46 +02:00
pub fn merge_internal_ids(self, writer: &mut heed::RwTxn<MainT>, new_ids: &sdset::Set<DocumentId>) -> ZResult<()> {
use sdset::SetOperation;
// We do an union of the old and new internal ids.
let internal_ids = self.internal_ids(writer)?;
2020-05-19 13:12:02 +02:00
let internal_ids = sdset::duo::Union::new(&internal_ids, new_ids).into_set_buf();
self.put_internal_ids(writer, &internal_ids)
}
pub fn remove_internal_ids(self, writer: &mut heed::RwTxn<MainT>, ids: &sdset::Set<DocumentId>) -> ZResult<()> {
use sdset::SetOperation;
// We do a difference of the old and new internal ids.
let internal_ids = self.internal_ids(writer)?;
let internal_ids = sdset::duo::Difference::new(&internal_ids, ids).into_set_buf();
2020-05-19 11:45:46 +02:00
self.put_internal_ids(writer, &internal_ids)
}
pub fn put_user_ids(self, writer: &mut heed::RwTxn<MainT>, ids: &fst::Map) -> ZResult<()> {
self.main.put::<_, Str, ByteSlice>(writer, USER_IDS_KEY, ids.as_fst().as_bytes())
}
2020-05-19 11:45:46 +02:00
pub fn merge_user_ids(self, writer: &mut heed::RwTxn<MainT>, new_ids: &fst::Map) -> ZResult<()> {
use fst::{Streamer, IntoStreamer};
2020-05-19 13:12:02 +02:00
// Do an union of the old and the new set of user ids.
2020-05-19 11:45:46 +02:00
let user_ids = self.user_ids(writer)?;
2020-05-19 13:12:02 +02:00
let mut op = user_ids.op().add(new_ids.into_stream()).r#union();
let mut build = fst::MapBuilder::memory();
while let Some((userid, values)) = op.next() {
build.insert(userid, values[0].value).unwrap();
}
let user_ids = build.into_inner().unwrap();
// TODO prefer using self.put_user_ids
self.main.put::<_, Str, ByteSlice>(writer, USER_IDS_KEY, user_ids.as_slice())
}
pub fn remove_user_ids(self, writer: &mut heed::RwTxn<MainT>, ids: &fst::Map) -> ZResult<()> {
use fst::{Streamer, IntoStreamer};
2020-05-19 11:45:46 +02:00
// Do an union of the old and the new set of user ids.
2020-05-19 13:12:02 +02:00
let user_ids = self.user_ids(writer)?;
let mut op = user_ids.op().add(ids.into_stream()).difference();
2020-05-19 11:45:46 +02:00
let mut build = fst::MapBuilder::memory();
while let Some((userid, values)) = op.next() {
build.insert(userid, values[0].value).unwrap();
}
let user_ids = build.into_inner().unwrap();
// TODO prefer using self.put_user_ids
self.main.put::<_, Str, ByteSlice>(writer, USER_IDS_KEY, user_ids.as_slice())
}
pub fn user_ids(self, reader: &heed::RoTxn<MainT>) -> ZResult<fst::Map> {
match self.main.get::<_, Str, ByteSlice>(reader, USER_IDS_KEY)? {
Some(bytes) => {
let len = bytes.len();
let bytes = Arc::new(bytes.to_owned());
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap();
Ok(fst::Map::from(fst))
},
None => Ok(fst::Map::default()),
}
}
2020-05-19 13:12:02 +02:00
pub fn user_to_internal_id(self, reader: &heed::RoTxn<MainT>, userid: &str) -> ZResult<Option<DocumentId>> {
let user_ids = self.user_ids(reader)?;
Ok(user_ids.get(userid).map(|id| DocumentId(id as u32)))
2020-05-19 13:12:02 +02:00
}
pub fn put_words_fst(self, writer: &mut heed::RwTxn<MainT>, fst: &fst::Set) -> ZResult<()> {
self.main.put::<_, Str, ByteSlice>(writer, WORDS_KEY, fst.as_fst().as_bytes())
2019-10-03 15:04:11 +02:00
}
pub unsafe fn static_words_fst(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<fst::Set>> {
match self.main.get::<_, Str, ByteSlice>(reader, WORDS_KEY)? {
Some(bytes) => {
let bytes: &'static [u8] = std::mem::transmute(bytes);
let set = fst::Set::from_static_slice(bytes).unwrap();
Ok(Some(set))
},
None => Ok(None),
}
}
pub fn words_fst(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<fst::Set>> {
match self.main.get::<_, Str, ByteSlice>(reader, WORDS_KEY)? {
2019-10-16 17:05:24 +02:00
Some(bytes) => {
2019-10-03 15:04:11 +02:00
let len = bytes.len();
2019-11-17 11:14:01 +01:00
let bytes = Arc::new(bytes.to_owned());
2019-10-16 17:05:24 +02:00
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap();
2019-10-03 15:04:11 +02:00
Ok(Some(fst::Set::from(fst)))
},
2019-10-03 15:04:11 +02:00
None => Ok(None),
}
}
pub fn put_schema(self, writer: &mut heed::RwTxn<MainT>, schema: &Schema) -> ZResult<()> {
self.main.put::<_, Str, SerdeBincode<Schema>>(writer, SCHEMA_KEY, schema)
2019-10-04 17:23:46 +02:00
}
pub fn schema(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<Schema>> {
self.main.get::<_, Str, SerdeBincode<Schema>>(reader, SCHEMA_KEY)
}
pub fn delete_schema(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<bool> {
self.main.delete::<_, Str>(writer, SCHEMA_KEY)
2019-10-04 17:23:46 +02:00
}
pub fn put_ranked_map(self, writer: &mut heed::RwTxn<MainT>, ranked_map: &RankedMap) -> ZResult<()> {
self.main.put::<_, Str, SerdeBincode<RankedMap>>(writer, RANKED_MAP_KEY, &ranked_map)
2019-10-03 15:04:11 +02:00
}
pub fn ranked_map(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<RankedMap>> {
self.main.get::<_, Str, SerdeBincode<RankedMap>>(reader, RANKED_MAP_KEY)
2019-10-03 15:04:11 +02:00
}
pub fn put_synonyms_fst(self, writer: &mut heed::RwTxn<MainT>, fst: &fst::Set) -> ZResult<()> {
2019-10-16 17:05:24 +02:00
let bytes = fst.as_fst().as_bytes();
self.main.put::<_, Str, ByteSlice>(writer, SYNONYMS_KEY, bytes)
}
pub fn synonyms_fst(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<fst::Set>> {
match self.main.get::<_, Str, ByteSlice>(reader, SYNONYMS_KEY)? {
2019-10-16 17:05:24 +02:00
Some(bytes) => {
let len = bytes.len();
2019-11-17 11:14:01 +01:00
let bytes = Arc::new(bytes.to_owned());
2019-10-16 17:05:24 +02:00
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap();
Ok(Some(fst::Set::from(fst)))
}
None => Ok(None),
}
}
pub fn put_stop_words_fst(self, writer: &mut heed::RwTxn<MainT>, fst: &fst::Set) -> ZResult<()> {
let bytes = fst.as_fst().as_bytes();
2020-01-18 16:12:02 +01:00
self.main.put::<_, Str, ByteSlice>(writer, STOP_WORDS_KEY, bytes)
}
pub fn stop_words_fst(self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<fst::Set>> {
match self.main.get::<_, Str, ByteSlice>(reader, STOP_WORDS_KEY)? {
Some(bytes) => {
let len = bytes.len();
2019-11-17 11:14:01 +01:00
let bytes = Arc::new(bytes.to_owned());
let fst = fst::raw::Fst::from_shared_bytes(bytes, 0, len).unwrap();
Ok(Some(fst::Set::from(fst)))
2019-10-18 13:05:28 +02:00
}
None => Ok(None),
}
}
pub fn put_number_of_documents<F>(self, writer: &mut heed::RwTxn<MainT>, f: F) -> ZResult<u64>
2019-10-18 13:05:28 +02:00
where
F: Fn(u64) -> u64,
2019-10-03 15:04:11 +02:00
{
let new = self.number_of_documents(&*writer).map(f)?;
2019-10-18 13:05:28 +02:00
self.main
.put::<_, Str, OwnedType<u64>>(writer, NUMBER_OF_DOCUMENTS_KEY, &new)?;
2019-10-03 16:13:09 +02:00
Ok(new)
}
pub fn number_of_documents(self, reader: &heed::RoTxn<MainT>) -> ZResult<u64> {
2019-10-18 13:05:28 +02:00
match self
.main
.get::<_, Str, OwnedType<u64>>(reader, NUMBER_OF_DOCUMENTS_KEY)?
2019-10-18 13:05:28 +02:00
{
2019-10-16 17:05:24 +02:00
Some(value) => Ok(value),
2019-10-03 16:13:09 +02:00
None => Ok(0),
}
2019-10-03 15:04:11 +02:00
}
2019-10-11 15:33:35 +02:00
2019-11-20 11:24:08 +01:00
pub fn put_fields_frequency(
self,
writer: &mut heed::RwTxn<MainT>,
2019-11-20 11:24:08 +01:00
fields_frequency: &FreqsMap,
) -> ZResult<()> {
self.main
.put::<_, Str, SerdeFreqsMap>(writer, FIELDS_FREQUENCY_KEY, fields_frequency)
}
pub fn fields_frequency(&self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<FreqsMap>> {
match self
.main
.get::<_, Str, SerdeFreqsMap>(reader, FIELDS_FREQUENCY_KEY)?
{
Some(freqs) => Ok(Some(freqs)),
None => Ok(None),
}
}
2020-05-05 22:19:34 +02:00
pub fn attributes_for_faceting<'txn>(&self, reader: &'txn heed::RoTxn<MainT>) -> ZResult<Option<Cow<'txn, Set<FieldId>>>> {
self.main.get::<_, Str, CowSet<FieldId>>(reader, ATTRIBUTES_FOR_FACETING)
}
pub fn put_attributes_for_faceting(self, writer: &mut heed::RwTxn<MainT>, attributes: &Set<FieldId>) -> ZResult<()> {
self.main.put::<_, Str, CowSet<FieldId>>(writer, ATTRIBUTES_FOR_FACETING, attributes)
}
pub fn delete_attributes_for_faceting(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<bool> {
self.main.delete::<_, Str>(writer, ATTRIBUTES_FOR_FACETING)
}
2020-01-29 18:30:21 +01:00
pub fn ranking_rules(&self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<Vec<RankingRule>>> {
self.main.get::<_, Str, SerdeBincode<Vec<RankingRule>>>(reader, RANKING_RULES_KEY)
2020-01-08 14:17:38 +01:00
}
2020-02-11 15:16:02 +01:00
pub fn put_ranking_rules(self, writer: &mut heed::RwTxn<MainT>, value: &[RankingRule]) -> ZResult<()> {
self.main.put::<_, Str, SerdeBincode<Vec<RankingRule>>>(writer, RANKING_RULES_KEY, &value.to_vec())
2020-01-08 14:17:38 +01:00
}
pub fn delete_ranking_rules(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<bool> {
self.main.delete::<_, Str>(writer, RANKING_RULES_KEY)
}
pub fn distinct_attribute(&self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<String>> {
if let Some(value) = self.main.get::<_, Str, Str>(reader, DISTINCT_ATTRIBUTE_KEY)? {
2020-02-11 15:16:02 +01:00
return Ok(Some(value.to_owned()))
}
return Ok(None)
2020-01-08 14:17:38 +01:00
}
pub fn put_distinct_attribute(self, writer: &mut heed::RwTxn<MainT>, value: &str) -> ZResult<()> {
self.main.put::<_, Str, Str>(writer, DISTINCT_ATTRIBUTE_KEY, value)
2020-01-08 14:17:38 +01:00
}
pub fn delete_distinct_attribute(self, writer: &mut heed::RwTxn<MainT>) -> ZResult<bool> {
self.main.delete::<_, Str>(writer, DISTINCT_ATTRIBUTE_KEY)
2020-01-08 14:17:38 +01:00
}
pub fn put_customs(self, writer: &mut heed::RwTxn<MainT>, customs: &[u8]) -> ZResult<()> {
2019-10-18 13:05:28 +02:00
self.main
.put::<_, Str, ByteSlice>(writer, CUSTOMS_KEY, customs)
2019-10-11 15:33:35 +02:00
}
pub fn customs<'txn>(self, reader: &'txn heed::RoTxn<MainT>) -> ZResult<Option<&'txn [u8]>> {
self.main.get::<_, Str, ByteSlice>(reader, CUSTOMS_KEY)
2019-10-11 15:33:35 +02:00
}
2019-10-03 15:04:11 +02:00
}