Xuanwo commented on code in PR #3811: URL: https://github.com/apache/incubator-opendal/pull/3811#discussion_r1435945748
########## core/src/raw/oio/read/buffer_reader.rs: ########## @@ -0,0 +1,457 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 bytes::BufMut; +use bytes::{Bytes, BytesMut}; + +use tokio::io::ReadBuf; + +use std::cmp::min; +use std::io::SeekFrom; + +use std::task::ready; +use std::task::Context; +use std::task::Poll; + +use crate::raw::*; +use crate::*; + +/// [BufferReader] allows the underlying reader to fetch data at the buffer's size +/// and is used to amortize the IO's overhead. +pub struct BufferReader<R> { + r: R, + cur: u64, + + buf: BytesMut, + cap: usize, + /// Indicate buffered data range start. + /// `None` stand for no buffered data. + buf_start: Option<usize>, +} + +impl<R> BufferReader<R> { + /// Create a new [`oio::Reader`] with a buffer. + /// + /// # Input + /// + /// The input is an Accessor will may return a seekable reader. + pub fn new(r: R, cap: usize) -> BufferReader<R> { + BufferReader { + r, + cur: 0, + + // Lazy allocation. + buf: BytesMut::new(), + cap, + buf_start: None, + } + } + + /// Returns the capability of buffer. + pub fn cap(&self) -> usize { + self.cap + } +} + +impl<R> BufferReader<R> +where + R: oio::Read, +{ + /// Fill the internal buffer. + fn poll_fill_buf(&mut self, cx: &mut Context<'_>) -> Poll<Result<usize>> { + self.clear_buffer(); + self.buf.reserve(self.cap); + + if let Err(err) = ready!(self.r.poll_seek(cx, SeekFrom::Start(self.cur))) { + return Poll::Ready(Err(err)); + } + + let mut nread = 0; + + while self.buf.len() < self.buf.capacity() { + let dst = self.buf.spare_capacity_mut(); + let length = dst.len(); + let mut buf = ReadBuf::uninit(dst); + + // Safety: must initialized. + unsafe { + buf.assume_init(length); + } + + match ready!(self.r.poll_read(cx, buf.initialized_mut())) { Review Comment: tokio buf reader implement in this way: https://docs.rs/tokio/latest/src/tokio/io/util/buf_reader.rs.html#122-139 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
