1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
use super::{Metric, TimedEvent}; use crate::io::codec::vectorize; use crate::io::provider::{ColId, RowId, StreamType}; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; #[derive(Debug)] pub struct TableMetric; impl Metric for TableMetric { type State = TableState; type Event = TableEvent; fn stream_type() -> StreamType { StreamType::from("rillrate.table.v0") } fn apply(state: &mut Self::State, event: TimedEvent<Self::Event>) { match event.event { TableEvent::AddCol { col, alias } => { let record = ColRecord { alias }; state.columns.insert(col, record); } TableEvent::DelCol { col } => { state.columns.remove(&col); for (_row, record) in state.rows.iter_mut() { record.cols.remove(&col); } } TableEvent::AddRow { row, alias } => { let record = RowRecord { alias, cols: BTreeMap::new(), }; state.rows.insert(row, record); } TableEvent::DelRow { row } => { state.rows.remove(&row); } TableEvent::SetCell { row, col, value } => { if let Some(record) = state.rows.get_mut(&row) { if state.columns.contains_key(&col) { record.cols.insert(col, value); } } } } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct TableState { #[serde(with = "vectorize")] pub columns: BTreeMap<ColId, ColRecord>, #[serde(with = "vectorize")] pub rows: BTreeMap<RowId, RowRecord>, } #[allow(clippy::new_without_default)] impl TableState { pub fn new() -> Self { Self { columns: BTreeMap::new(), rows: BTreeMap::new(), } } } pub type TableDelta = Vec<TimedEvent<TableEvent>>; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum TableEvent { AddCol { col: ColId, alias: Option<String>, }, DelCol { col: ColId, }, AddRow { row: RowId, alias: Option<String>, }, DelRow { row: RowId, }, SetCell { row: RowId, col: ColId, value: String, }, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ColRecord { pub alias: Option<String>, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RowRecord { pub alias: Option<String>, #[serde(with = "vectorize")] pub cols: BTreeMap<ColId, String>, }