Skip to main content

cobble_table/catalog/
model.rs

1use super::file_catalog::CatalogRuntimeContext;
2use crate::TableSchema;
3use serde::{Deserialize, Serialize};
4use std::fmt::{Display, Formatter};
5use std::sync::Arc;
6
7/// Stable identity of a table, independent of its catalog name.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct TableId(u32);
11
12impl TableId {
13    pub(crate) fn new(value: u32) -> Self {
14        Self(value)
15    }
16
17    pub fn as_u32(self) -> u32 {
18        self.0
19    }
20}
21
22impl Display for TableId {
23    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
24        Display::fmt(&self.0, formatter)
25    }
26}
27
28/// Per-table catalog schema identity, starting at zero and increasing monotonically.
29#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
30#[serde(transparent)]
31pub struct CatalogSchemaId(u32);
32
33impl CatalogSchemaId {
34    pub(crate) const INITIAL: Self = Self(0);
35
36    pub(crate) fn next(self) -> Option<Self> {
37        self.0.checked_add(1).map(Self)
38    }
39
40    pub fn as_u32(self) -> u32 {
41        self.0
42    }
43}
44
45impl From<u32> for CatalogSchemaId {
46    fn from(value: u32) -> Self {
47        Self(value)
48    }
49}
50
51impl Display for CatalogSchemaId {
52    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
53        Display::fmt(&self.0, formatter)
54    }
55}
56
57/// A semantic table name with an extensible, multi-component namespace.
58#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
59pub struct TableIdentifier {
60    namespace: Vec<String>,
61    name: String,
62}
63
64impl TableIdentifier {
65    pub fn new<I, S>(namespace: I, name: impl Into<String>) -> Self
66    where
67        I: IntoIterator<Item = S>,
68        S: Into<String>,
69    {
70        Self {
71            namespace: namespace.into_iter().map(Into::into).collect(),
72            name: name.into(),
73        }
74    }
75
76    pub fn namespace(&self) -> &[String] {
77        &self.namespace
78    }
79
80    pub fn name(&self) -> &str {
81        &self.name
82    }
83
84    pub(crate) fn renamed(&self, name: String) -> Self {
85        Self {
86            namespace: self.namespace.clone(),
87            name,
88        }
89    }
90}
91
92/// Semantic table descriptor returned by a catalog.
93#[derive(Clone)]
94pub struct CatalogTable {
95    pub(crate) identifier: TableIdentifier,
96    pub(crate) table_id: TableId,
97    pub(crate) catalog_schema_id: CatalogSchemaId,
98    pub(crate) schema: TableSchema,
99    pub(crate) runtime_context: Arc<CatalogRuntimeContext>,
100}
101
102impl std::fmt::Debug for CatalogTable {
103    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
104        formatter
105            .debug_struct("CatalogTable")
106            .field("identifier", &self.identifier)
107            .field("table_id", &self.table_id)
108            .field("catalog_schema_id", &self.catalog_schema_id)
109            .field("schema", &self.schema)
110            .finish()
111    }
112}
113
114impl CatalogTable {
115    pub fn identifier(&self) -> &TableIdentifier {
116        &self.identifier
117    }
118
119    pub fn table_id(&self) -> TableId {
120        self.table_id
121    }
122
123    pub fn catalog_schema_id(&self) -> CatalogSchemaId {
124        self.catalog_schema_id
125    }
126
127    pub fn schema(&self) -> &TableSchema {
128        &self.schema
129    }
130}