Skip to content

Commit 065cb0c

Browse files
committed
adds index_codecs to the sharding spec
1 parent 46ed6cd commit 065cb0c

File tree

3 files changed

+190
-51
lines changed

3 files changed

+190
-51
lines changed

docs/v3/codecs/crc32c/v1.0.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
.. _crc32c-codec-v1:
2+
3+
============================
4+
CRC32C checksum codec (version 1.0)
5+
============================
6+
7+
**Editor's draft 26 July 2019**
8+
9+
Specification URI:
10+
https://zarr-specs.readthedocs.io/en/latest/v3/codecs/endian/v1.0.html
11+
Corresponding ZEP:
12+
`ZEP 1 — Zarr specification version 3 <https://zarr.dev/zeps/draft/ZEP0001.html>`_
13+
Issue tracking:
14+
`GitHub issues <https://github.com/zarr-developers/zarr-specs/labels/codec>`_
15+
Suggest an edit for this spec:
16+
`GitHub editor <https://github.com/zarr-developers/zarr-specs/blob/main/docs/v3/codecs/endian/v1.0.rst>`_
17+
18+
Copyright 2020 `Zarr core development team
19+
<https://github.com/orgs/zarr-developers/teams/core-devs>`_. This work
20+
is licensed under a `Creative Commons Attribution 3.0 Unported License
21+
<https://creativecommons.org/licenses/by/3.0/>`_.
22+
23+
----
24+
25+
26+
Abstract
27+
========
28+
29+
Defines an ``bytes -> bytes`` codec that appends a CRC32C checksum of the input bytestream.
30+
31+
32+
Status of this document
33+
=======================
34+
35+
.. warning::
36+
This document is a draft for review and subject to changes.
37+
It will become final when the `Zarr Enhancement Proposal (ZEP) 2 <https://zarr.dev/zeps/draft/ZEP0002.html>`_
38+
is approved via the `ZEP process <https://zarr.dev/zeps/active/ZEP0000.html>`_.
39+
40+
41+
Document conventions
42+
====================
43+
44+
Conformance requirements are expressed with a combination of
45+
descriptive assertions and [RFC2119]_ terminology. The key words
46+
"MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
47+
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative
48+
parts of this document are to be interpreted as described in
49+
[RFC2119]_. However, for readability, these words do not appear in all
50+
uppercase letters in this specification.
51+
52+
All of the text of this specification is normative except sections
53+
explicitly marked as non-normative, examples, and notes. Examples in
54+
this specification are introduced with the words "for example".
55+
56+
57+
Codec name
58+
==========
59+
60+
The value of the ``name`` member in the codec object MUST be ``crc32c``.
61+
62+
63+
Configuration parameters
64+
========================
65+
66+
None.
67+
68+
69+
Format and algorithm
70+
====================
71+
72+
This is an ``bytes -> bytes`` codec.
73+
74+
The codec computes the CRC32C checksum as defined in [RFC3720]_ of the input
75+
bytestream. The output bytestream is composed of the unchanged input byte
76+
stream with the appended checksum. The checksum is represented as a 32-bit
77+
unsigned integer represented in little endian.
78+
79+
80+
References
81+
==========
82+
83+
.. [RFC2119] S. Bradner. Key words for use in RFCs to Indicate
84+
Requirement Levels. March 1997. Best Current Practice. URL:
85+
https://tools.ietf.org/html/rfc2119
86+
87+
.. [RFC3720] J. Satran et al. Internet Small Computer Systems
88+
Interface (iSCSI). April 2004. Proposed Standard. URL:
89+
https://tools.ietf.org/html/rfc3720
90+
91+
92+
Change log
93+
==========
94+
95+
No changes yet.

docs/v3/codecs/sharding-indexed/v1.0.rst

Lines changed: 90 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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

125163
Binary shard format
126164
===================
127165

128166
This 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
131169
shard, where unused space between them is allowed, followed by an index
132170
referencing 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.
155179
The ``offset[i]`` specifies the byte offset within the shard at which the
156180
encoded representation of chunk ``i`` begins, and ``nbytes[i]`` specifies the
157181
encoded 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

170204
The actual order of the chunk content is not fixed and may be chosen by the
171205
implementation. All possible write orders are valid according to this
172206
specification 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
198232
The section suggests a non-normative implementation of the codec including
199233
common 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

docs/v3/core/v3.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,11 @@ of an additional operation:
11861186
encoded representation is a byte string, then ``decoded_regions``
11871187
specifies a list of byte ranges.
11881188

1189+
- ``c.compute_encoded_size(input_size)``, a procedure that determines the byte
1190+
size of the encoded representation given a byte size of the decoded representation.
1191+
This procedure cannot be implemented for codecs that produce variable-sized
1192+
encoded representations, such as compression algorithms.
1193+
11891194
.. note::
11901195

11911196
If ``partial_decode`` is not supported by a particular codec, it can

0 commit comments

Comments
 (0)