Debian Codegraph @

docs/source/spec.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 References specification
2 ========================
3
4 The content of a reference set should match the given description here.
5 ``fsspec``'s ``ReferenceFileSystem`` expects this kind of input.
6
7 Version 0
8 ---------
9
10 Prototype spec for the structure required by ReferenceFileSystem:
11
12 .. code-block:: json
13
14 {
15 "key0": "data",
16 "key1": ["protocol://target_url", 10000, 100]
17 }
18
19 where:
20
21 * ``key0`` includes data as-is (stored as text)
22 * ``key1`` refers to a data file URL, the offset within the file (in bytes), and the length of the data item (in bytes).
23
24 For example, Zarr data in this proposed spec might be represented as:
25
26 .. code-block:: json
27
28 {
29 ".zgroup": "{\n \"zarr_format\": 2\n}",
30 ".zattrs": "{\n \"Conventions\": \"UGRID-0.9.0\n\"}",
31 "x/.zattrs": "{\n \"_ARRAY_DIMENSIONS\": [\n \"node\"\n ...",
32 "x/.zarray": "{\n \"chunks\": [\n 9228245\n ],\n \"compressor\": null,\n \"dtype\": \"<f8\",\n ...",
33 "x/0": ["s3://bucket/path/file.nc", 294094376, 73825960]
34 }
35
36 Data can also be written as a JSON object instead of a string, in which case the value is interpreted as a JSON file. For example, the above could equivalently be written as:
37
38 .. code-block:: json
39
40 {
41 ".zgroup": {"zarr_format": 2},
42 ".zattrs": {"Conventions": "UGRID-0.9.0\n"},
43 "x/.zattrs": {"_ARRAY_DIMENSIONS": ["node"]},
44 "x/.zarray": {"chunks": [9228245], "compressor": null, "dtype": "<f8"},
45 "x/0": ["s3://bucket/path/file.nc", 294094376, 73825960]
46 }
47
48 Version 1
49 ---------
50
51 Metadata structure in JSON. We note, for future possible binary storage, that "version", "gen" and "templates" should
52 be considered attributes, and "refs" as the data that ought to dominate the storage size. The previous definition,
53 Version 0, is compatible with the "refs" entry, but here we add features. It will also be possible to *expand*
54 this new enhanced spec into Version 0 format.
55
56
57 .. code-block::
58
59 {
60 "version": (required, must be equal to) 1,
61 "templates": (optional, zero or more arbitrary keys) {
62 "template_name": jinja-str
63 },
64 "gen": (optional, zero or more items) [
65 "key": (required) jinja-str,
66 "url": (required) jinja-str,
67 "offset": (optional, required with "length") jinja-str,
68 "length": (optional, required with "offset") jinja-str,
69 "dimensions": (required, one or more arbitrary keys) {
70 "variable_name": (required)
71 {"start": (optional) int, "stop": (required) int, "step": (optional) int}
72 OR
73 [int, ...]
74 }
75 ],
76 "refs": (optional, zero or more arbitrary keys) {
77 "key_name": (required) str OR [url(jinja-str)] OR [url(jinja-str), offset(int), length(int)]
78 }
79 }
80
81 Where:
82
83 - ``jinja-str`` is a string which will be rendered by jinja2 or its non-python equivalent; i.e., it may be
84 a literal string, or may include "{{..}}" annotations, where:
85
86 - for the values associated with a template_name, the variables are to be passed in reference URL strings that use this template
87 - for the values within a "gen" object, variables come from the "dimensions" and "templates"
88
89 - the ``str`` format of a reference value may be:
90
91 - a string starting "base64:", which will be decoded to binary
92 - any other string, interpreted as ascii data
93
94 - the str version of ref values indicates data, the one-element array a whole url, and the three-element version
95 a binary section of a url
96
97 Here is an example
98
99 .. code-block:: json
100
101 {
102 "version": 1,
103 "templates": {
104 "u": "server.domain/path",
105 "f": "{{c}}"
106 },
107 "gen": [
108 {
109 "key": "gen_key{{i}}",
110 "url": "http://{{u}}_{{i}}",
111 "offset": "{{(i + 1) * 1000}}",
112 "length": "1000",
113 "dimensions":
114 {
115 "i": {"stop": 5}
116 }
117 }
118 ],
119 "refs": {
120 "key0": "data",
121 "key1": ["http://target_url", 10000, 100],
122 "key2": ["http://{{u}}", 10000, 100],
123 "key3": ["http://{{f(c='text')}}", 10000, 100]
124 }
125 }
126
127 Here the variable ``i`` takes the values ``[0, 1, 2, 3, 4]``, which could have been provided in array form. Where there
128 is more than one variable, a cartesian product is formed.
129
130 This example evaluates to the Version 0 equivalent
131
132 .. code-block:: json
133
134 {
135 "key0": "data",
136 "key1": ["http://target_url", 10000, 100],
137 "key2": ["http://server.domain/path", 10000, 100],
138 "key3": ["http://text", 10000, 100],
139 "gen_key0": ["http://server.domain/path_0", 1000, 1000],
140 "gen_key1": ["http://server.domain/path_1", 2000, 1000],
141 "gen_key2": ["http://server.domain/path_2", 3000, 1000],
142 "gen_key3": ["http://server.domain/path_3", 4000, 1000],
143 "gen_key4": ["http://server.domain/path_4", 5000, 1000]
144 }
145
146 such that accessing, for instance, "key0" returns ``b"data"`` and accessing "gen_key0" returns 1000 bytes
147 from the given URL, at an offset of 1000.
148
149 .. raw:: html
150
151 <script data-goatcounter="https://kerchunk.goatcounter.com/count"
152 async src="//gc.zgo.at/count.js"></script>
153
154 Parquet references
155 ------------------
156
157 Since JSON is rather verbose, it is easy with enough chunks to make a references file
158 that is too big: slow to load and heavy on memory. Although the former can be
159 alleviated by compression (I recommend Zstd), the latter cannot. This can
160 become particularly apparent during the combine phase when loading many reference sets.
161
162 The class `fsspec.implementations.reference.LazyReferenceMapper`_ provides an
163 alternative *implementation*, and its on-disk layout effectively is a new reference
164 spec, and we describe it here. The class itself has a dict mapper interface, just
165 like the rendered references from JSON files; except that it assumes that it is
166 working on a zarr dataset. This is because the references are split into files, and
167 an array's shape/chunk information is used to figure out which reference file
168 to load.
169
170 .. _fsspec.implementations.reference.LazyReferenceMapper: https://filesystem-spec.readthedocs.io/en/latest/api.html?highlight=lazyreference#fsspec.implementations.reference.LazyReferenceMapper
171
172 The following code
173
174 .. code-block:: python
175
176 lz = fsspec.implementations.reference.LazyReferenceMapper.create("ref.parquet")
177 z = zarr.open_group(lz, mode="w")
178 d = z.create_dataset("name", shape=(1,))
179 d[:] = 1
180 g2 = z.create_group("deep")
181 d = g2.create_dataset("name", shape=(1,))
182 d[:] = 1
183
184 produces files
185
186 .. code-block:: text
187
188 ref.parquet/deep/name/refs.0.parq
189 ref.parquet/name/refs.0.parq
190 ref.parquet/.zmetadata
191
192 Here, .zmetadata is all of the metadata of all of all subgroups/arrays (similar to
193 zarr "consolidated metadata"), with two top-level fields: "metadata" (dict[str, str]
194 all of the
195 zarr metadata key/values) and "record_size", an integer set during ``.create()``.
196
197 Each parquet file contains references within the corresponding path to where it is.
198 For example, key "name/0" will be the zeroth reference in "./name/refs.0.parq". If
199 there are multiple dimensions, normal C indexing is used to find the Nth reference,
200 and there are up to "record_size" references (default 10000) in the first file;
201 reference >10000,<=20000 would be in "./name/refs.2.parquet". Each file is (for now)
202 padded to record_size, but they compress really well.
203
204 Each row of the parquet data contains fields
205
206 .. code-block::
207
208 path: optional str/categorical, remote location URL
209 offset: int, start location of block
210 size: int, number of bytes in block
211 raw: optional bytes, binary data
212
213 If ``raw`` is populated, this is the data of the key. If ``path`` is
214 populated but size is 0, it is the whole file indicated (like a JSON [url] reference).
215 Otherwise, it is a byte block in the indicated file (like a JSON [url, offset, size] reference).
216 If both ``raw`` and ``path`` are NULL, the key does not exist.
217
218 We reserve the possibility to store small array data in .zmetadata instead
219 of creating a small/mostly empty parquet file for each.