start implementing DataStore

This commit is contained in:
Dietmar Maurer 2018-12-17 13:00:39 +01:00
parent 4818c8b6f7
commit 529de6c7a3
3 changed files with 48 additions and 9 deletions

43
src/backup/datastore.rs Normal file
View File

@ -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<Self, Error> {
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<P: AsRef<Path>>(&mut self, filename: P, size: usize) -> Result<ImageIndexWriter, Error> {
let index = ImageIndexWriter::create(&mut self.chunk_store, filename.as_ref(), size)?;
Ok(index)
}
pub fn open_image_reader<P: AsRef<Path>>(&mut self, filename: P) -> Result<ImageIndexReader, Error> {
let index = ImageIndexReader::open(&mut self.chunk_store, filename.as_ref())?;
Ok(index)
}
}

View File

@ -9,6 +9,7 @@ use apitest::api::schema::*;
use apitest::api::router::*; use apitest::api::router::*;
use apitest::backup::chunk_store::*; use apitest::backup::chunk_store::*;
use apitest::backup::image_index::*; use apitest::backup::image_index::*;
use apitest::backup::datastore::*;
use serde_json::{Value}; use serde_json::{Value};
use apitest::config::datastore; use apitest::config::datastore;
@ -23,13 +24,7 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
let filename = required_string_param(&param, "filename"); let filename = required_string_param(&param, "filename");
let store = required_string_param(&param, "store"); let store = required_string_param(&param, "store");
let config = datastore::config()?; let mut datastore = DataStore::open(store)?;
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)?;
println!("Backup file '{}' to '{}'", filename, store); println!("Backup file '{}' to '{}'", filename, store);
@ -41,7 +36,7 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
let size = stat.st_size as usize; 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| { tools::file_chunker(file, 64*1024, |pos, chunk| {
index.add_chunk(pos, chunk)?; index.add_chunk(pos, chunk)?;
@ -51,7 +46,7 @@ fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
index.close()?; // commit changes index.close()?; // commit changes
} }
let idx = ImageIndexReader::open(&mut chunk_store, target.as_ref())?; let idx = datastore.open_image_reader(target)?;
idx.print_info(); idx.print_info();
Ok(Value::Null) Ok(Value::Null)

View File

@ -36,6 +36,7 @@ pub mod backup {
pub mod chunk_store; pub mod chunk_store;
pub mod image_index; pub mod image_index;
pub mod datastore;
} }
pub mod config { pub mod config {