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
// 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::fmt::Display;
use std::fmt::Formatter;
use std::io::ErrorKind;
use std::io::Result;
use std::ops::RangeBounds;
use std::sync::Arc;
use std::time::SystemTime;
use futures::io;
use futures::io::Cursor;
use futures::AsyncWriteExt;
use crate::io::BytesRead;
use crate::ops::OpDelete;
use crate::ops::OpRead;
use crate::ops::OpStat;
use crate::ops::OpWrite;
use crate::Accessor;
use crate::BytesWrite;
/// Handler for all object related operations.
#[derive(Clone, Debug)]
pub struct Object {
acc: Arc<dyn Accessor>,
meta: Metadata,
}
impl Object {
/// Creates a new Object.
pub fn new(acc: Arc<dyn Accessor>, path: &str) -> Self {
Self {
acc,
meta: Metadata {
path: path.to_string(),
..Default::default()
},
}
}
pub(crate) fn accessor(&self) -> Arc<dyn Accessor> {
self.acc.clone()
}
/// Read the whole object into a bytes.
///
/// This function will allocate a new bytes internally. For more precise memory control or
/// reading data lazily, please use [`Object::reader`]
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::TryStreamExt;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// # let o = op.object("path/to/file");
/// # o.write(&vec![0; 4096]).await?;
/// let bs = o.read().await?;
/// # Ok(())
/// # }
/// ```
pub async fn read(&self) -> Result<Vec<u8>> {
self.range_read(..).await
}
/// Read the specified range of object into a bytes.
///
/// This function will allocate a new bytes internally. For more precise memory control or
/// reading data lazily, please use [`Object::range_reader`]
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::TryStreamExt;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// # let o = op.object("path/to/file");
/// # o.write(&vec![0; 4096]).await?;
/// let bs = o.range_read(1024..2048).await?;
/// # Ok(())
/// # }
/// ```
pub async fn range_read(&self, range: impl RangeBounds<u64>) -> Result<Vec<u8>> {
let op = OpRead::new(self.meta.path(), range);
let s = self.acc.read(&op).await?;
let mut bs = Cursor::new(Vec::new());
io::copy(s, &mut bs).await?;
Ok(bs.into_inner())
}
/// Create a new reader which can read the whole object.
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::TryStreamExt;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// # let o = op.object("path/to/file");
/// # o.write(&vec![0; 4096]).await?;
/// let r = o.reader().await?;
/// # Ok(())
/// # }
/// ```
pub async fn reader(&self) -> Result<impl BytesRead> {
self.range_reader(..).await
}
/// Create a new reader which can read the whole object.
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::TryStreamExt;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// # let o = op.object("path/to/file");
/// # o.write(&vec![0; 4096]).await?;
/// let r = o.range_reader(1024..2048).await?;
/// # Ok(())
/// # }
/// ```
pub async fn range_reader(&self, range: impl RangeBounds<u64>) -> Result<impl BytesRead> {
let op = OpRead::new(self.meta.path(), range);
Ok(self.acc.read(&op).await?)
}
/// Write bytes into object.
///
/// # Notes
///
/// - Write will make sure all bytes has been written, or an error will be returned.
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::StreamExt;
/// # use futures::SinkExt;
/// use bytes::Bytes;
///
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// let o = op.object("path/to/file");
/// let _ = o.write(vec![0; 4096]).await?;
/// # Ok(())
/// # }
/// ```
pub async fn write(&self, bs: impl AsRef<[u8]>) -> Result<()> {
let op = OpWrite::new(self.meta.path(), bs.as_ref().len() as u64);
let mut s = self.acc.write(&op).await?;
s.write_all(bs.as_ref()).await?;
s.close().await?;
Ok(())
}
/// Create a new writer which can write data into the object.
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use std::io::Result;
/// # use opendal::Operator;
/// # use futures::StreamExt;
/// # use futures::SinkExt;
/// use bytes::Bytes;
///
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # use futures::AsyncWriteExt;
/// let op = Operator::new(memory::Backend::build().finish().await?);
/// let o = op.object("path/to/file");
/// let mut w = o.writer(4096).await?;
/// w.write(&[1; 4096]);
/// w.close();
/// # Ok(())
/// # }
/// ```
pub async fn writer(&self, size: u64) -> Result<impl BytesWrite> {
let op = OpWrite::new(self.meta.path(), size);
let s = self.acc.write(&op).await?;
Ok(s)
}
/// Delete object.
///
/// # Notes
///
/// - Delete not existing error won't return errors.
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use anyhow::Result;
/// # use futures::io;
/// # use opendal::Operator;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// op.object("test").delete().await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete(&self) -> Result<()> {
let op = &OpDelete::new(self.meta.path());
self.acc.delete(op).await
}
pub(crate) fn metadata_ref(&self) -> &Metadata {
&self.meta
}
pub(crate) fn metadata_mut(&mut self) -> &mut Metadata {
&mut self.meta
}
/// Get current object's metadata.
///
/// # Examples
///
/// ```
/// # use opendal::services::memory;
/// # use anyhow::Result;
/// # use futures::io;
/// # use opendal::Operator;
/// use std::io::ErrorKind;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// # let op = Operator::new(memory::Backend::build().finish().await?);
/// if let Err(e) = op.object("test").metadata().await {
/// if e.kind() == ErrorKind::NotFound {
/// println!("object not exist")
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn metadata(&self) -> Result<Metadata> {
let op = &OpStat::new(self.meta.path());
self.acc.stat(op).await
}
/// Use local cached metadata if possible.
///
/// # Example
///
/// ```
/// use opendal::services::memory;
/// use anyhow::Result;
/// use futures::io;
/// use opendal::Operator;
///
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let op = Operator::new(memory::Backend::build().finish().await?);
/// let mut o = op.object("test");
///
/// o.metadata_cached().await;
/// // The second call to metadata_cached will have no cost.
/// o.metadata_cached().await;
///
/// Ok(())
/// }
/// ```
pub async fn metadata_cached(&mut self) -> Result<&Metadata> {
if self.meta.complete() {
return Ok(&self.meta);
}
let op = &OpStat::new(self.meta.path());
self.meta = self.acc.stat(op).await?;
Ok(&self.meta)
}
/// Check if this object exist or not.
///
/// # Example
///
/// ```
/// use opendal::services::memory;
/// use anyhow::Result;
/// use futures::io;
/// use opendal::Operator;
///
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let op = Operator::new(memory::Backend::build().finish().await?);
/// let _ = op.object("test").is_exist().await?;
///
/// Ok(())
/// }
/// ```
pub async fn is_exist(&self) -> Result<bool> {
let r = self.metadata().await;
match r {
Ok(_) => Ok(true),
Err(err) => match err.kind() {
ErrorKind::NotFound => Ok(false),
_ => Err(err),
},
}
}
}
/// Metadata carries all object metadata.
#[derive(Debug, Clone, Default)]
pub struct Metadata {
complete: bool,
path: String,
mode: Option<ObjectMode>,
content_length: Option<u64>,
content_md5: Option<String>,
last_modified: Option<SystemTime>,
}
impl Metadata {
pub fn complete(&self) -> bool {
self.complete
}
pub(crate) fn set_complete(&mut self) -> &mut Self {
self.complete = true;
self
}
/// Returns object path that relative to corresponding backend's root.
pub fn path(&self) -> &str {
&self.path
}
pub(crate) fn set_path(&mut self, path: &str) -> &mut Self {
self.path = path.to_string();
self
}
/// Object mode represent this object' mode.
pub fn mode(&self) -> ObjectMode {
debug_assert!(self.mode.is_some(), "mode must exist");
self.mode.unwrap_or_default()
}
pub(crate) fn set_mode(&mut self, mode: ObjectMode) -> &mut Self {
self.mode = Some(mode);
self
}
/// Content length of this object
pub fn content_length(&self) -> u64 {
debug_assert!(self.content_length.is_some(), "content length must exist");
self.content_length.unwrap_or_default()
}
pub(crate) fn set_content_length(&mut self, content_length: u64) -> &mut Self {
self.content_length = Some(content_length);
self
}
/// Content MD5 of this object.
pub fn content_md5(&self) -> Option<String> {
self.content_md5.clone()
}
pub(crate) fn set_content_md5(&mut self, content_md5: &str) -> &mut Self {
self.content_md5 = Some(content_md5.to_string());
self
}
/// Last modified of this object.
pub fn last_modified(&self) -> Option<SystemTime> {
self.last_modified
}
pub(crate) fn set_last_modified(&mut self, last_modified: SystemTime) -> &mut Self {
self.last_modified = Some(last_modified);
self
}
}
/// ObjectMode represents the corresponding object's mode.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ObjectMode {
/// FILE means the object has data to read.
FILE,
/// DIR means the object can be listed.
DIR,
/// Unknown means we don't know what we can do on thi object.
Unknown,
}
impl Default for ObjectMode {
fn default() -> Self {
Self::Unknown
}
}
impl Display for ObjectMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ObjectMode::FILE => write!(f, "file"),
ObjectMode::DIR => write!(f, "dir"),
ObjectMode::Unknown => write!(f, "unknown"),
}
}
}
/// ObjectStream represents a stream of object.
pub trait ObjectStream: futures::Stream<Item = Result<Object>> + Unpin + Send {}
impl<T> ObjectStream for T where T: futures::Stream<Item = Result<Object>> + Unpin + Send {}
/// ObjectStreamer is a boxed dyn [`ObjectStream`]
pub type ObjectStreamer = Box<dyn ObjectStream>;