Skip to content

Commit 66f85cc

Browse files
fix: added support and tests for opening TreeStore
1 parent cb7dfeb commit 66f85cc

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/blosc2/schunk.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,12 +1471,18 @@ def __dealloc__(self):
14711471

14721472

14731473
def _open_special_store(urlpath, mode, offset, **kwargs):
1474-
if urlpath.endswith(".b2z") or urlpath.endswith(".b2d"):
1474+
if urlpath.endswith(".b2d"):
14751475
if offset != 0:
14761476
raise ValueError("Offset must be 0 for DictStore")
14771477
from blosc2.dict_store import DictStore
14781478

14791479
return DictStore(urlpath, mode=mode, **kwargs)
1480+
elif urlpath.endswith(".b2z"):
1481+
if offset != 0:
1482+
raise ValueError("Offset must be 0 for TreeStore")
1483+
from blosc2.tree_store import TreeStore
1484+
1485+
return TreeStore(urlpath, mode=mode, **kwargs)
14801486
elif urlpath.endswith(".b2e"):
14811487
if offset != 0:
14821488
raise ValueError("Offset must be 0 for EmbedStore")
@@ -1520,9 +1526,18 @@ def _process_opened_object(res):
15201526

15211527
def open(
15221528
urlpath: str | pathlib.Path | blosc2.URLPath, mode: str = "a", offset: int = 0, **kwargs: dict
1523-
) -> blosc2.SChunk | blosc2.NDArray | blosc2.C2Array | blosc2.LazyArray | blosc2.Proxy | Any:
1529+
) -> (
1530+
blosc2.SChunk
1531+
| blosc2.NDArray
1532+
| blosc2.C2Array
1533+
| blosc2.LazyArray
1534+
| blosc2.Proxy
1535+
| blosc2.DictStore
1536+
| blosc2.TreeStore
1537+
| blosc2.EmbedStore
1538+
):
15241539
"""Open a persistent :ref:`SChunk`, :ref:`NDArray`, a remote :ref:`C2Array`,
1525-
a :ref:`Proxy`, a :ref:`DictStore` or an :ref:`EmbedStore`.
1540+
a :ref:`Proxy`, a :ref:`DictStore`, :ref:`EmbedStore`, or :ref:`TreeStore`.
15261541
15271542
See the `Notes` section for more info on opening `Proxy` objects.
15281543
@@ -1558,7 +1573,7 @@ def open(
15581573
15591574
Returns
15601575
-------
1561-
out: :ref:`SChunk`, :ref:`NDArray`, :ref:`C2Array`, :ref:`DictStore` or :ref:`EmbedStore`
1576+
out: :ref:`SChunk`, :ref:`NDArray`, :ref:`C2Array`, :ref:`DictStore`, :ref:`EmbedStore`, or :ref:`TreeStore`
15621577
The object found in the path.
15631578
15641579
Notes

tests/test_tree_store.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,18 @@ def test_key_normalization():
919919
assert "/group/data2" in tstore
920920

921921
os.remove("test_key_normalization.b2z")
922+
923+
924+
def test_open_context_manager(populated_tree_store):
925+
"""Test opening via blosc2.open as a context manager."""
926+
tstore_fixture, path = populated_tree_store
927+
if ".b2d" in path:
928+
pytest.skip("This test is only for b2z storage")
929+
# Close the fixture store to ensure data is written to disk
930+
tstore_fixture.close()
931+
932+
# Test opening via blosc2.open as a context manager
933+
with blosc2.open(path, mode="r") as tstore:
934+
assert isinstance(tstore, TreeStore)
935+
assert "/child0/data" in tstore
936+
assert np.array_equal(tstore["/child0/data"][:], np.array([1, 2, 3]))

0 commit comments

Comments
 (0)