use arrow_array::RecordBatch;
use crate::Result;
pub struct Sealed {
pub tables: Vec<(&'static str, RecordBatch)>,
pub sidecars: Vec<(&'static str, Vec<u8>)>,
pub min_ts: i64,
pub max_ts: i64,
pub num_rows: usize,
}
impl Sealed {
pub fn new(
num_rows: usize,
tables: Vec<(&'static str, RecordBatch)>,
min_ts: i64,
max_ts: i64,
) -> Sealed {
Sealed::with(Sidecars::Build, num_rows, tables, min_ts, max_ts)
}
pub fn with(
sidecars: Sidecars,
num_rows: usize,
tables: Vec<(&'static str, RecordBatch)>,
min_ts: i64,
max_ts: i64,
) -> Sealed {
let mut built = Vec::new();
if sidecars == Sidecars::Build {
if let Some(b) = crate::attrs::index(&tables) {
built.push((crate::bloom::ATTR_IDX, b));
}
if let Some(b) = crate::zone::index(&tables) {
built.push((crate::zone::ZONE_IDX, b));
}
}
let sidecars = built;
Sealed {
num_rows,
tables,
sidecars,
min_ts: if min_ts == i64::MAX { 0 } else { min_ts.max(0) },
max_ts: if max_ts == i64::MIN { 0 } else { max_ts.max(0) },
}
}
pub fn with_sidecar(mut self, name: &'static str, bytes: Option<Vec<u8>>) -> Sealed {
if let Some(b) = bytes {
self.sidecars.push((name, b));
}
self
}
pub fn table(&self, name: &str) -> Option<&RecordBatch> {
self.tables.iter().find(|(n, _)| *n == name).map(|(_, b)| b)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Sidecars {
Build,
Skip,
}
pub struct Open {
pub node: u32,
pub seq: u64,
pub sealed: Sealed,
}
pub trait SignalBuilder: Default + Send + 'static {
type Request: Send + 'static;
const SIGNAL: &'static str;
fn has_headroom_for(&self, req: &Self::Request) -> bool;
fn append_request(&mut self, req: &Self::Request) -> Result<usize>;
fn approx_bytes(&self) -> usize;
fn is_empty(&self) -> bool;
fn finish(&mut self) -> Result<Sealed>;
fn snapshot(&self) -> Result<Sealed>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_sealed_range_is_never_negative() {
let s = Sealed::new(1, vec![], -1, -1);
assert_eq!((s.min_ts, s.max_ts), (0, 0));
let s = Sealed::new(1, vec![], i64::MIN, 5_000);
assert_eq!((s.min_ts, s.max_ts), (0, 5_000));
let s = Sealed::new(0, vec![], i64::MAX, i64::MIN);
assert_eq!((s.min_ts, s.max_ts), (0, 0));
assert!(s.sidecars.is_empty(), "no attribute table, no filter");
}
}