nodedb_columnar/memtable/
core.rs1use nodedb_types::columnar::{ColumnDef, ColumnType, ColumnarSchema};
6
7use super::column_data::ColumnData;
8
9pub const DEFAULT_FLUSH_THRESHOLD: usize = 65_536;
11
12pub struct ColumnarMemtable {
17 pub(super) schema: ColumnarSchema,
18 pub(super) columns: Vec<ColumnData>,
19 pub(super) row_count: usize,
20 pub(super) flush_threshold: usize,
21}
22
23impl ColumnarMemtable {
24 pub fn new(schema: &ColumnarSchema) -> Self {
26 Self::with_threshold(schema, DEFAULT_FLUSH_THRESHOLD)
27 }
28
29 pub fn with_threshold(schema: &ColumnarSchema, flush_threshold: usize) -> Self {
31 let columns = schema
32 .columns
33 .iter()
34 .map(|col| ColumnData::new(&col.column_type, col.nullable))
35 .collect();
36 Self {
37 schema: schema.clone(),
38 columns,
39 row_count: 0,
40 flush_threshold,
41 }
42 }
43
44 pub fn row_count(&self) -> usize {
46 self.row_count
47 }
48
49 pub fn should_flush(&self) -> bool {
51 self.row_count >= self.flush_threshold
52 }
53
54 pub fn flush_threshold(&self) -> usize {
56 self.flush_threshold
57 }
58
59 pub fn is_empty(&self) -> bool {
61 self.row_count == 0
62 }
63
64 pub fn schema(&self) -> &ColumnarSchema {
66 &self.schema
67 }
68
69 pub fn columns(&self) -> &[ColumnData] {
71 &self.columns
72 }
73
74 pub(crate) fn from_raw_columns(
82 schema: &ColumnarSchema,
83 columns: Vec<ColumnData>,
84 row_count: usize,
85 ) -> Self {
86 Self {
87 schema: schema.clone(),
88 columns,
89 row_count,
90 flush_threshold: DEFAULT_FLUSH_THRESHOLD,
91 }
92 }
93
94 pub fn add_column(&mut self, name: String, column_type: ColumnType, nullable: bool) {
96 if self.schema.columns.iter().any(|c| c.name == name) {
97 return;
98 }
99
100 let existing_rows = self.row_count;
101 let mut col = ColumnData::new(&column_type, nullable);
102 if existing_rows > 0 {
103 col.backfill_nulls(existing_rows);
104 }
105
106 self.columns.push(col);
107 let col_def = if nullable {
108 ColumnDef::nullable(name, column_type)
109 } else {
110 ColumnDef::required(name, column_type)
111 };
112 self.schema.columns.push(col_def);
113 }
114}