MeiliSearch/meilisearch-schema/src/schema.rs

369 lines
11 KiB
Rust
Raw Normal View History

2020-01-13 19:10:58 +01:00
use crate::{FieldsMap, FieldId, SResult, Error, IndexedPos};
2020-02-11 15:16:02 +01:00
use serde::{Serialize, Deserialize};
use std::collections::{HashMap, HashSet};
2020-07-08 21:01:26 +02:00
use std::borrow::Cow;
#[derive(Clone, Debug, Serialize, Deserialize)]
enum OptionAll<T> {
All,
Some(T),
None,
}
impl<T> OptionAll<T> {
// replace the value with None and return the previous value
fn take(&mut self) -> OptionAll<T> {
std::mem::replace(self, OptionAll::None)
}
fn map<U, F: FnOnce(T) -> U>(self, f: F) -> OptionAll<U> {
match self {
OptionAll::Some(x) => OptionAll::Some(f(x)),
OptionAll::All => OptionAll::All,
OptionAll::None => OptionAll::None,
}
}
pub fn is_all(&self) -> bool {
2020-10-13 10:01:57 +02:00
matches!(self, OptionAll::All)
}
2020-07-08 21:01:26 +02:00
}
impl<T> Default for OptionAll<T> {
fn default() -> OptionAll<T> {
OptionAll::All
}
}
2020-01-10 18:20:30 +01:00
2020-06-26 22:09:34 +02:00
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
2020-01-10 18:20:30 +01:00
pub struct Schema {
fields_map: FieldsMap,
primary_key: Option<FieldId>,
2020-01-10 18:20:30 +01:00
ranked: HashSet<FieldId>,
2020-07-08 21:01:26 +02:00
displayed: OptionAll<HashSet<FieldId>>,
2020-01-10 18:20:30 +01:00
indexed: OptionAll<HashSet<FieldId>>,
2020-01-10 18:20:30 +01:00
indexed_map: HashMap<FieldId, IndexedPos>,
}
impl Schema {
pub fn new() -> Schema {
Schema::default()
}
pub fn with_primary_key(name: &str) -> Schema {
2020-01-27 08:52:36 +01:00
let mut fields_map = FieldsMap::default();
2020-02-02 22:59:19 +01:00
let field_id = fields_map.insert(name).unwrap();
2020-01-27 08:52:36 +01:00
2020-03-04 15:58:36 +01:00
let mut displayed = HashSet::new();
let mut indexed_map = HashMap::new();
displayed.insert(field_id);
indexed_map.insert(field_id, 0.into());
2020-01-27 08:52:36 +01:00
Schema {
fields_map,
primary_key: Some(field_id),
2020-01-27 08:52:36 +01:00
ranked: HashSet::new(),
2020-07-08 21:01:26 +02:00
displayed: OptionAll::All,
indexed: OptionAll::All,
2020-03-04 15:58:36 +01:00
indexed_map,
2020-01-27 08:52:36 +01:00
}
2020-01-10 18:20:30 +01:00
}
pub fn primary_key(&self) -> Option<&str> {
self.primary_key.map(|id| self.fields_map.name(id).unwrap())
2020-01-10 18:20:30 +01:00
}
pub fn set_primary_key(&mut self, name: &str) -> SResult<FieldId> {
if self.primary_key.is_some() {
return Err(Error::PrimaryKeyAlreadyPresent)
2020-01-13 19:10:58 +01:00
}
let id = self.insert(name)?;
self.primary_key = Some(id);
self.set_indexed(name)?;
self.set_displayed(name)?;
Ok(id)
2020-01-13 19:10:58 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn id(&self, name: &str) -> Option<FieldId> {
self.fields_map.id(name)
2020-01-10 18:20:30 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn name<I: Into<FieldId>>(&self, id: I) -> Option<&str> {
self.fields_map.name(id)
2020-01-10 18:20:30 +01:00
}
2020-04-06 20:05:31 +02:00
pub fn names(&self) -> impl Iterator<Item = &str> {
self.fields_map.iter().map(|(k, _)| k.as_ref())
}
2020-01-29 18:30:21 +01:00
pub fn contains(&self, name: &str) -> bool {
2020-02-02 22:59:19 +01:00
self.fields_map.id(name).is_some()
2020-01-10 18:20:30 +01:00
}
2020-02-11 15:16:02 +01:00
pub fn insert(&mut self, name: &str) -> SResult<FieldId> {
2020-01-10 18:20:30 +01:00
self.fields_map.insert(name)
}
2020-02-11 15:16:02 +01:00
pub fn insert_and_index(&mut self, name: &str) -> SResult<FieldId> {
2020-02-02 22:59:19 +01:00
match self.fields_map.id(name) {
2020-01-10 18:20:30 +01:00
Some(id) => {
2020-01-13 19:10:58 +01:00
Ok(id)
2020-01-10 18:20:30 +01:00
}
None => {
let id = self.fields_map.insert(name)?;
let pos = self.indexed_map.len() as u16;
self.indexed_map.insert(id, pos.into());
Ok(id)
2020-01-10 18:20:30 +01:00
}
}
}
2020-02-11 15:16:02 +01:00
pub fn ranked(&self) -> &HashSet<FieldId> {
&self.ranked
2020-01-13 19:10:58 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn ranked_name(&self) -> HashSet<&str> {
self.ranked.iter().filter_map(|a| self.name(*a)).collect()
2020-01-13 19:10:58 +01:00
}
2020-07-08 21:01:26 +02:00
pub fn displayed(&self) -> Cow<HashSet<FieldId>> {
match self.displayed {
OptionAll::Some(ref v) => Cow::Borrowed(v),
OptionAll::All => {
let fields = self
.fields_map
.iter()
.map(|(_, &v)| v)
.collect::<HashSet<_>>();
Cow::Owned(fields)
}
OptionAll::None => Cow::Owned(HashSet::new())
}
}
pub fn is_displayed_all(&self) -> bool {
self.displayed.is_all()
2020-01-13 19:10:58 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn displayed_name(&self) -> HashSet<&str> {
2020-07-08 21:01:26 +02:00
match self.displayed {
OptionAll::All => self.fields_map.iter().filter_map(|(_, &v)| self.name(v)).collect(),
OptionAll::Some(ref v) => v.iter().filter_map(|a| self.name(*a)).collect(),
OptionAll::None => HashSet::new(),
}
}
pub fn indexed(&self) -> Vec<FieldId> {
match self.indexed {
OptionAll::Some(ref v) => v.iter().cloned().collect(),
2020-07-08 21:01:26 +02:00
OptionAll::All => {
let fields = self
.indexed_map
2020-07-08 21:01:26 +02:00
.iter()
.map(|(&f, _)| f)
.collect();
fields
},
OptionAll::None => Vec::new()
2020-07-08 21:01:26 +02:00
}
2020-01-13 19:10:58 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn indexed_name(&self) -> Vec<&str> {
self.indexed().iter().filter_map(|a| self.name(*a)).collect()
2020-01-13 19:10:58 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn set_ranked(&mut self, name: &str) -> SResult<FieldId> {
2020-02-02 22:59:19 +01:00
let id = self.fields_map.insert(name)?;
2020-01-10 18:20:30 +01:00
self.ranked.insert(id);
Ok(id)
}
2020-01-29 18:30:21 +01:00
pub fn set_displayed(&mut self, name: &str) -> SResult<FieldId> {
2020-02-02 22:59:19 +01:00
let id = self.fields_map.insert(name)?;
2020-07-08 21:01:26 +02:00
self.displayed = match self.displayed.take() {
OptionAll::All => OptionAll::All,
OptionAll::None => {
2020-07-08 21:01:26 +02:00
let mut displayed = HashSet::new();
displayed.insert(id);
OptionAll::Some(displayed)
},
OptionAll::Some(mut v) => {
v.insert(id);
OptionAll::Some(v)
}
};
2020-01-10 18:20:30 +01:00
Ok(id)
}
2020-01-29 18:30:21 +01:00
pub fn set_indexed(&mut self, name: &str) -> SResult<(FieldId, IndexedPos)> {
2020-02-02 22:59:19 +01:00
let id = self.fields_map.insert(name)?;
println!("setting {} indexed", name);
self.indexed = self.indexed.take().map(|mut v| {
v.insert(id);
v
});
2020-01-15 18:53:49 +01:00
if let Some(indexed_pos) = self.indexed_map.get(&id) {
return Ok((id, *indexed_pos))
};
let pos = self.indexed_map.len() as u16;
2020-01-10 18:20:30 +01:00
self.indexed_map.insert(id, pos.into());
Ok((id, pos.into()))
}
2020-03-02 14:34:29 +01:00
pub fn clear_ranked(&mut self) {
self.ranked.clear();
}
2020-01-29 18:30:21 +01:00
pub fn remove_ranked(&mut self, name: &str) {
2020-02-02 22:59:19 +01:00
if let Some(id) = self.fields_map.id(name) {
2020-01-13 19:10:58 +01:00
self.ranked.remove(&id);
}
}
2020-07-08 21:01:26 +02:00
/// remove field from displayed attributes. If diplayed attributes is OptionAll::All,
/// dipslayed attributes is turned into OptionAll::Some(v) where v is all displayed attributes
/// except name.
2020-01-29 18:30:21 +01:00
pub fn remove_displayed(&mut self, name: &str) {
2020-02-02 22:59:19 +01:00
if let Some(id) = self.fields_map.id(name) {
2020-07-08 21:01:26 +02:00
self.displayed = match self.displayed.take() {
OptionAll::Some(mut v) => {
v.remove(&id);
OptionAll::Some(v)
}
OptionAll::All => {
let displayed = self.fields_map
.iter()
.filter_map(|(key, &value)| {
if key != name {
Some(value)
} else {
None
}
})
.collect::<HashSet<_>>();
OptionAll::Some(displayed)
}
OptionAll::None => OptionAll::None,
};
2020-01-13 19:10:58 +01:00
}
}
2020-01-29 18:30:21 +01:00
pub fn remove_indexed(&mut self, name: &str) {
2020-02-02 22:59:19 +01:00
if let Some(id) = self.fields_map.id(name) {
2020-01-13 19:10:58 +01:00
self.indexed_map.remove(&id);
self.indexed = match self.indexed.take() {
// valid because indexed is All and indexed() return the content of
// indexed_map that is already updated
OptionAll::All => {
let indexed = self.indexed_map
.iter()
.filter_map(|(&i, _)| if i != id { Some(i) } else { None })
.collect();
OptionAll::Some(indexed)
},
OptionAll::Some(mut v) => {
v.retain(|x| *x != id);
OptionAll::Some(v)
}
OptionAll::None => OptionAll::None,
}
2020-01-13 19:10:58 +01:00
}
}
2020-01-29 18:30:21 +01:00
pub fn is_ranked(&self, id: FieldId) -> bool {
2020-01-10 18:20:30 +01:00
self.ranked.get(&id).is_some()
}
2020-01-29 18:30:21 +01:00
pub fn is_displayed(&self, id: FieldId) -> bool {
2020-07-08 21:01:26 +02:00
match self.displayed {
2020-07-16 16:12:23 +02:00
OptionAll::Some(ref v) => v.contains(&id),
2020-07-08 21:01:26 +02:00
OptionAll::All => true,
OptionAll::None => false,
}
2020-01-10 18:20:30 +01:00
}
2020-01-29 18:30:21 +01:00
pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
match self.indexed {
OptionAll::All => self.indexed_map.get(&id),
OptionAll::Some(ref v) => {
if v.contains(&id) {
self.indexed_map.get(&id)
} else {
None
}
}
OptionAll::None => None
}
2020-01-10 18:20:30 +01:00
}
pub fn is_indexed_all(&self) -> bool {
self.indexed.is_all()
}
pub fn indexed_pos_to_field_id<I: Into<IndexedPos>>(&self, pos: I) -> Option<FieldId> {
let indexed_pos = pos.into().0;
self
.indexed_map
.iter()
.find(|(_, &v)| v.0 == indexed_pos)
.map(|(&k, _)| k)
}
2020-01-29 18:30:21 +01:00
pub fn update_ranked<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
2020-02-11 15:16:02 +01:00
self.ranked.clear();
2020-01-10 18:20:30 +01:00
for name in data {
2020-01-29 18:30:21 +01:00
self.set_ranked(name.as_ref())?;
2020-01-10 18:20:30 +01:00
}
Ok(())
}
2020-01-29 18:30:21 +01:00
pub fn update_displayed<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
self.displayed = match self.displayed.take() {
OptionAll::Some(mut v) => {
v.clear();
OptionAll::Some(v)
}
_ => OptionAll::Some(HashSet::new())
};
2020-01-10 18:20:30 +01:00
for name in data {
2020-01-29 18:30:21 +01:00
self.set_displayed(name.as_ref())?;
2020-01-10 18:20:30 +01:00
}
Ok(())
}
2020-01-29 18:30:21 +01:00
pub fn update_indexed<S: AsRef<str>>(&mut self, data: Vec<S>) -> SResult<()> {
self.indexed = match self.indexed.take() {
OptionAll::Some(mut v) => {
v.clear();
OptionAll::Some(v)
},
_ => OptionAll::Some(HashSet::new()),
};
2020-01-10 18:20:30 +01:00
for name in data {
2020-01-29 18:30:21 +01:00
self.set_indexed(name.as_ref())?;
2020-01-10 18:20:30 +01:00
}
Ok(())
}
2020-01-27 08:52:36 +01:00
2020-02-26 18:49:17 +01:00
pub fn set_all_fields_as_indexed(&mut self) {
self.indexed = OptionAll::All;
self.indexed_map.clear();
for (_name, id) in self.fields_map.iter() {
let pos = self.indexed_map.len() as u16;
self.indexed_map.insert(*id, pos.into());
}
}
2020-02-26 18:49:17 +01:00
pub fn set_all_fields_as_displayed(&mut self) {
2020-07-08 21:01:26 +02:00
self.displayed = OptionAll::All
}
2020-01-10 18:20:30 +01:00
}