diff --git a/meilidb-data/src/database/index/main_index.rs b/meilidb-data/src/database/index/main_index.rs index 285aba868..b7141c80f 100644 --- a/meilidb-data/src/database/index/main_index.rs +++ b/meilidb-data/src/database/index/main_index.rs @@ -19,7 +19,7 @@ impl MainIndex { pub fn schema(&self) -> Result, Error> { match self.0.get(SCHEMA_KEY)? { Some(bytes) => { - let schema = Schema::read_from_bin(bytes.as_ref())?; + let schema = bincode::deserialize_from(bytes.as_ref())?; Ok(Some(schema)) }, None => Ok(None), @@ -27,8 +27,7 @@ impl MainIndex { } pub fn set_schema(&self, schema: &Schema) -> Result<(), Error> { - let mut bytes = Vec::new(); - schema.write_to_bin(&mut bytes)?; + let bytes = bincode::serialize(schema)?; self.0.insert(SCHEMA_KEY, bytes)?; Ok(()) } diff --git a/meilidb-schema/src/lib.rs b/meilidb-schema/src/lib.rs index 7e6fcf4b5..4655e2f07 100644 --- a/meilidb-schema/src/lib.rs +++ b/meilidb-schema/src/lib.rs @@ -105,49 +105,6 @@ impl Schema { SchemaBuilder { identifier, attributes } } - pub fn from_toml(mut reader: R) -> Result> { - 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(&self, mut writer: W) -> Result<(), Box> { - let builder = self.to_builder(); - let string = toml::to_string_pretty(&builder)?; - writer.write_all(string.as_bytes())?; - - Ok(()) - } - - pub fn from_json(mut reader: R) -> Result> { - let mut buffer = Vec::new(); - reader.read_to_end(&mut buffer)?; - let builder: SchemaBuilder = serde_json::from_slice(&buffer)?; - Ok(builder.build()) - } - - pub fn to_json(&self, mut writer: W) -> Result<(), Box> { - let builder = self.to_builder(); - let string = serde_json::to_string_pretty(&builder)?; - writer.write_all(string.as_bytes())?; - - Ok(()) - } - - pub fn read_from_bin(reader: R) -> bincode::Result { - let builder: SchemaBuilder = bincode::deserialize_from(reader)?; - Ok(builder.build()) - } - - pub fn write_to_bin(&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) -> IndexMap { let mut ordered = BTreeMap::new(); for (name, attr) in &self.inner.attrs { @@ -253,9 +210,8 @@ mod tests { let schema = builder.build(); let mut buffer = Vec::new(); - - schema.write_to_bin(&mut buffer)?; - let schema2 = Schema::read_from_bin(buffer.as_slice())?; + bincode::serialize_into(&mut buffer, &schema)?; + let schema2 = bincode::deserialize_from(buffer.as_slice())?; assert_eq!(schema, schema2); @@ -270,10 +226,9 @@ mod tests { builder.new_attribute("gamma", INDEXED); let schema = builder.build(); - let mut buffer = Vec::new(); - schema.to_toml(&mut buffer)?; + let buffer = toml::to_vec(&schema)?; + let schema2 = toml::from_slice(buffer.as_slice())?; - let schema2 = Schema::from_toml(buffer.as_slice())?; assert_eq!(schema, schema2); let data = r#" @@ -289,7 +244,7 @@ mod tests { [attributes."gamma"] indexed = true "#; - let schema2 = Schema::from_toml(data.as_bytes())?; + let schema2 = toml::from_str(data)?; assert_eq!(schema, schema2); Ok(()) @@ -303,10 +258,9 @@ mod tests { builder.new_attribute("gamma", INDEXED); let schema = builder.build(); - let mut buffer = Vec::new(); - schema.to_json(&mut buffer)?; + let buffer = serde_json::to_vec(&schema)?; + let schema2 = serde_json::from_slice(buffer.as_slice())?; - let schema2 = Schema::from_json(buffer.as_slice())?; assert_eq!(schema, schema2); let data = r#" @@ -325,7 +279,7 @@ mod tests { } } }"#; - let schema2 = Schema::from_json(data.as_bytes())?; + let schema2 = serde_json::from_str(data)?; assert_eq!(schema, schema2); Ok(()) diff --git a/meilidb/Cargo.toml b/meilidb/Cargo.toml index 96b597741..c4a4a07e9 100644 --- a/meilidb/Cargo.toml +++ b/meilidb/Cargo.toml @@ -13,8 +13,8 @@ meilidb-schema = { path = "../meilidb-schema", version = "0.1.0" } csv = "1.0.7" diskus = "0.5.0" env_logger = "0.6.1" -jemallocator = "0.1.9" indexmap = { version = "1.1.0", features = ["serde-1"] } +jemallocator = "0.1.9" meilidb-core = { path = "../meilidb-core", version = "0.1.0" } quickcheck = "0.8.2" rand = "0.6.5" @@ -26,3 +26,4 @@ structopt = "0.2.15" sysinfo = "0.8.4" tempfile = "3.0.7" termcolor = "1.0.4" +toml = "0.5.3" diff --git a/meilidb/examples/create-database.rs b/meilidb/examples/create-database.rs index d49979f28..c9326b6c1 100644 --- a/meilidb/examples/create-database.rs +++ b/meilidb/examples/create-database.rs @@ -6,7 +6,7 @@ use std::io::{self, BufRead, BufReader}; use std::path::{Path, PathBuf}; use std::time::Instant; use std::error::Error; -use std::fs::File; +use std::fs::{self, File}; use diskus::Walk; use sysinfo::{SystemExt, ProcessExt}; @@ -181,8 +181,8 @@ fn main() -> Result<(), Box> { let opt = Opt::from_args(); let schema = { - let file = File::open(&opt.schema_path)?; - Schema::from_toml(file)? + let string = fs::read_to_string(&opt.schema_path)?; + toml::from_str(&string)? }; let stop_words = match opt.stop_words {