2019-08-23 12:31:23 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{Error};
|
2019-05-23 10:29:33 +00:00
|
|
|
use futures::*;
|
2020-11-10 13:33:33 +00:00
|
|
|
use pin_project::pin_project;
|
2019-05-23 10:29:33 +00:00
|
|
|
|
2019-06-13 05:35:02 +00:00
|
|
|
use crate::backup::ChunkInfo;
|
2019-05-23 10:29:33 +00:00
|
|
|
|
|
|
|
pub enum MergedChunkInfo {
|
2019-08-22 11:46:31 +00:00
|
|
|
Known(Vec<(u64, [u8; 32])>),
|
2019-05-23 10:29:33 +00:00
|
|
|
New(ChunkInfo),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait MergeKnownChunks: Sized {
|
2019-05-26 08:52:56 +00:00
|
|
|
fn merge_known_chunks(self) -> MergeKnownChunksQueue<Self>;
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:33:33 +00:00
|
|
|
#[pin_project]
|
2019-05-23 10:29:33 +00:00
|
|
|
pub struct MergeKnownChunksQueue<S> {
|
2020-11-10 13:33:33 +00:00
|
|
|
#[pin]
|
2019-05-23 10:29:33 +00:00
|
|
|
input: S,
|
2019-05-24 05:36:09 +00:00
|
|
|
buffer: Option<MergedChunkInfo>,
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:46:31 +00:00
|
|
|
impl<S> MergeKnownChunks for S
|
|
|
|
where
|
2019-08-23 12:31:23 +00:00
|
|
|
S: Stream<Item = Result<MergedChunkInfo, Error>>,
|
2019-05-23 10:29:33 +00:00
|
|
|
{
|
2019-05-26 08:52:56 +00:00
|
|
|
fn merge_known_chunks(self) -> MergeKnownChunksQueue<Self> {
|
2019-08-22 11:46:31 +00:00
|
|
|
MergeKnownChunksQueue {
|
|
|
|
input: self,
|
|
|
|
buffer: None,
|
|
|
|
}
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 11:46:31 +00:00
|
|
|
impl<S> Stream for MergeKnownChunksQueue<S>
|
|
|
|
where
|
2019-08-23 12:31:23 +00:00
|
|
|
S: Stream<Item = Result<MergedChunkInfo, Error>>,
|
2019-05-23 10:29:33 +00:00
|
|
|
{
|
2019-08-23 12:31:23 +00:00
|
|
|
type Item = Result<MergedChunkInfo, Error>;
|
|
|
|
|
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
2020-11-10 13:33:33 +00:00
|
|
|
let mut this = self.project();
|
2019-05-23 10:29:33 +00:00
|
|
|
|
|
|
|
loop {
|
2020-11-10 13:33:33 +00:00
|
|
|
match ready!(this.input.as_mut().poll_next(cx)) {
|
2019-08-23 12:31:23 +00:00
|
|
|
Some(Err(err)) => return Poll::Ready(Some(Err(err))),
|
|
|
|
None => {
|
|
|
|
if let Some(last) = this.buffer.take() {
|
|
|
|
return Poll::Ready(Some(Ok(last)));
|
2019-05-23 10:29:33 +00:00
|
|
|
} else {
|
2019-08-23 12:31:23 +00:00
|
|
|
return Poll::Ready(None);
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 12:31:23 +00:00
|
|
|
Some(Ok(mergerd_chunk_info)) => {
|
2019-05-26 08:52:56 +00:00
|
|
|
match mergerd_chunk_info {
|
|
|
|
MergedChunkInfo::Known(list) => {
|
2019-08-23 12:31:23 +00:00
|
|
|
let last = this.buffer.take();
|
2019-05-23 10:29:33 +00:00
|
|
|
|
2019-05-26 08:52:56 +00:00
|
|
|
match last {
|
|
|
|
None => {
|
2020-11-10 13:33:33 +00:00
|
|
|
*this.buffer = Some(MergedChunkInfo::Known(list));
|
2019-05-26 08:52:56 +00:00
|
|
|
// continue
|
2019-05-24 05:36:09 +00:00
|
|
|
}
|
2019-05-26 08:52:56 +00:00
|
|
|
Some(MergedChunkInfo::Known(mut last_list)) => {
|
|
|
|
last_list.extend_from_slice(&list);
|
|
|
|
let len = last_list.len();
|
2020-11-10 13:33:33 +00:00
|
|
|
*this.buffer = Some(MergedChunkInfo::Known(last_list));
|
2019-05-24 05:36:09 +00:00
|
|
|
|
2019-05-26 08:52:56 +00:00
|
|
|
if len >= 64 {
|
2019-08-23 12:31:23 +00:00
|
|
|
return Poll::Ready(this.buffer.take().map(Ok));
|
2019-05-26 08:52:56 +00:00
|
|
|
}
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
Some(MergedChunkInfo::New(_)) => {
|
2020-11-10 13:33:33 +00:00
|
|
|
*this.buffer = Some(MergedChunkInfo::Known(list));
|
2019-08-23 12:31:23 +00:00
|
|
|
return Poll::Ready(last.map(Ok));
|
2019-05-26 08:52:56 +00:00
|
|
|
}
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 08:52:56 +00:00
|
|
|
MergedChunkInfo::New(chunk_info) => {
|
|
|
|
let new = MergedChunkInfo::New(chunk_info);
|
2019-08-23 12:31:23 +00:00
|
|
|
if let Some(last) = this.buffer.take() {
|
2020-11-10 13:33:33 +00:00
|
|
|
*this.buffer = Some(new);
|
2019-08-23 12:31:23 +00:00
|
|
|
return Poll::Ready(Some(Ok(last)));
|
2019-05-26 08:52:56 +00:00
|
|
|
} else {
|
2019-08-23 12:31:23 +00:00
|
|
|
return Poll::Ready(Some(Ok(new)));
|
2019-05-26 08:52:56 +00:00
|
|
|
}
|
2019-05-24 05:36:09 +00:00
|
|
|
}
|
2019-05-23 10:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|