summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Batischev <eual.jp@gmail.com>2022-06-24 22:40:03 +0300
committerLars Wirzenius <liw@liw.fi>2022-10-26 17:19:55 +0300
commit892df3d488f2df04566f9c19285971f2954d3361 (patch)
treebaa47511605cac36df1e5e7fd55ac032fc3a5af6
parentbe9275342cde83ee51840e5859bf9fa0b2cee4eb (diff)
downloadobnam2-892df3d488f2df04566f9c19285971f2954d3361.tar.gz
Make LocalStore Sync (provide interior mutability)
-rw-r--r--src/chunkstore.rs25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/chunkstore.rs b/src/chunkstore.rs
index cb78891..85d5007 100644
--- a/src/chunkstore.rs
+++ b/src/chunkstore.rs
@@ -13,6 +13,7 @@ use log::{debug, error, info};
use reqwest::header::HeaderMap;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
+use tokio::sync::Mutex;
/// A chunk store.
///
@@ -41,7 +42,7 @@ impl ChunkStore {
/// Does the store have a chunk with a given label?
pub async fn find_by_label(&self, meta: &ChunkMeta) -> Result<Vec<ChunkId>, StoreError> {
match self {
- Self::Local(store) => store.find_by_label(meta),
+ Self::Local(store) => store.find_by_label(meta).await,
Self::Remote(store) => store.find_by_label(meta).await,
}
}
@@ -49,9 +50,9 @@ impl ChunkStore {
/// Store a chunk in the store.
///
/// The store chooses an id for the chunk.
- pub async fn put(&mut self, chunk: Vec<u8>, meta: &ChunkMeta) -> Result<ChunkId, StoreError> {
+ pub async fn put(&self, chunk: Vec<u8>, meta: &ChunkMeta) -> Result<ChunkId, StoreError> {
match self {
- Self::Local(store) => store.put(chunk, meta),
+ Self::Local(store) => store.put(chunk, meta).await,
Self::Remote(store) => store.put(chunk, meta).await,
}
}
@@ -59,7 +60,7 @@ impl ChunkStore {
/// Get a chunk given its id.
pub async fn get(&self, id: &ChunkId) -> Result<(Vec<u8>, ChunkMeta), StoreError> {
match self {
- Self::Local(store) => store.get(id),
+ Self::Local(store) => store.get(id).await,
Self::Remote(store) => store.get(id).await,
}
}
@@ -68,24 +69,26 @@ impl ChunkStore {
/// A local chunk store.
pub struct LocalStore {
path: PathBuf,
- index: Index,
+ index: Mutex<Index>,
}
impl LocalStore {
fn new(path: &Path) -> Result<Self, StoreError> {
Ok(Self {
path: path.to_path_buf(),
- index: Index::new(path)?,
+ index: Mutex::new(Index::new(path)?),
})
}
- fn find_by_label(&self, meta: &ChunkMeta) -> Result<Vec<ChunkId>, StoreError> {
+ async fn find_by_label(&self, meta: &ChunkMeta) -> Result<Vec<ChunkId>, StoreError> {
self.index
+ .lock()
+ .await
.find_by_label(meta.label())
.map_err(StoreError::Index)
}
- fn put(&mut self, chunk: Vec<u8>, meta: &ChunkMeta) -> Result<ChunkId, StoreError> {
+ async fn put(&self, chunk: Vec<u8>, meta: &ChunkMeta) -> Result<ChunkId, StoreError> {
let id = ChunkId::new();
let (dir, filename) = self.filename(&id);
@@ -96,13 +99,15 @@ impl LocalStore {
std::fs::write(&filename, &chunk)
.map_err(|err| StoreError::WriteChunk(filename.clone(), err))?;
self.index
+ .lock()
+ .await
.insert_meta(id.clone(), meta.clone())
.map_err(StoreError::Index)?;
Ok(id)
}
- fn get(&self, id: &ChunkId) -> Result<(Vec<u8>, ChunkMeta), StoreError> {
- let meta = self.index.get_meta(id)?;
+ async fn get(&self, id: &ChunkId) -> Result<(Vec<u8>, ChunkMeta), StoreError> {
+ let meta = self.index.lock().await.get_meta(id)?;
let (_, filename) = &self.filename(id);