yah01 commented on code in PR #2780: URL: https://github.com/apache/incubator-opendal/pull/2780#discussion_r1285193537
########## core/src/layers/blocking.rs: ########## @@ -0,0 +1,262 @@ +// 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::sync::Arc; + +use async_trait::async_trait; +use bytes; +use bytes::Bytes; +use tokio::runtime; + +use crate::raw::oio::ReadExt; +use crate::raw::*; +use crate::*; + +/// Add blocking API support for every operations. +/// +/// # Blocking API +/// +/// - This layer is auto-added to the operator if it's accessor doesn't support blocking APIs. +/// +/// Tracking issue: #2678 +#[derive(Debug, Clone)] +pub struct BlockingLayer { + runtime: Arc<runtime::Runtime>, +} + +impl Default for BlockingLayer { + fn default() -> Self { + let runtime = runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + Self { + runtime: Arc::new(runtime), + } + } +} + +impl<A: Accessor> Layer<A> for BlockingLayer { + type LayeredAccessor = BlockingAccessor<A>; + + fn layer(&self, inner: A) -> Self::LayeredAccessor { + BlockingAccessor { + inner: inner, + runtime: self.runtime.clone(), + } + } +} + +#[derive(Clone, Debug)] +pub struct BlockingAccessor<A: Accessor> { + inner: A, + + runtime: Arc<runtime::Runtime>, +} + +#[async_trait] +impl<A: Accessor> LayeredAccessor for BlockingAccessor<A> { + type Inner = A; + type Reader = A::Reader; + type BlockingReader = BlockingReader<A::Reader>; + type Writer = A::Writer; + type BlockingWriter = BlockingWriter<A::Writer>; + type Appender = A::Appender; + type Pager = A::Pager; + type BlockingPager = BlockingPager<A::Pager>; + + fn inner(&self) -> &Self::Inner { + &self.inner + } + + fn metadata(&self) -> AccessorInfo { + let mut info = self.inner.info(); + let capability = info.capability_mut(); + capability.blocking = true; + capability.read_can_next = true; Review Comment: > > Should the blocking capability be `false` even with the blocking layer? > > It's a bit tricky. We need a new issue to address this. But at least, we should not change `read_can_next`. What about `read_can_seek`? I found `BlockingReader` requires this to create -- 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