common_base/
range_read.rs

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::future::Future;
use std::io;
use std::ops::Range;
use std::path::Path;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};

use async_trait::async_trait;
use bytes::{BufMut, Bytes};
use futures::AsyncRead;
use pin_project::pin_project;
use tokio::io::{AsyncReadExt as _, AsyncSeekExt as _};
use tokio::sync::Mutex;

/// `Metadata` contains the metadata of a source.
pub struct Metadata {
    /// The length of the source in bytes.
    pub content_length: u64,
}

/// `SizeAwareRangeReader` is a `RangeReader` that supports setting a file size hint.
pub trait SizeAwareRangeReader: RangeReader {
    /// Sets the file size hint for the reader.
    ///
    /// It's used to optimize the reading process by reducing the number of remote requests.
    fn with_file_size_hint(&mut self, file_size_hint: u64);
}

/// `RangeReader` reads a range of bytes from a source.
#[async_trait]
pub trait RangeReader: Sync + Send + Unpin {
    /// Returns the metadata of the source.
    async fn metadata(&self) -> io::Result<Metadata>;

    /// Reads the bytes in the given range.
    async fn read(&self, range: Range<u64>) -> io::Result<Bytes>;

    /// Reads the bytes in the given range into the buffer.
    ///
    /// Handles the buffer based on its capacity:
    /// - If the buffer is insufficient to hold the bytes, it will either:
    ///   - Allocate additional space (e.g., for `Vec<u8>`)
    ///   - Panic (e.g., for `&mut [u8]`)
    async fn read_into(&self, range: Range<u64>, buf: &mut (impl BufMut + Send)) -> io::Result<()> {
        let bytes = self.read(range).await?;
        buf.put_slice(&bytes);
        Ok(())
    }

    /// Reads the bytes in the given ranges.
    async fn read_vec(&self, ranges: &[Range<u64>]) -> io::Result<Vec<Bytes>> {
        let mut result = Vec::with_capacity(ranges.len());
        for range in ranges {
            result.push(self.read(range.clone()).await?);
        }
        Ok(result)
    }
}

#[async_trait]
impl<R: ?Sized + RangeReader> RangeReader for &R {
    async fn metadata(&self) -> io::Result<Metadata> {
        (*self).metadata().await
    }

    async fn read(&self, range: Range<u64>) -> io::Result<Bytes> {
        (*self).read(range).await
    }

    async fn read_into(&self, range: Range<u64>, buf: &mut (impl BufMut + Send)) -> io::Result<()> {
        (*self).read_into(range, buf).await
    }

    async fn read_vec(&self, ranges: &[Range<u64>]) -> io::Result<Vec<Bytes>> {
        (*self).read_vec(ranges).await
    }
}

/// `AsyncReadAdapter` adapts a `RangeReader` to an `AsyncRead`.
#[pin_project]
pub struct AsyncReadAdapter<R> {
    /// The inner `RangeReader`.
    /// Use `Mutex` to get rid of the borrow checker issue.
    inner: Arc<Mutex<R>>,

    /// The current position from the view of the reader.
    position: u64,

    /// The buffer for the read bytes.
    buffer: Vec<u8>,

    /// The length of the content.
    content_length: u64,

    /// The future for reading the next bytes.
    #[pin]
    read_fut: Option<Pin<Box<dyn Future<Output = io::Result<Bytes>> + Send>>>,
}

impl<R: RangeReader + 'static> AsyncReadAdapter<R> {
    pub async fn new(inner: R) -> io::Result<Self> {
        let inner = inner;
        let metadata = inner.metadata().await?;
        Ok(AsyncReadAdapter {
            inner: Arc::new(Mutex::new(inner)),
            position: 0,
            buffer: Vec::new(),
            content_length: metadata.content_length,
            read_fut: None,
        })
    }
}

/// The maximum size per read for the inner reader in `AsyncReadAdapter`.
const MAX_SIZE_PER_READ: usize = 8 * 1024 * 1024; // 8MB

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

        if *this.position >= *this.content_length {
            return Poll::Ready(Ok(0));
        }

        if !this.buffer.is_empty() {
            let to_read = this.buffer.len().min(buf.len());
            buf[..to_read].copy_from_slice(&this.buffer[..to_read]);
            this.buffer.drain(..to_read);
            *this.position += to_read as u64;
            return Poll::Ready(Ok(to_read));
        }

        if this.read_fut.is_none() {
            let size = (*this.content_length - *this.position).min(MAX_SIZE_PER_READ as u64);
            let range = *this.position..(*this.position + size);
            let inner = this.inner.clone();
            let fut = async move {
                let inner = inner.lock().await;
                inner.read(range).await
            };

            *this.read_fut = Some(Box::pin(fut));
        }

        match this
            .read_fut
            .as_mut()
            .as_pin_mut()
            .expect("checked above")
            .poll(cx)
        {
            Poll::Pending => Poll::Pending,
            Poll::Ready(Ok(bytes)) => {
                *this.read_fut = None;

                if !bytes.is_empty() {
                    this.buffer.extend_from_slice(&bytes);
                    self.poll_read(cx, buf)
                } else {
                    Poll::Ready(Ok(0))
                }
            }
            Poll::Ready(Err(e)) => {
                *this.read_fut = None;
                Poll::Ready(Err(e))
            }
        }
    }
}

#[async_trait]
impl RangeReader for Vec<u8> {
    async fn metadata(&self) -> io::Result<Metadata> {
        Ok(Metadata {
            content_length: self.len() as u64,
        })
    }

    async fn read(&self, range: Range<u64>) -> io::Result<Bytes> {
        let bytes = Bytes::copy_from_slice(&self[range.start as usize..range.end as usize]);
        Ok(bytes)
    }
}

// TODO(weny): considers replacing `tokio::fs::File` with opendal reader.
/// `FileReader` is a `RangeReader` for reading a file.
pub struct FileReader {
    content_length: u64,
    position: AtomicU64,
    file: Mutex<tokio::fs::File>,
}

impl FileReader {
    /// Creates a new `FileReader` for the file at the given path.
    pub async fn new(path: impl AsRef<Path>) -> io::Result<Self> {
        let file = tokio::fs::File::open(path).await?;
        let metadata = file.metadata().await?;
        Ok(FileReader {
            content_length: metadata.len(),
            position: AtomicU64::new(0),
            file: Mutex::new(file),
        })
    }
}

#[cfg(any(test, feature = "testing"))]
impl SizeAwareRangeReader for FileReader {
    fn with_file_size_hint(&mut self, _file_size_hint: u64) {
        // do nothing
    }
}

#[async_trait]
impl RangeReader for FileReader {
    async fn metadata(&self) -> io::Result<Metadata> {
        Ok(Metadata {
            content_length: self.content_length,
        })
    }

    async fn read(&self, mut range: Range<u64>) -> io::Result<Bytes> {
        let mut file = self.file.lock().await;

        if range.start != self.position.load(Ordering::Relaxed) {
            file.seek(io::SeekFrom::Start(range.start)).await?;
            self.position.store(range.start, Ordering::Relaxed);
        }

        range.end = range.end.min(self.content_length);
        if range.end <= self.position.load(Ordering::Relaxed) {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Start of range is out of bounds",
            ));
        }

        let mut buf = vec![0; (range.end - range.start) as usize];

        file.read_exact(&mut buf).await?;
        self.position.store(range.end, Ordering::Relaxed);

        Ok(Bytes::from(buf))
    }
}

#[cfg(test)]
mod tests {
    use common_test_util::temp_dir::create_named_temp_file;
    use futures::io::AsyncReadExt as _;

    use super::*;

    #[tokio::test]
    async fn test_async_read_adapter() {
        let data = b"hello world";
        let reader = Vec::from(data);
        let mut adapter = AsyncReadAdapter::new(reader).await.unwrap();

        let mut buf = Vec::new();
        adapter.read_to_end(&mut buf).await.unwrap();
        assert_eq!(buf, data);
    }

    #[tokio::test]
    async fn test_async_read_adapter_large() {
        let data = (0..20 * 1024 * 1024).map(|i| i as u8).collect::<Vec<u8>>();
        let mut adapter = AsyncReadAdapter::new(data.clone()).await.unwrap();

        let mut buf = Vec::new();
        adapter.read_to_end(&mut buf).await.unwrap();
        assert_eq!(buf, data);
    }

    #[tokio::test]
    async fn test_file_reader() {
        let file = create_named_temp_file();
        let path = file.path();
        let data = b"hello world";
        tokio::fs::write(path, data).await.unwrap();

        let reader = FileReader::new(path).await.unwrap();
        let metadata = reader.metadata().await.unwrap();
        assert_eq!(metadata.content_length, data.len() as u64);

        let bytes = reader.read(0..metadata.content_length).await.unwrap();
        assert_eq!(&*bytes, data);

        let bytes = reader.read(0..5).await.unwrap();
        assert_eq!(&*bytes, &data[..5]);
    }
}