use std::sync::Arc;
use iceberg_rust_spec::spec::{schema::Schema, view_metadata::ViewMetadata};
use object_store::ObjectStore;
use crate::{
catalog::{create::CreateViewBuilder, identifier::Identifier, Catalog},
error::Error,
object_store::Bucket,
};
use self::transaction::Transaction as ViewTransaction;
pub mod transaction;
#[derive(Debug)]
pub struct View {
identifier: Identifier,
metadata: ViewMetadata,
catalog: Arc<dyn Catalog>,
}
impl View {
pub fn builder() -> CreateViewBuilder<Option<()>> {
CreateViewBuilder::default()
}
pub async fn new(
identifier: Identifier,
catalog: Arc<dyn Catalog>,
metadata: ViewMetadata,
) -> Result<Self, Error> {
Ok(View {
identifier,
metadata,
catalog,
})
}
pub fn identifier(&self) -> &Identifier {
&self.identifier
}
pub fn catalog(&self) -> Arc<dyn Catalog> {
self.catalog.clone()
}
pub fn object_store(&self) -> Arc<dyn ObjectStore> {
self.catalog
.object_store(Bucket::from_path(&self.metadata.location).unwrap())
}
pub fn current_schema(&self, branch: Option<&str>) -> Result<&Schema, Error> {
self.metadata.current_schema(branch).map_err(Error::from)
}
pub fn metadata(&self) -> &ViewMetadata {
&self.metadata
}
pub fn new_transaction(&mut self, branch: Option<&str>) -> ViewTransaction {
ViewTransaction::new(self, branch)
}
}