| 1 |
Store |
| 2 |
===== |
| 3 |
|
| 4 |
Overview |
| 5 |
-------- |
| 6 |
|
| 7 |
The high-level Store API implementation transparently deals with nesting and |
| 8 |
soft deletion, so the caller doesn't need to care much about that, and the backend |
| 9 |
API can be much simpler: |
| 10 |
|
| 11 |
- create/destroy: initialize or remove the whole store. |
| 12 |
- list: flat list of the items in the given namespace (by default, only non-deleted |
| 13 |
items; optionally, only soft-deleted items). |
| 14 |
- store: write a new item into the store (providing its key/value pair). |
| 15 |
- load: read a value from the store (given its key); partial loads specifying |
| 16 |
an offset and/or size are supported. |
| 17 |
- info: get information about an item via its key (exists, size, ...). |
| 18 |
- hash: computes the hexdigest for the content of an item (given its key). |
| 19 |
Supported algorithms are all algorithms supported by ``hashlib`` (e.g. |
| 20 |
"sha256", the default) and also "blake3" (requires the optional ``blake3`` |
| 21 |
package, see :doc:`installation`). |
| 22 |
- delete: immediately remove an item from the store (given its key). |
| 23 |
- move: implements renaming, soft delete/undelete, and moving to the current |
| 24 |
nesting level. |
| 25 |
- defrag: general purpose defragmentation helper (copies blocks to new items). |
| 26 |
If the target name is computed from the content, the same algorithms as for |
| 27 |
hash are supported. |
| 28 |
- quota: return quota limit and usage (-1 if quotas not enabled or not supported) |
| 29 |
- stats: API call counters, time spent in API methods, data volume/throughput. |
| 30 |
- latency/bandwidth emulator: see :ref:`store-latency-bandwidth-emulator`. |
| 31 |
|
| 32 |
Store operations (and per-op timing and volume) are logged at DEBUG log level. |
| 33 |
|
| 34 |
See also :doc:`store_caching` for optional Store-level caching with a secondary backend. |
| 35 |
|
| 36 |
|
| 37 |
Initialization |
| 38 |
-------------- |
| 39 |
|
| 40 |
The ``Store`` is the main entry point to the API. It is initialized with a |
| 41 |
backend URL and an optional configuration dictionary: |
| 42 |
|
| 43 |
.. code-block:: python |
| 44 |
|
| 45 |
from borgstore import Store |
| 46 |
|
| 47 |
store = Store( |
| 48 |
url="file:///abs/path", |
| 49 |
config={ |
| 50 |
"data": {"levels": [1]}, |
| 51 |
"config": {"levels": [0]}, |
| 52 |
}, |
| 53 |
) |
| 54 |
|
| 55 |
The ``config`` dictionary maps namespace names to their configuration. |
| 56 |
The following keys are supported in each namespace configuration: |
| 57 |
|
| 58 |
- ``levels`` (required): A list of integers specifying the nesting depths |
| 59 |
supported for the namespace. |
| 60 |
|
| 61 |
See :doc:`store_caching` for details on caching. |
| 62 |
|
| 63 |
.. _store-latency-bandwidth-emulator: |
| 64 |
|
| 65 |
Latency and bandwidth emulator |
| 66 |
------------------------------ |
| 67 |
|
| 68 |
The Store can emulate slower backend behavior using environment variables: |
| 69 |
|
| 70 |
- ``BORGSTORE_LATENCY``: per-primary-call latency in microseconds (``[us]``). |
| 71 |
- ``BORGSTORE_BANDWIDTH``: primary-call bandwidth limit in bits per second |
| 72 |
(``[bit/s]``). |
| 73 |
|
| 74 |
Current behavior with Store caching enabled: |
| 75 |
|
| 76 |
- Emulation applies to **primary backend** operations. |
| 77 |
- Emulation does **not** apply to **cache backend** operations. |
| 78 |
|
| 79 |
This means: |
| 80 |
|
| 81 |
- On cache miss paths (for example writethrough/mirror reads that load from the |
| 82 |
primary backend), emulation affects the primary backend calls. |
| 83 |
- On cache hit paths, cached reads avoid primary backend load operations and |
| 84 |
therefore do not incur emulated bandwidth delay for the cache backend read. |
| 85 |
- Name resolution for Store operations still uses primary backend lookups, |
| 86 |
therefore configured latency can still be visible even when data comes from |
| 87 |
cache. |
| 88 |
|
| 89 |
Keys |
| 90 |
---- |
| 91 |
|
| 92 |
A key (str) can look like: |
| 93 |
|
| 94 |
- 0123456789abcdef... (usually a long, hex-encoded hash value) |
| 95 |
- Any other pure ASCII string without '/', '..', or spaces. |
| 96 |
|
| 97 |
|
| 98 |
Namespaces |
| 99 |
---------- |
| 100 |
|
| 101 |
To keep things separate, keys should be prefixed with a namespace, such as: |
| 102 |
|
| 103 |
- config/settings |
| 104 |
- meta/0123456789abcdef... |
| 105 |
- data/0123456789abcdef... |
| 106 |
|
| 107 |
Please note: |
| 108 |
|
| 109 |
1. You should always use namespaces. |
| 110 |
2. Nested namespaces like namespace1/namespace2/key are not supported. |
| 111 |
3. The code can work without a namespace (empty namespace ""), but then you |
| 112 |
can't add another namespace later, because that would create |
| 113 |
nested namespaces. |
| 114 |
|
| 115 |
.. _store-values: |
| 116 |
|
| 117 |
Values |
| 118 |
------ |
| 119 |
|
| 120 |
Values can be any arbitrary binary data. |
| 121 |
|
| 122 |
``store(name, value)`` accepts the value either as ``bytes`` or as a |
| 123 |
``memoryview`` of bytes, so a caller that already has the data inside a bigger |
| 124 |
buffer can avoid copying it: |
| 125 |
|
| 126 |
.. code-block:: python |
| 127 |
|
| 128 |
buffer = bytearray(...) # e.g. a big buffer, filled with multiple objects |
| 129 |
store.store("data/0123456789abcdef", memoryview(buffer)[offset : offset + size]) |
| 130 |
|
| 131 |
Notes about ``memoryview`` values: |
| 132 |
|
| 133 |
- The view must be C-contiguous (a plain slice of a buffer is). Otherwise, a |
| 134 |
``ValueError`` is raised - the value must be storable as one consecutive |
| 135 |
sequence of bytes. |
| 136 |
- If the view has an itemsize > 1 (e.g. a view of an ``array.array("I", ...)``), |
| 137 |
the bytes it consists of are stored, so the stored size is |
| 138 |
``len(value) * value.itemsize``. |
| 139 |
- The value is only used while ``store`` runs. Afterwards, the caller may reuse |
| 140 |
or release the underlying buffer. |
| 141 |
- Not all backends can hand a ``memoryview`` to the library they use: ``posixfs``, |
| 142 |
``rest`` and ``rclone`` store it without copying, ``sftp`` and ``s3`` internally |
| 143 |
create a ``bytes`` object (one copy) first. |
| 144 |
|
| 145 |
Loading always gives ``bytes``. |
| 146 |
|
| 147 |
Automatic Nesting |
| 148 |
----------------- |
| 149 |
|
| 150 |
For the Store user, items have names such as: |
| 151 |
|
| 152 |
- namespace/0123456789abcdef... |
| 153 |
- namespace/abcdef0123456789... |
| 154 |
|
| 155 |
If there are very many items in the namespace, this could lead to scalability |
| 156 |
issues in the backend. The Store implementation therefore offers transparent |
| 157 |
nesting, so that internally the backend API is called with names such as: |
| 158 |
|
| 159 |
- namespace/01/23/45/0123456789abcdef... |
| 160 |
- namespace/ab/cd/ef/abcdef0123456789... |
| 161 |
|
| 162 |
The nesting depth can be configured from 0 (= no nesting) to N levels and |
| 163 |
there can be different nesting configurations depending on the namespace. |
| 164 |
|
| 165 |
The Store supports operating at different nesting levels in the same |
| 166 |
namespace at the same time. |
| 167 |
|
| 168 |
When using nesting depth > 0, the backends assume that keys are hashes |
| 169 |
(contain hex digits) because some backends pre-create the nesting |
| 170 |
directories at initialization time to optimize backend performance. |
| 171 |
|
| 172 |
Soft deletion |
| 173 |
------------- |
| 174 |
|
| 175 |
To soft-delete an item (so its value can still be read or it can be |
| 176 |
undeleted), the store just renames the item, appending ".del" to its name. |
| 177 |
|
| 178 |
Undelete reverses this by removing the ".del" suffix from the name. |
| 179 |
|
| 180 |
Some store operations provide a boolean flag "deleted" to control whether they |
| 181 |
consider soft-deleted items. |
| 182 |
|
| 183 |
Scalability |
| 184 |
----------- |
| 185 |
|
| 186 |
- Count of key/value pairs stored in a namespace: automatic nesting is |
| 187 |
provided for keys to address common scalability issues. |
| 188 |
- Key size: there are no special provisions for extremely long keys (e.g., |
| 189 |
exceeding backend limitations). Usually this is not a problem, though. |
| 190 |
- Value size: there are no special provisions for dealing with large value |
| 191 |
sizes (e.g., more than available memory, more than backend storage limitations, |
| 192 |
etc.). If one deals with very large values, one usually cuts them into |
| 193 |
chunks before storing them in the store. |
| 194 |
- Partial loads improve performance by avoiding a full load if only part |
| 195 |
of the value is needed (e.g., a header with metadata). |
| 196 |
|