From 3c6c19e12658f44793a7c8cfedf9b74a6ba92933 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sat, 20 Nov 2021 11:02:07 +0000 Subject: [PATCH] Calculate chunk ID before submission --- yama/src/chunking.rs | 9 +++++---- yama/src/pile.rs | 7 ++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/yama/src/chunking.rs b/yama/src/chunking.rs index d06d548..286fca3 100644 --- a/yama/src/chunking.rs +++ b/yama/src/chunking.rs @@ -73,10 +73,10 @@ impl<'pile, RP: RawPile> RecursiveChunker<'pile, RP> { let is_final = chunk.offset + chunk.length == self.buffer.len(); if !is_final || finalise { consumed_until = Some(chunk.offset + chunk.length); - let chunk_id = self - .pile - .submit_chunk(&self.buffer[chunk.offset..chunk.offset + chunk.length])?; + let chunk_data = &self.buffer[chunk.offset..chunk.offset + chunk.length]; + let chunk_id = calculate_chunkid(chunk_data); new_chunks.extend_from_slice(&chunk_id); + self.pile.submit_chunk(chunk_id, chunk_data)?; } } @@ -101,7 +101,8 @@ impl<'pile, RP: RawPile> RecursiveChunker<'pile, RP> { Ok(rcr) } else { // no chunking, so depth=0 (raw) and just emit our unchunked data - let chunk_id = self.pile.submit_chunk(&self.buffer)?; + let chunk_id = calculate_chunkid(&self.buffer); + self.pile.submit_chunk(chunk_id, &self.buffer)?; Ok(RecursiveChunkRef { chunk_id, depth: 0 }) } } diff --git a/yama/src/pile.rs b/yama/src/pile.rs index df2ec2d..0f99fd9 100644 --- a/yama/src/pile.rs +++ b/yama/src/pile.rs @@ -19,7 +19,6 @@ use std::path::PathBuf; use serde::{Deserialize, Serialize}; -use crate::chunking::calculate_chunkid; use crate::definitions::{ChunkId, PointerData}; use crossbeam_channel::Sender; use std::collections::HashSet; @@ -290,9 +289,7 @@ impl Pile { Ok(result) } - pub fn submit_chunk(&self, chunk_data: &[u8]) -> anyhow::Result { - let chunk_id = calculate_chunkid(chunk_data); - + pub fn submit_chunk(&self, chunk_id: ChunkId, chunk_data: &[u8]) -> anyhow::Result<()> { let mut racy_submissions = self.racy_submission_mutex.lock().unwrap(); if racy_submissions.insert(chunk_id) { drop(racy_submissions); @@ -311,7 +308,7 @@ impl Pile { } } } - Ok(chunk_id) + Ok(()) } /// Flushes buffered writes. Should really run this before exiting, so I can sleep better at