start implementing DataStore
This commit is contained in:
		
							
								
								
									
										43
									
								
								src/backup/datastore.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/backup/datastore.rs
									
									
									
									
									
										Normal 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)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<Value, Error> {
 | 
			
		||||
    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<Value, Error> {
 | 
			
		||||
        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<Value, Error> {
 | 
			
		||||
        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)
 | 
			
		||||
 | 
			
		||||
@ -36,6 +36,7 @@ pub mod backup {
 | 
			
		||||
 | 
			
		||||
    pub mod chunk_store;
 | 
			
		||||
    pub mod image_index;
 | 
			
		||||
    pub mod datastore;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub mod config {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user