diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-01-09 16:52:35 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-01-10 18:17:27 +0200 |
commit | e5f68184bfe91f6874fe8c2344dbd5fa613d6bee (patch) | |
tree | 9860e2bc6c827bf69d1c324303e01c86efd94e65 /src/indexedstore.rs | |
parent | 7ff248232a414b907b3abe464cc015e5ea48c236 (diff) | |
download | obnam2-e5f68184bfe91f6874fe8c2344dbd5fa613d6bee.tar.gz |
feat! use SQLite db for chunk index on server
This speeds startup a lot. However, the backup repository needs to be
re-created from scratch and internal APIs have change in incompatible
ways.
Diffstat (limited to 'src/indexedstore.rs')
-rw-r--r-- | src/indexedstore.rs | 64 |
1 files changed, 16 insertions, 48 deletions
diff --git a/src/indexedstore.rs b/src/indexedstore.rs index 3f6235f..0366013 100644 --- a/src/indexedstore.rs +++ b/src/indexedstore.rs @@ -2,87 +2,55 @@ use crate::chunk::DataChunk; use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; use crate::index::Index; -use crate::store::{LoadedChunk, Store}; -use std::path::{Path, PathBuf}; -use walkdir::WalkDir; +use crate::store::Store; +use std::path::Path; /// 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, } impl IndexedStore { - pub fn new(dirname: &Path) -> Self { + pub fn new(dirname: &Path) -> anyhow::Result<Self> { let store = Store::new(dirname); - let index = Index::default(); - 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(()) + let index = Index::new(dirname)?; + Ok(Self { store, index }) } 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); + 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()); - } + fn insert_meta(&mut self, id: &ChunkId, meta: &ChunkMeta) -> anyhow::Result<()> { + self.index.insert_meta(id.clone(), meta.clone())?; + Ok(()) } - pub fn load(&self, id: &ChunkId) -> anyhow::Result<LoadedChunk> { - self.store.load(id) + pub fn load(&self, id: &ChunkId) -> anyhow::Result<(DataChunk, ChunkMeta)> { + Ok((self.store.load(id)?, self.load_meta(id)?)) } pub fn load_meta(&self, id: &ChunkId) -> anyhow::Result<ChunkMeta> { - self.store.load_meta(id) + self.index.get_meta(id) } - pub fn find_by_sha256(&self, sha256: &str) -> Vec<ChunkId> { - self.index.find("sha256", sha256) + pub fn find_by_sha256(&self, sha256: &str) -> anyhow::Result<Vec<ChunkId>> { + self.index.find_by_sha256(sha256) } - pub fn find_generations(&self) -> Vec<ChunkId> { + pub fn find_generations(&self) -> anyhow::Result<Vec<ChunkId>> { self.index.find_generations() } pub fn remove(&mut self, id: &ChunkId) -> anyhow::Result<()> { - let loaded = self.store.load(id)?; - self.index.remove("sha256", loaded.meta().sha256()); - self.index.remove_generation(id); + self.index.remove_meta(id).unwrap(); self.store.delete(id)?; Ok(()) } |