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 list_prefix #171

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
59 changes: 57 additions & 2 deletions icechunk/src/zarr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,20 @@ impl Store {
for node in repository.list_nodes().await? {
// TODO: handle non-utf8?
let meta_key = Key::Metadata { node_path: node.path }.to_string();
if meta_key.starts_with(prefix) {
yield meta_key;
match meta_key.strip_prefix(prefix) {
None => {}
Some(rest) => {
// we have a few cases
if prefix.is_empty() // if prefix was empty anything matches
|| rest.is_empty() // if stripping prefix left empty we have a match
|| rest.starts_with('/') // next component so we match
// what we don't include is other matches,
// we want to catch prefix/foo but not prefix-foo
{
yield meta_key;
}

}
}
}
};
Expand Down Expand Up @@ -2023,6 +2035,49 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_list_dir_with_prefix() -> Result<(), Box<dyn std::error::Error>> {
let storage: Arc<dyn Storage + Send + Sync> =
Arc::new(ObjectStorage::new_in_memory_store(Some("prefix".into())));
let ds = Repository::init(Arc::clone(&storage), false).await?.build();
let mut store = Store::from_repository(
ds,
AccessMode::ReadWrite,
Some("main".to_string()),
None,
);

store
.borrow_mut()
.set(
"zarr.json",
Bytes::copy_from_slice(br#"{"zarr_format":3, "node_type":"group"}"#),
)
.await?;

store
.borrow_mut()
.set(
"group/zarr.json",
Bytes::copy_from_slice(br#"{"zarr_format":3, "node_type":"group"}"#),
)
.await?;

store
.borrow_mut()
.set(
"group-suffix/zarr.json",
Bytes::copy_from_slice(br#"{"zarr_format":3, "node_type":"group"}"#),
)
.await?;

assert_eq!(
store.list_dir("group/").await?.try_collect::<Vec<_>>().await?,
vec!["zarr.json"]
);
Ok(())
}

#[tokio::test]
async fn test_get_partial_values() -> Result<(), Box<dyn std::error::Error>> {
let storage: Arc<dyn Storage + Send + Sync> =
Expand Down
Loading