opendal/services/hdfs/
reader.rs1use std::io::Read;
19
20use bytes::BytesMut;
21use futures::AsyncReadExt;
22use hdrs::AsyncFile;
23use hdrs::File;
24use tokio::io::ReadBuf;
25
26use crate::raw::*;
27use crate::*;
28
29pub struct HdfsReader<F> {
30 f: F,
31 read: usize,
32 size: usize,
33 buf_size: usize,
34 buf: BytesMut,
35}
36
37impl<F> HdfsReader<F> {
38 pub fn new(f: F, size: usize) -> Self {
39 Self {
40 f,
41 read: 0,
42 size,
43 buf_size: 2 * 1024 * 1024,
45 buf: BytesMut::new(),
46 }
47 }
48}
49
50impl oio::Read for HdfsReader<AsyncFile> {
51 async fn read(&mut self) -> Result<Buffer> {
52 if self.read >= self.size {
53 return Ok(Buffer::new());
54 }
55
56 let size = (self.size - self.read).min(self.buf_size);
57 self.buf.reserve(size);
58
59 let buf = &mut self.buf.spare_capacity_mut()[..size];
60 let mut read_buf: ReadBuf = ReadBuf::uninit(buf);
61
62 unsafe {
64 read_buf.assume_init(size);
65 }
66
67 let n = self
68 .f
69 .read(read_buf.initialize_unfilled())
70 .await
71 .map_err(new_std_io_error)?;
72 read_buf.advance(n);
73 self.read += n;
74
75 let filled = read_buf.filled().len();
77 unsafe { self.buf.set_len(filled) }
78
79 let frozen = self.buf.split().freeze();
80 Ok(Buffer::from(frozen))
81 }
82}
83
84impl oio::BlockingRead for HdfsReader<File> {
85 fn read(&mut self) -> Result<Buffer> {
86 if self.read >= self.size {
87 return Ok(Buffer::new());
88 }
89
90 let size = (self.size - self.read).min(self.buf_size);
91 self.buf.reserve(size);
92
93 let buf = &mut self.buf.spare_capacity_mut()[..size];
94 let mut read_buf: ReadBuf = ReadBuf::uninit(buf);
95
96 unsafe {
98 read_buf.assume_init(size);
99 }
100
101 let n = self
102 .f
103 .read(read_buf.initialize_unfilled())
104 .map_err(new_std_io_error)?;
105 read_buf.advance(n);
106 self.read += n;
107
108 let filled = read_buf.filled().len();
110 unsafe { self.buf.set_len(filled) }
111
112 let frozen = self.buf.split().freeze();
113 Ok(Buffer::from(frozen))
114 }
115}