Diffinery

homeflare/celldpublic

celld/crates/ltx/src/bundle.rs
132 lines · 4.2 KBRaw History
1//! The bundle envelope: a container of verbatim L0 LTX files, never a
2//! format.
3//!
4//! celld-original, not a Litestream port. Layout: the concatenated raw L0
5//! bytes, a binary footer of rows, the footer length as a little-endian
6//! u32, and a four-byte magic. A row is `u16 cell_len | cell utf-8 |
7//! u64 cell_epoch | u64 txid | u64 offset | u64 len`, all little-endian.
8//! Un-bundling is arithmetic. The inner bytes are exactly what the
9//! per-cell writer produces, so byte-level compatibility stays checkable.
10//! At rest, the bucket is pure Litestream. A drained bundle leaves no trace
11//! that this module ever existed. Nothing else in this crate depends on bundles;
12//! deleting this file returns the crate to its upstream shape.
13
14use crate::error::{Error, Result};
15use crate::TXID;
16
17const MAGIC: &[u8; 4] = b"CLB1";
18
19fn malformed(what: &str) -> Error {
20 Error::Other(format!("bundle: {what}").into())
21}
22
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct BundleRow {
25 pub cell: String,
26 pub cell_epoch: u64,
27 pub txid: u64,
28 pub offset: u64,
29 pub len: u64,
30}
31
32impl BundleRow {
33 pub fn txid(&self) -> TXID {
34 TXID(self.txid)
35 }
36}
37
38/// One captured L0 segment headed for a bundle.
39pub struct BundleEntry {
40 pub cell: String,
41 pub cell_epoch: u64,
42 pub txid: u64,
43 pub bytes: Vec<u8>,
44}
45
46pub fn encode(entries: &[BundleEntry]) -> Result<Vec<u8>> {
47 let mut out = Vec::new();
48 let mut footer = Vec::new();
49 for entry in entries {
50 let cell = entry.cell.as_bytes();
51 let cell_len: u16 = cell
52 .len()
53 .try_into()
54 .map_err(|_| malformed("cell name too long"))?;
55 footer.extend_from_slice(&cell_len.to_le_bytes());
56 footer.extend_from_slice(cell);
57 for value in [
58 entry.cell_epoch,
59 entry.txid,
60 out.len() as u64,
61 entry.bytes.len() as u64,
62 ] {
63 footer.extend_from_slice(&value.to_le_bytes());
64 }
65 out.extend_from_slice(&entry.bytes);
66 }
67 let len: u32 = footer
68 .len()
69 .try_into()
70 .map_err(|_| malformed("footer too large"))?;
71 out.extend_from_slice(&footer);
72 out.extend_from_slice(&len.to_le_bytes());
73 out.extend_from_slice(MAGIC);
74 Ok(out)
75}
76
77/// The rows, from a complete bundle object.
78pub fn decode_rows(bundle: &[u8]) -> Result<Vec<BundleRow>> {
79 if bundle.len() < 8 {
80 return Err(malformed("shorter than its trailer"));
81 }
82 let (rest, magic) = bundle.split_at(bundle.len() - 4);
83 if magic != MAGIC {
84 return Err(malformed("bad magic"));
85 }
86 let (rest, len_bytes) = rest.split_at(rest.len() - 4);
87 let len = u32::from_le_bytes(len_bytes.try_into().unwrap()) as usize;
88 if rest.len() < len {
89 return Err(malformed("footer overruns the object"));
90 }
91 let mut footer = &rest[rest.len() - len..];
92 let mut rows = Vec::new();
93 while !footer.is_empty() {
94 if footer.len() < 2 {
95 return Err(malformed("truncated row"));
96 }
97 let cell_len = u16::from_le_bytes(footer[..2].try_into().unwrap()) as usize;
98 footer = &footer[2..];
99 if footer.len() < cell_len + 32 {
100 return Err(malformed("truncated row"));
101 }
102 let cell = std::str::from_utf8(&footer[..cell_len])
103 .map_err(|_| malformed("cell name not utf-8"))?
104 .to_string();
105 footer = &footer[cell_len..];
106 let mut values = [0_u64; 4];
107 for value in &mut values {
108 *value = u64::from_le_bytes(footer[..8].try_into().unwrap());
109 footer = &footer[8..];
110 }
111 rows.push(BundleRow {
112 cell,
113 cell_epoch: values[0],
114 txid: values[1],
115 offset: values[2],
116 len: values[3],
117 });
118 }
119 Ok(rows)
120}
121
122/// One row's verbatim L0 bytes out of a complete bundle object.
123pub fn slice<'a>(bundle: &'a [u8], row: &BundleRow) -> Result<&'a [u8]> {
124 let start = row.offset as usize;
125 let end = start
126 .checked_add(row.len as usize)
127 .ok_or_else(|| malformed("row overflows"))?;
128 if end > bundle.len() {
129 return Err(malformed("row overruns the object"));
130 }
131 Ok(&bundle[start..end])
132}