From 543107a70eeffaa6932c87c02b43b0fd4f8558e5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 3 Jan 2021 13:09:35 +0200 Subject: feat: load chunk metadata into index at startup This needs to be replace with a database or something, but it'll do for now. --- src/bin/obnam-server.rs | 4 +++- src/chunkid.rs | 9 +++++++++ src/indexedstore.rs | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs index dc8aa82..76d018f 100644 --- a/src/bin/obnam-server.rs +++ b/src/bin/obnam-server.rs @@ -36,7 +36,9 @@ async fn main() -> anyhow::Result<()> { return Err(ConfigError::BadServerAddress.into()); } - let store = IndexedStore::new(&config.chunks); + let mut store = IndexedStore::new(&config.chunks); + store.fill_index()?; + println!("existing generations: {:?}", store.find_generations()); let store = Arc::new(Mutex::new(store)); let store = warp::any().map(move || Arc::clone(&store)); diff --git a/src/chunkid.rs b/src/chunkid.rs index 73e0b27..9eec41f 100644 --- a/src/chunkid.rs +++ b/src/chunkid.rs @@ -2,6 +2,7 @@ use crate::checksummer::sha256; use rusqlite::types::ToSqlOutput; use rusqlite::ToSql; use serde::{Deserialize, Serialize}; +use std::ffi::OsStr; use std::fmt; use std::hash::Hash; use std::str::FromStr; @@ -68,6 +69,14 @@ impl From<&String> for ChunkId { } } +impl From<&OsStr> for ChunkId { + fn from(s: &OsStr) -> Self { + ChunkId { + id: s.to_string_lossy().to_string(), + } + } +} + impl FromStr for ChunkId { type Err = (); 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 { 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 { -- cgit v1.2.1