1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::ReadAt;
use async_trait::async_trait;
use futures::{lock::Mutex, AsyncRead, AsyncReadExt};
use pin_project::pin_project;
use std::{
    collections::VecDeque,
    fmt, io,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

/// Can build readers that read starting at a given offset
#[async_trait(?Send)]
pub trait GetReaderAt {
    type Reader: AsyncRead + Unpin;

    /// Returns a type reading the resource starting at `offset`
    async fn get_reader_at(self: &Arc<Self>, offset: u64) -> io::Result<Self::Reader>;
}

/// Wrapper that provides `ReadAt` from a type that implements `GetReaderAt`
pub struct ReadAtWrapper<Source>
where
    Source: GetReaderAt,
{
    heads: Mutex<VecDeque<Head<Source::Reader>>>,
    source: Arc<Source>,
    len: u64,
    max_heads: usize,
}

impl<Source> ReadAtWrapper<Source>
where
    Source: GetReaderAt,
{
    pub const DEFAULT_MAX_HEADS: usize = 3;

    pub fn new(
        source: Arc<Source>,
        len: u64,
        mut initial_head: Option<(u64, Source::Reader)>,
    ) -> Self {
        let mut heads: VecDeque<Head<Source::Reader>> = Default::default();
        if let Some((offset, reader)) = initial_head.take() {
            let head = Head { offset, reader };
            tracing::debug!("{:?}: initial", head);
            heads.push_back(head);
        }

        Self {
            heads: Mutex::new(heads),
            source,
            len,
            max_heads: Self::DEFAULT_MAX_HEADS,
        }
    }

    async fn borrow_head(&self, offset: u64) -> io::Result<Head<Source::Reader>> {
        let mut heads = self.heads.lock().await;
        let candidate_index = heads
            .iter()
            .enumerate()
            .find(|(_, head)| head.offset == offset)
            .map(|(i, _)| i);

        let head = match candidate_index {
            Some(index) => {
                let head = heads
                    .remove(index)
                    .expect("internal logic error in heads pool manipulation");
                tracing::trace!("{:?}: borrowing", head);
                head
            }
            None => {
                drop(heads);
                let reader = self.source.get_reader_at(offset).await?;
                let head = Head { offset, reader };
                tracing::debug!("{:?}: new head", head);
                head
            }
        };

        Ok(head)
    }

    async fn return_head(&self, head: Head<Source::Reader>) {
        tracing::trace!("{:?}: returning", head);
        let mut heads = self.heads.lock().await;

        // returned heads are pushed to the back of the double-ended queue,
        // and expired heads are popped from the front, which effectively
        // functions as a cache with LRU (least-recently used) eviction policy
        heads.push_back(head);
        if heads.len() > self.max_heads {
            heads.pop_front();
        }
    }
}

#[async_trait(?Send)]
impl<Source> ReadAt for ReadAtWrapper<Source>
where
    Source: GetReaderAt,
{
    async fn read_at(&self, offset: u64, buf: &mut [u8]) -> io::Result<usize> {
        let mut head = self.borrow_head(offset).await?;
        // sic.: if this read fails, the head is considered unusable
        // and will not be returned. this can happen when dealing with
        // readers that are in fact network connections, and which can
        // expire, etc.
        let res = head.read(buf).await?;
        self.return_head(head).await;
        Ok(res)
    }

    fn len(&self) -> u64 {
        self.len
    }
}

#[pin_project]
struct Head<R>
where
    R: AsyncRead + Unpin,
{
    offset: u64,
    #[pin]
    reader: R,
}

impl<R> fmt::Debug for Head<R>
where
    R: AsyncRead + Unpin,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Head(offset = {})", self.offset)
    }
}

impl<R> AsyncRead for Head<R>
where
    R: AsyncRead + Unpin,
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        let head = self.project();

        let res = head.reader.poll_read(cx, buf);
        if let Poll::Ready(Ok(n)) = &res {
            *head.offset += *n as u64;
        }
        res
    }
}