summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-10-14 08:57:04 +0300
committerLars Wirzenius <liw@liw.fi>2020-10-14 08:57:04 +0300
commitb58261f996883a4f63487a50f48cbb20cf7d8fdb (patch)
tree4e6cec82e18ab73482ddd498e0efb480f8978b1a /src
parentafebc46461edea5e14d5b63a10631af9660a3439 (diff)
downloadobnam2-b58261f996883a4f63487a50f48cbb20cf7d8fdb.tar.gz
refactor: move chunk metadata out of chunk struct
Diffstat (limited to 'src')
-rw-r--r--src/bin/obnam-backup.rs15
-rw-r--r--src/bin/obnam-server.rs23
-rw-r--r--src/chunk.rs11
-rw-r--r--src/store.rs8
4 files changed, 25 insertions, 32 deletions
diff --git a/src/bin/obnam-backup.rs b/src/bin/obnam-backup.rs
index 2b767af..7f761bc 100644
--- a/src/bin/obnam-backup.rs
+++ b/src/bin/obnam-backup.rs
@@ -41,11 +41,11 @@ fn main() -> anyhow::Result<()> {
loop {
match read_chunk(&mut stdin)? {
None => break,
- Some(chunk) => {
+ Some((meta, chunk)) => {
let n = chunk.data().len() as u64;
- if !has_chunk(&client, &config, &chunk.meta())? {
+ if !has_chunk(&client, &config, &meta)? {
pb.inc(n);
- upload_chunk(&client, &config, chunk)?;
+ upload_chunk(&client, &config, meta, chunk)?;
} else {
dup += n;
}
@@ -75,7 +75,7 @@ impl Config {
}
}
-fn read_chunk<H>(handle: &mut H) -> anyhow::Result<Option<Chunk>>
+fn read_chunk<H>(handle: &mut H) -> anyhow::Result<Option<(ChunkMeta, Chunk)>>
where
H: Read + BufRead,
{
@@ -101,13 +101,14 @@ where
let hash = format!("{:x}", hash);
let meta = ChunkMeta::new(&hash);
- let chunk = Chunk::new(meta, buffer.to_vec());
- Ok(Some(chunk))
+ let chunk = Chunk::new(buffer.to_vec());
+ Ok(Some((meta, chunk)))
}
fn upload_chunk(
client: &reqwest::blocking::Client,
config: &Config,
+ meta: ChunkMeta,
chunk: Chunk,
) -> anyhow::Result<()> {
let url = format!(
@@ -117,7 +118,7 @@ fn upload_chunk(
client
.post(&url)
- .header("chunk-meta", chunk.meta().to_json())
+ .header("chunk-meta", meta.to_json())
.body(chunk.data().to_vec())
.send()?;
Ok(())
diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs
index 17ca916..7d37383 100644
--- a/src/bin/obnam-server.rs
+++ b/src/bin/obnam-server.rs
@@ -140,9 +140,9 @@ pub async fn create_chunk(
}
};
- let chunk = Chunk::new(meta.clone(), data.to_vec());
+ let chunk = Chunk::new(data.to_vec());
- match store.save(&id, &chunk) {
+ match store.save(&id, &meta, &chunk) {
Ok(_) => (),
Err(e) => {
error!("could not write chunk to disk: {}", e);
@@ -168,9 +168,9 @@ pub async fn fetch_chunk(
let store = Store::new(&config.chunks);
let id: ChunkId = id.parse().unwrap();
match store.load(&id) {
- Ok(chunk) => {
- info!("found chunk {}: {:?}", id, chunk.meta());
- Ok(ChunkResult::Fetched(chunk))
+ Ok((meta, chunk)) => {
+ info!("found chunk {}: {:?}", id, meta);
+ Ok(ChunkResult::Fetched(meta, chunk))
}
Err(e) => {
error!("chunk not found: {}: {:?}", id, e);
@@ -252,10 +252,10 @@ pub async fn delete_chunk(
let store = Store::new(&config.chunks);
let id: ChunkId = id.parse().unwrap();
- let chunk = match store.load(&id) {
- Ok(chunk) => {
+ let (meta, _) = match store.load(&id) {
+ Ok((meta, chunk)) => {
debug!("found chunk to delete: {}", id);
- chunk
+ (meta, chunk)
}
Err(e) => {
error!("could not find chunk to delete: {}: {:?}", id, e);
@@ -263,7 +263,6 @@ pub async fn delete_chunk(
}
};
- let meta = chunk.meta();
index.remove("sha256", meta.sha256());
index.remove_generation(&id);
@@ -281,7 +280,7 @@ pub async fn delete_chunk(
enum ChunkResult {
Created(ChunkId),
- Fetched(Chunk),
+ Fetched(ChunkMeta, Chunk),
Found(SearchHits),
Deleted,
NotFound,
@@ -304,11 +303,11 @@ impl warp::Reply for ChunkResult {
let body = serde_json::to_string(&body).unwrap();
json_response(StatusCode::CREATED, body, None)
}
- ChunkResult::Fetched(chunk) => {
+ ChunkResult::Fetched(meta, chunk) => {
let mut headers = HashMap::new();
headers.insert(
"chunk-meta".to_string(),
- serde_json::to_string(&chunk.meta()).unwrap(),
+ serde_json::to_string(&meta).unwrap(),
);
into_response(
StatusCode::OK,
diff --git a/src/chunk.rs b/src/chunk.rs
index 33d2d32..f3a7b79 100644
--- a/src/chunk.rs
+++ b/src/chunk.rs
@@ -1,4 +1,3 @@
-use crate::chunkmeta::ChunkMeta;
use serde::Serialize;
/// Store an arbitrary chunk of data.
@@ -9,19 +8,13 @@ use serde::Serialize;
/// identifier.
#[derive(Debug, Serialize)]
pub struct Chunk {
- meta: ChunkMeta,
data: Vec<u8>,
}
impl Chunk {
/// Construct a new chunk.
- pub fn new(meta: ChunkMeta, data: Vec<u8>) -> Self {
- Chunk { meta, data }
- }
-
- /// Return a chunk's metadata.
- pub fn meta(&self) -> &ChunkMeta {
- &self.meta
+ pub fn new(data: Vec<u8>) -> Self {
+ Chunk { data }
}
/// Return a chunk's data.
diff --git a/src/store.rs b/src/store.rs
index 873b8f2..bad52ce 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -26,8 +26,8 @@ impl Store {
}
/// Save a chunk into a store.
- pub fn save(&self, id: &ChunkId, chunk: &Chunk) -> anyhow::Result<()> {
- std::fs::write(&self.filename(id, "meta"), chunk.meta().to_json())?;
+ pub fn save(&self, id: &ChunkId, meta: &ChunkMeta, chunk: &Chunk) -> anyhow::Result<()> {
+ std::fs::write(&self.filename(id, "meta"), meta.to_json())?;
std::fs::write(&self.filename(id, "data"), chunk.data())?;
Ok(())
}
@@ -39,10 +39,10 @@ impl Store {
}
/// Load a chunk from a store.
- pub fn load(&self, id: &ChunkId) -> anyhow::Result<Chunk> {
+ pub fn load(&self, id: &ChunkId) -> anyhow::Result<(ChunkMeta, Chunk)> {
let meta = self.load_meta(id)?;
let data = std::fs::read(&self.filename(id, "data"))?;
- Ok(Chunk::new(meta, data))
+ Ok((meta, Chunk::new(data)))
}
/// Delete a chunk from a store.