@@ -91,12 +91,27 @@ Sharding can be configured per array in the :ref:`array-metadata` as follows::
9191 "configuration": {
9292 "chunk_shape": [32, 32],
9393 "codecs": [
94+ {
95+ "name": "endian",
96+ "configuration": {
97+ "endian": "little",
98+ }
99+ },
94100 {
95101 "name": "gzip",
96102 "configuration": {
97103 "level": 1
98104 }
99105 }
106+ ],
107+ "index_codecs": [
108+ {
109+ "name": "endian",
110+ "configuration": {
111+ "endian": "little",
112+ }
113+ },
114+ { "name": "crc32c"}
100115 ]
101116 }
102117 }
@@ -105,85 +120,104 @@ Sharding can be configured per array in the :ref:`array-metadata` as follows::
105120
106121``chunk_shape ``
107122
108- An array of integers specifying the size of the inner chunks in a shard
109- along each dimension of the outer array. The length of the ``chunk_shape ``
110- array must match the number of dimensions of the outer chunk to which this
111- sharding codec is applied, and the chunk size along each dimension must
112- evenly divide the size of the outer chunk. For example, an inner chunk
113- shape of ``[32, 2] `` with an outer chunk shape ``[64, 64] `` indicates that
114- 64 chunks are combined in one shard, 2 along the first dimension, and for
123+ An array of integers specifying the shape of the inner chunks in a shard
124+ along each dimension of the outer array. The length of the ``chunk_shape ``
125+ array must match the number of dimensions of the shard shape to which this
126+ sharding codec is applied, and the inner chunk shape along each dimension must
127+ evenly divide the size of the shard shape. For example, an inner chunk
128+ shape of ``[32, 2] `` with an shard shape ``[64, 64] `` indicates that
129+ 64 inner chunks are combined in one shard, 2 along the first dimension, and for
115130 each of those 32 along the second dimension.
116131
117132``codecs ``
118133
119134 Specifies a list of codecs to be used for encoding and decoding inner chunks.
120135 The value must be an array of objects, as specified in the
121- :ref: `array-metadata `. An absent ``codecs `` member is equivalent to
122- specifying an empty list of codecs.
136+ :ref: `array-metadata `. The ``codecs `` member is required and needs to contain
137+ exactly one ``array -> bytes `` codec.
138+
139+ ``index_codecs ``
140+
141+ Specifies a list of codecs to be used for encoding and decoding shard index.
142+ The value must be an array of objects, as specified in the
143+ :ref: `array-metadata `. The ``index_codecs `` member is required and needs to
144+ contain exactly one ``array -> bytes `` codec. Codecs that produce
145+ variable-sized encoded representation, such as compression codecs, MUST NOT
146+ be used for index codecs. It is RECOMMENDED to use a little-endian codec
147+ followed by a crc32c checksum as index codecs.
148+
149+ Definitions
150+ ===========
151+
152+ * **Shard ** is a chunk of the outer array that corresponds to one storage object.
153+ As described in this document, shards MAY have multiple inner chunks.
154+ * **Inner chunk ** is a chunk within the shard.
155+ * **Shard shape ** is the chunk shape of the outer array.
156+ * **Inner chunk shape ** is defined by the ``chunk_shape `` configuration of the codec.
157+ The inner chunk shape needs to have the same dimensions as the shard shape and the
158+ inner chunk shape along each dimension must evenly divide the size of the shard shape.
159+ * **Chunks per shard ** is the element-wise division of the shard shape by the
160+ inner chunk shape.
123161
124162
125163Binary shard format
126164===================
127165
128166This is an ``array -> bytes `` codec.
129167
130- In the ``sharding_indexed `` binary format, chunks are written successively in a
168+ In the ``sharding_indexed `` binary format, inner chunks are written successively in a
131169shard, where unused space between them is allowed, followed by an index
132170referencing them.
133171
134- The index is placed at the end of the file and has a size of ``16 * n + 4 ``
135- bytes, where ``n `` is the number of chunks in the shard, i.e. the product of the
136- sizes specified in ``chunk_shape ``. For example, ``16 * 4 + 4 = 68 bytes `` for a
137- shard shape of ``[64, 64] `` and inner chunk shape of ``[32, 32] ``.
138-
139- The index format is:
140-
141- - ``offset[0] : uint64le ``
142- - ``nbytes[0] : uint64le ``
143- - ``offset[1] : uint64le ``
144- - ``nbytes[1] : uint64le ``
145- - ...
146- - ``offset[n-1] : uint64le ``
147- - ``nbytes[n-1] : uint64le ``
148- - ``checksum : uint32le ``
149-
150- The final 4 bytes of the index is the CRC-32C checksum of the first ``16 * n ``
151- bytes of the index (everything except the final checksum).
152-
153- The chunks are listed in the index in row-major (C) order.
172+ The index is an array with 64-bit unsigned integers with a shape that matches the
173+ chunks per shard tuple with an appended dimension of size 2.
174+ For example, given a shard shape of ``[128, 128] `` and chunk shape of ``[32, 32] ``,
175+ there are ``[4, 4] `` inner chunks in a shard. The corresponding shard index has a
176+ shape of ``[4, 4, 2] ``.
154177
178+ The index contains the ``offset `` and ``nbytes `` values for each inner chunk.
155179The ``offset[i] `` specifies the byte offset within the shard at which the
156180encoded representation of chunk ``i `` begins, and ``nbytes[i] `` specifies the
157181encoded length in bytes.
158182
159- Given the example of 2x2 inner chunks in a shard, the index would look like::
183+ Empty inner chunks are denoted by setting both offset and nbytes to ``2^64 - 1 ``.
184+ Empty inner chunks are interpreted as being filled with the fill value. The index
185+ always has the full shape of all possible inner chunks per shard, even if they extend
186+ beyond the array shape.
187+
188+ The index is placed at the end of the file and encoded into binary representations
189+ using the specified index codecs. The byte size of the index is determined by the
190+ number of inner chunks in the shard ``n ``, i.e. the product of the sizes specified in
191+ ``chunk_shape ``, and the choice of index codecs. For a combination of little-endian
192+ codec and crc32c checksum codec, the index has a size of ``16 * n + 4 ``. For example,
193+ ``16 * 4 + 4 = 68 bytes `` for a shard shape of ``[64, 64] `` and inner chunk shape of
194+ ``[32, 32] ``.
195+
196+ Given the example of 2x2 inner chunks in a shard and recommended index codecs, i.e.
197+ little-endian codec followed by crc32c checksum codec, the index would look like::
160198
161199 | chunk (0, 0) | chunk (0, 1) | chunk (1, 0) | chunk (1, 1) | |
162200 | offset | nbytes | offset | nbytes | offset | nbytes | offset | nbytes | checksum |
163201 | uint64 | uint64 | uint64 | uint64 | uint64 | uint64 | uint64 | uint64 | uint32 |
164202
165- Empty chunks are denoted by setting both offset and nbytes to ``2^64 - 1 ``.
166- Empty chunks are interpreted as being filled with the fill value. The index
167- always has the full shape of all possible chunks per shard, even if they extend
168- beyond the array shape.
169203
170204The actual order of the chunk content is not fixed and may be chosen by the
171205implementation. All possible write orders are valid according to this
172206specification and therefore can be read by any other implementation. When
173- writing partial chunks into an existing shard, no specific order of the existing
174- chunks may be expected. Some writing strategies might be
207+ writing partial inner chunks into an existing shard, no specific order of the existing
208+ inner chunks may be expected. Some writing strategies might be
175209
176210* **Fixed order **: Specify a fixed order (e.g. row-, column-major, or Morton
177- order). When replacing existing chunks larger or equal-sized chunks may be
211+ order). When replacing existing inner chunks larger or equal-sized inner chunks may be
178212 replaced in-place, leaving unused space up to an upper limit that might
179213 possibly be specified. Please note that, for regular-sized uncompressed data,
180- all chunks have the same size and can therefore be replaced in-place.
214+ all inner chunks have the same size and can therefore be replaced in-place.
181215* **Append-only **: Any chunk to write is appended to the existing shard,
182- followed by an updated index. If previous chunks are updated, their storage
216+ followed by an updated index. If previous inner chunks are updated, their storage
183217 space becomes unused, as well as the previous index. This might be useful for
184218 storage that only allows append-only updates.
185219* **Other formats **: Other formats that accept additional bytes at the end of
186- the file (such as HDF) could be used for storing shards, by writing the chunks
220+ the file (such as HDF) could be used for storing shards, by writing the inner chunks
187221 in the order the format prescribes and appending a binary index derived from
188222 the byte offsets and lengths at the end of the file.
189223
@@ -198,7 +232,7 @@ Implementation notes
198232The section suggests a non-normative implementation of the codec including
199233common optimizations.
200234
201- * **Decoding **: A simple implementation to decode chunks in a shard would (a)
235+ * **Decoding **: A simple implementation to decode inner chunks in a shard would (a)
202236 read the entire value from the store into a byte buffer, (b) parse the shard
203237 index as specified above from the end of the buffer and (c) cut out the
204238 relevant bytes that belong to the requested chunk. The relevant bytes are
@@ -207,27 +241,32 @@ common optimizations.
207241 configuration applying the :ref: `decoding_procedure `. This is similar to how
208242 an implementation would access a sub-slice of a chunk.
209243
210- When reading all chunks of a shard at once, a useful optimization would be to
244+ The size of the index can be determined by applying ``c.compute_encoded_size ``
245+ for each index codec recursively. The initial size is the byte size of the index
246+ array, i.e. ``16 * chunks per shard ``.
247+
248+ When reading all inner chunks of a shard at once, a useful optimization would be to
211249 read the entire shard once into a byte buffer and then cut out and decode all
212- chunks from that buffer in one pass.
250+ inner chunks from that buffer in one pass.
213251
214252 If the underlying store supports partial reads, the decoding of single inner
215253 chunks can be optimized. In that case, the shard index can be read from the
216- store by requesting the ``n `` last bytes, where ``n `` is 16 bytes multiplied
217- by the number of chunks in a shard. After parsing the shard index, single
218- chunks can be requested from the store by specifying the byte range. The
219- bytestream, then, needs to be decoded as above.
254+ store by requesting the ``n `` last bytes, where ``n `` is the size of the index
255+ as determined by the number of inner chunks in the shard and choice of index
256+ codecs. After parsing the shard index, single inner chunks can be requested from
257+ the store by specifying the byte range. The bytestream, then, needs to be
258+ decoded as above.
220259
221260* **Encoding **: A simple implementation to encode a chunk in a shard would (a)
222261 encode the new chunk per :ref: `encoding_procedure ` in a byte buffer using the
223262 shard's inner codecs, (b) read an existing shard from the store, (c) create a
224- new bytestream with all encoded chunks of that shard including the overwritten
263+ new bytestream with all encoded inner chunks of that shard including the overwritten
225264 chunk, (d) generate a new shard index that is appended to the chunk bytestream
226265 and (e) writes the shard to the store. If there was no existing shard, an
227- empty shard is assumed. When writing entire chunks, reading the existing shard
266+ empty shard is assumed. When writing entire inner chunks, reading the existing shard
228267 first may be skipped.
229268
230- When working with chunks that have a fixed byte size (e.g., uncompressed) and
269+ When working with inner chunks that have a fixed byte size (e.g., uncompressed) and
231270 a store that supports partial writes, a optimization would be to replace the
232271 new chunk by writing to the store at the specified byte range.
233272
0 commit comments