Debian Codegraph @

docs/source/spec.rst

raw ◀ ▶
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
150 Parquet references
151 ------------------
152
153 Since JSON is rather verbose, it is easy with enough chunks to make a references file
154 that is too big: slow to load and heavy on memory. Although the former can be
155 alleviated by compression (I recommend Zstd), the latter cannot. This can
156 become particularly apparent during the combine phase when loading many reference sets.
157
158 The class `fsspec.implementations.reference.LazyReferenceMapper`_ provides an
159 alternative *implementation*, and its on-disk layout effectively is a new reference
160 spec, and we describe it here. The class itself has a dict mapper interface, just
161 like the rendered references from JSON files; except that it assumes that it is
162 working on a zarr dataset. This is because the references are split into files, and
163 an array's shape/chunk information is used to figure out which reference file
164 to load.
165
166 .. _fsspec.implementations.reference.LazyReferenceMapper: https://filesystem-spec.readthedocs.io/en/latest/api.html?highlight=lazyreference#fsspec.implementations.reference.LazyReferenceMapper
167
168 The following code
169
170 .. code-block:: python
171
172 lz = fsspec.implementations.reference.LazyReferenceMapper.create("ref.parquet")
173 z = zarr.open_group(lz, mode="w")
174 d = z.create_dataset("name", shape=(1,))
175 d[:] = 1
176 g2 = z.create_group("deep")
177 d = g2.create_dataset("name", shape=(1,))
178 d[:] = 1
179
180 produces files
181
182 .. code-block:: text
183
184 ref.parquet/deep/name/refs.0.parq
185 ref.parquet/name/refs.0.parq
186 ref.parquet/.zmetadata
187
188 Here, .zmetadata is all of the metadata of all of all subgroups/arrays (similar to
189 zarr "consolidated metadata"), with two top-level fields: "metadata" (dict[str, str]
190 all of the
191 zarr metadata key/values) and "record_size", an integer set during ``.create()``.
192
193 Each parquet file contains references within the corresponding path to where it is.
194 For example, key "name/0" will be the zeroth reference in "./name/refs.0.parq". If
195 there are multiple dimensions, normal C indexing is used to find the Nth reference,
196 and there are up to "record_size" references (default 10000) in the first file;
197 reference >10000,<=20000 would be in "./name/refs.2.parquet". Each file is (for now)
198 padded to record_size, but they compress really well.
199
200 Each row of the parquet data contains fields
201
202 .. code-block::
203
204 path: optional str/categorical, remote location URL
205 offset: int, start location of block
206 size: int, number of bytes in block
207 raw: optional bytes, binary data
208
209 If ``raw`` is populated, this is the data of the key. If ``path`` is
210 populated but size is 0, it is the whole file indicated (like a JSON [url] reference).
211 Otherwise, it is a byte block in the indicated file (like a JSON [url, offset, size] reference).
212 If both ``raw`` and ``path`` are NULL, the key does not exist.
213
214 We reserve the possibility to store small array data in .zmetadata instead
215 of creating a small/mostly empty parquet file for each.