Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Fix failed list related tests #5058

Merged
merged 7 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion core/src/raw/oio/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ impl Entry {
}

/// Create a new entry with given value.
pub fn with(path: String, meta: Metadata) -> Entry {
pub fn with(mut path: String, meta: Metadata) -> Entry {
// Normalize path as `/` if it's empty.
if path.is_empty() {
path = "/".to_string();
}

debug_assert!(
meta.mode().is_dir() == path.ends_with('/'),
"mode {:?} not match with path {}",
Expand Down
91 changes: 31 additions & 60 deletions core/src/services/aliyun_drive/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,19 @@ impl oio::PageList for AliyunDriveLister {
};

let offset = if ctx.token.is_empty() {
if !parent.path.ends_with('/') {
// List "dir" should contains "dir/".
let path = if !parent.path.starts_with('/') {
format!("/{}", parent.path)
} else {
parent.path.clone()
};
ctx.entries.push_back(Entry::new(
&format!("{}/", path),
Metadata::new(EntryMode::DIR).with_last_modified(
parent
.updated_at
.parse::<chrono::DateTime<Utc>>()
.map_err(|e| {
Error::new(ErrorKind::Unexpected, "parse last modified time")
.set_source(e)
})?,
),
));
}
// Push self into the list result.
ctx.entries.push_back(Entry::new(
&parent.path,
Metadata::new(EntryMode::DIR).with_last_modified(
parent
.updated_at
.parse::<chrono::DateTime<Utc>>()
.map_err(|e| {
Error::new(ErrorKind::Unexpected, "parse last modified time")
.set_source(e)
})?,
),
));
None
} else {
Some(ctx.token.clone())
Expand All @@ -107,57 +100,35 @@ impl oio::PageList for AliyunDriveLister {
let result: AliyunDriveFileList =
serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;

let n = result.items.len();

for item in result.items {
let path = if parent.path.starts_with('/') {
build_abs_path(&parent.path, &item.name)
let (path, mut md) = if item.path_type == "folder" {
let path = format!("{}{}/", &parent.path.trim_start_matches('/'), &item.name);
(path, Metadata::new(EntryMode::DIR))
} else {
build_abs_path(&format!("/{}", &parent.path), &item.name)
let path = format!("{}{}", &parent.path.trim_start_matches('/'), &item.name);
(path, Metadata::new(EntryMode::FILE))
};

let (path, md) = if item.path_type == "folder" {
let path = format!("{}/", path);
let meta = Metadata::new(EntryMode::DIR).with_last_modified(
item.updated_at
.parse::<chrono::DateTime<Utc>>()
.map_err(|e| {
Error::new(ErrorKind::Unexpected, "parse last modified time")
.set_source(e)
})?,
);
(path, meta)
} else {
let mut meta = Metadata::new(EntryMode::FILE).with_last_modified(
item.updated_at
.parse::<chrono::DateTime<Utc>>()
.map_err(|e| {
Error::new(ErrorKind::Unexpected, "parse last modified time")
.set_source(e)
})?,
);
if let Some(v) = item.size {
meta = meta.with_content_length(v);
}
if let Some(v) = item.content_type {
meta = meta.with_content_type(v);
}
(path, meta)
};
md = md.with_last_modified(item.updated_at.parse::<chrono::DateTime<Utc>>().map_err(
|e| Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e),
)?);
if let Some(v) = item.size {
md = md.with_content_length(v);
}
if let Some(v) = item.content_type {
md = md.with_content_type(v);
}

ctx.entries.push_back(Entry::new(&path, md));
}

if self.limit.is_some_and(|x| x < n) || result.next_marker.is_none() {
let next_marker = result.next_marker.unwrap_or_default();
if next_marker.is_empty() {
ctx.done = true;
} else {
ctx.token = next_marker;
}

if let Some(marker) = result.next_marker {
if marker.is_empty() {
ctx.done = true;
}
ctx.token = marker;
}
Ok(())
}
}
8 changes: 7 additions & 1 deletion core/src/services/azdls/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl oio::PageList for AzdlsLister {
return Err(parse_error(resp).await?);
}

// Return self at the first page.
if ctx.token.is_empty() && !ctx.done {
let e = oio::Entry::new(&self.path, Metadata::new(EntryMode::DIR));
ctx.entries.push_back(e);
}

// Check whether this list is done.
if let Some(value) = resp.headers().get("x-ms-continuation") {
let value = value.to_str().map_err(|err| {
Expand Down Expand Up @@ -90,7 +96,7 @@ impl oio::PageList for AzdlsLister {
.with_last_modified(parse_datetime_from_rfc2822(&object.last_modified)?);

let mut path = build_rel_path(&self.core.root, &object.name);
if mode == EntryMode::DIR {
if mode.is_dir() {
path += "/"
};

Expand Down
6 changes: 6 additions & 0 deletions core/src/services/azfile/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl oio::PageList for AzfileLister {
return Err(parse_error(resp).await?);
}

// Return self at the first page.
if ctx.token.is_empty() && !ctx.done {
let e = oio::Entry::new(&self.path, Metadata::new(EntryMode::DIR));
ctx.entries.push_back(e);
}

let bs = resp.into_body();

let results: EnumerationResults =
Expand Down
3 changes: 0 additions & 3 deletions core/src/services/b2/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ impl oio::PageList for B2Lister {
continue;
}
}
if file.file_name == build_abs_path(&self.core.root, &self.path) {
continue;
}
let file_name = file.file_name.clone();
let metadata = parse_file_info(&file);

Expand Down
7 changes: 3 additions & 4 deletions core/src/services/cos/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ impl oio::PageList for CosLister {
}

for object in output.contents {
let path = build_rel_path(&self.core.root, &object.key);

if path == self.path || path.is_empty() {
continue;
let mut path = build_rel_path(&self.core.root, &object.key);
if path.is_empty() {
path = "/".to_string();
}

let meta = Metadata::new(EntryMode::from_path(&path)).with_content_length(object.size);
Expand Down
7 changes: 7 additions & 0 deletions core/src/services/gdrive/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ impl oio::PageList for GdriveLister {
return Ok(());
}

// Return self at the first page.
if ctx.token.is_empty() && !ctx.done {
let path = build_rel_path(&self.core.root, &self.path);
let e = oio::Entry::new(&path, Metadata::new(EntryMode::DIR));
ctx.entries.push_back(e);
}

let decoded_response =
serde_json::from_slice::<GdriveFileList>(&bytes).map_err(new_json_deserialize_error)?;

Expand Down
5 changes: 2 additions & 3 deletions core/src/services/sqlite/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,10 @@ impl kv::Adapter for Adapter {
let pool = self.get_client().await?;

let value = sqlx::query_scalar(&format!(
"SELECT `{}` FROM `{}` WHERE `{}` LIKE $1 and `{}` <> $2",
self.key_field, self.table, self.key_field, self.key_field
"SELECT `{}` FROM `{}` WHERE `{}` LIKE $1",
self.key_field, self.table, self.key_field
))
.bind(format!("{path}%"))
.bind(path)
.fetch_all(pool)
.await
.map_err(parse_sqlite_error)?;
Expand Down
Loading