meilisearch/meilisearch-schema/src/schema.rs

203 lines
5.5 KiB
Rust
Raw Normal View History

2020-01-11 01:20:30 +08:00
use std::collections::{HashMap, HashSet};
2020-01-14 02:10:58 +08:00
use serde::{Serialize, Deserialize};
2020-01-11 01:20:30 +08:00
2020-01-14 02:10:58 +08:00
use crate::{FieldsMap, FieldId, SResult, Error, IndexedPos};
2020-01-11 01:20:30 +08:00
2020-01-27 15:52:36 +08:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2020-01-11 01:20:30 +08:00
pub struct Schema {
fields_map: FieldsMap,
identifier: FieldId,
ranked: HashSet<FieldId>,
displayed: HashSet<FieldId>,
indexed: Vec<FieldId>,
indexed_map: HashMap<FieldId, IndexedPos>,
2020-01-27 15:52:36 +08:00
2020-01-30 01:30:21 +08:00
index_new_fields: bool,
2020-01-11 01:20:30 +08:00
}
impl Schema {
2020-01-30 01:30:21 +08:00
pub fn with_identifier(name: &str) -> Schema {
2020-01-27 15:52:36 +08:00
let mut fields_map = FieldsMap::default();
let field_id = fields_map.insert(name.into()).unwrap();
Schema {
fields_map,
identifier: field_id,
ranked: HashSet::new(),
displayed: HashSet::new(),
indexed: Vec::new(),
indexed_map: HashMap::new(),
2020-01-30 01:30:21 +08:00
index_new_fields: true,
2020-01-27 15:52:36 +08:00
}
2020-01-11 01:20:30 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn identifier(&self) -> &str {
self.fields_map.name(self.identifier).unwrap()
2020-01-11 01:20:30 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn set_identifier(&mut self, id: &str) -> SResult<()> {
match self.id(id) {
2020-01-14 02:10:58 +08:00
Some(id) => {
self.identifier = id;
Ok(())
},
2020-01-30 01:30:21 +08:00
None => Err(Error::FieldNameNotFound(id.to_string()))
2020-01-14 02:10:58 +08:00
}
}
2020-01-30 01:30:21 +08:00
pub fn id(&self, name: &str) -> Option<FieldId> {
self.fields_map.id(name)
2020-01-11 01:20:30 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn name<I: Into<FieldId>>(&self, id: I) -> Option<&str> {
self.fields_map.name(id)
2020-01-11 01:20:30 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn contains(&self, name: &str) -> bool {
self.fields_map.id(name.into()).is_some()
2020-01-11 01:20:30 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn get_or_create_empty(&mut self, name: &str) -> SResult<FieldId> {
2020-01-11 01:20:30 +08:00
self.fields_map.insert(name)
}
2020-01-30 01:30:21 +08:00
pub fn get_or_create(&mut self, name: &str) -> SResult<FieldId> {
match self.fields_map.id(name.clone()) {
2020-01-11 01:20:30 +08:00
Some(id) => {
2020-01-14 02:10:58 +08:00
Ok(id)
2020-01-11 01:20:30 +08:00
}
None => {
2020-01-30 01:30:21 +08:00
if self.index_new_fields {
2020-01-27 15:52:36 +08:00
self.set_indexed(name.clone())?;
self.set_displayed(name)
} else {
self.fields_map.insert(name.clone())
}
2020-01-11 01:20:30 +08:00
}
}
}
2020-01-30 01:30:21 +08:00
pub fn ranked(&self) -> HashSet<FieldId> {
2020-01-14 02:10:58 +08:00
self.ranked.clone()
}
2020-01-30 01:30:21 +08:00
pub fn ranked_name(&self) -> HashSet<&str> {
self.ranked.iter().filter_map(|a| self.name(*a)).collect()
2020-01-14 02:10:58 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn displayed(&self) -> HashSet<FieldId> {
2020-01-14 02:10:58 +08:00
self.displayed.clone()
}
2020-01-30 01:30:21 +08:00
pub fn displayed_name(&self) -> HashSet<&str> {
self.displayed.iter().filter_map(|a| self.name(*a)).collect()
2020-01-14 02:10:58 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn indexed(&self) -> Vec<FieldId> {
2020-01-14 02:10:58 +08:00
self.indexed.clone()
}
2020-01-30 01:30:21 +08:00
pub fn indexed_name(&self) -> Vec<&str> {
self.indexed.iter().filter_map(|a| self.name(*a)).collect()
2020-01-14 02:10:58 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn set_ranked(&mut self, name: &str) -> SResult<FieldId> {
2020-01-11 01:20:30 +08:00
let id = self.fields_map.insert(name.into())?;
self.ranked.insert(id);
Ok(id)
}
2020-01-30 01:30:21 +08:00
pub fn set_displayed(&mut self, name: &str) -> SResult<FieldId> {
2020-01-11 01:20:30 +08:00
let id = self.fields_map.insert(name.into())?;
self.displayed.insert(id);
Ok(id)
}
2020-01-30 01:30:21 +08:00
pub fn set_indexed(&mut self, name: &str) -> SResult<(FieldId, IndexedPos)> {
2020-01-11 01:20:30 +08:00
let id = self.fields_map.insert(name.into())?;
2020-01-16 01:53:49 +08:00
if let Some(indexed_pos) = self.indexed_map.get(&id) {
return Ok((id, *indexed_pos))
};
2020-01-11 01:20:30 +08:00
let pos = self.indexed.len() as u16;
self.indexed.push(id);
self.indexed_map.insert(id, pos.into());
Ok((id, pos.into()))
}
2020-01-30 01:30:21 +08:00
pub fn remove_ranked(&mut self, name: &str) {
if let Some(id) = self.fields_map.id(name.into()) {
2020-01-14 02:10:58 +08:00
self.ranked.remove(&id);
}
}
2020-01-30 01:30:21 +08:00
pub fn remove_displayed(&mut self, name: &str) {
if let Some(id) = self.fields_map.id(name.into()) {
2020-01-14 02:10:58 +08:00
self.displayed.remove(&id);
}
}
2020-01-30 01:30:21 +08:00
pub fn remove_indexed(&mut self, name: &str) {
if let Some(id) = self.fields_map.id(name.into()) {
2020-01-14 02:10:58 +08:00
self.indexed_map.remove(&id);
self.indexed.retain(|x| *x != id);
}
}
2020-01-30 01:30:21 +08:00
pub fn is_ranked(&self, id: FieldId) -> bool {
2020-01-11 01:20:30 +08:00
self.ranked.get(&id).is_some()
}
2020-01-30 01:30:21 +08:00
pub fn is_displayed(&self, id: FieldId) -> bool {
2020-01-11 01:20:30 +08:00
self.displayed.get(&id).is_some()
}
2020-01-30 01:30:21 +08:00
pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
2020-01-11 01:20:30 +08:00
self.indexed_map.get(&id)
}
pub fn indexed_pos_to_field_id<I: Into<IndexedPos>>(&self, pos: I) -> Option<FieldId> {
let indexed_pos = pos.into().0 as usize;
if indexed_pos < self.indexed.len() {
Some(self.indexed[indexed_pos as usize])
} else {
None
}
}
2020-01-30 01:30:21 +08:00
pub fn update_ranked<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
2020-01-11 01:20:30 +08:00
self.ranked = HashSet::new();
for name in data {
2020-01-30 01:30:21 +08:00
self.set_ranked(name.as_ref())?;
2020-01-11 01:20:30 +08:00
}
Ok(())
}
2020-01-30 01:30:21 +08:00
pub fn update_displayed<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
2020-01-11 01:20:30 +08:00
self.displayed = HashSet::new();
for name in data {
2020-01-30 01:30:21 +08:00
self.set_displayed(name.as_ref())?;
2020-01-11 01:20:30 +08:00
}
Ok(())
}
2020-01-30 01:30:21 +08:00
pub fn update_indexed<S: AsRef<str>>(&mut self, data: Vec<S>) -> SResult<()> {
2020-01-11 01:20:30 +08:00
self.indexed = Vec::new();
self.indexed_map = HashMap::new();
for name in data {
2020-01-30 01:30:21 +08:00
self.set_indexed(name.as_ref())?;
2020-01-11 01:20:30 +08:00
}
Ok(())
}
2020-01-27 15:52:36 +08:00
2020-01-30 01:30:21 +08:00
pub fn index_new_fields(&self) -> bool {
self.index_new_fields
2020-01-27 15:52:36 +08:00
}
2020-01-30 01:30:21 +08:00
pub fn set_index_new_fields(&mut self, value: bool) {
self.index_new_fields = value;
2020-01-27 15:52:36 +08:00
}
2020-01-11 01:20:30 +08:00
}