Skip to main content

qualia_core_db/q42/asset_import/
budgets.rs

1//! Byte / record / chunk budgets for bounded import jobs.
2
3use super::error::ImportError;
4use crate::q42::asset_envelope::SENTINEL_PASS_BUDGET_BYTES;
5
6/// Cold-construction budgets for one import job.
7///
8/// `chunk_byte_budget` is the per-`feed_chunk` pass ceiling and must stay within
9/// [`SENTINEL_PASS_BUDGET_BYTES`] (42 MiB). `max_bytes` / `max_records` bound the
10/// whole job across chunks.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct ImportBudgets {
13    pub max_bytes: u64,
14    pub max_records: u64,
15    pub chunk_byte_budget: u64,
16}
17
18impl ImportBudgets {
19    /// Single-pass job capped at the Sentinel budget.
20    pub fn sentinel_pass() -> Self {
21        Self {
22            max_bytes: SENTINEL_PASS_BUDGET_BYTES,
23            max_records: u64::MAX / 4,
24            chunk_byte_budget: SENTINEL_PASS_BUDGET_BYTES,
25        }
26    }
27
28    /// Validate positivity and the 42 MiB chunk pass ceiling.
29    pub fn validate(self) -> Result<Self, ImportError> {
30        if self.max_bytes == 0 || self.max_records == 0 || self.chunk_byte_budget == 0 {
31            return Err(ImportError::InvalidBudgets);
32        }
33        if self.chunk_byte_budget > SENTINEL_PASS_BUDGET_BYTES {
34            return Err(ImportError::ChunkBudgetExceeded);
35        }
36        Ok(self)
37    }
38}