mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
introduce a new schemaless way
This commit is contained in:
parent
bbe1845f66
commit
130fb74928
22 changed files with 365 additions and 418 deletions
|
@ -5,6 +5,7 @@ pub type SResult<T> = Result<T, Error>;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
FieldNameNotFound(String),
|
||||
MaxFieldsLimitExceeded,
|
||||
}
|
||||
|
||||
|
@ -12,6 +13,7 @@ impl fmt::Display for Error {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::Error::*;
|
||||
match self {
|
||||
FieldNameNotFound(field) => write!(f, "The field {} doesn't exist", field),
|
||||
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reatributed field id has been reached"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,9 +3,8 @@ use std::collections::HashMap;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{SResult, SchemaAttr};
|
||||
use crate::{SResult, FieldId};
|
||||
|
||||
pub type FieldId = SchemaAttr;
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct FieldsMap {
|
||||
|
@ -43,13 +42,13 @@ impl FieldsMap {
|
|||
self.name_map.remove(&name);
|
||||
}
|
||||
|
||||
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<&FieldId> {
|
||||
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<FieldId> {
|
||||
let name = name.into();
|
||||
self.name_map.get(&name)
|
||||
self.name_map.get(&name).map(|s| *s)
|
||||
}
|
||||
|
||||
pub fn get_name<I: Into<SchemaAttr>>(&self, id: I) -> Option<&String> {
|
||||
self.id_map.get(&id.into())
|
||||
pub fn get_name<I: Into<FieldId>>(&self, id: I) -> Option<String> {
|
||||
self.id_map.get(&id.into()).map(|s| s.to_string())
|
||||
}
|
||||
|
||||
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<FieldsMap> {
|
||||
|
@ -74,14 +73,14 @@ mod tests {
|
|||
assert_eq!(fields_map.insert("id").unwrap(), 0.into());
|
||||
assert_eq!(fields_map.insert("title").unwrap(), 1.into());
|
||||
assert_eq!(fields_map.insert("descritpion").unwrap(), 2.into());
|
||||
assert_eq!(fields_map.get_id("id"), Some(&0.into()));
|
||||
assert_eq!(fields_map.get_id("title"), Some(&1.into()));
|
||||
assert_eq!(fields_map.get_id("descritpion"), Some(&2.into()));
|
||||
assert_eq!(fields_map.get_id("id"), Some(0.into()));
|
||||
assert_eq!(fields_map.get_id("title"), Some(1.into()));
|
||||
assert_eq!(fields_map.get_id("descritpion"), Some(2.into()));
|
||||
assert_eq!(fields_map.get_id("date"), None);
|
||||
assert_eq!(fields_map.len(), 3);
|
||||
assert_eq!(fields_map.get_name(0), Some(&"id".to_owned()));
|
||||
assert_eq!(fields_map.get_name(1), Some(&"title".to_owned()));
|
||||
assert_eq!(fields_map.get_name(2), Some(&"descritpion".to_owned()));
|
||||
assert_eq!(fields_map.get_name(0), Some("id".to_owned()));
|
||||
assert_eq!(fields_map.get_name(1), Some("title".to_owned()));
|
||||
assert_eq!(fields_map.get_name(2), Some("descritpion".to_owned()));
|
||||
assert_eq!(fields_map.get_name(4), None);
|
||||
fields_map.remove("title");
|
||||
assert_eq!(fields_map.get_id("title"), None);
|
||||
|
|
|
@ -3,48 +3,88 @@ mod fields_map;
|
|||
mod schema;
|
||||
|
||||
pub use error::{Error, SResult};
|
||||
pub use fields_map::{FieldsMap, FieldId};
|
||||
pub use schema::{Schema, IndexedPos};
|
||||
pub use fields_map::FieldsMap;
|
||||
pub use schema::Schema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
||||
pub struct SchemaAttr(pub u16);
|
||||
pub struct IndexedPos(pub u16);
|
||||
|
||||
impl SchemaAttr {
|
||||
pub const fn new(value: u16) -> SchemaAttr {
|
||||
SchemaAttr(value)
|
||||
impl IndexedPos {
|
||||
pub const fn new(value: u16) -> IndexedPos {
|
||||
IndexedPos(value)
|
||||
}
|
||||
|
||||
pub const fn min() -> SchemaAttr {
|
||||
SchemaAttr(u16::min_value())
|
||||
pub const fn min() -> IndexedPos {
|
||||
IndexedPos(u16::min_value())
|
||||
}
|
||||
|
||||
pub const fn max() -> SchemaAttr {
|
||||
SchemaAttr(u16::max_value())
|
||||
pub const fn max() -> IndexedPos {
|
||||
IndexedPos(u16::max_value())
|
||||
}
|
||||
|
||||
pub fn next(self) -> SResult<SchemaAttr> {
|
||||
self.0.checked_add(1).map(SchemaAttr).ok_or(Error::MaxFieldsLimitExceeded)
|
||||
pub fn next(self) -> SResult<IndexedPos> {
|
||||
self.0.checked_add(1).map(IndexedPos).ok_or(Error::MaxFieldsLimitExceeded)
|
||||
}
|
||||
|
||||
pub fn prev(self) -> SResult<SchemaAttr> {
|
||||
self.0.checked_sub(1).map(SchemaAttr).ok_or(Error::MaxFieldsLimitExceeded)
|
||||
pub fn prev(self) -> SResult<IndexedPos> {
|
||||
self.0.checked_sub(1).map(IndexedPos).ok_or(Error::MaxFieldsLimitExceeded)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for SchemaAttr {
|
||||
fn from(value: u16) -> SchemaAttr {
|
||||
SchemaAttr(value)
|
||||
impl From<u16> for IndexedPos {
|
||||
fn from(value: u16) -> IndexedPos {
|
||||
IndexedPos(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u16> for SchemaAttr {
|
||||
impl Into<u16> for IndexedPos {
|
||||
fn into(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
||||
pub struct FieldId(pub u16);
|
||||
|
||||
impl FieldId {
|
||||
pub const fn new(value: u16) -> FieldId {
|
||||
FieldId(value)
|
||||
}
|
||||
|
||||
pub const fn min() -> FieldId {
|
||||
FieldId(u16::min_value())
|
||||
}
|
||||
|
||||
pub const fn max() -> FieldId {
|
||||
FieldId(u16::max_value())
|
||||
}
|
||||
|
||||
pub fn next(self) -> SResult<FieldId> {
|
||||
self.0.checked_add(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
|
||||
}
|
||||
|
||||
pub fn prev(self) -> SResult<FieldId> {
|
||||
self.0.checked_sub(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u16> for FieldId {
|
||||
fn from(value: u16) -> FieldId {
|
||||
FieldId(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<u16> for FieldId {
|
||||
fn into(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// use std::collections::{BTreeMap, HashMap};
|
||||
// use std::ops::BitOr;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::{FieldsMap, FieldId, SResult, SchemaAttr};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
pub type IndexedPos = SchemaAttr;
|
||||
use crate::{FieldsMap, FieldId, SResult, Error, IndexedPos};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct Schema {
|
||||
fields_map: FieldsMap,
|
||||
|
||||
|
@ -30,11 +30,21 @@ impl Schema {
|
|||
self.fields_map.get_name(self.identifier).unwrap().to_string()
|
||||
}
|
||||
|
||||
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<&FieldId> {
|
||||
pub fn set_identifier(&mut self, id: String) -> SResult<()> {
|
||||
match self.get_id(id.clone()) {
|
||||
Some(id) => {
|
||||
self.identifier = id;
|
||||
Ok(())
|
||||
},
|
||||
None => Err(Error::FieldNameNotFound(id))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<FieldId> {
|
||||
self.fields_map.get_id(name)
|
||||
}
|
||||
|
||||
pub fn get_name<I: Into<SchemaAttr>>(&self, id: I) -> Option<&String> {
|
||||
pub fn get_name<I: Into<FieldId>>(&self, id: I) -> Option<String> {
|
||||
self.fields_map.get_name(id)
|
||||
}
|
||||
|
||||
|
@ -52,7 +62,7 @@ impl Schema {
|
|||
pub fn get_or_create<S: Into<String> + std::clone::Clone>(&mut self, name: S) -> SResult<FieldId> {
|
||||
match self.fields_map.get_id(name.clone()) {
|
||||
Some(id) => {
|
||||
Ok(*id)
|
||||
Ok(id)
|
||||
}
|
||||
None => {
|
||||
self.set_indexed(name.clone())?;
|
||||
|
@ -61,6 +71,30 @@ impl Schema {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_ranked(&self) -> HashSet<FieldId> {
|
||||
self.ranked.clone()
|
||||
}
|
||||
|
||||
pub fn get_ranked_name(&self) -> HashSet<String> {
|
||||
self.ranked.iter().filter_map(|a| self.get_name(*a)).collect()
|
||||
}
|
||||
|
||||
pub fn get_displayed(&self) -> HashSet<FieldId> {
|
||||
self.displayed.clone()
|
||||
}
|
||||
|
||||
pub fn get_displayed_name(&self) -> HashSet<String> {
|
||||
self.displayed.iter().filter_map(|a| self.get_name(*a)).collect()
|
||||
}
|
||||
|
||||
pub fn get_indexed(&self) -> Vec<FieldId> {
|
||||
self.indexed.clone()
|
||||
}
|
||||
|
||||
pub fn get_indexed_name(&self) -> Vec<String> {
|
||||
self.indexed.iter().filter_map(|a| self.get_name(*a)).collect()
|
||||
}
|
||||
|
||||
pub fn set_ranked<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
|
||||
let id = self.fields_map.insert(name.into())?;
|
||||
self.ranked.insert(id);
|
||||
|
@ -81,23 +115,42 @@ impl Schema {
|
|||
Ok((id, pos.into()))
|
||||
}
|
||||
|
||||
pub fn is_ranked<S: Into<String>>(&self, name: S) -> Option<&FieldId> {
|
||||
pub fn remove_ranked<S: Into<String>>(&mut self, name: S) {
|
||||
if let Some(id) = self.fields_map.get_id(name.into()) {
|
||||
self.ranked.remove(&id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_displayed<S: Into<String>>(&mut self, name: S) {
|
||||
if let Some(id) = self.fields_map.get_id(name.into()) {
|
||||
self.displayed.remove(&id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_indexed<S: Into<String>>(&mut self, name: S) {
|
||||
if let Some(id) = self.fields_map.get_id(name.into()) {
|
||||
self.indexed_map.remove(&id);
|
||||
self.indexed.retain(|x| *x != id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ranked<S: Into<String>>(&self, name: S) -> Option<FieldId> {
|
||||
match self.fields_map.get_id(name.into()) {
|
||||
Some(id) => self.ranked.get(id),
|
||||
Some(id) => self.ranked.get(&id).map(|s| *s),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_displayed<S: Into<String>>(&self, name: S) -> Option<&FieldId> {
|
||||
pub fn is_displayed<S: Into<String>>(&self, name: S) -> Option<FieldId> {
|
||||
match self.fields_map.get_id(name.into()) {
|
||||
Some(id) => self.displayed.get(id),
|
||||
Some(id) => self.displayed.get(&id).map(|s| *s),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_indexed<S: Into<String>>(&self, name: S) -> Option<&IndexedPos> {
|
||||
pub fn is_indexed<S: Into<String>>(&self, name: S) -> Option<IndexedPos> {
|
||||
match self.fields_map.get_id(name.into()) {
|
||||
Some(id) => self.indexed_map.get(id),
|
||||
Some(id) => self.indexed_map.get(&id).map(|s| *s),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue