mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
squash-me
This commit is contained in:
parent
2ee90a891c
commit
bbe1845f66
20 changed files with 1118 additions and 676 deletions
20
meilisearch-schema/src/error.rs
Normal file
20
meilisearch-schema/src/error.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
|
||||
use std::{error, fmt};
|
||||
|
||||
pub type SResult<T> = Result<T, Error>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
MaxFieldsLimitExceeded,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::Error::*;
|
||||
match self {
|
||||
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reatributed field id has been reached"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {}
|
91
meilisearch-schema/src/fields_map.rs
Normal file
91
meilisearch-schema/src/fields_map.rs
Normal file
|
@ -0,0 +1,91 @@
|
|||
use std::io::{Read, Write};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{SResult, SchemaAttr};
|
||||
|
||||
pub type FieldId = SchemaAttr;
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct FieldsMap {
|
||||
name_map: HashMap<String, FieldId>,
|
||||
id_map: HashMap<FieldId, String>,
|
||||
next_id: FieldId
|
||||
}
|
||||
|
||||
impl FieldsMap {
|
||||
pub fn len(&self) -> usize {
|
||||
self.name_map.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.name_map.is_empty()
|
||||
}
|
||||
|
||||
pub fn insert<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
|
||||
let name = name.into();
|
||||
if let Some(id) = self.name_map.get(&name) {
|
||||
return Ok(*id)
|
||||
}
|
||||
let id = self.next_id.into();
|
||||
self.next_id = self.next_id.next()?;
|
||||
self.name_map.insert(name.clone(), id);
|
||||
self.id_map.insert(id, name);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn remove<S: Into<String>>(&mut self, name: S) {
|
||||
let name = name.into();
|
||||
if let Some(id) = self.name_map.get(&name) {
|
||||
self.id_map.remove(&id);
|
||||
}
|
||||
self.name_map.remove(&name);
|
||||
}
|
||||
|
||||
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<&FieldId> {
|
||||
let name = name.into();
|
||||
self.name_map.get(&name)
|
||||
}
|
||||
|
||||
pub fn get_name<I: Into<SchemaAttr>>(&self, id: I) -> Option<&String> {
|
||||
self.id_map.get(&id.into())
|
||||
}
|
||||
|
||||
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<FieldsMap> {
|
||||
bincode::deserialize_from(reader)
|
||||
}
|
||||
|
||||
pub fn write_to_bin<W: Write>(&self, writer: W) -> bincode::Result<()> {
|
||||
bincode::serialize_into(writer, &self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn fields_map() {
|
||||
let mut fields_map = FieldsMap::default();
|
||||
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.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("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(4), None);
|
||||
fields_map.remove("title");
|
||||
assert_eq!(fields_map.get_id("title"), None);
|
||||
assert_eq!(fields_map.insert("title").unwrap(), 3.into());
|
||||
assert_eq!(fields_map.len(), 3);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
141
meilisearch-schema/src/schema.rs
Normal file
141
meilisearch-schema/src/schema.rs
Normal file
|
@ -0,0 +1,141 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::{FieldsMap, FieldId, SResult, SchemaAttr};
|
||||
|
||||
pub type IndexedPos = SchemaAttr;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Schema {
|
||||
fields_map: FieldsMap,
|
||||
|
||||
identifier: FieldId,
|
||||
ranked: HashSet<FieldId>,
|
||||
displayed: HashSet<FieldId>,
|
||||
|
||||
indexed: Vec<FieldId>,
|
||||
indexed_map: HashMap<FieldId, IndexedPos>,
|
||||
}
|
||||
|
||||
impl Schema {
|
||||
|
||||
pub fn with_identifier<S: Into<String>>(name: S) -> Schema {
|
||||
let mut schema = Schema::default();
|
||||
let field_id = schema.fields_map.insert(name.into()).unwrap();
|
||||
schema.identifier = field_id;
|
||||
|
||||
schema
|
||||
}
|
||||
|
||||
pub fn identifier(&self) -> String {
|
||||
self.fields_map.get_name(self.identifier).unwrap().to_string()
|
||||
}
|
||||
|
||||
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> {
|
||||
self.fields_map.get_name(id)
|
||||
}
|
||||
|
||||
pub fn contains<S: Into<String>>(&self, name: S) -> bool {
|
||||
match self.fields_map.get_id(name.into()) {
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_create_empty<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
|
||||
self.fields_map.insert(name)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
None => {
|
||||
self.set_indexed(name.clone())?;
|
||||
self.set_displayed(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn set_displayed<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
|
||||
let id = self.fields_map.insert(name.into())?;
|
||||
self.displayed.insert(id);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn set_indexed<S: Into<String>>(&mut self, name: S) -> SResult<(FieldId, IndexedPos)> {
|
||||
let id = self.fields_map.insert(name.into())?;
|
||||
let pos = self.indexed.len() as u16;
|
||||
self.indexed.push(id);
|
||||
self.indexed_map.insert(id, pos.into());
|
||||
Ok((id, pos.into()))
|
||||
}
|
||||
|
||||
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),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id_is_ranked(&self, id: FieldId) -> bool {
|
||||
self.ranked.get(&id).is_some()
|
||||
}
|
||||
|
||||
pub fn id_is_displayed(&self, id: FieldId) -> bool {
|
||||
self.displayed.get(&id).is_some()
|
||||
}
|
||||
|
||||
pub fn id_is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
|
||||
self.indexed_map.get(&id)
|
||||
}
|
||||
|
||||
pub fn update_ranked<S: Into<String>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
|
||||
self.ranked = HashSet::new();
|
||||
for name in data {
|
||||
self.set_ranked(name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_displayed<S: Into<String>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
|
||||
self.displayed = HashSet::new();
|
||||
for name in data {
|
||||
self.set_displayed(name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_indexed<S: Into<String>>(&mut self, data: Vec<S>) -> SResult<()> {
|
||||
self.indexed = Vec::new();
|
||||
self.indexed_map = HashMap::new();
|
||||
for name in data {
|
||||
self.set_indexed(name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue