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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
// Copyright 2022 Datafuse Labs.
//
// Licensed 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::io::Result;
use std::sync::Arc;
use async_trait::async_trait;
use flagset::flags;
use flagset::FlagSet;
use crate::error::new_unsupported_object_error;
use crate::multipart::ObjectPart;
use crate::ops::OpAbortMultipart;
use crate::ops::OpCompleteMultipart;
use crate::ops::OpCreate;
use crate::ops::OpCreateMultipart;
use crate::ops::OpDelete;
use crate::ops::OpList;
use crate::ops::OpPresign;
use crate::ops::OpRead;
use crate::ops::OpStat;
use crate::ops::OpWrite;
use crate::ops::OpWriteMultipart;
use crate::ops::Operation;
use crate::ops::PresignedRequest;
use crate::BlockingBytesReader;
use crate::BytesReader;
use crate::ObjectIterator;
use crate::ObjectMetadata;
use crate::ObjectStreamer;
use crate::Scheme;
/// Underlying trait of all backends for implementors.
///
/// # Note
///
/// Only service implementor should care about this trait, users need to
/// use [`Operator`][crate::Operator] instead.
///
/// # Operations
///
/// | Name | Capability |
/// | ---- | ---------- |
/// | [`metadata`][crate::Accessor::metadata] | - |
/// | [`create`][crate::Accessor::create] | - |
/// | [`read`][crate::Accessor::read] | - |
/// | [`write`][crate::Accessor::write] | - |
/// | [`delete`][crate::Accessor::delete] | - |
/// | [`list`][crate::Accessor::list] | - |
/// | [`presign`][crate::Accessor::presign] | `Presign` |
/// | [`create_multipart`][crate::Accessor::create_multipart] | `Multipart` |
/// | [`write_multipart`][crate::Accessor::write_multipart] | `Multipart` |
/// | [`complete_multipart`][crate::Accessor::complete_multipart] | `Multipart` |
/// | [`abort_multipart`][crate::Accessor::abort_multipart] | `Multipart` |
/// | [`blocking_create`][crate::Accessor::blocking_create] | `Blocking` |
/// | [`blocking_read`][crate::Accessor::blocking_read] | `Blocking` |
/// | [`blocking_write`][crate::Accessor::blocking_write] | `Blocking` |
/// | [`blocking_delete`][crate::Accessor::blocking_delete] | `Blocking` |
/// | [`blocking_list`][crate::Accessor::blocking_list] | `Blocking` |
///
/// - Path in args will all be normalized into the same style, services
/// should handle them based on services' requirement.
/// - Path that ends with `/` means it's Dir, otherwise, it's File.
/// - Root dir is `/`
/// - Path will never be empty.
/// - Operations without capability requirement like `metadata`, `create` are
/// basic operations.
/// - All services must implement them.
/// - Use `unimplemented!()` if not implemented or can't implement.
/// - Operations with capability requirement like `presign` are optional operations.
/// - Services can implement them based on services capabilities.
/// - The default implementation should return [`std::io::ErrorKind::Unsupported`].
#[async_trait]
pub trait Accessor: Send + Sync + Debug {
/// Invoke the `metadata` operation to get metadata of accessor.
fn metadata(&self) -> AccessorMetadata {
unimplemented!()
}
/// Invoke the `create` operation on the specified path
///
/// # Behavior
///
/// - Input path MUST match with ObjectMode, DON'T NEED to check object mode.
/// - Create on existing dir SHOULD succeed.
/// - Create on existing file SHOULD overwrite and truncate.
async fn create(&self, path: &str, args: OpCreate) -> Result<()> {
let (_, _) = (path, args);
unimplemented!()
}
/// Invoke the `read` operation on the specified path, returns a
/// [`BytesReader`][crate::BytesReader] if operate successful.
///
/// # Behavior
///
/// - Input path MUST be file path, DON'T NEED to check object mode.
async fn read(&self, path: &str, args: OpRead) -> Result<BytesReader> {
let (_, _) = (path, args);
unimplemented!()
}
/// Invoke the `write` operation on the specified path, returns a
/// written size if operate successful.
///
/// # Behavior
///
/// - Input path MUST be file path, DON'T NEED to check object mode.
async fn write(&self, path: &str, args: OpWrite, r: BytesReader) -> Result<u64> {
let (_, _, _) = (path, args, r);
unimplemented!()
}
/// Invoke the `stat` operation on the specified path.
///
/// # Behavior
///
/// - `stat` empty path means stat backend's root path.
/// - `stat` a path endswith "/" means stating a dir.
/// - `mode` and `content_length` must be set.
async fn stat(&self, path: &str, args: OpStat) -> Result<ObjectMetadata> {
let (_, _) = (path, args);
unimplemented!()
}
/// Invoke the `delete` operation on the specified path.
///
/// # Behavior
///
/// - `delete` is an idempotent operation, it's safe to call `Delete` on the same path multiple times.
/// - `delete` SHOULD return `Ok(())` if the path is deleted successfully or not exist.
async fn delete(&self, path: &str, args: OpDelete) -> Result<()> {
let (_, _) = (path, args);
unimplemented!()
}
/// Invoke the `list` operation on the specified path.
///
/// # Behavior
///
/// - Input path MUST be dir path, DON'T NEED to check object mode.
/// - List non-exist dir should return Empty.
async fn list(&self, path: &str, args: OpList) -> Result<ObjectStreamer> {
let (_, _) = (path, args);
unimplemented!()
}
/// Invoke the `presign` operation on the specified path.
///
/// # Behavior
///
/// - Require capability: `Presign`
/// - This API is optional, return [`std::io::ErrorKind::Unsupported`] if not supported.
fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
let _ = args;
Err(new_unsupported_object_error(Operation::Presign, path))
}
/// Invoke the `create_multipart` operation on the specified path.
///
/// # Behavior
///
/// - Require capability: `Multipart`
/// - This op returns a `upload_id` which is required to for following APIs.
async fn create_multipart(&self, path: &str, args: OpCreateMultipart) -> Result<String> {
let _ = args;
Err(new_unsupported_object_error(
Operation::CreateMultipart,
path,
))
}
/// Invoke the `write_multipart` operation on the specified path.
///
/// # Behavior
///
/// - Require capability: `Multipart`
async fn write_multipart(
&self,
path: &str,
args: OpWriteMultipart,
r: BytesReader,
) -> Result<ObjectPart> {
let (_, _) = (args, r);
Err(new_unsupported_object_error(
Operation::WriteMultipart,
path,
))
}
/// Invoke the `complete_multipart` operation on the specified path.
///
/// # Behavior
///
/// - Require capability: `Multipart`
async fn complete_multipart(&self, path: &str, args: OpCompleteMultipart) -> Result<()> {
let _ = args;
Err(new_unsupported_object_error(
Operation::CompleteMultipart,
path,
))
}
/// Invoke the `abort_multipart` operation on the specified path.
///
/// # Behavior
///
/// - Require capability: `Multipart`
async fn abort_multipart(&self, path: &str, args: OpAbortMultipart) -> Result<()> {
let _ = args;
Err(new_unsupported_object_error(
Operation::AbortMultipart,
path,
))
}
/// Invoke the `blocking_create` operation on the specified path.
///
/// This operation is the blocking version of [`Accessor::create`]
///
/// # Behavior
///
/// - Require capability: `Blocking`
fn blocking_create(&self, path: &str, args: OpCreate) -> Result<()> {
let _ = args;
Err(new_unsupported_object_error(
Operation::BlockingCreate,
path,
))
}
/// Invoke the `blocking_read` operation on the specified path.
///
/// This operation is the blocking version of [`Accessor::read`]
///
/// # Behavior
///
/// - Require capability: `Blocking`
fn blocking_read(&self, path: &str, args: OpRead) -> Result<BlockingBytesReader> {
let _ = args;
Err(new_unsupported_object_error(Operation::BlockingRead, path))
}
/// Invoke the `blocking_write` operation on the specified path.
///
/// This operation is the blocking version of [`Accessor::write`]
///
/// # Behavior
///
/// - Require capability: `Blocking`
fn blocking_write(&self, path: &str, args: OpWrite, r: BlockingBytesReader) -> Result<u64> {
let (_, _) = (args, r);
Err(new_unsupported_object_error(Operation::BlockingWrite, path))
}
/// Invoke the `blocking_stat` operation on the specified path.
///
/// This operation is the blocking version of [`Accessor::stat`]
///
/// # Behavior
///
/// - Require capability: `Blocking`
fn blocking_stat(&self, path: &str, args: OpStat) -> Result<ObjectMetadata> {
let _ = args;
Err(new_unsupported_object_error(Operation::BlockingStat, path))
}
/// Invoke the `blocking_delete` operation on the specified path.
///
/// This operation is the blocking version of [`Accessor::delete`]
///
/// # Behavior
///
/// - Require capability: `Blocking`
fn blocking_delete(&self, path: &str, args: OpDelete) -> Result<()> {
let _ = args;
Err(new_unsupported_object_error(
Operation::BlockingDelete,
path,
))
}
/// Invoke the `blocking_list` operation on the specified path.
///
/// This operation is the blocking version of [`Accessor::list`]
///
/// # Behavior
///
/// - Require capability: `Blocking`
/// - List non-exist dir should return Empty.
fn blocking_list(&self, path: &str, args: OpList) -> Result<ObjectIterator> {
let _ = args;
Err(new_unsupported_object_error(Operation::BlockingList, path))
}
}
/// All functions in `Accessor` only requires `&self`, so it's safe to implement
/// `Accessor` for `Arc<dyn Accessor>`.
#[async_trait]
impl<T: Accessor> Accessor for Arc<T> {
fn metadata(&self) -> AccessorMetadata {
self.as_ref().metadata()
}
async fn create(&self, path: &str, args: OpCreate) -> Result<()> {
self.as_ref().create(path, args).await
}
async fn read(&self, path: &str, args: OpRead) -> Result<BytesReader> {
self.as_ref().read(path, args).await
}
async fn write(&self, path: &str, args: OpWrite, r: BytesReader) -> Result<u64> {
self.as_ref().write(path, args, r).await
}
async fn stat(&self, path: &str, args: OpStat) -> Result<ObjectMetadata> {
self.as_ref().stat(path, args).await
}
async fn delete(&self, path: &str, args: OpDelete) -> Result<()> {
self.as_ref().delete(path, args).await
}
async fn list(&self, path: &str, args: OpList) -> Result<ObjectStreamer> {
self.as_ref().list(path, args).await
}
fn presign(&self, path: &str, args: OpPresign) -> Result<PresignedRequest> {
self.as_ref().presign(path, args)
}
async fn create_multipart(&self, path: &str, args: OpCreateMultipart) -> Result<String> {
self.as_ref().create_multipart(path, args).await
}
async fn write_multipart(
&self,
path: &str,
args: OpWriteMultipart,
r: BytesReader,
) -> Result<ObjectPart> {
self.as_ref().write_multipart(path, args, r).await
}
async fn complete_multipart(&self, path: &str, args: OpCompleteMultipart) -> Result<()> {
self.as_ref().complete_multipart(path, args).await
}
async fn abort_multipart(&self, path: &str, args: OpAbortMultipart) -> Result<()> {
self.as_ref().abort_multipart(path, args).await
}
fn blocking_create(&self, path: &str, args: OpCreate) -> Result<()> {
self.as_ref().blocking_create(path, args)
}
fn blocking_read(&self, path: &str, args: OpRead) -> Result<BlockingBytesReader> {
self.as_ref().blocking_read(path, args)
}
fn blocking_write(&self, path: &str, args: OpWrite, r: BlockingBytesReader) -> Result<u64> {
self.as_ref().blocking_write(path, args, r)
}
fn blocking_stat(&self, path: &str, args: OpStat) -> Result<ObjectMetadata> {
self.as_ref().blocking_stat(path, args)
}
fn blocking_delete(&self, path: &str, args: OpDelete) -> Result<()> {
self.as_ref().blocking_delete(path, args)
}
fn blocking_list(&self, path: &str, args: OpList) -> Result<ObjectIterator> {
self.as_ref().blocking_list(path, args)
}
}
/// Metadata for accessor, users can use this metadata to get information of underlying backend.
#[derive(Clone, Debug, Default)]
pub struct AccessorMetadata {
scheme: Scheme,
root: String,
name: String,
capabilities: FlagSet<AccessorCapability>,
}
impl AccessorMetadata {
/// [`Scheme`] of backend.
pub fn scheme(&self) -> Scheme {
self.scheme
}
/// Set [`Scheme`] for backend.
pub fn set_scheme(&mut self, scheme: Scheme) -> &mut Self {
self.scheme = scheme;
self
}
/// Root of backend, will be in format like `/path/to/dir/`
pub fn root(&self) -> &str {
&self.root
}
/// Set root for backend.
///
/// Note: input root must be normalized.
pub fn set_root(&mut self, root: &str) -> &mut Self {
self.root = root.to_string();
self
}
/// Name of backend, could be empty if underlying backend doesn't have namespace concept.
///
/// For example:
///
/// - name for `s3` => bucket name
/// - name for `azblob` => container name
pub fn name(&self) -> &str {
&self.name
}
/// Set name of this backend.
pub fn set_name(&mut self, name: &str) -> &mut Self {
self.name = name.to_string();
self
}
/// Check if current backend supports [`Accessor::read`] or not.
pub fn can_read(&self) -> bool {
self.capabilities.contains(AccessorCapability::Read)
}
/// Check if current backend supports [`Accessor::write`] or not.
pub fn can_write(&self) -> bool {
self.capabilities.contains(AccessorCapability::Write)
}
/// Check if current backend supports [`Accessor::list`] or not.
pub fn can_list(&self) -> bool {
self.capabilities.contains(AccessorCapability::List)
}
/// Check if current backend supports [`Accessor::presign`] or not.
pub fn can_presign(&self) -> bool {
self.capabilities.contains(AccessorCapability::Presign)
}
/// Check if current backend supports multipart operations or not.
pub fn can_multipart(&self) -> bool {
self.capabilities.contains(AccessorCapability::Multipart)
}
/// Check if current backend supports blocking operations or not.
pub fn can_blocking(&self) -> bool {
self.capabilities.contains(AccessorCapability::Blocking)
}
/// Get backend's capabilities.
pub fn capabilities(&self) -> FlagSet<AccessorCapability> {
self.capabilities
}
/// Set capabilities for backend.
pub fn set_capabilities(
&mut self,
capabilities: impl Into<FlagSet<AccessorCapability>>,
) -> &mut Self {
self.capabilities = capabilities.into();
self
}
}
flags! {
/// AccessorCapability describes accessor's advanced capability.
pub enum AccessorCapability: u32 {
/// Add this capability if service supports `read` and `stat`
Read,
/// Add this capability if service supports `write` and `delete`
Write,
/// Add this capability if service supports `list`
List,
/// Add this capability if service supports `presign`
Presign,
/// Add this capability if service supports `multipart`
Multipart,
/// Add this capability if service supports `blocking`
Blocking,
}
}