diff options
Diffstat (limited to 'src/indexedstore.rs')
-rw-r--r-- | src/indexedstore.rs | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/indexedstore.rs b/src/indexedstore.rs index 5a41406..3f6235f 100644 --- a/src/indexedstore.rs +++ b/src/indexedstore.rs @@ -3,13 +3,15 @@ use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; use crate::index::Index; use crate::store::{LoadedChunk, Store}; -use std::path::Path; +use std::path::{Path, PathBuf}; +use walkdir::WalkDir; /// A store for chunks and their metadata. /// /// This combines Store and Index into one interface to make it easier /// to handle the server side storage of chunks. pub struct IndexedStore { + dirname: PathBuf, store: Store, index: Index, } @@ -18,17 +20,47 @@ impl IndexedStore { pub fn new(dirname: &Path) -> Self { let store = Store::new(dirname); let index = Index::default(); - Self { store, index } + Self { + dirname: dirname.to_path_buf(), + store, + index, + } + } + + pub fn fill_index(&mut self) -> anyhow::Result<()> { + for entry in WalkDir::new(&self.dirname) { + let entry = entry?; + let path = entry.path(); + // println!("found entry: {:?} (ext: {:?})", path, path.extension()); + if let Some(ext) = path.extension() { + if ext == "meta" { + println!("found meta: {:?}", path); + let text = std::fs::read(path)?; + let meta: ChunkMeta = serde_json::from_slice(&text)?; + if let Some(stem) = path.file_stem() { + let id: ChunkId = stem.into(); + println!("id: {:?}", id); + self.insert_meta(&id, &meta); + } + } + } + println!(""); + } + Ok(()) } pub fn save(&mut self, meta: &ChunkMeta, chunk: &DataChunk) -> anyhow::Result<ChunkId> { let id = ChunkId::new(); self.store.save(&id, meta, chunk)?; + self.insert_meta(&id, meta); + Ok(id) + } + + fn insert_meta(&mut self, id: &ChunkId, meta: &ChunkMeta) { self.index.insert(id.clone(), "sha256", meta.sha256()); if meta.is_generation() { self.index.insert_generation(id.clone()); } - Ok(id) } pub fn load(&self, id: &ChunkId) -> anyhow::Result<LoadedChunk> { |