reifydb_engine/vm/instruction/dml/
shape.rs1use reifydb_catalog::catalog::Catalog;
5use reifydb_codec::row::shape::{RowFamily, RowShape, RowShapeField};
6use reifydb_core::interface::catalog::{queue::Queue, ringbuffer::RingBuffer, series::Series, table::Table};
7use reifydb_transaction::transaction::Transaction;
8use reifydb_value::value::{constraint::TypeConstraint, value_type::ValueType};
9
10use crate::Result;
11
12pub fn get_or_create_table_shape(catalog: &Catalog, table: &Table, txn: &mut Transaction<'_>) -> Result<RowShape> {
13 let mut fields = Vec::with_capacity(table.columns.len());
14
15 for col in &table.columns {
16 let constraint = if let Some(dict_id) = col.dictionary_id {
17 if let Some(dict) = catalog.find_dictionary(txn, dict_id)? {
18 TypeConstraint::dictionary(dict_id, dict.id_type)
19 } else {
20 col.constraint.clone()
21 }
22 } else {
23 col.constraint.clone()
24 };
25
26 fields.push(RowShapeField::new(col.name.clone(), constraint));
27 }
28
29 catalog.get_or_create_row_shape(txn, RowFamily::Table, fields)
30}
31
32pub fn get_or_create_ringbuffer_shape(
33 catalog: &Catalog,
34 ringbuffer: &RingBuffer,
35 txn: &mut Transaction<'_>,
36) -> Result<RowShape> {
37 let mut fields = Vec::with_capacity(ringbuffer.columns.len());
38
39 for col in &ringbuffer.columns {
40 let constraint = if let Some(dict_id) = col.dictionary_id {
41 if let Some(dict) = catalog.find_dictionary(txn, dict_id)? {
42 TypeConstraint::dictionary(dict_id, dict.id_type)
43 } else {
44 col.constraint.clone()
45 }
46 } else {
47 col.constraint.clone()
48 };
49
50 fields.push(RowShapeField::new(col.name.clone(), constraint));
51 }
52
53 catalog.get_or_create_row_shape(txn, RowFamily::RingBuffer, fields)
54}
55
56pub fn get_or_create_queue_shape(catalog: &Catalog, queue: &Queue, txn: &mut Transaction<'_>) -> Result<RowShape> {
57 let mut fields = Vec::with_capacity(queue.columns.len());
58
59 for col in &queue.columns {
60 let constraint = if let Some(dict_id) = col.dictionary_id {
61 if let Some(dict) = catalog.find_dictionary(txn, dict_id)? {
62 TypeConstraint::dictionary(dict_id, dict.id_type)
63 } else {
64 col.constraint.clone()
65 }
66 } else {
67 col.constraint.clone()
68 };
69
70 fields.push(RowShapeField::new(col.name.clone(), constraint));
71 }
72
73 catalog.get_or_create_row_shape(txn, RowFamily::Queue, fields)
74}
75
76pub fn get_or_create_series_shape(catalog: &Catalog, series: &Series, txn: &mut Transaction<'_>) -> Result<RowShape> {
77 let mut fields = Vec::with_capacity(1 + series.columns.len());
78
79 let key_column = series.key.column();
80 let key_col = series.columns.iter().find(|c| c.name == key_column);
81 let key_type =
82 key_col.map(|c| c.constraint.clone()).unwrap_or_else(|| TypeConstraint::unconstrained(ValueType::Int8));
83 fields.push(RowShapeField::new(key_column.to_string(), key_type));
84 for col in series.data_columns() {
85 let constraint = if let Some(dict_id) = col.dictionary_id {
86 if let Some(dict) = catalog.find_dictionary(txn, dict_id)? {
87 TypeConstraint::dictionary(dict_id, dict.id_type)
88 } else {
89 col.constraint.clone()
90 }
91 } else {
92 col.constraint.clone()
93 };
94 fields.push(RowShapeField::new(col.name.clone(), constraint));
95 }
96 catalog.get_or_create_row_shape(txn, RowFamily::Series, fields)
97}