pub trait SysRowSink {
fn begin_row(&mut self, pk: &[u128], weight: i64);
fn put_u64(&mut self, v: u64);
fn put_string(&mut self, s: &str);
fn put_bytes(&mut self, b: &[u8]);
fn end_row(&mut self);
}
pub trait SysRow {
const FAMILY: u64;
fn write(&self, sink: &mut impl SysRowSink, weight: i64);
}
pub struct SchemaTabRow<'a> {
pub schema_id: u64,
pub name: &'a str,
}
impl SysRow for SchemaTabRow<'_> {
const FAMILY: u64 = crate::SCHEMA_TAB;
fn write(&self, sink: &mut impl SysRowSink, weight: i64) {
sink.begin_row(&[self.schema_id as u128], weight);
sink.put_string(self.name);
sink.end_row();
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FkRef {
pub table_id: u64,
pub col: u32,
}
pub struct ColTabRow<'a> {
pub owner_id: u64,
pub col_idx: u64,
pub col: &'a crate::ColumnDef,
pub fk: Option<FkRef>,
}
impl SysRow for ColTabRow<'_> {
const FAMILY: u64 = crate::COL_TAB;
fn write(&self, sink: &mut impl SysRowSink, weight: i64) {
let (fk_table_id, fk_col_idx) = self.fk.map_or((0, 0), |fk| (fk.table_id, fk.col as u64));
sink.begin_row(&[self.owner_id as u128, self.col_idx as u128], weight);
sink.put_string(&self.col.name);
sink.put_u64(self.col.ty.tc.as_wire() as u64);
sink.put_u64(self.col.is_nullable as u64);
sink.put_u64(fk_table_id);
sink.put_u64(fk_col_idx);
sink.put_u64(self.col.is_hidden as u64);
sink.put_u64(self.col.ty.scale as u64);
sink.end_row();
}
}
pub struct TableTabRow<'a> {
pub table_id: u64,
pub schema_id: u64,
pub name: &'a str,
pub pk: crate::PkColList,
pub props: crate::TableProps,
}
impl SysRow for TableTabRow<'_> {
const FAMILY: u64 = crate::TABLE_TAB;
fn write(&self, sink: &mut impl SysRowSink, weight: i64) {
sink.begin_row(&[self.table_id as u128], weight);
sink.put_u64(self.schema_id);
sink.put_string(self.name);
sink.put_u64(self.pk.pack());
sink.put_u64(self.props.pack());
sink.end_row();
}
}
pub struct ViewTabRow<'a> {
pub view_id: u64,
pub schema_id: u64,
pub name: &'a str,
pub pk: crate::PkColList,
pub props: crate::ViewProps,
pub owner_view_id: u64,
pub pk_repeats: bool,
}
impl SysRow for ViewTabRow<'_> {
const FAMILY: u64 = crate::VIEW_TAB;
fn write(&self, sink: &mut impl SysRowSink, weight: i64) {
sink.begin_row(&[self.view_id as u128], weight);
sink.put_u64(self.schema_id);
sink.put_string(self.name);
sink.put_u64(self.pk.pack());
let (capacity, delta) = self.props.row_words();
sink.put_u64(capacity);
sink.put_u64(delta);
sink.put_u64(self.owner_view_id);
sink.put_u64(self.pk_repeats as u64);
sink.end_row();
}
}
pub struct IdxTabRow<'a> {
pub index_id: u64,
pub owner_id: u64,
pub cols: crate::PkColList,
pub name: &'a str,
pub is_unique: bool,
}
impl SysRow for IdxTabRow<'_> {
const FAMILY: u64 = crate::IDX_TAB;
fn write(&self, sink: &mut impl SysRowSink, weight: i64) {
sink.begin_row(&[self.index_id as u128], weight);
sink.put_u64(self.owner_id);
sink.put_u64(self.cols.pack());
sink.put_string(self.name);
sink.put_u64(self.is_unique as u64);
sink.end_row();
}
}
pub struct CircuitRow<'a> {
pub view_id: u64,
pub circuit: &'a crate::Circuit,
}
impl SysRow for CircuitRow<'_> {
const FAMILY: u64 = crate::CIRCUIT_TAB;
fn write(&self, sink: &mut impl SysRowSink, weight: i64) {
sink.begin_row(&[self.view_id as u128], weight);
sink.put_bytes(&self.circuit.encode());
sink.end_row();
}
}
#[cfg(test)]
#[path = "tests/sys_rows.rs"]
mod tests;