Skip to content

Commit

Permalink
refactor: change the signature of Access::info and AccessDyn::info
Browse files Browse the repository at this point in the history
Signed-off-by: Lzzzt <[email protected]>
  • Loading branch information
Lzzzzzt committed Jul 12, 2024
1 parent 2b70cd3 commit e45455f
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 41 deletions.
8 changes: 5 additions & 3 deletions core/src/layers/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;

use tokio::runtime::Handle;

use crate::raw::*;
Expand Down Expand Up @@ -178,10 +180,10 @@ impl<A: Access> LayeredAccess for BlockingAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
let mut meta = self.inner.info();
fn metadata(&self) -> Arc<AccessorInfo> {
let mut meta = (*self.inner.info()).clone();
meta.full_capability_mut().blocking = true;
meta
meta.into()
}

async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
Expand Down
13 changes: 7 additions & 6 deletions core/src/layers/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<A: Access> Layer<A> for CompleteLayer {

/// Provide complete wrapper for backend.
pub struct CompleteAccessor<A: Access> {
meta: AccessorInfo,
meta: Arc<AccessorInfo>,
inner: Arc<A>,
}

Expand Down Expand Up @@ -380,13 +380,14 @@ impl<A: Access> LayeredAccess for CompleteAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
let mut meta = self.meta.clone();
// Todo: May move the logic to the implement of Layer::layer of CompleteAccessor<A>
fn metadata(&self) -> Arc<AccessorInfo> {
let mut meta = (*self.meta).clone();
let cap = meta.full_capability_mut();
if cap.list && cap.write_can_empty {
cap.create_dir = true;
}
meta
meta.into()
}

async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
Expand Down Expand Up @@ -731,11 +732,11 @@ mod tests {
type BlockingWriter = oio::BlockingWriter;
type BlockingLister = oio::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut info = AccessorInfo::default();
info.set_native_capability(self.capability);

info
info.into()
}

async fn create_dir(&self, _: &str, _: OpCreateDir) -> Result<RpCreateDir> {
Expand Down
5 changes: 3 additions & 2 deletions core/src/layers/error_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;

use futures::TryFutureExt;

Expand Down Expand Up @@ -56,7 +57,7 @@ impl<A: Access> Layer<A> for ErrorContextLayer {

/// Provide error context wrapper for backend.
pub struct ErrorContextAccessor<A: Access> {
meta: AccessorInfo,
meta: Arc<AccessorInfo>,
inner: A,
}

Expand All @@ -79,7 +80,7 @@ impl<A: Access> LayeredAccess for ErrorContextAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.meta.clone()
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/layers/immutable_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::collections::HashSet;
use std::fmt::Debug;
use std::sync::Arc;
use std::vec::IntoIter;

use crate::raw::*;
Expand Down Expand Up @@ -145,14 +146,14 @@ impl<A: Access> LayeredAccess for ImmutableIndexAccessor<A> {
}

/// Add list capabilities for underlying storage services.
fn metadata(&self) -> AccessorInfo {
let mut meta = self.inner.info();
fn metadata(&self) -> Arc<AccessorInfo> {
let mut meta = (*self.inner.info()).clone();

let cap = meta.full_capability_mut();
cap.list = true;
cap.list_with_recursive = true;

meta
meta.into()
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
3 changes: 2 additions & 1 deletion core/src/layers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::fmt::Debug;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use bytes::Buf;
use futures::FutureExt;
Expand Down Expand Up @@ -221,7 +222,7 @@ impl<A: Access> LayeredAccess for LoggingAccessor<A> {
&self.inner
}

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
debug!(
target: LOGGING_TARGET,
"service={} operation={} -> started",
Expand Down
4 changes: 2 additions & 2 deletions core/src/layers/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ mod tests {
type BlockingWriter = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_native_capability(Capability {
read: true,
Expand All @@ -813,7 +813,7 @@ mod tests {
..Default::default()
});

am
am.into()
}

async fn stat(&self, _: &str, _: OpStat) -> Result<RpStat> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/layers/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,15 @@ mod tests {
type BlockingWriter = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_native_capability(Capability {
read: true,
delete: true,
..Default::default()
});

am
am.into()
}

/// This function will build a reader that always return pending.
Expand Down
17 changes: 9 additions & 8 deletions core/src/raw/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait Access: Send + Sync + Debug + Unpin + 'static {
///
/// - scheme: declare the scheme of backend.
/// - capabilities: declare the capabilities of current backend.
fn info(&self) -> AccessorInfo;
fn info(&self) -> Arc<AccessorInfo>;

/// Invoke the `create` operation on the specified path
///
Expand Down Expand Up @@ -399,7 +399,7 @@ pub trait Access: Send + Sync + Debug + Unpin + 'static {
/// `Box<dyn AccessDyn>`.
pub trait AccessDyn: Send + Sync + Debug + Unpin {
/// Dyn version of [`Accessor::info`]
fn info_dyn(&self) -> AccessorInfo;
fn info_dyn(&self) -> Arc<AccessorInfo>;
/// Dyn version of [`Accessor::create_dir`]
fn create_dir_dyn<'a>(
&'a self,
Expand Down Expand Up @@ -484,8 +484,8 @@ where
BlockingLister = oio::BlockingLister,
>,
{
fn info_dyn(&self) -> AccessorInfo {
self.info()
fn info_dyn(&self) -> Arc<AccessorInfo> {
self.info().into()
}

fn create_dir_dyn<'a>(
Expand Down Expand Up @@ -607,8 +607,8 @@ impl Access for dyn AccessDyn {
type Lister = oio::Lister;
type BlockingLister = oio::BlockingLister;

fn info(&self) -> AccessorInfo {
self.info_dyn()
fn info(&self) -> Arc<AccessorInfo> {
self.info_dyn().into()
}

async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
Expand Down Expand Up @@ -693,14 +693,15 @@ impl Access for () {
type BlockingWriter = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
AccessorInfo {
scheme: Scheme::Custom("dummy"),
root: "".to_string(),
name: "dummy".to_string(),
native_capability: Capability::default(),
full_capability: Capability::default(),
}
.into()
}
}

Expand All @@ -717,7 +718,7 @@ impl<T: Access + ?Sized> Access for Arc<T> {
type BlockingWriter = T::BlockingWriter;
type BlockingLister = T::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
self.as_ref().info()
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/raw/adapters/kv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<S: Adapter> Access for Backend<S> {
type Lister = HierarchyLister<KvLister>;
type BlockingLister = HierarchyLister<KvLister>;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am: AccessorInfo = self.kv.metadata().into();
am.set_root(&self.root);

Expand All @@ -84,7 +84,7 @@ impl<S: Adapter> Access for Backend<S> {

am.set_native_capability(cap);

am
am.into()
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/raw/adapters/typed_kv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<S: Adapter> Access for Backend<S> {
type Lister = HierarchyLister<KvLister>;
type BlockingLister = HierarchyLister<KvLister>;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let kv_info = self.kv.info();
let mut am: AccessorInfo = AccessorInfo::default();
am.set_root(&self.root);
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<S: Adapter> Access for Backend<S> {

am.set_native_capability(cap);

am
am.into()
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
Expand Down
9 changes: 5 additions & 4 deletions core/src/raw/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use std::fmt::Debug;
use std::sync::Arc;

use futures::Future;

Expand Down Expand Up @@ -139,7 +140,7 @@ pub trait LayeredAccess: Send + Sync + Debug + Unpin + 'static {

fn inner(&self) -> &Self::Inner;

fn metadata(&self) -> AccessorInfo {
fn metadata(&self) -> Arc<AccessorInfo> {
self.inner().info()
}

Expand Down Expand Up @@ -246,7 +247,7 @@ impl<L: LayeredAccess> Access for L {
type Lister = L::Lister;
type BlockingLister = L::BlockingLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
(self as &L).metadata()
}

Expand Down Expand Up @@ -358,10 +359,10 @@ mod tests {
type Lister = ();
type BlockingLister = ();

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_scheme(Scheme::Custom("test"));
am
am.into()
}

async fn delete(&self, _: &str, _: OpDelete) -> Result<RpDelete> {
Expand Down
5 changes: 3 additions & 2 deletions core/src/raw/oio/list/flat_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ where
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::sync::Arc;
use std::vec;
use std::vec::IntoIter;

Expand Down Expand Up @@ -216,11 +217,11 @@ mod tests {
type Lister = ();
type BlockingLister = MockLister;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.full_capability_mut().list = true;

am
am.into()
}

fn blocking_list(&self, path: &str, _: OpList) -> Result<(RpList, Self::BlockingLister)> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/services/fs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Access for FsBackend {
type BlockingWriter = FsWriter<std::fs::File>;
type BlockingLister = Option<FsLister<std::fs::ReadDir>>;

fn info(&self) -> AccessorInfo {
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_scheme(Scheme::Fs)
.set_root(&self.core.root.to_string_lossy())
Expand All @@ -206,7 +206,7 @@ impl Access for FsBackend {
..Default::default()
});

am
am.into()
}

async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
Expand Down
6 changes: 4 additions & 2 deletions core/src/types/operator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;

use crate::raw::*;
use crate::*;

/// Metadata for operator, users can use this metadata to get information of operator.
#[derive(Clone, Debug, Default)]
pub struct OperatorInfo(AccessorInfo);
pub struct OperatorInfo(Arc<AccessorInfo>);

impl OperatorInfo {
pub(super) fn new(acc: AccessorInfo) -> Self {
pub(super) fn new(acc: Arc<AccessorInfo>) -> Self {
OperatorInfo(acc)
}

Expand Down

0 comments on commit e45455f

Please sign in to comment.