MeiliSearch/src/database/schema.rs

244 lines
6.5 KiB
Rust
Raw Normal View History

2018-11-21 13:56:14 +01:00
use std::collections::{HashMap, BTreeMap};
2018-11-20 11:37:19 +01:00
use std::io::{Read, Write};
2018-12-28 19:24:50 +01:00
use std::error::Error;
use std::{fmt, u16};
use std::ops::BitOr;
2018-12-15 21:17:55 +01:00
use std::sync::Arc;
use serde_derive::{Serialize, Deserialize};
2018-11-21 13:56:14 +01:00
use linked_hash_map::LinkedHashMap;
use serde::Serialize;
2018-11-21 13:56:14 +01:00
use crate::database::serde::find_id::FindDocumentIdSerializer;
use crate::database::serde::SerializerError;
use crate::DocumentId;
pub const STORED: SchemaProps = SchemaProps { stored: true, indexed: false };
pub const INDEXED: SchemaProps = SchemaProps { stored: false, indexed: true };
2018-11-21 13:56:14 +01:00
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchemaProps {
2018-12-28 19:24:50 +01:00
#[serde(default)]
stored: bool,
2018-12-28 19:24:50 +01:00
#[serde(default)]
indexed: bool,
}
impl SchemaProps {
pub fn is_stored(&self) -> bool {
self.stored
}
pub fn is_indexed(&self) -> bool {
self.indexed
}
}
impl BitOr for SchemaProps {
type Output = Self;
fn bitor(self, other: Self) -> Self::Output {
SchemaProps {
stored: self.stored | other.stored,
indexed: self.indexed | other.indexed,
}
}
}
2018-12-28 19:24:50 +01:00
#[derive(Serialize, Deserialize)]
2018-11-20 11:37:19 +01:00
pub struct SchemaBuilder {
identifier: String,
2018-12-28 19:24:50 +01:00
attributes: LinkedHashMap<String, SchemaProps>,
2018-11-20 11:37:19 +01:00
}
impl SchemaBuilder {
pub fn with_identifier<S: Into<String>>(name: S) -> SchemaBuilder {
SchemaBuilder {
identifier: name.into(),
2018-12-28 19:24:50 +01:00
attributes: LinkedHashMap::new(),
}
}
pub fn new_attribute<S: Into<String>>(&mut self, name: S, props: SchemaProps) -> SchemaAttr {
2018-12-28 19:24:50 +01:00
let len = self.attributes.len();
if self.attributes.insert(name.into(), props).is_some() {
panic!("Field already inserted.")
}
SchemaAttr(len as u16)
}
pub fn build(self) -> Schema {
2018-11-21 13:56:14 +01:00
let mut attrs = HashMap::new();
let mut props = Vec::new();
2018-12-28 19:24:50 +01:00
for (i, (name, prop)) in self.attributes.into_iter().enumerate() {
attrs.insert(name.clone(), SchemaAttr(i as u16));
2018-12-15 21:17:55 +01:00
props.push((name, prop));
2018-11-21 13:56:14 +01:00
}
let identifier = self.identifier;
Schema { inner: Arc::new(InnerSchema { identifier, attrs, props }) }
}
}
2018-11-21 13:56:14 +01:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Schema {
2018-12-15 21:17:55 +01:00
inner: Arc<InnerSchema>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct InnerSchema {
identifier: String,
2018-11-21 13:56:14 +01:00
attrs: HashMap<String, SchemaAttr>,
2018-12-15 21:17:55 +01:00
props: Vec<(String, SchemaProps)>,
2018-11-21 13:56:14 +01:00
}
impl Schema {
2018-12-28 19:24:50 +01:00
pub fn from_toml<R: Read>(mut reader: R) -> Result<Schema, Box<Error>> {
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer)?;
let builder: SchemaBuilder = toml::from_slice(&buffer)?;
Ok(builder.build())
}
pub fn to_toml<W: Write>(&self, mut writer: W) -> Result<(), Box<Error>> {
let identifier = self.inner.identifier.clone();
let attributes = self.attributes_ordered();
let builder = SchemaBuilder { identifier, attributes };
let string = toml::to_string_pretty(&builder)?;
writer.write_all(string.as_bytes())?;
Ok(())
2018-11-20 11:37:19 +01:00
}
2018-12-28 19:24:50 +01:00
pub(crate) fn read_from_bin<R: Read>(reader: R) -> bincode::Result<Schema> {
let builder: SchemaBuilder = bincode::deserialize_from(reader)?;
2018-11-21 13:56:14 +01:00
Ok(builder.build())
2018-11-20 11:37:19 +01:00
}
2018-12-28 19:24:50 +01:00
pub(crate) fn write_to_bin<W: Write>(&self, writer: W) -> bincode::Result<()> {
let identifier = self.inner.identifier.clone();
let attributes = self.attributes_ordered();
let builder = SchemaBuilder { identifier, attributes };
bincode::serialize_into(writer, &builder)
}
fn attributes_ordered(&self) -> LinkedHashMap<String, SchemaProps> {
2018-11-21 13:56:14 +01:00
let mut ordered = BTreeMap::new();
for (name, attr) in &self.inner.attrs {
let (_, props) = self.inner.props[attr.0 as usize];
ordered.insert(attr.0, (name, props));
2018-11-21 13:56:14 +01:00
}
2018-12-28 19:24:50 +01:00
let mut attributes = LinkedHashMap::with_capacity(ordered.len());
2018-11-21 13:56:14 +01:00
for (_, (name, props)) in ordered {
2018-12-28 19:24:50 +01:00
attributes.insert(name.clone(), props);
2018-11-21 13:56:14 +01:00
}
2018-12-28 19:24:50 +01:00
attributes
}
pub fn document_id<T>(&self, document: &T) -> Result<DocumentId, Box<Error>>
where T: Serialize,
{
unimplemented!()
}
2018-11-21 13:56:14 +01:00
pub fn props(&self, attr: SchemaAttr) -> SchemaProps {
let (_, props) = self.inner.props[attr.0 as usize];
2018-12-15 21:17:55 +01:00
props
}
pub fn identifier_name(&self) -> &str {
&self.inner.identifier
}
2018-11-21 13:56:14 +01:00
pub fn attribute<S: AsRef<str>>(&self, name: S) -> Option<SchemaAttr> {
2018-12-15 21:17:55 +01:00
self.inner.attrs.get(name.as_ref()).cloned()
}
pub fn attribute_name(&self, attr: SchemaAttr) -> &str {
let (name, _) = &self.inner.props[attr.0 as usize];
2018-12-15 21:17:55 +01:00
name
}
}
2018-11-20 11:37:19 +01:00
2018-11-21 13:56:14 +01:00
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct SchemaAttr(pub(crate) u16);
2018-11-20 11:37:19 +01:00
2018-11-21 13:56:14 +01:00
impl SchemaAttr {
pub fn new(value: u16) -> SchemaAttr {
SchemaAttr(value)
}
pub fn max() -> SchemaAttr {
SchemaAttr(u16::MAX)
2018-11-20 11:37:19 +01:00
}
}
2018-11-21 13:56:14 +01:00
impl fmt::Display for SchemaAttr {
2018-11-20 11:37:19 +01:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
2018-11-20 11:37:19 +01:00
}
}
2018-11-21 13:56:14 +01:00
#[cfg(test)]
mod tests {
use super::*;
2018-12-28 19:24:50 +01:00
use std::error::Error;
2018-11-21 13:56:14 +01:00
#[test]
fn serialize_deserialize() -> bincode::Result<()> {
let mut builder = SchemaBuilder::with_identifier("id");
2018-12-28 19:24:50 +01:00
builder.new_attribute("alpha", STORED);
builder.new_attribute("beta", STORED | INDEXED);
builder.new_attribute("gamma", INDEXED);
2018-11-21 13:56:14 +01:00
let schema = builder.build();
let mut buffer = Vec::new();
2018-12-28 19:24:50 +01:00
schema.write_to_bin(&mut buffer)?;
let schema2 = Schema::read_from_bin(buffer.as_slice())?;
assert_eq!(schema, schema2);
Ok(())
}
#[test]
fn serialize_deserialize_toml() -> Result<(), Box<Error>> {
let mut builder = SchemaBuilder::with_identifier("id");
builder.new_attribute("alpha", STORED);
builder.new_attribute("beta", STORED | INDEXED);
builder.new_attribute("gamma", INDEXED);
let schema = builder.build();
let mut buffer = Vec::new();
schema.to_toml(&mut buffer)?;
let schema2 = Schema::from_toml(buffer.as_slice())?;
assert_eq!(schema, schema2);
let data = r#"
identifier = "id"
[attributes."alpha"]
stored = true
[attributes."beta"]
stored = true
indexed = true
2018-11-21 13:56:14 +01:00
2018-12-28 19:24:50 +01:00
[attributes."gamma"]
indexed = true
"#;
let schema2 = Schema::from_toml(data.as_bytes())?;
2018-11-21 13:56:14 +01:00
assert_eq!(schema, schema2);
Ok(())
}
}