diff options
Diffstat (limited to 'src/store.rs')
-rw-r--r-- | src/store.rs | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/store.rs b/src/store.rs index e6cc71f..185370e 100644 --- a/src/store.rs +++ b/src/store.rs @@ -1,7 +1,7 @@ +//! Store chunks on-disk on server. + use crate::chunk::DataChunk; use crate::chunkid::ChunkId; -use crate::chunkmeta::ChunkMeta; -use anyhow::Context; use std::path::{Path, PathBuf}; /// Store chunks, with metadata, persistently. @@ -13,6 +13,9 @@ pub struct Store { dir: PathBuf, } +/// An error from a `Store` operation. +pub type StoreError = std::io::Error; + impl Store { /// Create a new Store to represent on-disk storage of chunks.x pub fn new(dir: &Path) -> Self { @@ -38,34 +41,34 @@ impl Store { } /// Save a chunk into a store. - pub fn save(&self, id: &ChunkId, meta: &ChunkMeta, chunk: &DataChunk) -> anyhow::Result<()> { + pub fn save(&self, id: &ChunkId, chunk: &DataChunk) -> Result<(), StoreError> { let (dir, metaname, dataname) = &self.filenames(id); if !dir.exists() { - let res = std::fs::create_dir_all(dir).into(); - if let Err(_) = res { - return res.with_context(|| format!("creating directory {}", dir.display())); - } + std::fs::create_dir_all(dir)?; } - std::fs::write(&metaname, meta.to_json())?; - std::fs::write(&dataname, chunk.data())?; + std::fs::write(metaname, chunk.meta().to_json())?; + std::fs::write(dataname, chunk.data())?; Ok(()) } /// Load a chunk from a store. - pub fn load(&self, id: &ChunkId) -> anyhow::Result<DataChunk> { - let (_, _, dataname) = &self.filenames(id); - let data = std::fs::read(&dataname)?; - let data = DataChunk::new(data); + pub fn load(&self, id: &ChunkId) -> Result<DataChunk, StoreError> { + let (_, metaname, dataname) = &self.filenames(id); + let meta = std::fs::read(metaname)?; + let meta = serde_json::from_slice(&meta)?; + + let data = std::fs::read(dataname)?; + let data = DataChunk::new(data, meta); Ok(data) } /// Delete a chunk from a store. - pub fn delete(&self, id: &ChunkId) -> anyhow::Result<()> { + pub fn delete(&self, id: &ChunkId) -> Result<(), StoreError> { let (_, metaname, dataname) = &self.filenames(id); - std::fs::remove_file(&metaname)?; - std::fs::remove_file(&dataname)?; + std::fs::remove_file(metaname)?; + std::fs::remove_file(dataname)?; Ok(()) } } |