feat: Schema can be toml de/serialized

This commit is contained in:
Clément Renault 2018-12-28 19:24:50 +01:00
parent 70772eca5c
commit 8412c14b5b
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
4 changed files with 87 additions and 26 deletions

View File

@ -17,6 +17,11 @@ serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
unidecode = "0.3" unidecode = "0.3"
[dependencies.toml]
git = "https://github.com/Kerollmops/toml-rs.git"
features = ["preserve_order"]
rev = "0372ba6"
[dependencies.rocksdb] [dependencies.rocksdb]
git = "https://github.com/pingcap/rust-rocksdb.git" git = "https://github.com/pingcap/rust-rocksdb.git"
rev = "c2eb140" rev = "c2eb140"

View File

@ -39,7 +39,7 @@ impl Database {
let db = DB::open_cf(opts, &path, vec![("default", cf_opts)])?; let db = DB::open_cf(opts, &path, vec![("default", cf_opts)])?;
let mut schema_bytes = Vec::new(); let mut schema_bytes = Vec::new();
schema.write_to(&mut schema_bytes)?; schema.write_to_bin(&mut schema_bytes)?;
db.put(DATA_SCHEMA, &schema_bytes)?; db.put(DATA_SCHEMA, &schema_bytes)?;
let db = Arc::new(db); let db = Arc::new(db);
@ -62,7 +62,7 @@ impl Database {
// FIXME create a generic function to do that ! // FIXME create a generic function to do that !
let _schema = match db.get(DATA_SCHEMA)? { let _schema = match db.get(DATA_SCHEMA)? {
Some(value) => Schema::read_from(&*value)?, Some(value) => Schema::read_from_bin(&*value)?,
None => return Err(String::from("Database does not contain a schema").into()), None => return Err(String::from("Database does not contain a schema").into()),
}; };

View File

@ -46,7 +46,7 @@ fn retrieve_data_schema<D>(snapshot: &Snapshot<D>) -> Result<Schema, Box<Error>>
where D: Deref<Target=DB> where D: Deref<Target=DB>
{ {
match snapshot.get(DATA_SCHEMA)? { match snapshot.get(DATA_SCHEMA)? {
Some(vector) => Ok(Schema::read_from(&*vector)?), Some(vector) => Ok(Schema::read_from_bin(&*vector)?),
None => Err(String::from("BUG: no schema found in the database").into()), None => Err(String::from("BUG: no schema found in the database").into()),
} }
} }

View File

@ -2,11 +2,10 @@ use crate::database::update::SerializerError;
use std::collections::{HashMap, BTreeMap}; use std::collections::{HashMap, BTreeMap};
use crate::database::calculate_hash; use crate::database::calculate_hash;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::error::Error;
use std::{fmt, u16}; use std::{fmt, u16};
use std::path::Path;
use std::ops::BitOr; use std::ops::BitOr;
use std::sync::Arc; use std::sync::Arc;
use std::fs::File;
use serde_derive::{Serialize, Deserialize}; use serde_derive::{Serialize, Deserialize};
use serde::ser::{self, Serialize}; use serde::ser::{self, Serialize};
@ -19,7 +18,10 @@ pub const INDEXED: SchemaProps = SchemaProps { stored: false, indexed: true };
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchemaProps { pub struct SchemaProps {
#[serde(default)]
stored: bool, stored: bool,
#[serde(default)]
indexed: bool, indexed: bool,
} }
@ -44,22 +46,23 @@ impl BitOr for SchemaProps {
} }
} }
#[derive(Serialize, Deserialize)]
pub struct SchemaBuilder { pub struct SchemaBuilder {
identifier: String, identifier: String,
attrs: LinkedHashMap<String, SchemaProps>, attributes: LinkedHashMap<String, SchemaProps>,
} }
impl SchemaBuilder { impl SchemaBuilder {
pub fn with_identifier<S: Into<String>>(name: S) -> SchemaBuilder { pub fn with_identifier<S: Into<String>>(name: S) -> SchemaBuilder {
SchemaBuilder { SchemaBuilder {
identifier: name.into(), identifier: name.into(),
attrs: LinkedHashMap::new(), attributes: LinkedHashMap::new(),
} }
} }
pub fn new_attribute<S: Into<String>>(&mut self, name: S, props: SchemaProps) -> SchemaAttr { pub fn new_attribute<S: Into<String>>(&mut self, name: S, props: SchemaProps) -> SchemaAttr {
let len = self.attrs.len(); let len = self.attributes.len();
if self.attrs.insert(name.into(), props).is_some() { if self.attributes.insert(name.into(), props).is_some() {
panic!("Field already inserted.") panic!("Field already inserted.")
} }
SchemaAttr(len as u16) SchemaAttr(len as u16)
@ -69,7 +72,7 @@ impl SchemaBuilder {
let mut attrs = HashMap::new(); let mut attrs = HashMap::new();
let mut props = Vec::new(); let mut props = Vec::new();
for (i, (name, prop)) in self.attrs.into_iter().enumerate() { for (i, (name, prop)) in self.attributes.into_iter().enumerate() {
attrs.insert(name.clone(), SchemaAttr(i as u16)); attrs.insert(name.clone(), SchemaAttr(i as u16));
props.push((name, prop)); props.push((name, prop));
} }
@ -92,31 +95,50 @@ struct InnerSchema {
} }
impl Schema { impl Schema {
pub fn open<P: AsRef<Path>>(path: P) -> bincode::Result<Schema> { pub fn from_toml<R: Read>(mut reader: R) -> Result<Schema, Box<Error>> {
let file = File::open(path)?; let mut buffer = Vec::new();
Schema::read_from(file) reader.read_to_end(&mut buffer)?;
} let builder: SchemaBuilder = toml::from_slice(&buffer)?;
pub fn read_from<R: Read>(reader: R) -> bincode::Result<Schema> {
let (identifier, attrs) = bincode::deserialize_from(reader)?;
let builder = SchemaBuilder { identifier, attrs };
Ok(builder.build()) Ok(builder.build())
} }
pub fn write_to<W: Write>(&self, writer: W) -> bincode::Result<()> { 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(())
}
pub(crate) fn read_from_bin<R: Read>(reader: R) -> bincode::Result<Schema> {
let builder: SchemaBuilder = bincode::deserialize_from(reader)?;
Ok(builder.build())
}
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> {
let mut ordered = BTreeMap::new(); let mut ordered = BTreeMap::new();
for (name, attr) in &self.inner.attrs { for (name, attr) in &self.inner.attrs {
let (_, props) = self.inner.props[attr.0 as usize]; let (_, props) = self.inner.props[attr.0 as usize];
ordered.insert(attr.0, (name, props)); ordered.insert(attr.0, (name, props));
} }
let identifier = &self.inner.identifier; let mut attributes = LinkedHashMap::with_capacity(ordered.len());
let mut attrs = LinkedHashMap::with_capacity(ordered.len());
for (_, (name, props)) in ordered { for (_, (name, props)) in ordered {
attrs.insert(name, props); attributes.insert(name.clone(), props);
} }
bincode::serialize_into(writer, &(identifier, attrs)) attributes
} }
pub fn document_id<T>(&self, document: &T) -> Result<DocumentId, SerializerError> pub fn document_id<T>(&self, document: &T) -> Result<DocumentId, SerializerError>
@ -355,22 +377,56 @@ impl<'a> ser::SerializeStruct for FindDocumentIdStructSerializer<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::error::Error;
#[test] #[test]
fn serialize_deserialize() -> bincode::Result<()> { fn serialize_deserialize() -> bincode::Result<()> {
let mut builder = SchemaBuilder::with_identifier("id"); let mut builder = SchemaBuilder::with_identifier("id");
builder.new_attribute("alphabet", STORED); builder.new_attribute("alpha", STORED);
builder.new_attribute("beta", STORED | INDEXED); builder.new_attribute("beta", STORED | INDEXED);
builder.new_attribute("gamma", INDEXED); builder.new_attribute("gamma", INDEXED);
let schema = builder.build(); let schema = builder.build();
let mut buffer = Vec::new(); let mut buffer = Vec::new();
schema.write_to(&mut buffer)?; schema.write_to_bin(&mut buffer)?;
let schema2 = Schema::read_from(buffer.as_slice())?; let schema2 = Schema::read_from_bin(buffer.as_slice())?;
assert_eq!(schema, schema2); assert_eq!(schema, schema2);
Ok(()) 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
[attributes."gamma"]
indexed = true
"#;
let schema2 = Schema::from_toml(data.as_bytes())?;
assert_eq!(schema, schema2);
Ok(())
}
} }