Reorganising storage layer

This commit is contained in:
2020-01-14 11:04:13 +01:00
parent 08b4187ce3
commit c45f9c145d
7 changed files with 53 additions and 46 deletions

View File

@@ -1,2 +0,0 @@
pub mod model;
pub mod storage;

View File

@@ -11,6 +11,6 @@ extern crate arrayref;
extern crate serde_derive; extern crate serde_derive;
mod database; mod database;
pub mod json; pub mod storage;
pub use database::*; pub use database::*;

View File

@@ -1,8 +1,8 @@
#[macro_use] #[macro_use]
extern crate measure_time; extern crate measure_time;
use mercator_db::json::storage;
use mercator_db::space::Shape; use mercator_db::space::Shape;
use mercator_db::storage;
use mercator_db::CoreQueryParameters; use mercator_db::CoreQueryParameters;
use mercator_db::DataBase; use mercator_db::DataBase;
@@ -16,14 +16,14 @@ fn main() {
// Convert to binary the JSON data: // Convert to binary the JSON data:
if true { if true {
info_time!("Converting to binary JSON data"); info_time!("Converting to binary JSON data");
storage::convert::<Vec<mercator_db::json::model::Space>>("10k.spaces"); storage::json::from::<Vec<mercator_db::storage::model::Space>>("10k.spaces");
storage::convert::<Vec<mercator_db::json::model::SpatialObject>>("10k.objects"); storage::json::from::<Vec<mercator_db::storage::model::v1::SpatialObject>>("10k.objects");
} }
// Build a Database Index: // Build a Database Index:
if true { if true {
info_time!("Building database index"); info_time!("Building database index");
storage::build("10k", "v0.1", None, None); storage::bincode::build("10k", "v0.1", None, None);
} }
// Load a Database: // Load a Database:
@@ -69,7 +69,7 @@ fn main() {
let r = core.get_by_label(&c, id).unwrap(); let r = core.get_by_label(&c, id).unwrap();
println!("get_by_label {}: {}", id, r.len()); println!("get_by_label {}: {}", id, r.len());
if !r.is_empty() { if !r.is_empty() {
println!("{}: {:?}\n", id, r[0].1[0]); println!("{}: {:?}\n", id, r); // no overlaping point, so no results
} }
let lower = space.encode(&[0.2, 0.2, 0.2]).unwrap(); let lower = space.encode(&[0.2, 0.2, 0.2]).unwrap();

View File

@@ -5,31 +5,8 @@ use memmap::Mmap;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use crate::json::model; use super::model;
pub fn from_json<T>(from: &str, to: &str)
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));
// 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));
bincode::serialize_into(writer, &v).unwrap();
}
//FIXME: Move to ironsea_store?
pub fn load<T>(from: &str) -> T pub fn load<T>(from: &str) -> T
where where
T: DeserializeOwned, T: DeserializeOwned,
@@ -46,7 +23,6 @@ where
.unwrap_or_else(|e| panic!("Unable to parse the json data from: {}: {}", from, e)) .unwrap_or_else(|e| panic!("Unable to parse the json data from: {}: {}", from, e))
} }
//FIXME: Move to ironsea_store?
pub fn store<T>(data: T, to: &str) pub fn store<T>(data: T, to: &str)
where where
T: Serialize, T: Serialize,
@@ -60,17 +36,6 @@ where
bincode::serialize_into(writer, &data).unwrap(); bincode::serialize_into(writer, &data).unwrap();
} }
pub fn convert<T>(name: &str)
where
T: Serialize + DeserializeOwned,
{
// Convert definitions from json to bincode
let fn_in = format!("{}.json", name);
let fn_out = format!("{}.bin", name);
from_json::<T>(&fn_in, &fn_out);
}
pub fn build( pub fn build(
name: &str, name: &str,
version: &str, version: &str,
@@ -86,7 +51,7 @@ pub fn build(
.map(|s| s.into()) .map(|s| s.into())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let objects = load::<Vec<model::v1::SpatialObject>>(&fn_objects); let objects = load::<Vec<model::SpatialObject>>(&fn_objects);
let core = model::build_index(name, version, &spaces, &objects, scales, max_elements); let core = model::build_index(name, version, &spaces, &objects, scales, max_elements);

39
src/storage/json.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::fs::File;
use std::io::BufWriter;
use memmap::Mmap;
use serde::de::DeserializeOwned;
use serde::Serialize;
fn convert<T>(from: &str, to: &str)
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));
// 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));
bincode::serialize_into(writer, &v).unwrap();
}
pub fn from<T>(name: &str)
where
T: Serialize + DeserializeOwned,
{
// Convert definitions from json to bincode
let fn_in = format!("{}.json", name);
let fn_out = format!("{}.bin", name);
convert::<T>(&fn_in, &fn_out);
}

3
src/storage/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod bincode;
pub mod json;
pub mod model;

View File

@@ -233,11 +233,13 @@ impl From<&&database::Properties> for Properties {
} }
} }
pub use v1::SpatialObject;
pub fn build_index( pub fn build_index(
name: &str, name: &str,
version: &str, version: &str,
spaces: &[space::Space], spaces: &[space::Space],
objects: &[v1::SpatialObject], objects: &[SpatialObject],
scales: Option<Vec<Vec<u32>>>, scales: Option<Vec<Vec<u32>>>,
max_elements: Option<usize>, max_elements: Option<usize>,
) -> Core { ) -> Core {