Xuanwo commented on code in PR #6283:
URL: https://github.com/apache/opendal/pull/6283#discussion_r2249757654


##########
integrations/object_store/src/service/reader.rs:
##########
@@ -0,0 +1,121 @@
+// 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 std::collections::VecDeque;
+use std::sync::Arc;
+
+use bytes::Bytes;
+use futures::stream::BoxStream;
+use futures::StreamExt;
+use object_store::GetRange;
+use object_store::ObjectStore;
+
+use opendal::raw::*;
+use opendal::*;
+use tokio::sync::Mutex;
+
+use super::error::parse_error;
+
+/// ObjectStore reader
+pub struct ObjectStoreReader {
+    bytes_stream: Mutex<BoxStream<'static, object_store::Result<Bytes>>>,
+    meta: object_store::ObjectMeta,
+    args: OpRead,
+}
+
+impl ObjectStoreReader {
+    pub(crate) async fn new(
+        store: Arc<dyn ObjectStore + 'static>,
+        path: &str,
+        args: OpRead,
+    ) -> Result<Self> {
+        let path = object_store::path::Path::from(path);
+        let opts = convert_to_get_options(&args)?;
+        let result = store.get_opts(&path, opts).await.map_err(parse_error)?;
+        let meta = result.meta.clone();
+        let bytes_stream = Mutex::new(result.into_stream());
+        Ok(Self {
+            bytes_stream,
+            meta,
+            args,
+        })
+    }
+
+    pub(crate) fn rp(&self) -> RpRead {
+        let mut rp = RpRead::new().with_size(Some(self.meta.size));
+        if !self.args.range().is_full() {
+            let range = self.args.range();
+            let size = match range.size() {
+                Some(size) => size,
+                None => self.meta.size,
+            };
+            rp = rp.with_range(Some(
+                BytesContentRange::default().with_range(range.offset(), 
range.offset() + size - 1),
+            ));
+        }
+        rp
+    }
+}
+
+impl oio::Read for ObjectStoreReader {

Review Comment:
   Hi, this API might be a bit confusing. It doesn't require us to collect the 
entire stream into a single `Buffer`.
   
   In fact, since it takes `&mut self`, you can generate or fetch content 
continuously and use `Buffer::new()` at the end.
   
   If we receive a byte stream from upstream, the simplest way is to return it 
directly:
   
   ```rust
   let bs = bytes_stream.try_next().await?;
   Ok(bs.map(Buffer::from).unwrap_or_default())
   ```



-- 
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: commits-unsubscr...@opendal.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to