Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Feb 10, 2023
1 parent 4919d4f commit c36650e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 67 deletions.
8 changes: 6 additions & 2 deletions src/layers/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,24 @@ impl<A: Accessor> CompleteReaderAccessor<A> {
if can_flat {
let (rp, p) = self.inner.list(path, args).await?;
Ok((rp, CompletePager::AlreadyComplete(p)))
} else {
} else if can_hierarchy {
// TODO: Make this size configure.
let p = to_flat_pager(self.inner.clone(), path, 256);
Ok((RpList::default(), CompletePager::NeedFlat(p)))
} else {
unreachable!("service that support list can't neither can flat nor can hierarchy, must be a bug")
}
}
ListStyle::Hierarchy => {
if can_hierarchy {
let (rp, p) = self.inner.list(path, args).await?;
Ok((rp, CompletePager::AlreadyComplete(p)))
} else {
} else if can_flat {
let (rp, p) = self.inner.list(path, OpList::new(ListStyle::Flat)).await?;
let p = to_hierarchy_pager(p, path);
Ok((rp, CompletePager::NeedHierarchy(p)))
} else {
unreachable!("service that support list can't neither can flat nor can hierarchy, must be a bug")
}
}
}
Expand Down
50 changes: 33 additions & 17 deletions src/layers/immutable_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeSet;
use std::collections::HashSet;
use std::fmt::Debug;
use std::mem;
Expand Down Expand Up @@ -48,21 +47,21 @@ use crate::*;
/// ```
#[derive(Default, Debug, Clone)]
pub struct ImmutableIndexLayer {
set: BTreeSet<String>,
vec: Vec<String>,
}

impl ImmutableIndexLayer {
/// Insert a key into index.
pub fn insert(&mut self, key: String) {
self.set.insert(key);
self.vec.push(key);
}

/// Insert keys from iter.
pub fn extend_iter<I>(&mut self, iter: I)
where
I: IntoIterator<Item = String>,
{
self.set.extend(iter);
self.vec.extend(iter);
}
}

Expand All @@ -71,7 +70,7 @@ impl<A: Accessor> Layer<A> for ImmutableIndexLayer {

fn layer(&self, inner: A) -> Self::LayeredAccessor {
ImmutableIndexAccessor {
set: self.set.clone(),
vec: self.vec.clone(),
inner,
}
}
Expand All @@ -80,15 +79,22 @@ impl<A: Accessor> Layer<A> for ImmutableIndexLayer {
#[derive(Debug, Clone)]
pub struct ImmutableIndexAccessor<A: Accessor> {
inner: A,
/// TODO: we can introduce trie here to lower the memory footprint.
set: BTreeSet<String>,
vec: Vec<String>,
}

impl<A: Accessor> ImmutableIndexAccessor<A> {
fn children(&self, path: &str) -> Vec<String> {
fn children_flat(&self, path: &str) -> Vec<String> {
self.vec
.iter()
.filter(|v| v.starts_with(path) && v.as_str() != path)
.cloned()
.collect()
}

fn children_hierarchy(&self, path: &str) -> Vec<String> {
let mut res = HashSet::new();

for i in self.set.iter() {
for i in self.vec.iter() {
// `/xyz` should not belong to `/abc`
if !i.starts_with(path) {
continue;
Expand Down Expand Up @@ -140,6 +146,7 @@ impl<A: Accessor> LayeredAccessor for ImmutableIndexAccessor<A> {
fn metadata(&self) -> AccessorMetadata {
let mut meta = self.inner.metadata();
meta.set_capabilities(meta.capabilities() | AccessorCapability::List);
meta.set_hints(meta.hints() | AccessorHint::ListFlat | AccessorHint::ListHierarchy);

meta
}
Expand All @@ -148,26 +155,36 @@ impl<A: Accessor> LayeredAccessor for ImmutableIndexAccessor<A> {
self.inner.read(path, args).await
}

async fn list(&self, path: &str, _: OpList) -> Result<(RpList, Self::Pager)> {
async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Pager)> {
let mut path = path;
if path == "/" {
path = ""
}

Ok((RpList::default(), ImmutableDir::new(self.children(path))))
let childen = match args.style() {
ListStyle::Flat => self.children_flat(path),
ListStyle::Hierarchy => self.children_hierarchy(path),
};

Ok((RpList::default(), ImmutableDir::new(childen)))
}

fn blocking_read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::BlockingReader)> {
self.inner.blocking_read(path, args)
}

fn blocking_list(&self, path: &str, _: OpList) -> Result<(RpList, Self::BlockingPager)> {
fn blocking_list(&self, path: &str, args: OpList) -> Result<(RpList, Self::BlockingPager)> {
let mut path = path;
if path == "/" {
path = ""
}

Ok((RpList::default(), ImmutableDir::new(self.children(path))))
let childen = match args.style() {
ListStyle::Flat => self.children_flat(path),
ListStyle::Hierarchy => self.children_hierarchy(path),
};

Ok((RpList::default(), ImmutableDir::new(childen)))
}
}

Expand Down Expand Up @@ -251,6 +268,7 @@ mod tests {
let mut set = HashSet::new();
let mut ds = op.object("").list().await?;
while let Some(entry) = ds.try_next().await? {
debug!("got entry: {}", entry.path());
assert!(
set.insert(entry.path().to_string()),
"duplicated value: {}",
Expand Down Expand Up @@ -285,6 +303,7 @@ mod tests {
let mut set = HashSet::new();
let mut map = HashMap::new();
while let Some(entry) = ds.try_next().await? {
debug!("got entry: {}", entry.path());
assert!(
set.insert(entry.path().to_string()),
"duplicated value: {}",
Expand All @@ -295,10 +314,9 @@ mod tests {

debug!("current files: {:?}", map);

assert_eq!(map.len(), 6);
assert_eq!(map["file"], ObjectMode::FILE);
assert_eq!(map["dir/"], ObjectMode::DIR);
assert_eq!(map["dir_without_prefix/"], ObjectMode::DIR);
assert_eq!(map["dir_without_prefix/file"], ObjectMode::FILE);
Ok(())
}

Expand Down Expand Up @@ -351,7 +369,6 @@ mod tests {
map.insert(entry.path().to_string(), entry.mode().await?);
}

assert_eq!(map.len(), 3);
assert_eq!(
map["dataset/stateful/ontime_2007_200.csv"],
ObjectMode::FILE
Expand Down Expand Up @@ -402,7 +419,6 @@ mod tests {

debug!("current files: {:?}", map);

assert_eq!(map.len(), 6);
assert_eq!(
map["dataset/stateful/ontime_2007_200.csv"],
ObjectMode::FILE
Expand Down
3 changes: 2 additions & 1 deletion src/layers/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,8 @@ mod tests {

fn metadata(&self) -> AccessorMetadata {
let mut am = AccessorMetadata::default();
am.set_hints(AccessorHint::ReadStreamable);
am.set_capabilities(AccessorCapability::List);
am.set_hints(AccessorHint::ReadStreamable | AccessorHint::ListHierarchy);

am
}
Expand Down
10 changes: 10 additions & 0 deletions src/raw/io/output/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ impl Entry {
&self.path
}

/// Set mode for object entry.
///
/// # Note
///
/// Please use this function carefully.
pub fn set_mode(&mut self, mode: ObjectMode) -> &mut Self {
self.meta.set_mode(mode);
self
}

/// Get entry's object mode.
pub fn mode(&self) -> ObjectMode {
self.meta.mode()
Expand Down
Loading

1 comment on commit c36650e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for opendal ready!

✅ Preview
https://opendal-mw6i1e1mh-databend.vercel.app
https://opendal-git-scan-api.vercel.app

Built with commit c36650e.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.