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
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use super::concurrency::concurrency_chunks_and_codec;
use super::{Array, ArrayError, ArrayIndicesTinyVec, Element, IntoArrayBytes, update_array_bytes};
use crate::array::{ArrayBytes, ArraySubset, ArraySubsetTraits};
use zarrs_codec::{
ArrayPartialEncoderTraits, ArrayToBytesCodecTraits, CodecOptions, CodecTraits,
StoragePartialEncoder,
};
use zarrs_storage::{ReadableStorageTraits, ReadableWritableStorageTraits, StorageHandle};
impl<TStorage: ?Sized + ReadableWritableStorageTraits + 'static> Array<TStorage> {
/// Return a read-only instantiation of the array.
#[must_use]
pub fn readable(&self) -> Array<dyn ReadableStorageTraits> {
self.with_storage(self.storage.clone().readable())
}
/// Encode `chunk_subset_data` and store in `chunk_subset` of the chunk at `chunk_indices` with default codec options.
///
/// Use [`store_chunk_subset_opt`](Array::store_chunk_subset_opt) to control codec options.
/// Prefer to use [`store_chunk`](Array::store_chunk) where possible, since this function may decode the chunk before updating it and reencoding it.
///
/// # Errors
/// Returns an [`ArrayError`] if
/// - `chunk_subset` is invalid or out of bounds of the chunk,
/// - there is a codec encoding error, or
/// - an underlying store error.
///
/// # Panics
/// Panics if attempting to reference a byte beyond `usize::MAX`.
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_chunk_subset<'a>(
&self,
chunk_indices: &[u64],
chunk_subset: &dyn ArraySubsetTraits,
chunk_subset_data: impl IntoArrayBytes<'a>,
) -> Result<(), ArrayError> {
self.store_chunk_subset_opt(
chunk_indices,
chunk_subset,
chunk_subset_data,
&CodecOptions::default(),
)
}
#[deprecated(since = "0.23.0", note = "Use store_chunk_subset() instead")]
/// Encode `chunk_subset_elements` and store in `chunk_subset` of the chunk at `chunk_indices` with default codec options.
///
/// Use [`store_chunk_subset_elements_opt`](Array::store_chunk_subset_elements_opt) to control codec options.
/// Prefer to use [`store_chunk_elements`](Array::store_chunk_elements) where possible, since this will decode the chunk before updating it and reencoding it.
///
/// # Errors
/// Returns an [`ArrayError`] if
/// - the size of `T` does not match the data type size, or
/// - a [`store_chunk_subset`](Array::store_chunk_subset) error condition is met.
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_chunk_subset_elements<T: Element>(
&self,
chunk_indices: &[u64],
chunk_subset: &dyn ArraySubsetTraits,
chunk_subset_elements: &[T],
) -> Result<(), ArrayError> {
self.store_chunk_subset_opt(
chunk_indices,
chunk_subset,
chunk_subset_elements,
&CodecOptions::default(),
)
}
#[cfg(feature = "ndarray")]
#[deprecated(since = "0.23.0", note = "Use store_chunk_subset() instead")]
/// Encode `chunk_subset_array` and store in `chunk_subset` of the chunk in the subset starting at `chunk_subset_start`.
///
/// Use [`store_chunk_subset_ndarray_opt`](Array::store_chunk_subset_ndarray_opt) to control codec options.
/// Prefer to use [`store_chunk_ndarray`](Array::store_chunk_ndarray) where possible, since this will decode the chunk before updating it and reencoding it.
///
/// # Errors
/// Returns an [`ArrayError`] if a [`store_chunk_subset_elements`](Array::store_chunk_subset_elements) error condition is met.
pub fn store_chunk_subset_ndarray<T: Element, D: ndarray::Dimension>(
&self,
chunk_indices: &[u64],
chunk_subset_start: &[u64],
chunk_subset_array: &ndarray::ArrayRef<T, D>,
) -> Result<(), ArrayError> {
let subset = ArraySubset::new_with_start_shape(
chunk_subset_start.to_vec(),
chunk_subset_array
.shape()
.iter()
.map(|u| *u as u64)
.collect(),
)?;
self.store_chunk_subset_opt(
chunk_indices,
&subset,
chunk_subset_array.as_standard_layout().to_owned(),
&CodecOptions::default(),
)
}
/// Encode `subset_data` and store in `array_subset`.
///
/// Use [`store_array_subset_opt`](Array::store_array_subset_opt) to control codec options.
/// Prefer to use [`store_chunk`](Array::store_chunk) or [`store_chunks`](Array::store_chunks) where possible, since this will decode and encode each chunk intersecting `array_subset`.
///
/// # Errors
/// Returns an [`ArrayError`] if
/// - the dimensionality of `array_subset` does not match the chunk grid dimensionality
/// - the length of `subset_data` does not match the expected length governed by the shape of the array subset and the data type size,
/// - there is a codec encoding error, or
/// - an underlying store error.
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_array_subset<'a>(
&self,
array_subset: &dyn ArraySubsetTraits,
subset_data: impl IntoArrayBytes<'a>,
) -> Result<(), ArrayError> {
self.store_array_subset_opt(array_subset, subset_data, &CodecOptions::default())
}
#[deprecated(since = "0.23.0", note = "Use store_array_subset() instead")]
/// Encode `subset_elements` and store in `array_subset`.
///
/// Use [`store_array_subset_elements_opt`](Array::store_array_subset_elements_opt) to control codec options.
/// Prefer to use [`store_chunk_elements`](Array::store_chunk_elements) or [`store_chunks_elements`](Array::store_chunks_elements) where possible, since this will decode and encode each chunk intersecting `array_subset`.
///
/// # Errors
/// Returns an [`ArrayError`] if
/// - the size of `T` does not match the data type size, or
/// - a [`store_array_subset`](Array::store_array_subset) error condition is met.
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_array_subset_elements<T: Element>(
&self,
array_subset: &dyn ArraySubsetTraits,
subset_elements: &[T],
) -> Result<(), ArrayError> {
self.store_array_subset_opt(array_subset, subset_elements, &CodecOptions::default())
}
#[cfg(feature = "ndarray")]
#[deprecated(since = "0.23.0", note = "Use store_array_subset() instead")]
/// Encode `subset_array` and store in the array subset starting at `subset_start`.
///
/// Use [`store_array_subset_ndarray_opt`](Array::store_array_subset_ndarray_opt) to control codec options.
/// Prefer to use [`store_chunk_ndarray`](Array::store_chunk_ndarray) or [`store_chunks_ndarray`](Array::store_chunks_ndarray) where possible, since this will decode and encode each chunk intersecting `array_subset`.
///
/// # Errors
/// Returns an [`ArrayError`] if a [`store_array_subset_elements`](Array::store_array_subset_elements) error condition is met.
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_array_subset_ndarray<T: Element, D: ndarray::Dimension>(
&self,
subset_start: &[u64],
subset_array: &ndarray::ArrayRef<T, D>,
) -> Result<(), ArrayError> {
let subset = ArraySubset::new_with_start_shape(
subset_start.to_vec(),
subset_array.shape().iter().map(|u| *u as u64).collect(),
)?;
self.store_array_subset_opt(
&subset,
subset_array.as_standard_layout().to_owned(),
&CodecOptions::default(),
)
}
/// Retrieve the chunk at `chunk_indices`, compact it if possible, and store the compacted chunk back.
///
/// Compaction removes any extraneous data from the encoded chunk representation.
///
/// # Errors
/// Returns an [`ArrayError`] if
/// - there is a codec error, or
/// - an underlying store error.
pub fn compact_chunk(
&self,
chunk_indices: &[u64],
options: &CodecOptions,
) -> Result<bool, ArrayError> {
let chunk_bytes = self.retrieve_encoded_chunk(chunk_indices)?;
if let Some(chunk_bytes) = chunk_bytes {
if let Some(compacted_bytes) = self.codecs.compact(
chunk_bytes.into(),
&self.chunk_shape(chunk_indices)?,
self.data_type(),
self.fill_value(),
options,
)? {
// SAFETY: The compacted bytes are already encoded
unsafe {
self.store_encoded_chunk(
chunk_indices,
bytes::Bytes::from(compacted_bytes.into_owned()),
)?;
}
Ok(true)
} else {
Ok(false)
}
} else {
Ok(false)
}
}
/////////////////////////////////////////////////////////////////////////////
// Advanced methods
/////////////////////////////////////////////////////////////////////////////
/// Explicit options version of [`store_chunk_subset`](Array::store_chunk_subset).
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_chunk_subset_opt<'a>(
&self,
chunk_indices: &[u64],
chunk_subset: &dyn ArraySubsetTraits,
chunk_subset_data: impl IntoArrayBytes<'a>,
options: &CodecOptions,
) -> Result<(), ArrayError> {
let chunk_shape = self
.chunk_grid()
.chunk_shape_u64(chunk_indices)?
.ok_or_else(|| ArrayError::InvalidChunkGridIndicesError(chunk_indices.to_vec()))?;
if std::iter::zip(chunk_subset.end_exc(), &chunk_shape)
.any(|(end_exc, shape)| end_exc > *shape)
{
return Err(ArrayError::InvalidChunkSubset(
chunk_subset.to_array_subset(),
chunk_indices.to_vec(),
chunk_shape,
));
}
if chunk_subset.shape() == chunk_shape && chunk_subset.start().iter().all(|&x| x == 0) {
// The subset spans the whole chunk, so store the bytes directly and skip decoding
self.store_chunk_opt(chunk_indices, chunk_subset_data, options)
} else {
let chunk_subset_bytes = chunk_subset_data.into_array_bytes(self.data_type())?;
chunk_subset_bytes.validate(chunk_subset.num_elements(), self.data_type())?;
// Lock the chunk
// let key = self.chunk_key(chunk_indices);
// let mutex = self.storage.mutex(&key)?;
// let _lock = mutex.lock();
if options.experimental_partial_encoding()
&& self.codecs.partial_encoder_capability().partial_encode
&& self.storage.supports_set_partial()
{
let partial_encoder = self.partial_encoder(chunk_indices, options)?;
debug_assert!(
partial_encoder.supports_partial_encode(),
"partial encoder is misrepresenting its capabilities"
);
Ok(partial_encoder.partial_encode(chunk_subset, &chunk_subset_bytes, options)?)
} else {
// Decode the entire chunk
let chunk_bytes_old: ArrayBytes<'static> =
self.retrieve_chunk_opt(chunk_indices, options)?;
chunk_bytes_old.validate(chunk_shape.iter().product(), self.data_type())?;
// Update the chunk
let chunk_bytes_new = update_array_bytes(
chunk_bytes_old,
&chunk_shape,
chunk_subset,
&chunk_subset_bytes,
self.data_type().size(),
)?;
// Store the updated chunk
self.store_chunk_opt(chunk_indices, chunk_bytes_new, options)
}
}
}
#[deprecated(since = "0.23.0", note = "Use store_chunk_subset_opt() instead")]
/// Explicit options version of [`store_chunk_subset_elements`](Array::store_chunk_subset_elements).
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_chunk_subset_elements_opt<T: Element>(
&self,
chunk_indices: &[u64],
chunk_subset: &dyn ArraySubsetTraits,
chunk_subset_elements: &[T],
options: &CodecOptions,
) -> Result<(), ArrayError> {
let chunk_subset_bytes = T::to_array_bytes(self.data_type(), chunk_subset_elements)?;
self.store_chunk_subset_opt(chunk_indices, chunk_subset, chunk_subset_bytes, options)
}
#[cfg(feature = "ndarray")]
#[deprecated(since = "0.23.0", note = "Use store_chunk_subset_opt() instead")]
/// Explicit options version of [`store_chunk_subset_ndarray`](Array::store_chunk_subset_ndarray).
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_chunk_subset_ndarray_opt<T: Element, D: ndarray::Dimension>(
&self,
chunk_indices: &[u64],
chunk_subset_start: &[u64],
chunk_subset_array: &ndarray::ArrayRef<T, D>,
options: &CodecOptions,
) -> Result<(), ArrayError> {
let subset = ArraySubset::new_with_start_shape(
chunk_subset_start.to_vec(),
chunk_subset_array
.shape()
.iter()
.map(|u| *u as u64)
.collect(),
)?;
self.store_chunk_subset_opt(
chunk_indices,
&subset,
chunk_subset_array.as_standard_layout().to_owned(),
options,
)
}
/// Explicit options version of [`store_array_subset`](Array::store_array_subset).
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
#[allow(clippy::too_many_lines)]
pub fn store_array_subset_opt<'a>(
&self,
array_subset: &dyn ArraySubsetTraits,
subset_data: impl IntoArrayBytes<'a>,
options: &CodecOptions,
) -> Result<(), ArrayError> {
// Validation
if array_subset.dimensionality() != self.shape().len() {
return Err(ArrayError::InvalidArraySubset(
array_subset.to_array_subset(),
self.shape().to_vec(),
));
}
// Find the chunks intersecting this array subset
let chunks = self.chunks_in_array_subset(array_subset)?;
let Some(chunks) = chunks else {
return Err(ArrayError::InvalidArraySubset(
array_subset.to_array_subset(),
self.shape().to_vec(),
));
};
let num_chunks = chunks.num_elements_usize();
if num_chunks == 1 {
let chunk_indices = chunks.start();
let chunk_subset = self.chunk_subset(chunk_indices)?;
if array_subset == chunk_subset {
// A fast path if the array subset matches the chunk subset
// This skips the internal decoding occurring in store_chunk_subset
self.store_chunk_opt(chunk_indices, subset_data, options)?;
} else {
// Store the chunk subset
self.store_chunk_subset_opt(
chunk_indices,
&array_subset.relative_to(chunk_subset.start())?,
subset_data,
options,
)?;
}
} else {
let subset_bytes = subset_data.into_array_bytes(self.data_type())?;
subset_bytes.validate(array_subset.num_elements(), self.data_type())?;
// Calculate chunk/codec concurrency
let chunk_shape = self.chunk_shape(&vec![0; self.dimensionality()])?;
let codec_concurrency =
self.recommended_codec_concurrency(&chunk_shape, self.data_type())?;
let (chunk_concurrent_limit, options) = concurrency_chunks_and_codec(
options.concurrent_target(),
num_chunks,
options,
&codec_concurrency,
);
let store_chunk = |chunk_indices: ArrayIndicesTinyVec| -> Result<(), ArrayError> {
let chunk_subset_in_array = self.chunk_subset(&chunk_indices)?;
let overlap = array_subset.overlap(&chunk_subset_in_array)?;
let chunk_subset_in_array_subset = overlap.relative_to(&array_subset.start())?;
let chunk_subset_bytes = subset_bytes.extract_array_subset(
&chunk_subset_in_array_subset,
&array_subset.shape(),
self.data_type(),
)?;
let array_subset_in_chunk_subset =
overlap.relative_to(chunk_subset_in_array.start())?;
self.store_chunk_subset_opt(
&chunk_indices,
&array_subset_in_chunk_subset,
chunk_subset_bytes,
&options,
)
};
let indices = chunks.indices();
crate::iter_concurrent_limit!(
chunk_concurrent_limit,
indices,
try_for_each,
store_chunk
)?;
}
Ok(())
}
#[deprecated(since = "0.23.0", note = "Use store_array_subset_opt() instead")]
/// Explicit options version of [`store_array_subset_elements`](Array::store_array_subset_elements).
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_array_subset_elements_opt<T: Element>(
&self,
array_subset: &dyn ArraySubsetTraits,
subset_elements: &[T],
options: &CodecOptions,
) -> Result<(), ArrayError> {
let subset_bytes = T::to_array_bytes(self.data_type(), subset_elements)?;
self.store_array_subset_opt(array_subset, subset_bytes, options)
}
#[cfg(feature = "ndarray")]
#[deprecated(since = "0.23.0", note = "Use store_array_subset_opt() instead")]
/// Explicit options version of [`store_array_subset_ndarray`](Array::store_array_subset_ndarray).
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn store_array_subset_ndarray_opt<T: Element, D: ndarray::Dimension>(
&self,
subset_start: &[u64],
subset_array: &ndarray::ArrayRef<T, D>,
options: &CodecOptions,
) -> Result<(), ArrayError> {
let subset = ArraySubset::new_with_start_shape(
subset_start.to_vec(),
subset_array.shape().iter().map(|u| *u as u64).collect(),
)?;
self.store_array_subset_opt(
&subset,
subset_array.as_standard_layout().to_owned(),
options,
)
}
/// Initialises a partial encoder for the chunk at `chunk_indices`.
///
/// Only one partial encoder should be created for a chunk at a time because:
/// - partial encoders can hold internal state that may become out of sync, and
/// - parallel writing to the same chunk [may result in data loss](#parallel-writing).
///
/// Partial encoding with [`ArrayPartialEncoderTraits::partial_encode`] will use parallelism internally where possible.
///
/// # Errors
/// Returns an [`ArrayError`] if initialisation of the partial encoder fails.
pub fn partial_encoder(
&self,
chunk_indices: &[u64],
options: &CodecOptions,
) -> Result<Arc<dyn ArrayPartialEncoderTraits>, ArrayError> {
let storage_handle = Arc::new(StorageHandle::new(self.storage.clone()));
// Input/output
let storage_transformer = self
.storage_transformers()
.create_readable_writable_transformer(storage_handle)?;
let input_output_handle = Arc::new(StoragePartialEncoder::new(
storage_transformer,
self.chunk_key(chunk_indices),
));
Ok(self.codecs.clone().partial_encoder(
input_output_handle,
&self.chunk_shape(chunk_indices)?,
self.data_type(),
self.fill_value(),
options,
)?)
}
}