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-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,
|
|
|
|
|
2020-03-09 18:40:49 +01:00
|
|
|
primary_key: Option<FieldId>,
|
2020-01-10 18:20:30 +01:00
|
|
|
ranked: HashSet<FieldId>,
|
|
|
|
displayed: HashSet<FieldId>,
|
|
|
|
|
|
|
|
indexed: Vec<FieldId>,
|
|
|
|
indexed_map: HashMap<FieldId, IndexedPos>,
|
2020-01-27 08:52:36 +01:00
|
|
|
|
2020-02-25 15:51:37 +01:00
|
|
|
accept_new_fields: bool,
|
2020-01-10 18:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Schema {
|
2020-03-05 18:29:10 +01:00
|
|
|
pub fn new() -> Schema {
|
|
|
|
Schema {
|
|
|
|
accept_new_fields: true,
|
2020-06-26 22:09:34 +02:00
|
|
|
..Default::default()
|
2020-03-05 18:29:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:40:49 +01:00
|
|
|
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 = Vec::new();
|
|
|
|
let mut indexed_map = HashMap::new();
|
|
|
|
|
|
|
|
displayed.insert(field_id);
|
|
|
|
indexed.push(field_id);
|
|
|
|
indexed_map.insert(field_id, 0.into());
|
|
|
|
|
2020-01-27 08:52:36 +01:00
|
|
|
Schema {
|
|
|
|
fields_map,
|
2020-03-09 18:40:49 +01:00
|
|
|
primary_key: Some(field_id),
|
2020-01-27 08:52:36 +01:00
|
|
|
ranked: HashSet::new(),
|
2020-03-04 15:58:36 +01:00
|
|
|
displayed,
|
|
|
|
indexed,
|
|
|
|
indexed_map,
|
2020-02-25 15:51:37 +01:00
|
|
|
accept_new_fields: true,
|
2020-01-27 08:52:36 +01:00
|
|
|
}
|
2020-01-10 18:20:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 18:40:49 +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
|
|
|
}
|
|
|
|
|
2020-03-09 18:40:49 +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
|
|
|
}
|
2020-03-05 18:29:10 +01:00
|
|
|
|
|
|
|
let id = self.insert(name)?;
|
2020-03-09 18:40:49 +01:00
|
|
|
self.primary_key = Some(id);
|
2020-03-11 12:00:40 +01:00
|
|
|
if self.accept_new_fields {
|
|
|
|
self.set_indexed(name)?;
|
|
|
|
self.set_displayed(name)?;
|
|
|
|
}
|
2020-03-05 18:29:10 +01:00
|
|
|
|
|
|
|
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 => {
|
2020-02-25 15:51:37 +01:00
|
|
|
if self.accept_new_fields {
|
2020-02-02 22:59:19 +01:00
|
|
|
self.set_indexed(name)?;
|
2020-01-27 08:52:36 +01:00
|
|
|
self.set_displayed(name)
|
|
|
|
} else {
|
2020-02-02 22:59:19 +01:00
|
|
|
self.fields_map.insert(name)
|
2020-01-27 08:52:36 +01:00
|
|
|
}
|
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-02-11 15:16:02 +01:00
|
|
|
pub fn displayed(&self) -> &HashSet<FieldId> {
|
|
|
|
&self.displayed
|
2020-01-13 19:10:58 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
pub fn displayed_name(&self) -> HashSet<&str> {
|
|
|
|
self.displayed.iter().filter_map(|a| self.name(*a)).collect()
|
2020-01-13 19:10:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 15:16:02 +01:00
|
|
|
pub fn indexed(&self) -> &Vec<FieldId> {
|
|
|
|
&self.indexed
|
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-01-10 18:20:30 +01:00
|
|
|
self.displayed.insert(id);
|
|
|
|
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)?;
|
2020-01-15 18:53:49 +01:00
|
|
|
if let Some(indexed_pos) = self.indexed_map.get(&id) {
|
|
|
|
return Ok((id, *indexed_pos))
|
|
|
|
};
|
2020-01-10 18:20:30 +01:00
|
|
|
let pos = self.indexed.len() as u16;
|
|
|
|
self.indexed.push(id);
|
|
|
|
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-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-01-13 19:10:58 +01:00
|
|
|
self.displayed.remove(&id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.retain(|x| *x != id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-10 18:20:30 +01:00
|
|
|
self.displayed.get(&id).is_some()
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:30:21 +01:00
|
|
|
pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
|
2020-01-10 18:20:30 +01:00
|
|
|
self.indexed_map.get(&id)
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:34:49 +01:00
|
|
|
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-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<()> {
|
2020-02-11 15:16:02 +01:00
|
|
|
self.displayed.clear();
|
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<()> {
|
2020-02-11 15:16:02 +01:00
|
|
|
self.indexed.clear();
|
|
|
|
self.indexed_map.clear();
|
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) {
|
2020-02-26 17:34:26 +01:00
|
|
|
self.indexed.clear();
|
|
|
|
self.indexed_map.clear();
|
|
|
|
|
|
|
|
for (_name, id) in self.fields_map.iter() {
|
|
|
|
let pos = self.indexed.len() as u16;
|
|
|
|
self.indexed.push(*id);
|
|
|
|
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-02-26 17:34:26 +01:00
|
|
|
self.displayed.clear();
|
|
|
|
|
|
|
|
for (_name, id) in self.fields_map.iter() {
|
|
|
|
self.displayed.insert(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:51:37 +01:00
|
|
|
pub fn accept_new_fields(&self) -> bool {
|
|
|
|
self.accept_new_fields
|
2020-01-27 08:52:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:51:37 +01:00
|
|
|
pub fn set_accept_new_fields(&mut self, value: bool) {
|
|
|
|
self.accept_new_fields = value;
|
2020-01-27 08:52:36 +01:00
|
|
|
}
|
2020-01-10 18:20:30 +01:00
|
|
|
}
|