Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/nornir

crates/mimir-driver/tests/fixture-tables/iceberg/src/lib.rs

//! A stand-in for the storage engine: the SHAPES `mimir-driver` anchors on
//! (`TableIdent`, `Catalog`, `Table`, `Transaction`, `TableCreation`), with no
//! storage behind them.

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamespaceIdent(pub Vec<String>);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableIdent {
    pub namespace: NamespaceIdent,
    pub name: String,
}

impl TableIdent {
    pub fn new(namespace: NamespaceIdent, name: String) -> Self {
        Self { namespace, name }
    }
    pub fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Debug, Clone)]
pub struct Table {
    ident: TableIdent,
}

pub struct TableScanBuilder<'a> {
    pub table: &'a Table,
}

impl<'a> TableScanBuilder<'a> {
    pub fn rows(self) -> Vec<String> {
        vec![self.table.ident.name.clone()]
    }
}

impl Table {
    pub fn identifier(&self) -> &TableIdent {
        &self.ident
    }
    pub fn scan(&self) -> TableScanBuilder<'_> {
        TableScanBuilder { table: self }
    }
    /// Metadata only: NOT a read of the table's rows.
    pub fn location(&self) -> String {
        format!("mem://{}", self.ident.name)
    }
}

pub struct TableCreation {
    pub name: String,
}

pub struct TableCreationBuilder {
    name: Option<String>,
}

impl TableCreation {
    pub fn builder() -> TableCreationBuilder {
        TableCreationBuilder { name: None }
    }
}

impl TableCreationBuilder {
    pub fn name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }
    pub fn build(self) -> TableCreation {
        TableCreation { name: self.name.unwrap_or_default() }
    }
}

#[derive(Debug)]
pub struct Error;
pub type Result<T> = std::result::Result<T, Error>;

pub trait Catalog {
    fn load_table(&self, table: &TableIdent) -> Result<Table>;
    fn create_table(&self, namespace: &NamespaceIdent, creation: TableCreation) -> Result<Table>;
    fn drop_table(&self, table: &TableIdent) -> Result<()>;
    fn table_exists(&self, table: &TableIdent) -> Result<bool>;
    fn list_tables(&self, namespace: &NamespaceIdent) -> Result<Vec<TableIdent>>;
}

pub struct MemoryCatalog;

impl Catalog for MemoryCatalog {
    fn load_table(&self, table: &TableIdent) -> Result<Table> {
        Ok(Table { ident: table.clone() })
    }
    fn create_table(&self, namespace: &NamespaceIdent, creation: TableCreation) -> Result<Table> {
        Ok(Table { ident: TableIdent::new(namespace.clone(), creation.name) })
    }
    fn drop_table(&self, _table: &TableIdent) -> Result<()> {
        Ok(())
    }
    fn table_exists(&self, _table: &TableIdent) -> Result<bool> {
        Ok(true)
    }
    fn list_tables(&self, _namespace: &NamespaceIdent) -> Result<Vec<TableIdent>> {
        Ok(Vec::new())
    }
}

pub struct Transaction {
    table: Table,
    staged: Vec<String>,
}

impl Transaction {
    pub fn new(table: &Table) -> Self {
        Self { table: table.clone(), staged: Vec::new() }
    }
    pub fn fast_append(mut self, rows: &[String]) -> Self {
        self.staged.extend_from_slice(rows);
        self
    }
    pub fn commit(self, _catalog: &dyn Catalog) -> Result<Table> {
        Ok(self.table)
    }
}