-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
sharding.py
677 lines (576 loc) · 23.9 KB
/
sharding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
from __future__ import annotations
from collections.abc import Iterable, Mapping, MutableMapping
from dataclasses import dataclass, field, replace
from enum import Enum
from functools import lru_cache
from operator import itemgetter
from typing import TYPE_CHECKING, Any, NamedTuple
import numpy as np
import numpy.typing as npt
from zarr.abc.codec import (
ArrayBytesCodec,
ArrayBytesCodecPartialDecodeMixin,
ArrayBytesCodecPartialEncodeMixin,
Codec,
)
from zarr.abc.store import ByteGetter, ByteSetter
from zarr.array_spec import ArraySpec
from zarr.buffer import Buffer, BufferPrototype, NDBuffer, default_buffer_prototype
from zarr.chunk_grids import ChunkGrid, RegularChunkGrid
from zarr.codecs.bytes import BytesCodec
from zarr.codecs.crc32c_ import Crc32cCodec
from zarr.codecs.pipeline import BatchedCodecPipeline
from zarr.codecs.registry import register_codec
from zarr.common import (
ChunkCoords,
ChunkCoordsLike,
parse_enum,
parse_named_configuration,
parse_shapelike,
product,
)
from zarr.indexing import BasicIndexer, SelectorTuple, c_order_iter, get_indexer, morton_order_iter
from zarr.metadata import parse_codecs
if TYPE_CHECKING:
from collections.abc import Awaitable, Callable, Iterator
from typing_extensions import Self
from zarr.common import JSON
MAX_UINT_64 = 2**64 - 1
ShardMapping = Mapping[ChunkCoords, Buffer]
ShardMutableMapping = MutableMapping[ChunkCoords, Buffer]
class ShardingCodecIndexLocation(Enum):
start = "start"
end = "end"
def parse_index_location(data: JSON) -> ShardingCodecIndexLocation:
return parse_enum(data, ShardingCodecIndexLocation)
@dataclass(frozen=True)
class _ShardingByteGetter(ByteGetter):
shard_dict: ShardMapping
chunk_coords: ChunkCoords
async def get(
self, prototype: BufferPrototype, byte_range: tuple[int, int | None] | None = None
) -> Buffer | None:
assert byte_range is None, "byte_range is not supported within shards"
assert (
prototype is default_buffer_prototype
), "prototype is not supported within shards currently"
return self.shard_dict.get(self.chunk_coords)
@dataclass(frozen=True)
class _ShardingByteSetter(_ShardingByteGetter, ByteSetter):
shard_dict: ShardMutableMapping
async def set(self, value: Buffer, byte_range: tuple[int, int] | None = None) -> None:
assert byte_range is None, "byte_range is not supported within shards"
self.shard_dict[self.chunk_coords] = value
async def delete(self) -> None:
del self.shard_dict[self.chunk_coords]
class _ShardIndex(NamedTuple):
# dtype uint64, shape (chunks_per_shard_0, chunks_per_shard_1, ..., 2)
offsets_and_lengths: npt.NDArray[np.uint64]
@property
def chunks_per_shard(self) -> ChunkCoords:
return self.offsets_and_lengths.shape[0:-1]
def _localize_chunk(self, chunk_coords: ChunkCoords) -> ChunkCoords:
return tuple(
chunk_i % shard_i
for chunk_i, shard_i in zip(chunk_coords, self.offsets_and_lengths.shape, strict=False)
)
def is_all_empty(self) -> bool:
return bool(np.array_equiv(self.offsets_and_lengths, MAX_UINT_64))
def get_full_chunk_map(self) -> npt.NDArray[np.bool_]:
return np.not_equal(self.offsets_and_lengths[..., 0], MAX_UINT_64)
def get_chunk_slice(self, chunk_coords: ChunkCoords) -> tuple[int, int] | None:
localized_chunk = self._localize_chunk(chunk_coords)
chunk_start, chunk_len = self.offsets_and_lengths[localized_chunk]
if (chunk_start, chunk_len) == (MAX_UINT_64, MAX_UINT_64):
return None
else:
return (int(chunk_start), int(chunk_start) + int(chunk_len))
def set_chunk_slice(self, chunk_coords: ChunkCoords, chunk_slice: slice | None) -> None:
localized_chunk = self._localize_chunk(chunk_coords)
if chunk_slice is None:
self.offsets_and_lengths[localized_chunk] = (MAX_UINT_64, MAX_UINT_64)
else:
self.offsets_and_lengths[localized_chunk] = (
chunk_slice.start,
chunk_slice.stop - chunk_slice.start,
)
def is_dense(self, chunk_byte_length: int) -> bool:
sorted_offsets_and_lengths = sorted(
[
(offset, length)
for offset, length in self.offsets_and_lengths
if offset != MAX_UINT_64
],
key=itemgetter(0),
)
# Are all non-empty offsets unique?
if len(
set(offset for offset, _ in sorted_offsets_and_lengths if offset != MAX_UINT_64)
) != len(sorted_offsets_and_lengths):
return False
return all(
offset % chunk_byte_length == 0 and length == chunk_byte_length
for offset, length in sorted_offsets_and_lengths
)
@classmethod
def create_empty(cls, chunks_per_shard: ChunkCoords) -> _ShardIndex:
offsets_and_lengths = np.zeros(chunks_per_shard + (2,), dtype="<u8", order="C")
offsets_and_lengths.fill(MAX_UINT_64)
return cls(offsets_and_lengths)
class _ShardReader(ShardMapping):
buf: Buffer
index: _ShardIndex
@classmethod
async def from_bytes(
cls, buf: Buffer, codec: ShardingCodec, chunks_per_shard: ChunkCoords
) -> _ShardReader:
shard_index_size = codec._shard_index_size(chunks_per_shard)
obj = cls()
obj.buf = buf
if codec.index_location == ShardingCodecIndexLocation.start:
shard_index_bytes = obj.buf[:shard_index_size]
else:
shard_index_bytes = obj.buf[-shard_index_size:]
obj.index = await codec._decode_shard_index(shard_index_bytes, chunks_per_shard)
return obj
@classmethod
def create_empty(cls, chunks_per_shard: ChunkCoords) -> _ShardReader:
index = _ShardIndex.create_empty(chunks_per_shard)
obj = cls()
obj.buf = Buffer.create_zero_length()
obj.index = index
return obj
def __getitem__(self, chunk_coords: ChunkCoords) -> Buffer:
chunk_byte_slice = self.index.get_chunk_slice(chunk_coords)
if chunk_byte_slice:
return self.buf[chunk_byte_slice[0] : chunk_byte_slice[1]]
raise KeyError
def __len__(self) -> int:
return int(self.index.offsets_and_lengths.size / 2)
def __iter__(self) -> Iterator[ChunkCoords]:
return c_order_iter(self.index.offsets_and_lengths.shape[:-1])
def is_empty(self) -> bool:
return self.index.is_all_empty()
class _ShardBuilder(_ShardReader, ShardMutableMapping):
buf: Buffer
index: _ShardIndex
@classmethod
def merge_with_morton_order(
cls,
chunks_per_shard: ChunkCoords,
tombstones: set[ChunkCoords],
*shard_dicts: ShardMapping,
) -> _ShardBuilder:
obj = cls.create_empty(chunks_per_shard)
for chunk_coords in morton_order_iter(chunks_per_shard):
if chunk_coords in tombstones:
continue
for shard_dict in shard_dicts:
maybe_value = shard_dict.get(chunk_coords, None)
if maybe_value is not None:
obj[chunk_coords] = maybe_value
break
return obj
@classmethod
def create_empty(cls, chunks_per_shard: ChunkCoords) -> _ShardBuilder:
obj = cls()
obj.buf = Buffer.create_zero_length()
obj.index = _ShardIndex.create_empty(chunks_per_shard)
return obj
def __setitem__(self, chunk_coords: ChunkCoords, value: Buffer) -> None:
chunk_start = len(self.buf)
chunk_length = len(value)
self.buf = self.buf + value
self.index.set_chunk_slice(chunk_coords, slice(chunk_start, chunk_start + chunk_length))
def __delitem__(self, chunk_coords: ChunkCoords) -> None:
raise NotImplementedError
async def finalize(
self,
index_location: ShardingCodecIndexLocation,
index_encoder: Callable[[_ShardIndex], Awaitable[Buffer]],
) -> Buffer:
index_bytes = await index_encoder(self.index)
if index_location == ShardingCodecIndexLocation.start:
self.index.offsets_and_lengths[..., 0] += len(index_bytes)
index_bytes = await index_encoder(self.index) # encode again with corrected offsets
out_buf = index_bytes + self.buf
else:
out_buf = self.buf + index_bytes
return out_buf
@dataclass(frozen=True)
class _MergingShardBuilder(ShardMutableMapping):
old_dict: _ShardReader
new_dict: _ShardBuilder
tombstones: set[ChunkCoords] = field(default_factory=set)
def __getitem__(self, chunk_coords: ChunkCoords) -> Buffer:
chunk_bytes_maybe = self.new_dict.get(chunk_coords)
if chunk_bytes_maybe is not None:
return chunk_bytes_maybe
return self.old_dict[chunk_coords]
def __setitem__(self, chunk_coords: ChunkCoords, value: Buffer) -> None:
self.new_dict[chunk_coords] = value
def __delitem__(self, chunk_coords: ChunkCoords) -> None:
self.tombstones.add(chunk_coords)
def __len__(self) -> int:
return self.old_dict.__len__()
def __iter__(self) -> Iterator[ChunkCoords]:
return self.old_dict.__iter__()
def is_empty(self) -> bool:
full_chunk_coords_map = self.old_dict.index.get_full_chunk_map()
full_chunk_coords_map = np.logical_or(
full_chunk_coords_map, self.new_dict.index.get_full_chunk_map()
)
for tombstone in self.tombstones:
full_chunk_coords_map[tombstone] = False
return bool(np.array_equiv(full_chunk_coords_map, False))
async def finalize(
self,
index_location: ShardingCodecIndexLocation,
index_encoder: Callable[[_ShardIndex], Awaitable[Buffer]],
) -> Buffer:
shard_builder = _ShardBuilder.merge_with_morton_order(
self.new_dict.index.chunks_per_shard,
self.tombstones,
self.new_dict,
self.old_dict,
)
return await shard_builder.finalize(index_location, index_encoder)
@dataclass(frozen=True)
class ShardingCodec(
ArrayBytesCodec, ArrayBytesCodecPartialDecodeMixin, ArrayBytesCodecPartialEncodeMixin
):
chunk_shape: ChunkCoords
codecs: tuple[Codec, ...]
index_codecs: tuple[Codec, ...]
index_location: ShardingCodecIndexLocation = ShardingCodecIndexLocation.end
def __init__(
self,
*,
chunk_shape: ChunkCoordsLike,
codecs: Iterable[Codec | dict[str, JSON]] = (BytesCodec(),),
index_codecs: Iterable[Codec | dict[str, JSON]] = (BytesCodec(), Crc32cCodec()),
index_location: ShardingCodecIndexLocation = ShardingCodecIndexLocation.end,
) -> None:
chunk_shape_parsed = parse_shapelike(chunk_shape)
codecs_parsed = parse_codecs(codecs)
index_codecs_parsed = parse_codecs(index_codecs)
index_location_parsed = parse_index_location(index_location)
object.__setattr__(self, "chunk_shape", chunk_shape_parsed)
object.__setattr__(self, "codecs", codecs_parsed)
object.__setattr__(self, "index_codecs", index_codecs_parsed)
object.__setattr__(self, "index_location", index_location_parsed)
# Use instance-local lru_cache to avoid memory leaks
object.__setattr__(self, "_get_chunk_spec", lru_cache()(self._get_chunk_spec))
object.__setattr__(self, "_get_index_chunk_spec", lru_cache()(self._get_index_chunk_spec))
object.__setattr__(self, "_get_chunks_per_shard", lru_cache()(self._get_chunks_per_shard))
@classmethod
def from_dict(cls, data: dict[str, JSON]) -> Self:
_, configuration_parsed = parse_named_configuration(data, "sharding_indexed")
return cls(**configuration_parsed) # type: ignore[arg-type]
@property
def codec_pipeline(self) -> BatchedCodecPipeline:
return BatchedCodecPipeline.from_list(self.codecs)
def to_dict(self) -> dict[str, JSON]:
return {
"name": "sharding_indexed",
"configuration": {
"chunk_shape": list(self.chunk_shape),
"codecs": [s.to_dict() for s in self.codecs],
"index_codecs": [s.to_dict() for s in self.index_codecs],
"index_location": self.index_location,
},
}
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
shard_spec = self._get_chunk_spec(array_spec)
evolved_codecs = tuple(c.evolve_from_array_spec(array_spec=shard_spec) for c in self.codecs)
if evolved_codecs != self.codecs:
return replace(self, codecs=evolved_codecs)
return self
def validate(self, *, shape: ChunkCoords, dtype: np.dtype[Any], chunk_grid: ChunkGrid) -> None:
if len(self.chunk_shape) != len(shape):
raise ValueError(
"The shard's `chunk_shape` and array's `shape` need to have the same number of dimensions."
)
if not isinstance(chunk_grid, RegularChunkGrid):
raise ValueError("Sharding is only compatible with regular chunk grids.")
if not all(
s % c == 0
for s, c in zip(
chunk_grid.chunk_shape,
self.chunk_shape,
strict=False,
)
):
raise ValueError(
"The array's `chunk_shape` needs to be divisible by the shard's inner `chunk_shape`."
)
async def _decode_single(
self,
shard_bytes: Buffer,
shard_spec: ArraySpec,
) -> NDBuffer:
shard_shape = shard_spec.shape
chunk_shape = self.chunk_shape
chunks_per_shard = self._get_chunks_per_shard(shard_spec)
chunk_spec = self._get_chunk_spec(shard_spec)
indexer = BasicIndexer(
tuple(slice(0, s) for s in shard_shape),
shape=shard_shape,
chunk_grid=RegularChunkGrid(chunk_shape=chunk_shape),
)
# setup output array
out = chunk_spec.prototype.nd_buffer.create(
shape=shard_shape, dtype=shard_spec.dtype, order=shard_spec.order, fill_value=0
)
shard_dict = await _ShardReader.from_bytes(shard_bytes, self, chunks_per_shard)
if shard_dict.index.is_all_empty():
out.fill(shard_spec.fill_value)
return out
# decoding chunks and writing them into the output buffer
await self.codec_pipeline.read(
[
(
_ShardingByteGetter(shard_dict, chunk_coords),
chunk_spec,
chunk_selection,
out_selection,
)
for chunk_coords, chunk_selection, out_selection in indexer
],
out,
)
return out
async def _decode_partial_single(
self,
byte_getter: ByteGetter,
selection: SelectorTuple,
shard_spec: ArraySpec,
) -> NDBuffer | None:
shard_shape = shard_spec.shape
chunk_shape = self.chunk_shape
chunks_per_shard = self._get_chunks_per_shard(shard_spec)
chunk_spec = self._get_chunk_spec(shard_spec)
indexer = get_indexer(
selection,
shape=shard_shape,
chunk_grid=RegularChunkGrid(chunk_shape=chunk_shape),
)
# setup output array
out = shard_spec.prototype.nd_buffer.create(
shape=indexer.shape, dtype=shard_spec.dtype, order=shard_spec.order, fill_value=0
)
indexed_chunks = list(indexer)
all_chunk_coords = set(chunk_coords for chunk_coords, _, _ in indexed_chunks)
# reading bytes of all requested chunks
shard_dict: ShardMapping = {}
if self._is_total_shard(all_chunk_coords, chunks_per_shard):
# read entire shard
shard_dict_maybe = await self._load_full_shard_maybe(
byte_getter=byte_getter,
prototype=chunk_spec.prototype,
chunks_per_shard=chunks_per_shard,
)
if shard_dict_maybe is None:
return None
shard_dict = shard_dict_maybe
else:
# read some chunks within the shard
shard_index = await self._load_shard_index_maybe(byte_getter, chunks_per_shard)
if shard_index is None:
return None
shard_dict = {}
for chunk_coords in all_chunk_coords:
chunk_byte_slice = shard_index.get_chunk_slice(chunk_coords)
if chunk_byte_slice:
chunk_bytes = await byte_getter.get(
prototype=chunk_spec.prototype, byte_range=chunk_byte_slice
)
if chunk_bytes:
shard_dict[chunk_coords] = chunk_bytes
# decoding chunks and writing them into the output buffer
await self.codec_pipeline.read(
[
(
_ShardingByteGetter(shard_dict, chunk_coords),
chunk_spec,
chunk_selection,
out_selection,
)
for chunk_coords, chunk_selection, out_selection in indexer
],
out,
)
return out
async def _encode_single(
self,
shard_array: NDBuffer,
shard_spec: ArraySpec,
) -> Buffer | None:
shard_shape = shard_spec.shape
chunk_shape = self.chunk_shape
chunks_per_shard = self._get_chunks_per_shard(shard_spec)
chunk_spec = self._get_chunk_spec(shard_spec)
indexer = list(
BasicIndexer(
tuple(slice(0, s) for s in shard_shape),
shape=shard_shape,
chunk_grid=RegularChunkGrid(chunk_shape=chunk_shape),
)
)
shard_builder = _ShardBuilder.create_empty(chunks_per_shard)
await self.codec_pipeline.write(
[
(
_ShardingByteSetter(shard_builder, chunk_coords),
chunk_spec,
chunk_selection,
out_selection,
)
for chunk_coords, chunk_selection, out_selection in indexer
],
shard_array,
)
return await shard_builder.finalize(self.index_location, self._encode_shard_index)
async def _encode_partial_single(
self,
byte_setter: ByteSetter,
shard_array: NDBuffer,
selection: SelectorTuple,
shard_spec: ArraySpec,
) -> None:
shard_shape = shard_spec.shape
chunk_shape = self.chunk_shape
chunks_per_shard = self._get_chunks_per_shard(shard_spec)
chunk_spec = self._get_chunk_spec(shard_spec)
shard_dict = _MergingShardBuilder(
await self._load_full_shard_maybe(
byte_getter=byte_setter,
prototype=chunk_spec.prototype,
chunks_per_shard=chunks_per_shard,
)
or _ShardReader.create_empty(chunks_per_shard),
_ShardBuilder.create_empty(chunks_per_shard),
)
indexer = list(
get_indexer(
selection, shape=shard_shape, chunk_grid=RegularChunkGrid(chunk_shape=chunk_shape)
)
)
await self.codec_pipeline.write(
[
(
_ShardingByteSetter(shard_dict, chunk_coords),
chunk_spec,
chunk_selection,
out_selection,
)
for chunk_coords, chunk_selection, out_selection in indexer
],
shard_array,
)
if shard_dict.is_empty():
await byte_setter.delete()
else:
await byte_setter.set(
await shard_dict.finalize(
self.index_location,
self._encode_shard_index,
)
)
def _is_total_shard(
self, all_chunk_coords: set[ChunkCoords], chunks_per_shard: ChunkCoords
) -> bool:
return len(all_chunk_coords) == product(chunks_per_shard) and all(
chunk_coords in all_chunk_coords for chunk_coords in c_order_iter(chunks_per_shard)
)
async def _decode_shard_index(
self, index_bytes: Buffer, chunks_per_shard: ChunkCoords
) -> _ShardIndex:
index_array = next(
iter(
await BatchedCodecPipeline.from_list(self.index_codecs).decode(
[(index_bytes, self._get_index_chunk_spec(chunks_per_shard))],
)
)
)
assert index_array is not None
return _ShardIndex(index_array.as_numpy_array())
async def _encode_shard_index(self, index: _ShardIndex) -> Buffer:
index_bytes = next(
iter(
await BatchedCodecPipeline.from_list(self.index_codecs).encode(
[
(
NDBuffer.from_numpy_array(index.offsets_and_lengths),
self._get_index_chunk_spec(index.chunks_per_shard),
)
],
)
)
)
assert index_bytes is not None
assert isinstance(index_bytes, Buffer)
return index_bytes
def _shard_index_size(self, chunks_per_shard: ChunkCoords) -> int:
return BatchedCodecPipeline.from_list(self.index_codecs).compute_encoded_size(
16 * product(chunks_per_shard), self._get_index_chunk_spec(chunks_per_shard)
)
def _get_index_chunk_spec(self, chunks_per_shard: ChunkCoords) -> ArraySpec:
return ArraySpec(
shape=chunks_per_shard + (2,),
dtype=np.dtype("<u8"),
fill_value=MAX_UINT_64,
order="C", # Note: this is hard-coded for simplicity -- it is not surfaced into user code
prototype=default_buffer_prototype,
)
def _get_chunk_spec(self, shard_spec: ArraySpec) -> ArraySpec:
return ArraySpec(
shape=self.chunk_shape,
dtype=shard_spec.dtype,
fill_value=shard_spec.fill_value,
order=shard_spec.order,
prototype=shard_spec.prototype,
)
def _get_chunks_per_shard(self, shard_spec: ArraySpec) -> ChunkCoords:
return tuple(
s // c
for s, c in zip(
shard_spec.shape,
self.chunk_shape,
strict=False,
)
)
async def _load_shard_index_maybe(
self, byte_getter: ByteGetter, chunks_per_shard: ChunkCoords
) -> _ShardIndex | None:
shard_index_size = self._shard_index_size(chunks_per_shard)
if self.index_location == ShardingCodecIndexLocation.start:
index_bytes = await byte_getter.get(
prototype=default_buffer_prototype, byte_range=(0, shard_index_size)
)
else:
index_bytes = await byte_getter.get(
prototype=default_buffer_prototype, byte_range=(-shard_index_size, None)
)
if index_bytes is not None:
return await self._decode_shard_index(index_bytes, chunks_per_shard)
return None
async def _load_shard_index(
self, byte_getter: ByteGetter, chunks_per_shard: ChunkCoords
) -> _ShardIndex:
return (
await self._load_shard_index_maybe(byte_getter, chunks_per_shard)
) or _ShardIndex.create_empty(chunks_per_shard)
async def _load_full_shard_maybe(
self, byte_getter: ByteGetter, prototype: BufferPrototype, chunks_per_shard: ChunkCoords
) -> _ShardReader | None:
shard_bytes = await byte_getter.get(prototype=prototype)
return (
await _ShardReader.from_bytes(shard_bytes, self, chunks_per_shard)
if shard_bytes
else None
)
def compute_encoded_size(self, input_byte_length: int, shard_spec: ArraySpec) -> int:
chunks_per_shard = self._get_chunks_per_shard(shard_spec)
return input_byte_length + self._shard_index_size(chunks_per_shard)
register_codec("sharding_indexed", ShardingCodec)