Skip to content

Commit

Permalink
refactor: Remove metakey concept
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Nov 13, 2024
1 parent 297bbf9 commit db0fd61
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 1,201 deletions.
2 changes: 0 additions & 2 deletions bin/oay/src/services/s3/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use axum::routing::get;
use axum::Router;
use chrono::SecondsFormat;
use futures_util::StreamExt;
use opendal::Metakey;
use opendal::Operator;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -100,7 +99,6 @@ async fn handle_list_objects(
.op
.lister_with(&params.prefix)
.start_after(&params.start_after)
.metakey(Metakey::Mode | Metakey::LastModified | Metakey::Etag | Metakey::ContentLength)
.await?
.chunks(1000);

Expand Down
2 changes: 1 addition & 1 deletion bindings/java/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl From<opendal::Error> for Error {

impl From<jni::errors::Error> for Error {
fn from(err: jni::errors::Error) -> Self {
opendal::Error::new(ErrorKind::Unexpected, &err.to_string()).into()
opendal::Error::new(ErrorKind::Unexpected, err.to_string()).into()
}
}

Expand Down
85 changes: 24 additions & 61 deletions bindings/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use opendal::Capability;
use opendal::Entry;
use opendal::EntryMode;
use opendal::Metadata;
use opendal::Metakey;
use opendal::OperatorInfo;

mod async_operator;
Expand All @@ -51,7 +50,7 @@ fn make_presigned_request<'a>(env: &mut JNIEnv<'a>, req: PresignedRequest) -> Re
for (k, v) in req.header().iter() {
let key = k.to_string();
let value = v.to_str().map_err(|err| {
opendal::Error::new(opendal::ErrorKind::Unexpected, &err.to_string())
opendal::Error::new(opendal::ErrorKind::Unexpected, err.to_string())
})?;
map.insert(key, value.to_owned());
}
Expand Down Expand Up @@ -143,72 +142,36 @@ fn make_metadata<'a>(env: &mut JNIEnv<'a>, metadata: Metadata) -> Result<JObject
EntryMode::Unknown => 2,
};

let metakey = metadata.metakey();

let contains_metakey = |k| metakey.contains(k) || metakey.contains(Metakey::Complete);

let last_modified = if contains_metakey(Metakey::LastModified) {
metadata.last_modified().map_or_else(
|| Ok::<JObject<'_>, error::Error>(JObject::null()),
|v| {
Ok(env
.call_static_method(
"java/time/Instant",
"ofEpochSecond",
"(JJ)Ljava/time/Instant;",
&[
JValue::Long(v.timestamp()),
JValue::Long(v.timestamp_subsec_nanos() as jlong),
],
)?
.l()?)
},
)?
} else {
JObject::null()
};
let last_modified = metadata.last_modified().map_or_else(
|| Ok::<JObject<'_>, error::Error>(JObject::null()),
|v| {
Ok(env
.call_static_method(
"java/time/Instant",
"ofEpochSecond",
"(JJ)Ljava/time/Instant;",
&[
JValue::Long(v.timestamp()),
JValue::Long(v.timestamp_subsec_nanos() as jlong),
],
)?
.l()?)
},
)?;

let cache_control = if contains_metakey(Metakey::CacheControl) {
convert::string_to_jstring(env, metadata.cache_control())?
} else {
JObject::null()
};
let cache_control = convert::string_to_jstring(env, metadata.cache_control())?;

let content_disposition = if contains_metakey(Metakey::ContentDisposition) {
convert::string_to_jstring(env, metadata.content_disposition())?
} else {
JObject::null()
};
let content_disposition = convert::string_to_jstring(env, metadata.content_disposition())?;

let content_md5 = if contains_metakey(Metakey::ContentMd5) {
convert::string_to_jstring(env, metadata.content_md5())?
} else {
JObject::null()
};
let content_md5 = convert::string_to_jstring(env, metadata.content_md5())?;

let content_type = if contains_metakey(Metakey::ContentType) {
convert::string_to_jstring(env, metadata.content_type())?
} else {
JObject::null()
};
let content_type = convert::string_to_jstring(env, metadata.content_type())?;

let etag = if contains_metakey(Metakey::Etag) {
convert::string_to_jstring(env, metadata.etag())?
} else {
JObject::null()
};
let etag = convert::string_to_jstring(env, metadata.etag())?;

let version = if contains_metakey(Metakey::Version) {
convert::string_to_jstring(env, metadata.version())?
} else {
JObject::null()
};
let version = convert::string_to_jstring(env, metadata.version())?;

let content_length = if contains_metakey(Metakey::ContentLength) {
metadata.content_length() as jlong
} else {
-1
};
let content_length = metadata.content_length() as jlong;

let result = env
.new_object(
Expand Down
2 changes: 1 addition & 1 deletion bindings/java/src/operator_input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn intern_read_next_bytes(
match reader
.next()
.transpose()
.map_err(|err| opendal::Error::new(opendal::ErrorKind::Unexpected, &err.to_string()))?
.map_err(|err| opendal::Error::new(opendal::ErrorKind::Unexpected, err.to_string()))?
{
None => Ok(JObject::null().into_raw()),
Some(content) => {
Expand Down
14 changes: 2 additions & 12 deletions core/src/layers/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,7 @@ impl<A: Access> CompleteAccessor<A> {
}

// Forward to underlying storage directly since we don't know how to handle stat dir.
self.inner.stat(path, args).await.map(|v| {
v.map_metadata(|m| {
let bit = m.metakey();
m.with_metakey(bit | Metakey::Complete)
})
})
self.inner.stat(path, args).await
}

fn complete_blocking_stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
Expand Down Expand Up @@ -258,12 +253,7 @@ impl<A: Access> CompleteAccessor<A> {
}

// Forward to underlying storage directly since we don't know how to handle stat dir.
self.inner.blocking_stat(path, args).map(|v| {
v.map_metadata(|m| {
let bit = m.metakey();
m.with_metakey(bit | Metakey::Complete)
})
})
self.inner.blocking_stat(path, args)
}

async fn complete_list(
Expand Down
5 changes: 2 additions & 3 deletions core/src/layers/mime_guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ impl<A: Access> LayeredAccess for MimeGuessAccessor<A> {
mod tests {
use super::*;
use crate::services::Memory;
use crate::Metakey;
use crate::Operator;

const DATA: &str = "<html>test</html>";
Expand Down Expand Up @@ -196,7 +195,7 @@ mod tests {
Some(CUSTOM)
);

let entries = op.list_with("").metakey(Metakey::Complete).await.unwrap();
let entries = op.list_with("").await.unwrap();
assert_eq!(entries[0].metadata().content_type(), Some(HTML));
assert_eq!(entries[1].metadata().content_type(), None);
assert_eq!(entries[2].metadata().content_type(), Some(CUSTOM));
Expand All @@ -222,7 +221,7 @@ mod tests {
.unwrap();
assert_eq!(op.stat("test2.html").unwrap().content_type(), Some(CUSTOM));

let entries = op.list_with("").metakey(Metakey::Complete).call().unwrap();
let entries = op.list_with("").call().unwrap();
assert_eq!(entries[0].metadata().content_type(), Some(HTML));
assert_eq!(entries[1].metadata().content_type(), None);
assert_eq!(entries[2].metadata().content_type(), Some(CUSTOM));
Expand Down
23 changes: 0 additions & 23 deletions core/src/raw/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::time::Duration;

use crate::raw::*;
use crate::*;
use flagset::FlagSet;

/// Args for `create` operation.
///
Expand Down Expand Up @@ -85,13 +84,6 @@ pub struct OpList {
///
/// Default to `false`.
recursive: bool,
/// Metakey is used to control which meta should be returned.
///
/// Lister will make sure the result for specified meta is **known**:
///
/// - `Some(v)` means exist.
/// - `None` means services doesn't have this meta.
metakey: FlagSet<Metakey>,
/// The concurrent of stat operations inside list operation.
/// Users could use this to control the number of concurrent stat operation when metadata is unknown.
///
Expand All @@ -115,8 +107,6 @@ impl Default for OpList {
limit: None,
start_after: None,
recursive: false,
// By default, we want to know what's the mode of this entry.
metakey: Metakey::Mode.into(),
concurrent: 1,
version: false,
}
Expand Down Expand Up @@ -167,19 +157,6 @@ impl OpList {
self.recursive
}

/// Change the metakey of this list operation.
///
/// The default metakey is `Metakey::Mode`.
pub fn with_metakey(mut self, metakey: impl Into<FlagSet<Metakey>>) -> Self {
self.metakey = metakey.into();
self
}

/// Get the current metakey.
pub fn metakey(&self) -> FlagSet<Metakey> {
self.metakey
}

/// Change the concurrent of this list operation.
///
/// The default concurrent is 1.
Expand Down
8 changes: 4 additions & 4 deletions core/src/services/fs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Access for FsBackend {
}
}

async fn list(&self, path: &str, arg: OpList) -> Result<(RpList, Self::Lister)> {
async fn list(&self, path: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
let p = self.core.root.join(path.trim_end_matches('/'));

let f = match tokio::fs::read_dir(&p).await {
Expand All @@ -356,7 +356,7 @@ impl Access for FsBackend {
}
};

let rd = FsLister::new(&self.core.root, path, f, arg);
let rd = FsLister::new(&self.core.root, path, f);
Ok((RpList::default(), Some(rd)))
}

Expand Down Expand Up @@ -511,7 +511,7 @@ impl Access for FsBackend {
}
}

fn blocking_list(&self, path: &str, arg: OpList) -> Result<(RpList, Self::BlockingLister)> {
fn blocking_list(&self, path: &str, _: OpList) -> Result<(RpList, Self::BlockingLister)> {
let p = self.core.root.join(path.trim_end_matches('/'));

let f = match std::fs::read_dir(p) {
Expand All @@ -525,7 +525,7 @@ impl Access for FsBackend {
}
};

let rd = FsLister::new(&self.core.root, path, f, arg);
let rd = FsLister::new(&self.core.root, path, f);
Ok((RpList::default(), Some(rd)))
}

Expand Down
Loading

0 comments on commit db0fd61

Please sign in to comment.