use std::sync::Arc;
use reifydb_core::{
interface::catalog::vtable::VTable,
value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns},
};
use reifydb_transaction::transaction::Transaction;
use reifydb_type::{
fragment::Fragment,
value::{Value, r#type::Type},
};
use crate::{
CatalogStore, Result,
system::SystemCatalog,
vtable::{BaseVTable, Batch, VTableContext},
};
pub struct SystemTables {
pub(crate) vtable: Arc<VTable>,
exhausted: bool,
}
impl Default for SystemTables {
fn default() -> Self {
Self::new()
}
}
impl SystemTables {
pub fn new() -> Self {
Self {
vtable: SystemCatalog::get_system_tables_table().clone(),
exhausted: false,
}
}
}
impl BaseVTable for SystemTables {
fn initialize(&mut self, _txn: &mut Transaction<'_>, _ctx: VTableContext) -> Result<()> {
self.exhausted = false;
Ok(())
}
fn next(&mut self, txn: &mut Transaction<'_>) -> Result<Option<Batch>> {
if self.exhausted {
return Ok(None);
}
let tables: Vec<_> =
CatalogStore::list_tables_all(txn)?.into_iter().filter(|t| !t.underlying).collect();
let mut ids = ColumnBuffer::uint8_with_capacity(tables.len());
let mut namespaces = ColumnBuffer::uint8_with_capacity(tables.len());
let mut names = ColumnBuffer::utf8_with_capacity(tables.len());
let mut primary_keys = ColumnBuffer::uint4_with_capacity(tables.len());
for table in tables {
ids.push(table.id.0);
namespaces.push(table.namespace.0);
names.push(table.name.as_str());
primary_keys.push_value(
table.primary_key
.map(|pk| pk.id.0)
.map(Value::Uint8)
.unwrap_or(Value::none_of(Type::Uint8)),
);
}
let columns = vec![
ColumnWithName::new(Fragment::internal("id"), ids),
ColumnWithName::new(Fragment::internal("namespace_id"), namespaces),
ColumnWithName::new(Fragment::internal("name"), names),
ColumnWithName::new(Fragment::internal("primary_key_id"), primary_keys),
];
self.exhausted = true;
Ok(Some(Batch {
columns: Columns::new(columns),
}))
}
fn vtable(&self) -> &VTable {
&self.vtable
}
}