2019-05-23 10:29:33 +00:00
|
|
|
use failure::*;
|
|
|
|
use futures::*;
|
2019-05-24 05:36:09 +00:00
|
|
|
use std::collections::HashSet;
|
2019-05-23 10:29:33 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
pub struct ChunkInfo {
|
|
|
|
pub digest: [u8; 32],
|
|
|
|
pub data: bytes::BytesMut,
|
|
|
|
pub offset: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum MergedChunkInfo {
|
|
|
|
Known(Vec<ChunkInfo>),
|
|
|
|
New(ChunkInfo),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MergeKnownChunks: Sized {
|
|
|
|
fn merge_known_chunks(self, known_chunks: Arc<Mutex<HashSet<[u8;32]>>>) -> MergeKnownChunksQueue<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MergeKnownChunksQueue<S> {
|
|
|
|
input: S,
|
|
|
|
known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
|
2019-05-24 05:36:09 +00:00
|
|
|
buffer: Option<MergedChunkInfo>,
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <S> MergeKnownChunks for S
|
|
|
|
where S: Stream<Item=ChunkInfo, Error=Error>,
|
|
|
|
{
|
|
|
|
fn merge_known_chunks(self, known_chunks: Arc<Mutex<HashSet<[u8;32]>>>) -> MergeKnownChunksQueue<Self> {
|
2019-05-24 05:36:09 +00:00
|
|
|
MergeKnownChunksQueue { input: self, known_chunks, buffer: None }
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <S> Stream for MergeKnownChunksQueue<S>
|
|
|
|
where S: Stream<Item=ChunkInfo, Error=Error>,
|
|
|
|
{
|
|
|
|
type Item = MergedChunkInfo;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<MergedChunkInfo>, Error> {
|
|
|
|
loop {
|
|
|
|
match self.input.poll() {
|
|
|
|
Err(err) => {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(None)) => {
|
2019-05-24 05:36:09 +00:00
|
|
|
if let Some(last) = self.buffer.take() {
|
|
|
|
return Ok(Async::Ready(Some(last)));
|
2019-05-23 10:29:33 +00:00
|
|
|
} else {
|
2019-05-24 05:36:09 +00:00
|
|
|
return Ok(Async::Ready(None));
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(Some(chunk_info))) => {
|
|
|
|
|
|
|
|
let mut known_chunks = self.known_chunks.lock().unwrap();
|
|
|
|
let chunk_is_known = known_chunks.contains(&chunk_info.digest);
|
|
|
|
|
|
|
|
if chunk_is_known {
|
|
|
|
|
2019-05-24 05:36:09 +00:00
|
|
|
let last = self.buffer.take();
|
|
|
|
|
|
|
|
match last {
|
|
|
|
None => {
|
|
|
|
self.buffer = Some(MergedChunkInfo::Known(vec![chunk_info]));
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
Some(MergedChunkInfo::Known(mut list)) => {
|
2019-05-23 10:29:33 +00:00
|
|
|
list.push(chunk_info);
|
2019-05-24 05:36:09 +00:00
|
|
|
let len = list.len();
|
|
|
|
self.buffer = Some(MergedChunkInfo::Known(list));
|
|
|
|
|
|
|
|
if len >= 64 {
|
|
|
|
return Ok(Async::Ready(self.buffer.take()));
|
|
|
|
}
|
|
|
|
// continue
|
|
|
|
|
|
|
|
}
|
|
|
|
Some(MergedChunkInfo::New(_)) => {
|
|
|
|
self.buffer = Some(MergedChunkInfo::Known(vec![chunk_info]));
|
|
|
|
return Ok(Async::Ready(last));
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-24 05:36:09 +00:00
|
|
|
|
2019-05-23 10:29:33 +00:00
|
|
|
} else {
|
|
|
|
known_chunks.insert(chunk_info.digest);
|
2019-05-24 05:36:09 +00:00
|
|
|
let new = MergedChunkInfo::New(chunk_info);
|
|
|
|
if let Some(last) = self.buffer.take() {
|
|
|
|
self.buffer = Some(new);
|
|
|
|
return Ok(Async::Ready(Some(last)));
|
|
|
|
} else {
|
|
|
|
return Ok(Async::Ready(Some(new)));
|
|
|
|
}
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|