Skip to main content

zarrs_codec/
options.rs

1//! Codec options for encoding and decoding.
2
3/// Codec options for encoding/decoding.
4///
5/// The default values are:
6/// - `validate_checksums`: `true`
7/// - `store_empty_chunks`: `false`
8/// - `concurrent_target`: number of threads available to Rayon
9/// - `chunk_concurrent_minimum`: `4`
10/// - `experimental_partial_encoding`: `false`
11#[derive(Debug, Clone, Copy)]
12pub struct CodecOptions {
13    validate_checksums: bool,
14    store_empty_chunks: bool,
15    concurrent_target: usize,
16    chunk_concurrent_minimum: usize,
17    experimental_partial_encoding: bool,
18}
19
20impl Default for CodecOptions {
21    fn default() -> Self {
22        Self {
23            validate_checksums: true,
24            store_empty_chunks: false,
25            concurrent_target: rayon::current_num_threads(),
26            chunk_concurrent_minimum: 4,
27            experimental_partial_encoding: false,
28        }
29    }
30}
31
32impl CodecOptions {
33    /// Return the validate checksums setting.
34    #[must_use]
35    pub fn validate_checksums(&self) -> bool {
36        self.validate_checksums
37    }
38
39    /// Set whether or not to validate checksums.
40    pub fn set_validate_checksums(&mut self, validate_checksums: bool) -> &mut Self {
41        self.validate_checksums = validate_checksums;
42        self
43    }
44
45    /// Set whether or not to validate checksums.
46    #[must_use]
47    pub fn with_validate_checksums(mut self, validate_checksums: bool) -> Self {
48        self.validate_checksums = validate_checksums;
49        self
50    }
51
52    /// Return the store empty chunks setting.
53    #[must_use]
54    pub fn store_empty_chunks(&self) -> bool {
55        self.store_empty_chunks
56    }
57
58    /// Set whether or not to store empty chunks.
59    pub fn set_store_empty_chunks(&mut self, store_empty_chunks: bool) -> &mut Self {
60        self.store_empty_chunks = store_empty_chunks;
61        self
62    }
63
64    /// Set whether or not to store empty chunks.
65    #[must_use]
66    pub fn with_store_empty_chunks(mut self, store_empty_chunks: bool) -> Self {
67        self.store_empty_chunks = store_empty_chunks;
68        self
69    }
70
71    /// Return the concurrent target.
72    #[must_use]
73    pub fn concurrent_target(&self) -> usize {
74        self.concurrent_target
75    }
76
77    /// Set the concurrent target.
78    pub fn set_concurrent_target(&mut self, concurrent_target: usize) -> &mut Self {
79        self.concurrent_target = concurrent_target;
80        self
81    }
82
83    /// Set the concurrent target.
84    #[must_use]
85    pub fn with_concurrent_target(mut self, concurrent_target: usize) -> Self {
86        self.concurrent_target = concurrent_target;
87        self
88    }
89
90    /// Return the chunk concurrent minimum.
91    ///
92    /// Array operations involving multiple chunks can tune the chunk and codec concurrency to improve performance/reduce memory usage.
93    /// This option sets the preferred minimum chunk concurrency.
94    /// The concurrency of internal codecs is adjusted to accomodate for the chunk concurrency in accordance with the concurrent target.
95    #[must_use]
96    pub fn chunk_concurrent_minimum(&self) -> usize {
97        self.chunk_concurrent_minimum
98    }
99
100    /// Set the chunk concurrent minimum.
101    pub fn set_chunk_concurrent_minimum(&mut self, chunk_concurrent_minimum: usize) -> &mut Self {
102        self.chunk_concurrent_minimum = chunk_concurrent_minimum;
103        self
104    }
105
106    /// Set the chunk concurrent minimum.
107    #[must_use]
108    pub fn with_chunk_concurrent_minimum(mut self, chunk_concurrent_minimum: usize) -> Self {
109        self.chunk_concurrent_minimum = chunk_concurrent_minimum;
110        self
111    }
112
113    /// Return the experimental partial encoding setting.
114    #[must_use]
115    pub fn experimental_partial_encoding(&self) -> bool {
116        self.experimental_partial_encoding
117    }
118
119    /// Set whether or not to use experimental partial encoding.
120    pub fn set_experimental_partial_encoding(
121        &mut self,
122        experimental_partial_encoding: bool,
123    ) -> &mut Self {
124        self.experimental_partial_encoding = experimental_partial_encoding;
125        self
126    }
127
128    /// Set whether or not to use experimental partial encoding.
129    #[must_use]
130    pub fn with_experimental_partial_encoding(
131        mut self,
132        experimental_partial_encoding: bool,
133    ) -> Self {
134        self.experimental_partial_encoding = experimental_partial_encoding;
135        self
136    }
137}
138
139/// Options for codec metadata.
140#[derive(Debug, Clone, Copy)]
141pub struct CodecMetadataOptions {
142    codec_store_metadata_if_encode_only: bool,
143}
144
145impl Default for CodecMetadataOptions {
146    fn default() -> Self {
147        Self {
148            codec_store_metadata_if_encode_only: true,
149        }
150    }
151}
152
153impl CodecMetadataOptions {
154    /// Return the store metadata if encode only setting.
155    #[must_use]
156    pub fn codec_store_metadata_if_encode_only(&self) -> bool {
157        self.codec_store_metadata_if_encode_only
158    }
159
160    /// Set the store metadata if encode only setting.
161    #[must_use]
162    pub fn with_codec_store_metadata_if_encode_only(mut self, enabled: bool) -> Self {
163        self.codec_store_metadata_if_encode_only = enabled;
164        self
165    }
166
167    /// Set the codec store metadata if encode only setting.
168    pub fn set_codec_store_metadata_if_encode_only(&mut self, enabled: bool) -> &mut Self {
169        self.codec_store_metadata_if_encode_only = enabled;
170        self
171    }
172}