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


##########
integrations/object_store/src/service/deleter.rs:
##########
@@ -0,0 +1,71 @@
+// 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::future::Future;
+use std::sync::Arc;
+
+use futures::FutureExt;
+use object_store::ObjectStore;
+use opendal::raw::*;
+use opendal::*;
+
+use super::error::parse_error;
+
+pub struct ObjectStoreDeleter {
+    store: Arc<dyn ObjectStore + 'static>,
+    paths: VecDeque<object_store::path::Path>,
+}
+
+impl ObjectStoreDeleter {
+    pub(crate) fn new(store: Arc<dyn ObjectStore + 'static>) -> Self {
+        Self {
+            store,
+            paths: VecDeque::new(),
+        }
+    }
+}
+
+impl oio::Delete for ObjectStoreDeleter {

Review Comment:
   Please implement `oio::BatchDelete` instead.



##########
integrations/object_store/src/service/writer.rs:
##########
@@ -0,0 +1,283 @@
+// 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::borrow::Cow;
+use std::collections::hash_map::DefaultHasher;
+use std::collections::HashMap;
+use std::hash::{Hash, Hasher};
+use std::sync::Arc;
+
+use object_store::Attribute;
+use object_store::AttributeValue;
+use object_store::MultipartUpload;
+use object_store::ObjectStore;
+use object_store::PutOptions;
+use object_store::PutPayload;
+
+use opendal::raw::oio::MultipartPart;
+use opendal::raw::*;
+use opendal::*;
+
+use super::error::parse_error;
+
+pub struct ObjectStoreWriter {
+    store: Arc<dyn ObjectStore + 'static>,
+    path: object_store::path::Path,
+    args: OpWrite,
+    multipart_uploads: Arc<tokio::sync::Mutex<HashMap<String, Box<dyn 
MultipartUpload>>>>,
+}
+
+impl ObjectStoreWriter {
+    pub fn new(store: Arc<dyn ObjectStore + 'static>, path: &str, args: 
OpWrite) -> Self {
+        Self {
+            store,
+            path: object_store::path::Path::from(path),
+            args,
+            multipart_uploads: 
Arc::new(tokio::sync::Mutex::new(HashMap::new())),
+        }
+    }
+}
+
+impl oio::MultipartWrite for ObjectStoreWriter {
+    /// Write the entire object in one go.
+    /// Used when the object is small enough to bypass multipart upload.
+    async fn write_once(&self, size: u64, body: Buffer) -> Result<Metadata> {
+        // Validate that actual body size matches expected size
+        let actual_size = body.len() as u64;
+        if actual_size != size {
+            return Err(Error::new(
+                ErrorKind::Unexpected,
+                format!("Expected size {size} but got {actual_size}"),
+            ));
+        }
+
+        let bytes = body.to_bytes();
+        let payload = PutPayload::from(bytes);
+        let mut opts = convert_to_put_opts(&self.args)?;
+
+        // Add size metadata for tracking
+        opts.attributes.insert(
+            Attribute::Metadata("content-size".into()),
+            AttributeValue::from(size.to_string()),
+        );
+
+        let result = self
+            .store
+            .put_opts(&self.path, payload, opts)
+            .await
+            .map_err(parse_error)?;
+
+        // Build metadata from put result
+        let mut metadata = Metadata::new(EntryMode::FILE);
+        if let Some(etag) = &result.e_tag {
+            metadata.set_etag(etag);
+        }
+        if let Some(version) = &result.version {
+            metadata.set_version(version);
+        }
+
+        Ok(metadata)
+    }
+
+    // Generate a unique upload ID that we'll use to track this session
+    async fn initiate_part(&self) -> Result<String> {
+        // Create a unique upload ID using ULID
+        let upload_id = ulid::Ulid::new().to_string();

Review Comment:
   The `ObjectWriter` is created per-write, why we need a unique upload ID?



##########
integrations/object_store/src/service/lister.rs:
##########
@@ -0,0 +1,91 @@
+// 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::future::Future;
+use std::sync::Arc;
+use tokio::sync::Mutex;
+
+use futures::{stream::BoxStream, StreamExt};
+use object_store::{ObjectMeta, ObjectStore};
+use opendal::raw::*;
+use opendal::*;
+
+use super::error::parse_error;
+
+pub struct ObjectStoreLister {
+    stream: Arc<Mutex<BoxStream<'static, object_store::Result<ObjectMeta>>>>,
+}
+
+impl ObjectStoreLister {
+    pub(crate) async fn new(
+        store: Arc<dyn ObjectStore + 'static>,
+        path: &str,
+        args: OpList,
+    ) -> Result<Self> {
+        // If start_after is specified, use list_with_offset
+        let mut stream = if let Some(start_after) = args.start_after() {
+            store
+                .list_with_offset(
+                    Some(&object_store::path::Path::from(path)),
+                    &object_store::path::Path::from(start_after),
+                )
+                .boxed()
+        } else {
+            store
+                .list(Some(&object_store::path::Path::from(path)))
+                .boxed()
+        };
+
+        // Process listing arguments
+        if let Some(limit) = args.limit() {
+            stream = stream.take(limit).boxed();
+        }
+
+        Ok(Self {
+            stream: Arc::new(Mutex::new(stream)),
+        })
+    }
+}
+
+impl oio::List for ObjectStoreLister {
+    fn next(&mut self) -> impl Future<Output = Result<Option<oio::Entry>>> + 
MaybeSend {

Review Comment:
   We can implement `async fn next(&mut self)` here so we don't need a lock for 
it.



##########
core/Cargo.toml:
##########
@@ -329,6 +329,8 @@ redis = { version = "0.32", features = [
   "tokio-comp",
   "connection-manager",
 ], optional = true }
+# for service-object-store
+object_store = { version = "0.12.1", optional = true }

Review Comment:
   We don't need this.



##########
integrations/object_store/src/service/error.rs:
##########
@@ -0,0 +1,53 @@
+// 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 opendal::Error;
+use opendal::ErrorKind;
+use opendal::Scheme;
+
+pub(crate) fn parse_error(err: object_store::Error) -> Error {
+    let err = match err {
+        object_store::Error::NotFound { .. } => 
Error::new(ErrorKind::NotFound, "path not found"),
+
+        object_store::Error::AlreadyExists { .. } => {
+            Error::new(ErrorKind::AlreadyExists, "path already exists")

Review Comment:
   Please always include source errors by using `source(err)`.



##########
integrations/object_store/src/service/mod.rs:
##########
@@ -0,0 +1,385 @@
+// 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::fmt::Debug;
+use std::fmt::Formatter;
+use std::sync::Arc;
+
+use object_store::ObjectStore;
+use opendal::raw::oio::MultipartWriter;
+use opendal::raw::*;
+use opendal::Error;
+use opendal::ErrorKind;
+use opendal::*;
+
+mod deleter;
+mod error;
+mod lister;
+mod reader;
+mod writer;
+
+use deleter::ObjectStoreDeleter;
+use error::parse_error;
+use lister::ObjectStoreLister;
+use reader::ObjectStoreReader;
+use writer::ObjectStoreWriter;
+
+/// ObjectStore backend builder
+#[derive(Default)]
+pub struct ObjectStoreBuilder {
+    store: Option<Arc<dyn ObjectStore + 'static>>,
+}
+
+impl Debug for ObjectStoreBuilder {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        let mut d = f.debug_struct("ObjectStoreBuilder");
+        d.finish_non_exhaustive()
+    }
+}
+
+impl ObjectStoreBuilder {
+    /// Set the object store instance
+    pub fn new(store: Arc<dyn ObjectStore + 'static>) -> Self {
+        Self { store: Some(store) }
+    }
+}
+
+impl Builder for ObjectStoreBuilder {
+    type Config = ();
+
+    fn build(self) -> Result<impl Access> {
+        let store = self.store.ok_or_else(|| {
+            Error::new(ErrorKind::ConfigInvalid, "object store is required")
+                .with_context("service", Scheme::Custom("object_store"))
+        })?;
+
+        Ok(ObjectStoreService { store })
+    }
+}
+
+/// ObjectStore backend
+pub struct ObjectStoreService {
+    store: Arc<dyn ObjectStore + 'static>,
+}
+
+impl Debug for ObjectStoreService {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        let mut d = f.debug_struct("ObjectStoreBackend");
+        d.finish_non_exhaustive()
+    }
+}
+
+impl Access for ObjectStoreService {
+    type Reader = ObjectStoreReader;
+    type Writer = MultipartWriter<ObjectStoreWriter>;
+    type Lister = ObjectStoreLister;
+    type Deleter = ObjectStoreDeleter;
+
+    fn info(&self) -> Arc<AccessorInfo> {
+        let info = AccessorInfo::default();
+        info.set_scheme("object_store")
+            .set_root("/")
+            .set_name("object_store")
+            .set_native_capability(Capability {
+                stat: true,
+                stat_with_if_match: true,
+                stat_with_if_unmodified_since: true,
+                read: true,
+                write: true,
+                delete: true,
+                list: true,
+                list_with_limit: true,
+                list_with_start_after: true,
+                ..Default::default()
+            });
+        Arc::new(info)
+    }
+
+    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
+        let path = object_store::path::Path::from(path);
+        let meta = self.store.head(&path).await.map_err(parse_error)?;
+
+        let mut metadata = Metadata::new(EntryMode::FILE);

Review Comment:
   We have seem many silimar patterns, let's extract a util function for the 
metadata convert.



##########
integrations/object_store/src/service/lister.rs:
##########
@@ -0,0 +1,91 @@
+// 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::future::Future;
+use std::sync::Arc;
+use tokio::sync::Mutex;
+
+use futures::{stream::BoxStream, StreamExt};
+use object_store::{ObjectMeta, ObjectStore};
+use opendal::raw::*;
+use opendal::*;
+
+use super::error::parse_error;
+
+pub struct ObjectStoreLister {
+    stream: Arc<Mutex<BoxStream<'static, object_store::Result<ObjectMeta>>>>,
+}
+
+impl ObjectStoreLister {
+    pub(crate) async fn new(
+        store: Arc<dyn ObjectStore + 'static>,
+        path: &str,
+        args: OpList,
+    ) -> Result<Self> {
+        // If start_after is specified, use list_with_offset
+        let mut stream = if let Some(start_after) = args.start_after() {

Review Comment:
   We can defer this to users call `next`



##########
integrations/object_store/src/service/error.rs:
##########
@@ -0,0 +1,53 @@
+// 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 opendal::Error;
+use opendal::ErrorKind;
+use opendal::Scheme;
+
+pub(crate) fn parse_error(err: object_store::Error) -> Error {
+    let err = match err {
+        object_store::Error::NotFound { .. } => 
Error::new(ErrorKind::NotFound, "path not found"),
+
+        object_store::Error::AlreadyExists { .. } => {
+            Error::new(ErrorKind::AlreadyExists, "path already exists")
+        }
+
+        object_store::Error::PermissionDenied { .. }
+        | object_store::Error::Unauthenticated { .. } => {
+            Error::new(ErrorKind::PermissionDenied, "permission denied")
+        }
+
+        object_store::Error::InvalidPath { .. } => 
Error::new(ErrorKind::NotFound, "invalid path"),
+
+        object_store::Error::NotSupported { .. } => {
+            Error::new(ErrorKind::Unsupported, "operation not supported")
+        }
+
+        object_store::Error::Precondition { .. } => {
+            Error::new(ErrorKind::ConditionNotMatch, "precondition not met")
+        }
+
+        object_store::Error::Generic { store, .. } => {
+            Error::new(ErrorKind::Unexpected, format!("{store} operation 
failed")).set_source(err)
+        }
+
+        _ => Error::new(ErrorKind::Unexpected, "unknown 
error").set_source(err),
+    };
+
+    err.with_context("service", Scheme::Custom("object_store"))

Review Comment:
   We don't need to do this, opendal's error context layer will add them.



-- 
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