From 529de6c7a3f6301faca011eda7d45da46d36d9e0 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 17 Dec 2018 13:00:39 +0100 Subject: [PATCH] start implementing DataStore --- src/backup/datastore.rs | 43 ++++++++++++++++++++++++++++++++++++++++ src/bin/backup-client.rs | 13 ++++-------- src/lib.rs | 1 + 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/backup/datastore.rs diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs new file mode 100644 index 00000000..3c6c5b7f --- /dev/null +++ b/src/backup/datastore.rs @@ -0,0 +1,43 @@ +use failure::*; + +use std::path::Path; + +use crate::config::datastore; +use super::chunk_store::*; +use super::image_index::*; + +pub struct DataStore { + chunk_store: ChunkStore, +} + +impl DataStore { + + pub fn open(store_name: &str) -> Result { + + let config = datastore::config()?; + let (_, store_config) = config.sections.get(store_name) + .ok_or(format_err!("no such datastore '{}'", store_name))?; + + let path = store_config["path"].as_str().unwrap(); + + let chunk_store = ChunkStore::open(path)?; + + Ok(Self { + chunk_store: chunk_store, + }) + } + + pub fn create_image_writer>(&mut self, filename: P, size: usize) -> Result { + + let index = ImageIndexWriter::create(&mut self.chunk_store, filename.as_ref(), size)?; + + Ok(index) + } + + pub fn open_image_reader>(&mut self, filename: P) -> Result { + + let index = ImageIndexReader::open(&mut self.chunk_store, filename.as_ref())?; + + Ok(index) + } +} diff --git a/src/bin/backup-client.rs b/src/bin/backup-client.rs index e4606722..e080f2ec 100644 --- a/src/bin/backup-client.rs +++ b/src/bin/backup-client.rs @@ -9,6 +9,7 @@ use apitest::api::schema::*; use apitest::api::router::*; use apitest::backup::chunk_store::*; use apitest::backup::image_index::*; +use apitest::backup::datastore::*; use serde_json::{Value}; use apitest::config::datastore; @@ -23,13 +24,7 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result { let filename = required_string_param(¶m, "filename"); let store = required_string_param(¶m, "store"); - let config = datastore::config()?; - let (_, store_config) = config.sections.get(store) - .ok_or(format_err!("no such datastore '{}'", store))?; - - let path = store_config["path"].as_str().unwrap(); - - let mut chunk_store = ChunkStore::open(path)?; + let mut datastore = DataStore::open(store)?; println!("Backup file '{}' to '{}'", filename, store); @@ -41,7 +36,7 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result { if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } let size = stat.st_size as usize; - let mut index = ImageIndexWriter::create(&mut chunk_store, target.as_ref(), size)?; + let mut index = datastore.create_image_writer(target, size)?; tools::file_chunker(file, 64*1024, |pos, chunk| { index.add_chunk(pos, chunk)?; @@ -51,7 +46,7 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result { index.close()?; // commit changes } - let idx = ImageIndexReader::open(&mut chunk_store, target.as_ref())?; + let idx = datastore.open_image_reader(target)?; idx.print_info(); Ok(Value::Null) diff --git a/src/lib.rs b/src/lib.rs index 011232fc..cbf90d39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,7 @@ pub mod backup { pub mod chunk_store; pub mod image_index; + pub mod datastore; } pub mod config {