Skip to main content

qualia_core_db/q42/emf_profiles/
document.rs

1//! Shared metadata is admitted once; selected payloads remain independently verified.
2use super::*;
3
4/// A nonforgeable shared admission for the current EMF registry. Opening does not
5/// validate every payload; each chunk call verifies and admits that selected chunk.
6/// The caller may retain immutable returned chunks for hot access without rehashing.
7pub struct ProfileReader<'v, 'a> {
8    view: &'v StructuralView<'a>,
9    admission: semantics::SharedAdmission<'v, 'a>,
10    limits: Limits,
11    payload_work: u64,
12}
13impl<'v, 'a> ProfileReader<'v, 'a> {
14    pub fn open(
15        view: &'v StructuralView<'a>,
16        scratch: &mut Scratch<'_>,
17        limits: Limits,
18        total_payload_work: u64,
19    ) -> Result<Self, Error> {
20        if view.family() != super::super::field_envelope::Family::EmfV4Candidate {
21            return Err(Error::UnsupportedProfile);
22        }
23        let admission = semantics::admit_shared(view, &ProfileRegistry, scratch, limits.shared)
24            .map_err(shared_error)?;
25        Ok(Self {
26            view,
27            admission,
28            limits,
29            payload_work: total_payload_work,
30        })
31    }
32    pub fn entry_count(&self) -> u32 {
33        self.view.entry_count()
34    }
35    pub fn remaining_payload_work(&self) -> u64 {
36        self.payload_work
37    }
38    pub fn remaining_shared_work(&self) -> u64 {
39        self.admission.context.remaining_work()
40    }
41    /// Charge a conservative aggregate budget before hashing/scanning any payload.
42    /// Three units per stored byte cover selected digest/record scan plus a validity
43    /// mask (one byte per record, no larger than the selected raw payload). Per-chunk
44    /// limits and shared graph/reference work limits also remain enforced. Failed
45    /// calls consume their admitted budget rather than permitting unlimited retries.
46    pub fn chunk(&mut self, entry_index: u32) -> Result<ProfileChunk<'a>, Error> {
47        let entry = self.view.entry(entry_index).map_err(Error::Storage)?;
48        let cost = entry.stored_bytes().checked_mul(3).ok_or(Error::Overflow)?;
49        self.payload_work = self.payload_work.checked_sub(cost).ok_or(Error::Limit)?;
50        super::chunk::admit_with_context(
51            self.view,
52            &self.admission.context,
53            entry_index,
54            self.limits,
55        )
56    }
57}