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

Add zarr_python_compat_zip_store test #71

Merged
merged 2 commits into from
Sep 12, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Fix `ArrayShardedExt::inner_chunk_grid` when applied on a sharded array with the `transpose` codec preceding `sharding_indexed`
- Fix `ZipStorageAdapter` on windows

## [0.17.0-beta.0] - 2024-09-06

Expand Down
Binary file added zarrs/tests/data/zarr_python_compat/zarr.zip
Binary file not shown.
9 changes: 9 additions & 0 deletions zarrs/tests/data/zarr_python_zip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import numpy as np
import zarr
print(zarr.__version__) # zarr-3.0.0a4.dev25+ge9f808b4

store = zarr.store.ZipStore('zarrs/tests/data/zarr_python_compat/zarr.zip', mode='w')
root = zarr.group(store=store, zarr_version=3)
z = root.create_array(shape=(100, 100), chunks=(10, 10), name="foo", dtype=np.uint8) # fill_value=42: Broken
z[:] = 42
store.close()
20 changes: 20 additions & 0 deletions zarrs/tests/zarr_python_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::{error::Error, path::PathBuf, sync::Arc};

use zarrs::{array::Array, array_subset::ArraySubset};
use zarrs_storage::{storage_adapter::zip::ZipStorageAdapter, store, StoreKey};

#[test]
fn zarr_python_compat_zip_store() -> Result<(), Box<dyn Error>> {
let path = PathBuf::from("tests/data/zarr_python_compat");
let store = Arc::new(store::FilesystemStore::new(&path)?);
let store = Arc::new(ZipStorageAdapter::new(store, StoreKey::new("zarr.zip")?)?);

let array = Array::open(store, "/foo")?;
assert_eq!(array.shape(), vec![100, 100]);
let elements = array.retrieve_array_subset_elements::<u8>(&ArraySubset::new_with_shape(
array.shape().to_vec(),
))?;
assert_eq!(elements, vec![42u8; 100 * 100]);

Ok(())
}
20 changes: 13 additions & 7 deletions zarrs_storage/src/storage_adapter/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,25 @@
})
}

fn key_str_to_zip_path(&self, key: &str) -> String {
let mut zip_name = self.zip_path.clone();
zip_name.push(key);

let mut zip_name = zip_name.to_string_lossy();
if cfg!(windows) {
zip_name = zip_name.replace("\\", "/").into();

Check warning on line 71 in zarrs_storage/src/storage_adapter/zip.rs

View check run for this annotation

Codecov / codecov/patch

zarrs_storage/src/storage_adapter/zip.rs#L71

Added line #L71 was not covered by tests
}
zip_name.to_string()
}

fn get_impl(
&self,
key: &StoreKey,
byte_ranges: &[ByteRange],
) -> Result<Option<Vec<Bytes>>, StorageError> {
let mut zip_archive = self.zip_archive.lock();
let mut zip_name = self.zip_path.clone();
zip_name.push(key.as_str());

let mut file = {
let zip_file = zip_archive.by_name(&zip_name.to_string_lossy());
let zip_file = zip_archive.by_name(&self.key_str_to_zip_path(key.as_str()));
match zip_file {
Ok(zip_file) => zip_file,
Err(err) => match err {
Expand Down Expand Up @@ -145,9 +153,7 @@
.into_iter()
.filter_map(|name| {
if name.starts_with(prefix.as_str()) {
let mut zip_name = self.zip_path.clone();
zip_name.push(&name);
if let Ok(file) = zip_archive.by_name(&zip_name.to_string_lossy()) {
if let Ok(file) = zip_archive.by_name(&self.key_str_to_zip_path(&name)) {
if file.is_file() {
let name = name.strip_suffix('/').unwrap_or(&name);
if let Ok(store_key) = StoreKey::try_from(name) {
Expand Down