summaryrefslogtreecommitdiff
path: root/src/indexedstore.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-11-24 08:35:32 +0200
committerLars Wirzenius <liw@liw.fi>2020-11-24 09:14:46 +0200
commitcf18e86c351ddbaf655d02e66c1666cca4806947 (patch)
treec7c52a9f734cb6d582e4ec4d2633b93cd510ff1c /src/indexedstore.rs
parent1172320e0da7687c62c1f020761903029b6bf3a2 (diff)
downloadobnam2-cf18e86c351ddbaf655d02e66c1666cca4806947.tar.gz
refactor: add an abstraction for an indexed store
This makes it easier to write a server without the HTTP layer.
Diffstat (limited to 'src/indexedstore.rs')
-rw-r--r--src/indexedstore.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/indexedstore.rs b/src/indexedstore.rs
new file mode 100644
index 0000000..4cc90cc
--- /dev/null
+++ b/src/indexedstore.rs
@@ -0,0 +1,57 @@
+use crate::chunk::DataChunk;
+use crate::chunkid::ChunkId;
+use crate::chunkmeta::ChunkMeta;
+use crate::index::Index;
+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 {
+ store: Store,
+ index: Index,
+}
+
+impl IndexedStore {
+ pub fn new(dirname: &Path) -> Self {
+ let store = Store::new(dirname);
+ let index = Index::default();
+ 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.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<(ChunkMeta, DataChunk)> {
+ self.store.load(id)
+ }
+
+ pub fn load_meta(&self, id: &ChunkId) -> anyhow::Result<ChunkMeta> {
+ self.store.load_meta(id)
+ }
+
+ pub fn find_by_sha256(&self, sha256: &str) -> Vec<ChunkId> {
+ self.index.find("sha256", sha256)
+ }
+
+ pub fn find_generations(&self) -> Vec<ChunkId> {
+ self.index.find_generations()
+ }
+
+ pub fn remove(&mut self, id: &ChunkId) -> anyhow::Result<()> {
+ let (meta, _) = self.store.load(id)?;
+ self.index.remove("sha256", meta.sha256());
+ self.index.remove_generation(id);
+ self.store.delete(id)?;
+ Ok(())
+ }
+}