|
30 | 30 | from collections import OrderedDict
|
31 | 31 | from collections.abc import MutableMapping
|
32 | 32 | from functools import lru_cache
|
33 |
| -from os import scandir |
34 | 33 | from pickle import PicklingError
|
35 | 34 | from threading import Lock, RLock
|
36 | 35 | from typing import Sequence, Mapping, Optional, Union, List, Tuple, Dict, Any
|
@@ -269,9 +268,15 @@ def _getsize(store: BaseStore, path: Path = None) -> int:
|
269 | 268 | # also include zarr.json?
|
270 | 269 | # members += ['zarr.json']
|
271 | 270 | else:
|
272 |
| - members = listdir(store, path) |
273 |
| - prefix = _path_to_prefix(path) |
274 |
| - members = [prefix + k for k in members] |
| 271 | + to_visit = [path] |
| 272 | + members = [] |
| 273 | + while to_visit: |
| 274 | + print(to_visit) |
| 275 | + current_path = to_visit.pop() |
| 276 | + current_members = listdir(store, current_path) |
| 277 | + prefix = _path_to_prefix(current_path) |
| 278 | + members.extend([prefix + k for k in current_members]) |
| 279 | + to_visit.extend([prefix + k for k in current_members]) |
275 | 280 | for k in members:
|
276 | 281 | try:
|
277 | 282 | v = store[k]
|
@@ -971,8 +976,12 @@ def getsize(self, path: Path = None):
|
971 | 976 | elif isinstance(value, self.cls):
|
972 | 977 | # total size for directory
|
973 | 978 | size = 0
|
974 |
| - for v in value.values(): |
975 |
| - if not isinstance(v, self.cls): |
| 979 | + to_visit = list(value.values()) |
| 980 | + while to_visit: |
| 981 | + v = to_visit.pop() |
| 982 | + if isinstance(v, self.cls): |
| 983 | + to_visit.extend(v.values()) |
| 984 | + else: |
976 | 985 | size += buffer_size(v)
|
977 | 986 | return size
|
978 | 987 |
|
@@ -1269,9 +1278,10 @@ def getsize(self, path=None):
|
1269 | 1278 | return os.path.getsize(fs_path)
|
1270 | 1279 | elif os.path.isdir(fs_path):
|
1271 | 1280 | size = 0
|
1272 |
| - for child in scandir(fs_path): |
1273 |
| - if child.is_file(): |
1274 |
| - size += child.stat().st_size |
| 1281 | + for root, dirs, files in os.walk(fs_path): |
| 1282 | + for file in files: |
| 1283 | + file_path = os.path.join(root, file) |
| 1284 | + size += os.path.getsize(file_path) |
1275 | 1285 | return size
|
1276 | 1286 | else:
|
1277 | 1287 | return 0
|
@@ -1903,29 +1913,19 @@ def listdir(self, path=None):
|
1903 | 1913 | def getsize(self, path=None):
|
1904 | 1914 | path = normalize_storage_path(path)
|
1905 | 1915 | with self.mutex:
|
1906 |
| - children = self.listdir(path) |
1907 |
| - if children: |
1908 |
| - size = 0 |
1909 |
| - for child in children: |
1910 |
| - if path: |
1911 |
| - name = path + "/" + child |
1912 |
| - else: |
1913 |
| - name = child |
1914 |
| - try: |
1915 |
| - info = self.zf.getinfo(name) |
1916 |
| - except KeyError: |
1917 |
| - pass |
1918 |
| - else: |
1919 |
| - size += info.compress_size |
1920 |
| - return size |
1921 |
| - elif path: |
| 1916 | + to_visit = [path] if path else self.listdir(path) |
| 1917 | + total_size = 0 |
| 1918 | + while to_visit: |
| 1919 | + current_path = to_visit.pop() |
1922 | 1920 | try:
|
1923 |
| - info = self.zf.getinfo(path) |
1924 |
| - return info.compress_size |
| 1921 | + info = self.zf.getinfo(current_path) |
| 1922 | + total_size += info.compress_size |
1925 | 1923 | except KeyError:
|
1926 |
| - return 0 |
1927 |
| - else: |
1928 |
| - return 0 |
| 1924 | + children = self.listdir(current_path) |
| 1925 | + for child in children: |
| 1926 | + full_path = current_path + "/" + child if current_path else child |
| 1927 | + to_visit.append(full_path) |
| 1928 | + return total_size |
1929 | 1929 |
|
1930 | 1930 | def clear(self):
|
1931 | 1931 | if self.mode == "r":
|
@@ -2488,6 +2488,8 @@ def listdir(self, path: Path = None):
|
2488 | 2488 | return listing
|
2489 | 2489 |
|
2490 | 2490 | def getsize(self, path=None) -> int:
|
| 2491 | + print("WYF") |
| 2492 | + print(self._store, path) |
2491 | 2493 | return getsize(self._store, path=path)
|
2492 | 2494 |
|
2493 | 2495 | def _pop_value(self):
|
@@ -2745,10 +2747,9 @@ def getsize(self, path=None):
|
2745 | 2747 | size = self.cursor.execute(
|
2746 | 2748 | """
|
2747 | 2749 | SELECT COALESCE(SUM(LENGTH(v)), 0) FROM zarr
|
2748 |
| - WHERE k LIKE (? || "%") AND |
2749 |
| - 0 == INSTR(LTRIM(SUBSTR(k, LENGTH(?) + 1), "/"), "/") |
| 2750 | + WHERE k LIKE (? || "%") |
2750 | 2751 | """,
|
2751 |
| - (path, path), |
| 2752 | + (path,), |
2752 | 2753 | )
|
2753 | 2754 | for (s,) in size:
|
2754 | 2755 | return s
|
|
0 commit comments