Skip to content

WIP: Fix getsize to be recursive #362

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

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Bug fixes
* Ensure ``DictStore`` contains only ``bytes`` to facilitate comparisons and protect against writes.
By :user:`John Kirkham <jakirkham>`, :issue:`350`

* Fix size computation of ``DirectoryStore`` and subclasses to be recursive.
By :user:`John Kirkham <jakirkham>`, :issue:`253`, :issue:`362`

Maintenance
~~~~~~~~~~~

Expand Down
7 changes: 3 additions & 4 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,11 +843,10 @@ def getsize(self, path=None):
if os.path.isfile(fs_path):
return os.path.getsize(fs_path)
elif os.path.isdir(fs_path):
children = os.listdir(fs_path)
size = 0
for child in children:
child_fs_path = os.path.join(fs_path, child)
if os.path.isfile(child_fs_path):
for dirname, dirs, children in os.walk(fs_path):
for child in children:
child_fs_path = os.path.join(dirname, child)
size += os.path.getsize(child_fs_path)
return size
else:
Expand Down
9 changes: 7 additions & 2 deletions zarr/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,15 +1278,20 @@ def create_array(read_only=False, **kwargs):
cache_attrs=cache_attrs)

def test_nbytes_stored(self):

# dict as store
z = self.create_array(shape=1000, chunks=100)
expect_nbytes_stored = sum(buffer_size(v) for v in z.store.values())
assert expect_nbytes_stored == z.nbytes_stored
z[:] = 42
expect_nbytes_stored = sum(buffer_size(v) for v in z.store.values())
assert expect_nbytes_stored == z.nbytes_stored

z2 = self.create_array(shape=(1000, 1100), chunks=100)
expect_nbytes_stored = sum(buffer_size(v) for v in z2.store.values())
assert expect_nbytes_stored == z2.nbytes_stored
z2[:] = 42
expect_nbytes_stored = sum(buffer_size(v) for v in z2.store.values())
assert expect_nbytes_stored == z2.nbytes_stored


class TestArrayWithNestedDirectoryStore(TestArrayWithDirectoryStore):

Expand Down
4 changes: 2 additions & 2 deletions zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ def test_hierarchy(self):

# test getsize (optional)
if hasattr(store, 'getsize'):
assert 6 == store.getsize()
assert 15 == store.getsize()
assert 3 == store.getsize('a')
assert 3 == store.getsize('b')
assert 3 == store.getsize('c')
assert 9 == store.getsize('c')
assert 3 == store.getsize('c/d')
assert 6 == store.getsize('c/e')
assert 3 == store.getsize('c/e/f')
Expand Down