qualia_core_db/bundle/stream/formats/
range.rs1use crate::bundle::stream::{
2 proof::{ProofError, VerifiedLeafSpan},
3 selection::SelectedPlainEntry,
4};
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum FormatError {
7 Bounds,
8 Capacity,
9 Limit,
10 Identity,
11 Unsupported,
12 Geometry,
13 Integrity,
14 Proof(ProofError),
15}
16impl From<ProofError> for FormatError {
17 fn from(e: ProofError) -> Self {
18 Self::Proof(e)
19 }
20}
21#[derive(Clone, Copy)]
24pub struct AuthenticatedRange<'a> {
25 selected: SelectedPlainEntry<'a>,
26 offset: u64,
27 bytes: &'a [u8],
28}
29impl<'a> AuthenticatedRange<'a> {
30 pub fn new(
31 selected: SelectedPlainEntry<'a>,
32 span: &'a VerifiedLeafSpan<'_>,
33 offset: u64,
34 length: usize,
35 ) -> Result<Self, FormatError> {
36 if selected.tree_spec().map_err(|_| FormatError::Identity)? != *span.tree_spec()
37 || selected.root() != span.root()
38 {
39 return Err(FormatError::Identity);
40 }
41 let bytes = span.plaintext_range(offset, length)?;
42 Ok(Self {
43 selected,
44 offset,
45 bytes,
46 })
47 }
48 pub fn bytes(&self) -> &'a [u8] {
49 self.bytes
50 }
51 pub const fn offset(&self) -> u64 {
52 self.offset
53 }
54 pub const fn selected(&self) -> SelectedPlainEntry<'a> {
55 self.selected
56 }
57 pub fn subrange(&self, rel_offset: u64, length: usize) -> Result<Self, FormatError> {
59 let abs = self.offset.checked_add(rel_offset).ok_or(FormatError::Bounds)?;
60 let start = usize::try_from(rel_offset).map_err(|_| FormatError::Bounds)?;
61 let end = start.checked_add(length).ok_or(FormatError::Bounds)?;
62 let bytes = self.bytes.get(start..end).ok_or(FormatError::Bounds)?;
63 Ok(Self {
64 selected: self.selected,
65 offset: abs,
66 bytes,
67 })
68 }
69 pub(super) fn same_entry(&self, other: &Self) -> bool {
70 self.selected.candidate_identity() == other.selected.candidate_identity()
71 && self.selected.entry_id() == other.selected.entry_id()
72 && self.selected.ordinal() == other.selected.ordinal()
73 && self.selected.root() == other.selected.root()
74 && self.selected.logical_length() == other.selected.logical_length()
75 }
76}
77#[derive(Clone, Copy)]
79pub struct FormatLimits {
80 pub max_entry_bytes: u64,
81 pub max_rows: usize,
82 pub max_metadata_bytes: usize,
83 pub max_chunk_bytes: usize,
84}
85#[derive(Clone, Copy, Debug, Default)]
86pub struct PayloadInterval {
87 pub(crate) start: u64,
88 pub(crate) end: u64,
89}
90pub(super) fn extent(start: u64, length: u64, whole: u64) -> Result<u64, FormatError> {
91 let end = start.checked_add(length).ok_or(FormatError::Bounds)?;
92 if end > whole {
93 return Err(FormatError::Bounds);
94 }
95 Ok(end)
96}
97pub(super) fn overlap(a: u64, b: u64, c: u64, d: u64) -> bool {
98 a < d && c < b
99}
100pub(super) fn nonoverlap(rows: &mut [PayloadInterval]) -> Result<(), FormatError> {
101 fn sift(rows: &mut [PayloadInterval], mut root: usize, end: usize) {
103 while root < end / 2 {
104 let mut child = root * 2 + 1;
105 if child + 1 < end && rows[child].start < rows[child + 1].start {
106 child += 1;
107 }
108 if rows[root].start >= rows[child].start {
109 break;
110 }
111 rows.swap(root, child);
112 root = child;
113 }
114 }
115 for root in (0..rows.len() / 2).rev() {
116 sift(rows, root, rows.len());
117 }
118 for end in (1..rows.len()).rev() {
119 rows.swap(0, end);
120 sift(rows, 0, end);
121 }
122 if rows.windows(2).any(|w| w[0].end > w[1].start) {
123 return Err(FormatError::Geometry);
124 }
125 Ok(())
126}