summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-01-03 13:09:35 +0200
committerLars Wirzenius <liw@liw.fi>2021-01-03 13:53:43 +0200
commit543107a70eeffaa6932c87c02b43b0fd4f8558e5 (patch)
tree2cfb0b9e4dd69f93413d0e934e1113534d7352dc /src
parente6a4eb6edf83a68a6f73094126804beb0c569937 (diff)
downloadobnam2-543107a70eeffaa6932c87c02b43b0fd4f8558e5.tar.gz
feat: load chunk metadata into index at startup
This needs to be replace with a database or something, but it'll do for now.
Diffstat (limited to 'src')
-rw-r--r--src/bin/obnam-server.rs4
-rw-r--r--src/chunkid.rs9
-rw-r--r--src/indexedstore.rs38
3 files changed, 47 insertions, 4 deletions
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<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> {