|
3 | 3 | Ref |
4 | 4 | === |
5 | 5 |
|
6 | | -Durable reference to a persistent Blosc2 object, a member inside a |
7 | | -:ref:`DictStore`, or a remote :ref:`C2Array`. |
| 6 | +Overview |
| 7 | +-------- |
| 8 | + |
| 9 | +``Ref`` is a small durable reference object for locating reopenable Blosc2 |
| 10 | +objects without embedding their full value. |
| 11 | + |
| 12 | +Currently supported reference kinds are: |
| 13 | + |
| 14 | +- ``"urlpath"`` for persistent local objects |
| 15 | +- ``"dictstore_key"`` for members inside ``.b2d`` / ``.b2z`` ``DictStore`` containers |
| 16 | +- ``"c2array"`` for remote ``C2Array`` objects |
| 17 | + |
| 18 | +Use :meth:`Ref.open` to resolve a reference back into a live object. |
| 19 | + |
| 20 | +Example |
| 21 | +------- |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + import tempfile |
| 26 | + from pathlib import Path |
| 27 | +
|
| 28 | + import blosc2 |
| 29 | +
|
| 30 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 31 | + array_path = Path(tmpdir) / "array.b2nd" |
| 32 | + catalog_path = Path(tmpdir) / "catalog.b2nd" |
| 33 | +
|
| 34 | + # References are durable only for persistent objects. |
| 35 | + arr = blosc2.arange(5, urlpath=array_path, mode="w") |
| 36 | + ref = blosc2.Ref.from_object(arr) |
| 37 | +
|
| 38 | + # A Ref can itself be persisted, for example as variable-length metadata |
| 39 | + # in another persistent Blosc2 object. |
| 40 | + catalog = blosc2.zeros(1, urlpath=catalog_path, mode="w") |
| 41 | + catalog.schunk.vlmeta["array_ref"] = ref |
| 42 | +
|
| 43 | + # Reopen the metadata holder and resolve the persisted reference. |
| 44 | + catalog = blosc2.open(catalog_path, mode="r") |
| 45 | + restored_ref = catalog.schunk.vlmeta["array_ref"] |
| 46 | +
|
| 47 | + reopened = restored_ref.open() |
| 48 | + print(reopened[:]) # [0 1 2 3 4] |
8 | 49 |
|
9 | 50 | .. currentmodule:: blosc2 |
10 | 51 |
|
|
0 commit comments