Skip to main content

supertable_core/
table.rs

1//! # SuperTable High-Level Table API
2//!
3//! This module provides the `Table` struct, which is the primary entry point
4//! for interacting with SuperTable. It combines metadata management, storage access,
5//! and transactional consistency.
6
7use crate::catalog::{Catalog, TableIdentifier};
8use crate::metadata::TableMetadata;
9use crate::storage::Storage;
10use crate::transaction::Transaction;
11use std::sync::Arc;
12
13/// A high-level representation of a SuperTable.
14#[allow(unused)]
15#[derive(Clone)]
16pub struct Table {
17    pub(crate) identifier: String,
18    pub(crate) catalog: Arc<dyn Catalog>,
19    pub(crate) metadata: TableMetadata,
20    pub(crate) storage: Storage,
21}
22
23impl std::fmt::Debug for Table {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        f.debug_struct("Table")
26            .field("identifier", &self.identifier)
27            .field("metadata", &self.metadata)
28            .finish()
29    }
30}
31
32impl Table {
33    /// Creates a new table instance.
34    pub fn new(
35        identifier: impl Into<String>,
36        catalog: Arc<dyn Catalog>,
37        metadata: TableMetadata,
38        storage: Storage,
39    ) -> Self {
40        Self {
41            identifier: identifier.into(),
42            catalog,
43            metadata,
44            storage,
45        }
46    }
47
48    /// Returns the table metadata.
49    pub fn metadata(&self) -> &TableMetadata {
50        &self.metadata
51    }
52
53    /// Returns the table storage.
54    pub fn storage(&self) -> &Storage {
55        &self.storage
56    }
57
58    /// Returns the table identifier.
59    pub fn identifier(&self) -> &str {
60        &self.identifier
61    }
62
63    /// Starts a new transaction on this table.
64    pub fn new_transaction(&self) -> Transaction {
65        Transaction::new(self.metadata.clone())
66    }
67
68    /// Returns a view of the table at a specific snapshot.
69    pub fn at_snapshot(&self, snapshot_id: i64) -> anyhow::Result<Table> {
70        let snapshot = self
71            .metadata
72            .snapshot(snapshot_id)
73            .ok_or_else(|| anyhow::anyhow!("Snapshot {} not found", snapshot_id))?;
74
75        let mut metadata = self.metadata.clone();
76        metadata.current_snapshot_id = Some(snapshot.snapshot_id);
77
78        Ok(Table::new(
79            self.identifier.clone(),
80            self.catalog.clone(),
81            metadata,
82            self.storage.clone(),
83        ))
84    }
85
86    /// Returns a view of the table as of a specific timestamp.
87    pub fn as_of(&self, timestamp_ms: i64) -> anyhow::Result<Table> {
88        let snapshot = self
89            .metadata
90            .snapshot_at(timestamp_ms)
91            .ok_or_else(|| anyhow::anyhow!("No snapshot found at or before {}", timestamp_ms))?;
92
93        self.at_snapshot(snapshot.snapshot_id)
94    }
95
96    /// Refreshes the table metadata from the catalog.
97    pub async fn refresh(&mut self) -> anyhow::Result<()> {
98        let identifier = TableIdentifier::parse(&self.identifier);
99        let new_metadata = self.catalog.load_table(&identifier).await?;
100        self.metadata = new_metadata;
101        Ok(())
102    }
103
104    /// Creates a new scan planner for this table's current snapshot.
105    pub fn new_scan(&self) -> crate::scan::ScanPlanner<'_> {
106        let snapshot = self
107            .metadata
108            .current_snapshot()
109            .expect("Table must have a snapshot to scan");
110        crate::scan::ScanPlanner::new(snapshot, &self.storage)
111    }
112
113    // TODO: Implement high-level refresh(), commit(), etc.
114}