@@ -1470,11 +1470,59 @@ def __dealloc__(self):
14701470 super ().__dealloc__ ()
14711471
14721472
1473+ def _open_special_store (urlpath , mode , offset , ** kwargs ):
1474+ if urlpath .endswith (".b2z" ) or 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 (".b2e" ):
1481+ if offset != 0 :
1482+ raise ValueError ("Offset must be 0 for EmbedStore" )
1483+ from blosc2 .embed_store import EmbedStore
1484+
1485+ return EmbedStore (urlpath , mode = mode , ** kwargs )
1486+ return None
1487+
1488+
1489+ def _set_default_dparams (kwargs ):
1490+ dparams = kwargs .get ("dparams" )
1491+ if dparams is None :
1492+ # Use multiple threads for decompression by default, unless we are in WASM
1493+ # (does not support threads). The only drawback for using multiple threads
1494+ # is that access time will be slower because of the overhead of spawning threads
1495+ # (but could be fixed in the future with more intelligent thread pools).
1496+ dparams = (
1497+ blosc2 .DParams (nthreads = blosc2 .nthreads ) if not blosc2 .IS_WASM else blosc2 .DParams (nthreads = 1 )
1498+ )
1499+ kwargs ["dparams" ] = dparams
1500+
1501+
1502+ def _process_opened_object (res ):
1503+ meta = getattr (res , "schunk" , res ).meta
1504+ if "proxy-source" in meta :
1505+ proxy_src = meta ["proxy-source" ]
1506+ if proxy_src ["local_abspath" ] is not None :
1507+ src = blosc2 .open (proxy_src ["local_abspath" ])
1508+ return blosc2 .Proxy (src , _cache = res )
1509+ elif proxy_src ["urlpath" ] is not None :
1510+ src = blosc2 .C2Array (proxy_src ["urlpath" ][0 ], proxy_src ["urlpath" ][1 ], proxy_src ["urlpath" ][2 ])
1511+ return blosc2 .Proxy (src , _cache = res )
1512+ elif not proxy_src ["caterva2_env" ]:
1513+ raise RuntimeError ("Could not find the source when opening a Proxy" )
1514+
1515+ if isinstance (res , blosc2 .NDArray ) and "LazyArray" in res .schunk .meta :
1516+ return blosc2 ._open_lazyarray (res )
1517+ else :
1518+ return res
1519+
1520+
14731521def open (
14741522 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`
1523+ ) -> blosc2 .SChunk | blosc2 .NDArray | blosc2 .C2Array | blosc2 .LazyArray | blosc2 .Proxy | Any :
1524+ """Open a persistent :ref:`SChunk`, :ref:`NDArray`, a remote :ref:`C2Array`,
1525+ a :ref:`Proxy`, a :ref:`DictStore` or an :ref:`EmbedStore`.
14781526
14791527 See the `Notes` section for more info on opening `Proxy` objects.
14801528
@@ -1510,9 +1558,8 @@ def open(
15101558
15111559 Returns
15121560 -------
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.
1561+ out: :ref:`SChunk`, :ref:`NDArray`, :ref:`C2Array`, :ref:`DictStore` or :ref:`EmbedStore`
1562+ The object found in the path.
15161563
15171564 Notes
15181565 -----
@@ -1577,34 +1624,15 @@ def open(
15771624
15781625 if isinstance (urlpath , pathlib .PurePath ):
15791626 urlpath = str (urlpath )
1627+
1628+ special = _open_special_store (urlpath , mode , offset , ** kwargs )
1629+ if special is not None :
1630+ return special
1631+
15801632 if not os .path .exists (urlpath ):
15811633 raise FileNotFoundError (f"No such file or directory: { urlpath } " )
15821634
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
1635+ _set_default_dparams (kwargs )
15931636 res = blosc2_ext .open (urlpath , mode , offset , ** kwargs )
15941637
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
1638+ return _process_opened_object (res )
0 commit comments