mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 04:17:10 +02:00
review the internal schema to allow to create schema without identifier; fix #513
This commit is contained in:
parent
16a99aa95e
commit
86c3482cbd
11 changed files with 312 additions and 129 deletions
|
@ -6,6 +6,7 @@ pub type SResult<T> = Result<T, Error>;
|
|||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
FieldNameNotFound(String),
|
||||
IdentifierAlreadyPresent,
|
||||
MaxFieldsLimitExceeded,
|
||||
}
|
||||
|
||||
|
@ -14,6 +15,7 @@ impl fmt::Display for Error {
|
|||
use self::Error::*;
|
||||
match self {
|
||||
FieldNameNotFound(field) => write!(f, "The field {:?} doesn't exist", field),
|
||||
IdentifierAlreadyPresent => write!(f, "The schema already have an identifier. It's impossible to update it"),
|
||||
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reattributed field id has been reached"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};
|
|||
pub struct Schema {
|
||||
fields_map: FieldsMap,
|
||||
|
||||
identifier: FieldId,
|
||||
identifier: Option<FieldId>,
|
||||
ranked: HashSet<FieldId>,
|
||||
displayed: HashSet<FieldId>,
|
||||
|
||||
|
@ -17,6 +17,18 @@ pub struct Schema {
|
|||
}
|
||||
|
||||
impl Schema {
|
||||
pub fn new() -> Schema {
|
||||
Schema {
|
||||
fields_map: FieldsMap::default(),
|
||||
identifier: None,
|
||||
ranked: HashSet::new(),
|
||||
displayed: HashSet::new(),
|
||||
indexed: Vec::new(),
|
||||
indexed_map: HashMap::new(),
|
||||
accept_new_fields: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_identifier(name: &str) -> Schema {
|
||||
let mut fields_map = FieldsMap::default();
|
||||
let field_id = fields_map.insert(name).unwrap();
|
||||
|
@ -31,7 +43,7 @@ impl Schema {
|
|||
|
||||
Schema {
|
||||
fields_map,
|
||||
identifier: field_id,
|
||||
identifier: Some(field_id),
|
||||
ranked: HashSet::new(),
|
||||
displayed,
|
||||
indexed,
|
||||
|
@ -40,18 +52,21 @@ impl Schema {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn identifier(&self) -> &str {
|
||||
self.fields_map.name(self.identifier).unwrap()
|
||||
pub fn identifier(&self) -> Option<&str> {
|
||||
self.identifier.map(|id| self.fields_map.name(id).unwrap())
|
||||
}
|
||||
|
||||
pub fn set_identifier(&mut self, id: &str) -> SResult<()> {
|
||||
match self.id(id) {
|
||||
Some(id) => {
|
||||
self.identifier = id;
|
||||
Ok(())
|
||||
},
|
||||
None => Err(Error::FieldNameNotFound(id.to_string()))
|
||||
pub fn set_identifier(&mut self, name: &str) -> SResult<FieldId> {
|
||||
if self.identifier.is_some() {
|
||||
return Err(Error::IdentifierAlreadyPresent)
|
||||
}
|
||||
|
||||
let id = self.insert(name)?;
|
||||
self.identifier = Some(id);
|
||||
self.set_indexed(name)?;
|
||||
self.set_displayed(name)?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn id(&self, name: &str) -> Option<FieldId> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue