Skip to content

Commit

Permalink
object: Polish API for Metadata (#80)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored Mar 1, 2022
1 parent 07f8ff7 commit f1c29c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ OpenDAL is in **alpha** stage and has been early adopted by [databend](https://g
```rust
use anyhow::Result;
use futures::AsyncReadExt;
use futures::StreamExt;
use opendal::services::fs;
use opendal::ObjectMode;
use opendal::Operator;

#[tokio::main]
Expand All @@ -38,6 +40,16 @@ async fn main() -> Result<()> {
let meta = o.metadata().await?;
assert_eq!(meta.content_length(), 13);

// List current dir.
let mut obs = op.objects("").map(|o| o.expect("list object"));
while let Some(o) = obs.next().await {
let meta = o.metadata().await?;
if meta.path().contains("test_file") {
let mode = meta.mode();
assert!(mode.contains(ObjectMode::FILE));
}
}

// Delete file.
o.delete().await?;

Expand Down
4 changes: 2 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ async fn main() -> Result<()> {

// Get file's Metadata
let meta = o.metadata().await?;
assert_eq!(meta.content_length(), Some(13));
assert_eq!(meta.content_length(), 13);

// List current dir.
let mut obs = op.objects("").map(|o| o.expect("list object"));
let mut found = false;
while let Some(o) = obs.next().await {
let meta = o.metadata().await?;
if meta.path().contains("test_file") {
let mode = meta.mode().expect("object mode");
let mode = meta.mode();
assert!(mode.contains(ObjectMode::FILE));

found = true
Expand Down
7 changes: 1 addition & 6 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,7 @@ impl AsyncSeek for Reader {
) -> Poll<std::io::Result<u64>> {
if let ReadState::Seeking(future) = &mut self.state {
match ready!(Pin::new(future).poll(cx)) {
Ok(meta) => {
self.total_size = Some(
meta.content_length()
.expect("object doesn't have content length"),
)
}
Ok(meta) => self.total_size = Some(meta.content_length()),
Err(e) => return Poll::Ready(Err(io::Error::from(e))),
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,21 @@ impl Metadata {
self
}

pub fn mode(&self) -> Option<ObjectMode> {
self.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
}

pub fn content_length(&self) -> Option<u64> {
self.content_length
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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/behavior/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl BehaviorTest {

// Step 3: Stat this file
let meta = self.op.object(&path).metadata().await?;
assert_eq!(meta.content_length().unwrap(), size as u64, "stat file");
assert_eq!(meta.content_length(), size as u64, "stat file");

// Step 4: Read this file's content
// Step 4.1: Read the whole file.
Expand Down Expand Up @@ -103,7 +103,7 @@ impl BehaviorTest {
while let Some(o) = obs.next().await {
let meta = o.metadata().await?;
if meta.path() == path {
let mode = meta.mode().expect("object mode");
let mode = meta.mode();
assert!(
mode.contains(ObjectMode::FILE),
"expected: {:?}, actual: {:?}",
Expand Down

0 comments on commit f1c29c4

Please sign in to comment.