1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
// 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::sync::Arc;
use futures::Future;
use crate::raw::*;
use crate::*;
/// Layer is used to intercept the operations on the underlying storage.
///
/// Struct that implement this trait must accept input `Arc<dyn Accessor>` as inner,
/// and returns a new `Arc<dyn Accessor>` as output.
///
/// All functions in `Accessor` requires `&self`, so it's implementer's responsibility
/// to maintain the internal mutability. Please also keep in mind that `Accessor`
/// requires `Send` and `Sync`.
///
/// # Notes
///
/// ## Inner
///
/// It's required to implement `fn inner() -> Option<Arc<dyn Accessor>>` for layer's accessors.
///
/// By implement this method, all API calls will be forwarded to inner accessor instead.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
///
/// use opendal::raw::*;
/// use opendal::*;
///
/// /// Implement the real accessor logic here.
/// #[derive(Debug)]
/// struct TraceAccessor<A: Access> {
/// inner: A,
/// }
///
/// impl<A: Access> LayeredAccess for TraceAccessor<A> {
/// type Inner = A;
/// type Reader = A::Reader;
/// type BlockingReader = A::BlockingReader;
/// type Writer = A::Writer;
/// type BlockingWriter = A::BlockingWriter;
/// type Lister = A::Lister;
/// type BlockingLister = A::BlockingLister;
///
/// fn inner(&self) -> &Self::Inner {
/// &self.inner
/// }
///
/// async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
/// self.inner.read(path, args).await
/// }
///
/// fn blocking_read(
/// &self,
/// path: &str,
/// args: OpRead,
/// ) -> Result<(RpRead, Self::BlockingReader)> {
/// self.inner.blocking_read(path, args)
/// }
///
/// async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
/// self.inner.write(path, args).await
/// }
///
/// fn blocking_write(
/// &self,
/// path: &str,
/// args: OpWrite,
/// ) -> Result<(RpWrite, Self::BlockingWriter)> {
/// self.inner.blocking_write(path, args)
/// }
///
/// async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
/// self.inner.list(path, args).await
/// }
///
/// fn blocking_list(
/// &self,
/// path: &str,
/// args: OpList,
/// ) -> Result<(RpList, Self::BlockingLister)> {
/// self.inner.blocking_list(path, args)
/// }
/// }
///
/// /// The public struct that exposed to users.
/// ///
/// /// Will be used like `op.layer(TraceLayer)`
/// struct TraceLayer;
///
/// impl<A: Access> Layer<A> for TraceLayer {
/// type LayeredAccess = TraceAccessor<A>;
///
/// fn layer(&self, inner: A) -> Self::LayeredAccess {
/// TraceAccessor { inner }
/// }
/// }
/// ```
pub trait Layer<A: Access> {
/// The layered accessor that returned by this layer.
type LayeredAccess: Access;
/// Intercept the operations on the underlying storage.
fn layer(&self, inner: A) -> Self::LayeredAccess;
}
/// LayeredAccess is layered accessor that forward all not implemented
/// method to inner.
#[allow(missing_docs)]
pub trait LayeredAccess: Send + Sync + Debug + Unpin + 'static {
type Inner: Access;
type Reader: oio::Read;
type BlockingReader: oio::BlockingRead;
type Writer: oio::Write;
type BlockingWriter: oio::BlockingWrite;
type Lister: oio::List;
type BlockingLister: oio::BlockingList;
fn inner(&self) -> &Self::Inner;
fn metadata(&self) -> Arc<AccessorInfo> {
self.inner().info()
}
fn create_dir(
&self,
path: &str,
args: OpCreateDir,
) -> impl Future<Output = Result<RpCreateDir>> + MaybeSend {
self.inner().create_dir(path, args)
}
fn read(
&self,
path: &str,
args: OpRead,
) -> impl Future<Output = Result<(RpRead, Self::Reader)>> + MaybeSend;
fn write(
&self,
path: &str,
args: OpWrite,
) -> impl Future<Output = Result<(RpWrite, Self::Writer)>> + MaybeSend;
fn copy(
&self,
from: &str,
to: &str,
args: OpCopy,
) -> impl Future<Output = Result<RpCopy>> + MaybeSend {
self.inner().copy(from, to, args)
}
fn rename(
&self,
from: &str,
to: &str,
args: OpRename,
) -> impl Future<Output = Result<RpRename>> + MaybeSend {
self.inner().rename(from, to, args)
}
fn stat(&self, path: &str, args: OpStat) -> impl Future<Output = Result<RpStat>> + MaybeSend {
self.inner().stat(path, args)
}
fn delete(
&self,
path: &str,
args: OpDelete,
) -> impl Future<Output = Result<RpDelete>> + MaybeSend {
self.inner().delete(path, args)
}
fn list(
&self,
path: &str,
args: OpList,
) -> impl Future<Output = Result<(RpList, Self::Lister)>> + MaybeSend;
fn batch(&self, args: OpBatch) -> impl Future<Output = Result<RpBatch>> + MaybeSend {
self.inner().batch(args)
}
fn presign(
&self,
path: &str,
args: OpPresign,
) -> impl Future<Output = Result<RpPresign>> + MaybeSend {
self.inner().presign(path, args)
}
fn blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
self.inner().blocking_create_dir(path, args)
}
fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)>;
fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)>;
fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
self.inner().blocking_copy(from, to, args)
}
fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
self.inner().blocking_rename(from, to, args)
}
fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
self.inner().blocking_stat(path, args)
}
fn blocking_delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> {
self.inner().blocking_delete(path, args)
}
fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)>;
}
impl<L: LayeredAccess> Access for L {
type Reader = L::Reader;
type BlockingReader = L::BlockingReader;
type Writer = L::Writer;
type BlockingWriter = L::BlockingWriter;
type Lister = L::Lister;
type BlockingLister = L::BlockingLister;
fn info(&self) -> Arc<AccessorInfo> {
(self as &L).metadata()
}
async fn create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
(self as &L).create_dir(path, args).await
}
async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
(self as &L).read(path, args).await
}
async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
(self as &L).write(path, args).await
}
async fn copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
(self as &L).copy(from, to, args).await
}
async fn rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
(self as &L).rename(from, to, args).await
}
async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
(self as &L).stat(path, args).await
}
async fn delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> {
(self as &L).delete(path, args).await
}
async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
(self as &L).list(path, args).await
}
async fn batch(&self, args: OpBatch) -> Result<RpBatch> {
(self as &L).batch(args).await
}
async fn presign(&self, path: &str, args: OpPresign) -> Result<RpPresign> {
(self as &L).presign(path, args).await
}
fn blocking_create_dir(&self, path: &str, args: OpCreateDir) -> Result<RpCreateDir> {
(self as &L).blocking_create_dir(path, args)
}
fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
(self as &L).blocking_read(path, args)
}
fn blocking_write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::BlockingWriter)> {
(self as &L).blocking_write(path, args)
}
fn blocking_copy(&self, from: &str, to: &str, args: OpCopy) -> Result<RpCopy> {
(self as &L).blocking_copy(from, to, args)
}
fn blocking_rename(&self, from: &str, to: &str, args: OpRename) -> Result<RpRename> {
(self as &L).blocking_rename(from, to, args)
}
fn blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
(self as &L).blocking_stat(path, args)
}
fn blocking_delete(&self, path: &str, args: OpDelete) -> Result<RpDelete> {
(self as &L).blocking_delete(path, args)
}
fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingLister)> {
(self as &L).blocking_list(path, args)
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use futures::lock::Mutex;
use super::*;
use crate::services::Memory;
#[derive(Debug)]
struct Test<A: Access> {
#[allow(dead_code)]
inner: Option<A>,
deleted: Arc<Mutex<bool>>,
}
impl<A: Access> Layer<A> for &Test<A> {
type LayeredAccess = Test<A>;
fn layer(&self, inner: A) -> Self::LayeredAccess {
Test {
inner: Some(inner),
deleted: self.deleted.clone(),
}
}
}
impl<A: Access> Access for Test<A> {
type Reader = ();
type BlockingReader = ();
type Writer = ();
type BlockingWriter = ();
type Lister = ();
type BlockingLister = ();
fn info(&self) -> Arc<AccessorInfo> {
let mut am = AccessorInfo::default();
am.set_scheme(Scheme::Custom("test"));
am.into()
}
async fn delete(&self, _: &str, _: OpDelete) -> Result<RpDelete> {
let mut x = self.deleted.lock().await;
*x = true;
assert!(self.inner.is_some());
// We will not call anything here to test the layer.
Ok(RpDelete::default())
}
}
#[tokio::test]
async fn test_layer() {
let test = Test {
inner: None,
deleted: Arc::new(Mutex::new(false)),
};
let op = Operator::new(Memory::default())
.unwrap()
.layer(&test)
.finish();
op.delete("xxxxx").await.unwrap();
assert!(*test.deleted.clone().lock().await);
}
}