MeiliSearch/src/database/schema.rs

166 lines
4.2 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};
use std::{fmt, u16};
use std::path::Path;
use std::ops::BitOr;
2018-12-15 21:17:55 +01:00
use std::sync::Arc;
2018-11-20 11:37:19 +01:00
use std::fs::File;
use serde_derive::{Serialize, Deserialize};
2018-11-21 13:56:14 +01:00
use linked_hash_map::LinkedHashMap;
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 {
stored: bool,
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-11-20 11:37:19 +01:00
pub struct SchemaBuilder {
2018-11-21 13:56:14 +01:00
attrs: LinkedHashMap<String, SchemaProps>,
2018-11-20 11:37:19 +01:00
}
impl SchemaBuilder {
pub fn new() -> SchemaBuilder {
2018-11-21 13:56:14 +01:00
SchemaBuilder { attrs: LinkedHashMap::new() }
}
pub fn new_attribute<S: Into<String>>(&mut self, name: S, props: SchemaProps) -> SchemaAttr {
2018-11-21 13:56:14 +01:00
let len = self.attrs.len();
if self.attrs.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();
for (i, (name, prop)) in self.attrs.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
}
2018-12-15 21:17:55 +01:00
Schema { inner: Arc::new(InnerSchema { 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 {
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-11-21 13:56:14 +01:00
pub fn open<P: AsRef<Path>>(path: P) -> bincode::Result<Schema> {
2018-11-20 11:37:19 +01:00
let file = File::open(path)?;
Schema::read_from(file)
}
2018-11-21 13:56:14 +01:00
pub fn read_from<R: Read>(reader: R) -> bincode::Result<Schema> {
let attrs = bincode::deserialize_from(reader)?;
let builder = SchemaBuilder { attrs };
Ok(builder.build())
2018-11-20 11:37:19 +01:00
}
2018-11-21 13:56:14 +01:00
pub fn write_to<W: Write>(&self, writer: W) -> bincode::Result<()> {
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
}
let mut attrs = LinkedHashMap::with_capacity(ordered.len());
for (_, (name, props)) in ordered {
attrs.insert(name, props);
}
bincode::serialize_into(writer, &attrs)
}
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
}
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::*;
#[test]
fn serialize_deserialize() -> bincode::Result<()> {
let mut builder = SchemaBuilder::new();
builder.new_attribute("alphabet", 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();
schema.write_to(&mut buffer)?;
let schema2 = Schema::read_from(buffer.as_slice())?;
assert_eq!(schema, schema2);
Ok(())
}
}