2020-01-14 02:10:58 +08:00
|
|
|
use crate::{FieldsMap, FieldId, SResult, Error, IndexedPos};
|
2020-02-11 22:16:02 +08:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use std::collections::{HashMap, HashSet};
|
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-02-25 22:51:37 +08:00
|
|
|
accept_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();
|
2020-02-03 05:59:19 +08:00
|
|
|
let field_id = fields_map.insert(name).unwrap();
|
2020-01-27 15:52:36 +08:00
|
|
|
|
|
|
|
Schema {
|
|
|
|
fields_map,
|
|
|
|
identifier: field_id,
|
|
|
|
ranked: HashSet::new(),
|
|
|
|
displayed: HashSet::new(),
|
|
|
|
indexed: Vec::new(),
|
|
|
|
indexed_map: HashMap::new(),
|
2020-02-25 22:51:37 +08:00
|
|
|
accept_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 {
|
2020-02-03 05:59:19 +08:00
|
|
|
self.fields_map.id(name).is_some()
|
2020-01-11 01:20:30 +08:00
|
|
|
}
|
|
|
|
|
2020-02-11 22:16:02 +08:00
|
|
|
pub fn insert(&mut self, name: &str) -> SResult<FieldId> {
|
2020-01-11 01:20:30 +08:00
|
|
|
self.fields_map.insert(name)
|
|
|
|
}
|
|
|
|
|
2020-02-11 22:16:02 +08:00
|
|
|
pub fn insert_and_index(&mut self, name: &str) -> SResult<FieldId> {
|
2020-02-03 05:59:19 +08:00
|
|
|
match self.fields_map.id(name) {
|
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-02-25 22:51:37 +08:00
|
|
|
if self.accept_new_fields {
|
2020-02-03 05:59:19 +08:00
|
|
|
self.set_indexed(name)?;
|
2020-01-27 15:52:36 +08:00
|
|
|
self.set_displayed(name)
|
|
|
|
} else {
|
2020-02-03 05:59:19 +08:00
|
|
|
self.fields_map.insert(name)
|
2020-01-27 15:52:36 +08:00
|
|
|
}
|
2020-01-11 01:20:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 22:16:02 +08:00
|
|
|
pub fn ranked(&self) -> &HashSet<FieldId> {
|
|
|
|
&self.ranked
|
2020-01-14 02:10:58 +08:00
|
|
|
}
|
|
|
|
|
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-02-11 22:16:02 +08:00
|
|
|
pub fn displayed(&self) -> &HashSet<FieldId> {
|
|
|
|
&self.displayed
|
2020-01-14 02:10:58 +08:00
|
|
|
}
|
|
|
|
|
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-02-11 22:16:02 +08:00
|
|
|
pub fn indexed(&self) -> &Vec<FieldId> {
|
|
|
|
&self.indexed
|
2020-01-14 02:10:58 +08:00
|
|
|
}
|
|
|
|
|
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-02-03 05:59:19 +08:00
|
|
|
let id = self.fields_map.insert(name)?;
|
2020-01-11 01:20:30 +08:00
|
|
|
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-02-03 05:59:19 +08:00
|
|
|
let id = self.fields_map.insert(name)?;
|
2020-01-11 01:20:30 +08:00
|
|
|
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-02-03 05:59:19 +08:00
|
|
|
let id = self.fields_map.insert(name)?;
|
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) {
|
2020-02-03 05:59:19 +08:00
|
|
|
if let Some(id) = self.fields_map.id(name) {
|
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) {
|
2020-02-03 05:59:19 +08:00
|
|
|
if let Some(id) = self.fields_map.id(name) {
|
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) {
|
2020-02-03 05:59:19 +08:00
|
|
|
if let Some(id) = self.fields_map.id(name) {
|
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)
|
|
|
|
}
|
|
|
|
|
2020-01-14 02:34:49 +08: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-30 01:30:21 +08:00
|
|
|
pub fn update_ranked<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
|
2020-02-11 22:16:02 +08:00
|
|
|
self.ranked.clear();
|
2020-01-11 01:20:30 +08:00
|
|
|
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-02-11 22:16:02 +08:00
|
|
|
self.displayed.clear();
|
2020-01-11 01:20:30 +08:00
|
|
|
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-02-11 22:16:02 +08:00
|
|
|
self.indexed.clear();
|
|
|
|
self.indexed_map.clear();
|
2020-01-11 01:20:30 +08:00
|
|
|
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-02-27 00:34:26 +08:00
|
|
|
pub fn set_all_fields_indexed(&mut self) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_all_fields_displayed(&mut self) {
|
|
|
|
self.displayed.clear();
|
|
|
|
|
|
|
|
for (_name, id) in self.fields_map.iter() {
|
|
|
|
self.displayed.insert(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:51:37 +08:00
|
|
|
pub fn accept_new_fields(&self) -> bool {
|
|
|
|
self.accept_new_fields
|
2020-01-27 15:52:36 +08:00
|
|
|
}
|
|
|
|
|
2020-02-25 22:51:37 +08:00
|
|
|
pub fn set_accept_new_fields(&mut self, value: bool) {
|
|
|
|
self.accept_new_fields = value;
|
2020-01-27 15:52:36 +08:00
|
|
|
}
|
2020-01-11 01:20:30 +08:00
|
|
|
}
|