Skip to main content

xml_arrow/reader/
options.rs

1const DEFAULT_BATCH_SIZE: usize = 1024;
2const DEFAULT_MAX_RECORD_BYTES: usize = 8 * 1024 * 1024;
3const DEFAULT_MAX_BATCH_BYTES: usize = 64 * 1024 * 1024;
4const DEFAULT_MAX_DEPTH: usize = 128;
5
6/// Batch size, projection, and XML limits.
7#[derive(Clone, Debug)]
8pub struct ReaderOptions {
9    /// Maximum rows returned per batch. Default: 1024.
10    pub batch_size: usize,
11    /// Maximum record bytes, including inherited namespaces. Default: 8 MiB.
12    /// Also caps input events. Records are checked after writing each event;
13    /// temporary buffers may exceed this limit.
14    pub max_record_bytes: usize,
15    /// Maximum sum of serialized record bytes per batch. Default: 64 MiB.
16    /// Must be at least `max_record_bytes`; excludes Arrow allocations.
17    pub max_batch_bytes: usize,
18    /// Maximum element depth (`1..65535`). Default: 128.
19    pub max_depth: usize,
20    /// Output columns in order; `None` selects all, `Some(vec![])` only counts rows.
21    pub projection: Option<Vec<usize>>,
22}
23
24impl Default for ReaderOptions {
25    fn default() -> Self {
26        Self {
27            batch_size: DEFAULT_BATCH_SIZE,
28            max_record_bytes: DEFAULT_MAX_RECORD_BYTES,
29            max_batch_bytes: DEFAULT_MAX_BATCH_BYTES,
30            max_depth: DEFAULT_MAX_DEPTH,
31            projection: None,
32        }
33    }
34}
35
36const DEFAULT_MAX_SAMPLE_RECORDS: usize = 128;
37const DEFAULT_MAX_SAMPLE_BYTES: usize = 8 * 1024 * 1024;
38
39/// Bounds the records retained for schema inference.
40#[derive(Clone, Debug)]
41pub struct InferenceOptions {
42    /// Maximum sampled records. Default: 128.
43    pub max_records: usize,
44    /// Maximum sampled serialized XML bytes. Default: 8 MiB.
45    /// One additional record may be read ahead, bounded by `max_record_bytes`.
46    pub max_bytes: usize,
47}
48
49impl Default for InferenceOptions {
50    fn default() -> Self {
51        Self {
52            max_records: DEFAULT_MAX_SAMPLE_RECORDS,
53            max_bytes: DEFAULT_MAX_SAMPLE_BYTES,
54        }
55    }
56}
57
58impl ReaderOptions {
59    /// Validates limits, the record path and projection without reading input.
60    pub fn validate(
61        &self,
62        schema: &crate::XmlSchema,
63        record_path: &str,
64    ) -> super::ReaderResult<()> {
65        super::validation::validate_reader(schema, record_path, self)?;
66        if let Some(indices) = &self.projection {
67            schema
68                .validate_projection(indices)
69                .map_err(super::ReadError::configuration)?;
70        }
71        Ok(())
72    }
73}