@@ -1470,11 +1470,74 @@ def __dealloc__(self):
14701470 super ().__dealloc__ ()
14711471
14721472
1473+ def _open_special_store (urlpath , mode , offset , ** kwargs ):
1474+ if urlpath .endswith (".b2d" ):
1475+ if offset != 0 :
1476+ raise ValueError ("Offset must be 0 for DictStore" )
1477+ from blosc2 .dict_store import DictStore
1478+
1479+ 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 )
1486+ elif urlpath .endswith (".b2e" ):
1487+ if offset != 0 :
1488+ raise ValueError ("Offset must be 0 for EmbedStore" )
1489+ from blosc2 .embed_store import EmbedStore
1490+
1491+ return EmbedStore (urlpath , mode = mode , ** kwargs )
1492+ return None
1493+
1494+
1495+ def _set_default_dparams (kwargs ):
1496+ dparams = kwargs .get ("dparams" )
1497+ if dparams is None :
1498+ # Use multiple threads for decompression by default, unless we are in WASM
1499+ # (does not support threads). The only drawback for using multiple threads
1500+ # is that access time will be slower because of the overhead of spawning threads
1501+ # (but could be fixed in the future with more intelligent thread pools).
1502+ dparams = (
1503+ blosc2 .DParams (nthreads = blosc2 .nthreads ) if not blosc2 .IS_WASM else blosc2 .DParams (nthreads = 1 )
1504+ )
1505+ kwargs ["dparams" ] = dparams
1506+
1507+
1508+ def _process_opened_object (res ):
1509+ meta = getattr (res , "schunk" , res ).meta
1510+ if "proxy-source" in meta :
1511+ proxy_src = meta ["proxy-source" ]
1512+ if proxy_src ["local_abspath" ] is not None :
1513+ src = blosc2 .open (proxy_src ["local_abspath" ])
1514+ return blosc2 .Proxy (src , _cache = res )
1515+ elif proxy_src ["urlpath" ] is not None :
1516+ src = blosc2 .C2Array (proxy_src ["urlpath" ][0 ], proxy_src ["urlpath" ][1 ], proxy_src ["urlpath" ][2 ])
1517+ return blosc2 .Proxy (src , _cache = res )
1518+ elif not proxy_src ["caterva2_env" ]:
1519+ raise RuntimeError ("Could not find the source when opening a Proxy" )
1520+
1521+ if isinstance (res , blosc2 .NDArray ) and "LazyArray" in res .schunk .meta :
1522+ return blosc2 ._open_lazyarray (res )
1523+ else :
1524+ return res
1525+
1526+
14731527def open (
14741528 urlpath : str | pathlib .Path | blosc2 .URLPath , mode : str = "a" , offset : int = 0 , ** kwargs : dict
1475- ) -> blosc2 .SChunk | blosc2 .NDArray | blosc2 .C2Array | blosc2 .LazyArray | blosc2 .Proxy :
1476- """Open a persistent :ref:`SChunk`, :ref:`NDArray`, a remote :ref:`C2Array`
1477- or a :ref:`Proxy`
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+ ):
1539+ """Open a persistent :ref:`SChunk`, :ref:`NDArray`, a remote :ref:`C2Array`,
1540+ a :ref:`Proxy`, a :ref:`DictStore`, :ref:`EmbedStore`, or :ref:`TreeStore`.
14781541
14791542 See the `Notes` section for more info on opening `Proxy` objects.
14801543
@@ -1510,9 +1573,8 @@ def open(
15101573
15111574 Returns
15121575 -------
1513- out: :ref:`SChunk`, :ref:`NDArray` or :ref:`C2Array`
1514- The SChunk or NDArray (if there is a "b2nd" metalayer")
1515- or the C2Array if :paramref:`urlpath` is a :ref:`blosc2.URLPath <URLPath>` instance.
1576+ out: :ref:`SChunk`, :ref:`NDArray`, :ref:`C2Array`, :ref:`DictStore`, :ref:`EmbedStore`, or :ref:`TreeStore`
1577+ The object found in the path.
15161578
15171579 Notes
15181580 -----
@@ -1577,34 +1639,15 @@ def open(
15771639
15781640 if isinstance (urlpath , pathlib .PurePath ):
15791641 urlpath = str (urlpath )
1642+
1643+ special = _open_special_store (urlpath , mode , offset , ** kwargs )
1644+ if special is not None :
1645+ return special
1646+
15801647 if not os .path .exists (urlpath ):
15811648 raise FileNotFoundError (f"No such file or directory: { urlpath } " )
15821649
1583- dparams = kwargs .get ("dparams" )
1584- if dparams is None :
1585- # Use multiple threads for decompression by default, unless we are in WASM
1586- # (does not support threads). The only drawback for using multiple threads
1587- # is that access time will be slower because of the overhead of spawning threads
1588- # (but could be fixed in the future with more intelligent thread pools).
1589- dparams = (
1590- blosc2 .DParams (nthreads = blosc2 .nthreads ) if not blosc2 .IS_WASM else blosc2 .DParams (nthreads = 1 )
1591- )
1592- kwargs ["dparams" ] = dparams
1650+ _set_default_dparams (kwargs )
15931651 res = blosc2_ext .open (urlpath , mode , offset , ** kwargs )
15941652
1595- meta = getattr (res , "schunk" , res ).meta
1596- if "proxy-source" in meta :
1597- proxy_src = meta ["proxy-source" ]
1598- if proxy_src ["local_abspath" ] is not None :
1599- src = blosc2 .open (proxy_src ["local_abspath" ])
1600- return blosc2 .Proxy (src , _cache = res )
1601- elif proxy_src ["urlpath" ] is not None :
1602- src = blosc2 .C2Array (proxy_src ["urlpath" ][0 ], proxy_src ["urlpath" ][1 ], proxy_src ["urlpath" ][2 ])
1603- return blosc2 .Proxy (src , _cache = res )
1604- elif not proxy_src ["caterva2_env" ]:
1605- raise RuntimeError ("Could not find the source when opening a Proxy" )
1606-
1607- if isinstance (res , blosc2 .NDArray ) and "LazyArray" in res .schunk .meta :
1608- return blosc2 ._open_lazyarray (res )
1609- else :
1610- return res
1653+ return _process_opened_object (res )
0 commit comments