opendal/services/hdfs_native/
reader.rs1use bytes::Bytes;
19use futures::StreamExt;
20use hdfs_native::file::FileReader;
21use hdfs_native::HdfsError;
22
23use crate::raw::*;
24use crate::services::hdfs_native::error::parse_hdfs_error;
25use crate::*;
26
27pub struct HdfsNativeReader {
28 read: usize,
29 size: usize,
30 stream: futures::stream::BoxStream<'static, Result<Bytes, HdfsError>>,
31}
32
33unsafe impl Sync for HdfsNativeReader {}
34
35impl HdfsNativeReader {
36 pub fn new(f: FileReader, offset: usize, size: usize) -> Self {
37 let size = size.min(f.file_length() - offset);
38 HdfsNativeReader {
39 read: 0,
40 size,
41 stream: Box::pin(f.read_range_stream(offset, size)),
42 }
43 }
44}
45
46impl oio::Read for HdfsNativeReader {
47 async fn read(&mut self) -> Result<Buffer> {
48 if self.read >= self.size {
49 return Ok(Buffer::new());
50 }
51
52 if let Some(bytes) = self.stream.as_mut().next().await {
53 let bytes = bytes.map_err(parse_hdfs_error)?;
54 let buf = Buffer::from(bytes);
55 self.read += buf.len();
56
57 Ok(buf)
58 } else {
59 Ok(Buffer::new())
60 }
61 }
62}