1use crate::catalog::{Catalog, TableIdentifier};
8use crate::metadata::TableMetadata;
9use crate::storage::Storage;
10use crate::transaction::Transaction;
11use std::sync::Arc;
12
13#[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 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 pub fn metadata(&self) -> &TableMetadata {
50 &self.metadata
51 }
52
53 pub fn storage(&self) -> &Storage {
55 &self.storage
56 }
57
58 pub fn identifier(&self) -> &str {
60 &self.identifier
61 }
62
63 pub fn new_transaction(&self) -> Transaction {
65 Transaction::new(self.metadata.clone())
66 }
67
68 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 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 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 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 }