Debian Codegraph @

docs/store.rst

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