use crate::catalog::{Catalog, TableIdentifier};
use crate::metadata::TableMetadata;
use crate::storage::Storage;
use crate::transaction::Transaction;
use std::sync::Arc;
#[allow(unused)]
#[derive(Clone)]
pub struct Table {
pub(crate) identifier: String,
pub(crate) catalog: Arc<dyn Catalog>,
pub(crate) metadata: TableMetadata,
pub(crate) storage: Storage,
}
impl std::fmt::Debug for Table {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Table")
.field("identifier", &self.identifier)
.field("metadata", &self.metadata)
.finish()
}
}
impl Table {
pub fn new(
identifier: impl Into<String>,
catalog: Arc<dyn Catalog>,
metadata: TableMetadata,
storage: Storage,
) -> Self {
Self {
identifier: identifier.into(),
catalog,
metadata,
storage,
}
}
pub fn metadata(&self) -> &TableMetadata {
&self.metadata
}
pub fn storage(&self) -> &Storage {
&self.storage
}
pub fn identifier(&self) -> &str {
&self.identifier
}
pub fn new_transaction(&self) -> Transaction {
Transaction::new(self.metadata.clone())
}
pub fn at_snapshot(&self, snapshot_id: i64) -> anyhow::Result<Table> {
let snapshot = self
.metadata
.snapshot(snapshot_id)
.ok_or_else(|| anyhow::anyhow!("Snapshot {} not found", snapshot_id))?;
let mut metadata = self.metadata.clone();
metadata.current_snapshot_id = Some(snapshot.snapshot_id);
Ok(Table::new(
self.identifier.clone(),
self.catalog.clone(),
metadata,
self.storage.clone(),
))
}
pub fn as_of(&self, timestamp_ms: i64) -> anyhow::Result<Table> {
let snapshot = self
.metadata
.snapshot_at(timestamp_ms)
.ok_or_else(|| anyhow::anyhow!("No snapshot found at or before {}", timestamp_ms))?;
self.at_snapshot(snapshot.snapshot_id)
}
pub async fn refresh(&mut self) -> anyhow::Result<()> {
let identifier = TableIdentifier::parse(&self.identifier);
let new_metadata = self.catalog.load_table(&identifier).await?;
self.metadata = new_metadata;
Ok(())
}
pub fn new_scan(&self) -> crate::scan::ScanPlanner<'_> {
let snapshot = self
.metadata
.current_snapshot()
.expect("Table must have a snapshot to scan");
crate::scan::ScanPlanner::new(snapshot, &self.storage)
}
}