Improve error handling.
Return to caller error codes as soon as they cannot be handled locally.
This commit is contained in:
@@ -85,8 +85,7 @@ impl Core {
|
||||
space_objects: Vec<SpaceSetObject>,
|
||||
scales: Option<Vec<Vec<u32>>>,
|
||||
max_elements: Option<usize>,
|
||||
) -> Self
|
||||
//Result<Self, String>
|
||||
) -> Result<Self, String>
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
@@ -112,18 +111,18 @@ impl Core {
|
||||
|
||||
for object in filtered.iter_mut() {
|
||||
let position: Vec<f64> = object.position().into();
|
||||
object.set_position(space.encode(&position).unwrap());
|
||||
object.set_position(space.encode(&position)?);
|
||||
}
|
||||
|
||||
space_dbs.push(SpaceDB::new(&space, filtered, scales.clone(), max_elements))
|
||||
}
|
||||
|
||||
Core {
|
||||
Ok(Core {
|
||||
title: title.into(),
|
||||
version: version.into(),
|
||||
properties,
|
||||
space_db: space_dbs,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &String {
|
||||
|
||||
@@ -16,14 +16,15 @@ fn main() {
|
||||
// Convert to binary the JSON data:
|
||||
if true {
|
||||
info_time!("Converting to binary JSON data");
|
||||
storage::json::from::<Vec<mercator_db::storage::model::Space>>("10k.spaces");
|
||||
storage::json::from::<Vec<mercator_db::storage::model::v1::SpatialObject>>("10k.objects");
|
||||
storage::json::from::<Vec<mercator_db::storage::model::Space>>("10k.spaces").unwrap();
|
||||
storage::json::from::<Vec<mercator_db::storage::model::v1::SpatialObject>>("10k.objects")
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Build a Database Index:
|
||||
if true {
|
||||
info_time!("Building database index");
|
||||
storage::bincode::build("10k", "v0.1", None, None);
|
||||
storage::bincode::build("10k", "v0.1", None, None).unwrap();
|
||||
}
|
||||
|
||||
// Load a Database:
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::fs::File;
|
||||
use std::io::BufWriter;
|
||||
use std::io::Error;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
use memmap::Mmap;
|
||||
use serde::de::DeserializeOwned;
|
||||
@@ -7,33 +9,39 @@ use serde::Serialize;
|
||||
|
||||
use super::model;
|
||||
|
||||
pub fn load<T>(from: &str) -> T
|
||||
pub fn load<T>(from: &str) -> Result<T, Error>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let file_in =
|
||||
File::open(from).unwrap_or_else(|e| panic!("Unable to read file: {}: {}", from, e));
|
||||
let file_in = File::open(from)?;
|
||||
|
||||
let mmap = unsafe {
|
||||
Mmap::map(&file_in)
|
||||
.unwrap_or_else(|e| panic!("Unable to map in memory the file: {}: {}", from, e))
|
||||
};
|
||||
let mmap = unsafe { Mmap::map(&file_in)? };
|
||||
|
||||
bincode::deserialize(&mmap[..])
|
||||
.unwrap_or_else(|e| panic!("Unable to parse the json data from: {}: {}", from, e))
|
||||
match bincode::deserialize(&mmap[..]) {
|
||||
Ok(data) => Ok(data),
|
||||
Err(e) => Err(Error::new(
|
||||
ErrorKind::InvalidData,
|
||||
format!("Bincode could not deserialize: {:?}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store<T>(data: T, to: &str)
|
||||
pub fn store<T>(data: T, to: &str) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let file_out =
|
||||
File::create(to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e));
|
||||
let file_out = File::create(to)?;
|
||||
|
||||
// We create a buffered writer from the file we get
|
||||
let writer = BufWriter::new(&file_out);
|
||||
|
||||
bincode::serialize_into(writer, &data).unwrap();
|
||||
match bincode::serialize_into(writer, &data) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) => Err(Error::new(
|
||||
ErrorKind::InvalidData,
|
||||
format!("Bincode could not serialize: {:?}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(
|
||||
@@ -41,19 +49,19 @@ pub fn build(
|
||||
version: &str,
|
||||
scales: Option<Vec<Vec<u32>>>,
|
||||
max_elements: Option<usize>,
|
||||
) {
|
||||
) -> Result<(), Error> {
|
||||
let fn_spaces = format!("{}.spaces.bin", name);
|
||||
let fn_objects = format!("{}.objects.bin", name);
|
||||
let fn_index = format!("{}.index", name);
|
||||
|
||||
let spaces = load::<Vec<model::Space>>(&fn_spaces)
|
||||
let spaces = load::<Vec<model::Space>>(&fn_spaces)?
|
||||
.iter()
|
||||
.map(|s| s.into())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let objects = load::<Vec<model::SpatialObject>>(&fn_objects);
|
||||
let objects = load::<Vec<model::SpatialObject>>(&fn_objects)?;
|
||||
|
||||
let core = model::build_index(name, version, &spaces, &objects, scales, max_elements);
|
||||
|
||||
store((spaces, core), &fn_index);
|
||||
store((spaces, core), &fn_index)
|
||||
}
|
||||
|
||||
@@ -1,33 +1,35 @@
|
||||
use std::fs::File;
|
||||
use std::io::BufWriter;
|
||||
use std::io::Error;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
use memmap::Mmap;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
fn convert<T>(from: &str, to: &str)
|
||||
fn convert<T>(from: &str, to: &str) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
let file_in =
|
||||
File::open(from).unwrap_or_else(|e| panic!("Unable to read file: {}: {}", from, e));
|
||||
let file_out =
|
||||
File::create(to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e));
|
||||
let file_in = File::open(from)?;
|
||||
let file_out = File::create(to)?;
|
||||
|
||||
// We create a buffered writer from the file we get
|
||||
let writer = BufWriter::new(&file_out);
|
||||
|
||||
let mmap = unsafe {
|
||||
Mmap::map(&file_in)
|
||||
.unwrap_or_else(|e| panic!("Unable to map in memory the file: {}: {}", from, e))
|
||||
};
|
||||
let v: T = serde_json::from_slice(&mmap[..])
|
||||
.unwrap_or_else(|e| panic!("Unable to parse the json data from: {}: {}", from, e));
|
||||
let mmap = unsafe { Mmap::map(&file_in)? };
|
||||
let v: T = serde_json::from_slice(&mmap[..])?;
|
||||
|
||||
bincode::serialize_into(writer, &v).unwrap();
|
||||
match bincode::serialize_into(writer, &v) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) => Err(Error::new(
|
||||
ErrorKind::InvalidData,
|
||||
format!("Bincode could not serialize: {:?}", e),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from<T>(name: &str)
|
||||
pub fn from<T>(name: &str) -> Result<(), Error>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
@@ -35,5 +37,5 @@ where
|
||||
let fn_in = format!("{}.json", name);
|
||||
let fn_out = format!("{}.bin", name);
|
||||
|
||||
convert::<T>(&fn_in, &fn_out);
|
||||
convert::<T>(&fn_in, &fn_out)
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ pub fn build_index(
|
||||
objects: &[SpatialObject],
|
||||
scales: Option<Vec<Vec<u32>>>,
|
||||
max_elements: Option<usize>,
|
||||
) -> Core {
|
||||
) -> Result<Core, String> {
|
||||
let mut properties = vec![];
|
||||
let mut space_set_objects = vec![];
|
||||
{
|
||||
|
||||
@@ -34,7 +34,7 @@ fn convert(string: &str) -> Result<Vec<SpatialObject>, Error> {
|
||||
"SCALE" => (),
|
||||
_ if values[0].starts_with("#A") => {
|
||||
// Update the oid value.
|
||||
oid = Some(format!("{}", values[0].trim_start_matches("#")));
|
||||
oid = Some(values[0].trim_start_matches('#').to_string());
|
||||
trace!("FOUND OID {:?}", oid);
|
||||
}
|
||||
_ if line.contains("WHS") => {
|
||||
|
||||
Reference in New Issue
Block a user