Skip to content

Commit

Permalink
sql: move Catalog trait to engine module
Browse files Browse the repository at this point in the history
Also renames `scan_tables()` to `list_tables()` and returns a vec.
  • Loading branch information
erikgrinaker committed Jun 17, 2024
1 parent fc5e3d8 commit 1c7aeac
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 74 deletions.
8 changes: 5 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::encoding::{self, Value as _};
use crate::error::{Error, Result};
use crate::raft;
use crate::sql;
use crate::sql::engine::{Engine as _, StatementResult};
use crate::sql::types::schema::{Catalog as _, Table};
use crate::sql::engine::{Catalog as _, Engine as _, StatementResult};
use crate::sql::types::schema::Table;
use crate::sql::types::Row;
use crate::storage;

Expand Down Expand Up @@ -293,7 +293,9 @@ impl Server {
.with_txn_read_only(|txn| txn.must_read_table(&table))
.map(Response::GetTable),
Request::ListTables => session
.with_txn_read_only(|txn| Ok(txn.scan_tables()?.map(|t| t.name).collect()))
.with_txn_read_only(|txn| {
Ok(txn.list_tables()?.into_iter().map(|t| t.name).collect())
})
.map(Response::ListTables),
Request::Status => session
.status()
Expand Down
40 changes: 39 additions & 1 deletion src/sql/engine/engine.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::module_inception)]

use super::Session;
use crate::errinput;
use crate::error::Result;
use crate::sql::types::schema::Catalog;
use crate::sql::types::schema::Table;
use crate::sql::types::{Expression, Row, Value};

use std::collections::HashSet;
Expand Down Expand Up @@ -64,3 +65,40 @@ pub type Scan = Box<dyn DoubleEndedIterator<Item = Result<Row>> + Send>;

/// An index scan iterator
pub type IndexScan = Box<dyn DoubleEndedIterator<Item = Result<(Value, HashSet<Value>)>> + Send>;

/// The catalog stores schema information
pub trait Catalog {
/// Creates a new table
fn create_table(&mut self, table: Table) -> Result<()>;
/// Deletes an existing table, or errors if it does not exist
fn delete_table(&mut self, table: &str) -> Result<()>;
/// Reads a table, if it exists
fn read_table(&self, table: &str) -> Result<Option<Table>>;
/// Lists tables.
fn list_tables(&self) -> Result<Vec<Table>>;

/// Reads a table, and errors if it does not exist
fn must_read_table(&self, table: &str) -> Result<Table> {
self.read_table(table)?.ok_or(errinput!("table {table} does not exist"))
}

/// Returns all references to a table, as table,column pairs.
fn table_references(&self, table: &str, with_self: bool) -> Result<Vec<(String, Vec<String>)>> {
Ok(self
.list_tables()?
.into_iter()
.filter(|t| with_self || t.name != table)
.map(|t| {
(
t.name,
t.columns
.iter()
.filter(|c| c.references.as_deref() == Some(table))
.map(|c| c.name.clone())
.collect::<Vec<_>>(),
)
})
.filter(|(_, cs)| !cs.is_empty())
.collect())
}
}
19 changes: 8 additions & 11 deletions src/sql/engine/local.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::types::schema::{Catalog, Table, Tables};
use super::super::types::schema::Table;
use super::super::types::{Expression, Row, Value};
use super::Transaction as _;
use super::{Catalog, Transaction as _};
use crate::encoding::{self, Key as _, Value as _};
use crate::error::Result;
use crate::storage;
Expand Down Expand Up @@ -297,15 +297,12 @@ impl<E: storage::Engine> Catalog for Transaction<E> {
self.txn.get(&Key::Table(table.into()).encode())?.map(|v| Table::decode(&v)).transpose()
}

fn scan_tables(&self) -> Result<Tables> {
Ok(Box::new(
self.txn
.scan_prefix(&KeyPrefix::Table.encode())?
.iter()
.map(|r| r.and_then(|(_, v)| Table::decode(&v)))
.collect::<Result<Vec<_>>>()?
.into_iter(),
))
fn list_tables(&self) -> Result<Vec<Table>> {
self.txn
.scan_prefix(&KeyPrefix::Table.encode())?
.iter()
.map(|r| r.and_then(|(_, v)| Table::decode(&v)))
.collect()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sql/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod local;
mod raft;
mod session;

pub use engine::{Engine, IndexScan, Scan, Transaction};
pub use engine::{Catalog, Engine, IndexScan, Scan, Transaction};
pub use local::Local;
pub use raft::{Raft, Status};
pub use session::{Session, StatementResult};
14 changes: 5 additions & 9 deletions src/sql/engine/raft.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::super::types::schema::{Catalog, Table, Tables};
use super::super::types::schema::Table;
use super::super::types::{Expression, Row, Value};
use super::{Engine as _, IndexScan, Scan, Transaction as _};
use super::{Catalog, Engine as _, IndexScan, Scan, Transaction as _};
use crate::encoding::{self, bincode, Value as _};
use crate::errdata;
use crate::error::Result;
Expand Down Expand Up @@ -282,10 +282,8 @@ impl Catalog for Transaction {
self.client.query(Query::ReadTable { txn: self.state.clone(), table: table.to_string() })
}

fn scan_tables(&self) -> Result<Tables> {
Ok(Box::new(
self.client.query::<Vec<_>>(Query::ScanTables { txn: self.state.clone() })?.into_iter(),
))
fn list_tables(&self) -> Result<Vec<Table>> {
self.client.query(Query::ScanTables { txn: self.state.clone() })
}
}

Expand Down Expand Up @@ -388,9 +386,7 @@ impl<E: storage::Engine> raft::State for State<E> {
Query::ReadTable { txn, table } => {
self.engine.resume(txn)?.read_table(&table)?.encode()
}
Query::ScanTables { txn } => {
self.engine.resume(txn)?.scan_tables()?.collect::<Vec<_>>().encode()
}
Query::ScanTables { txn } => self.engine.resume(txn)?.list_tables()?.encode(),
};
Ok(response)
}
Expand Down
2 changes: 1 addition & 1 deletion src/sql/plan/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::super::types::schema::Catalog;
use super::super::types::{Expression, Value};
use super::plan::Node;
use crate::error::Result;
use crate::sql::engine::Catalog;

use std::mem::replace;

Expand Down
4 changes: 2 additions & 2 deletions src/sql/plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use super::optimizer::{self, Optimizer as _};
use super::planner::Planner;
use crate::error::Result;
use crate::sql::engine::Transaction;
use crate::sql::engine::{Catalog, Transaction};
use crate::sql::execution::{self, ExecutionResult};
use crate::sql::parser::ast;
use crate::sql::types::schema::{Catalog, Table};
use crate::sql::types::schema::Table;
use crate::sql::types::{Expression, Value};

use serde::{Deserialize, Serialize};
Expand Down
3 changes: 2 additions & 1 deletion src/sql/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::super::parser::ast;
use super::super::types::schema::{Catalog, Column, Table};
use super::super::types::schema::{Column, Table};
use super::super::types::{Expression, Value};
use super::{plan::Node, plan::Plan, Aggregate, Direction};
use crate::errinput;
use crate::error::Result;
use crate::sql::engine::Catalog;

use std::collections::{HashMap, HashSet};
use std::mem::replace;
Expand Down
39 changes: 0 additions & 39 deletions src/sql/types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,6 @@ use crate::sql::parser::format_ident;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

/// The catalog stores schema information
pub trait Catalog {
/// Creates a new table
fn create_table(&mut self, table: Table) -> Result<()>;
/// Deletes an existing table, or errors if it does not exist
fn delete_table(&mut self, table: &str) -> Result<()>;
/// Reads a table, if it exists
fn read_table(&self, table: &str) -> Result<Option<Table>>;
/// Iterates over all tables
fn scan_tables(&self) -> Result<Tables>;

/// Reads a table, and errors if it does not exist
fn must_read_table(&self, table: &str) -> Result<Table> {
self.read_table(table)?.ok_or(errinput!("table {table} does not exist"))
}

/// Returns all references to a table, as table,column pairs.
fn table_references(&self, table: &str, with_self: bool) -> Result<Vec<(String, Vec<String>)>> {
Ok(self
.scan_tables()?
.filter(|t| with_self || t.name != table)
.map(|t| {
(
t.name,
t.columns
.iter()
.filter(|c| c.references.as_deref() == Some(table))
.map(|c| c.name.clone())
.collect::<Vec<_>>(),
)
})
.filter(|(_, cs)| !cs.is_empty())
.collect())
}
}

/// A table scan iterator
pub type Tables = Box<dyn DoubleEndedIterator<Item = Table> + Send>;

/// A table schema
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Table {
Expand Down
5 changes: 2 additions & 3 deletions tests/sql/mutation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Mutation tests, using an in-memory database against golden files in tests/sql/mutation/
use toydb::error::Result;
use toydb::sql::engine::{Engine as _, Transaction as _};
use toydb::sql::types::schema::Catalog as _;
use toydb::sql::engine::{Catalog as _, Engine as _, Transaction as _};

use goldenfile::Mint;
use std::io::Write;
Expand Down Expand Up @@ -31,7 +30,7 @@ macro_rules! test_mutation {

write!(f, "Storage:")?;
let txn = engine.begin()?;
for table in txn.scan_tables()? {
for table in txn.list_tables()? {
write!(f, "\n{}\n", table)?;
for row in txn.scan(&table.name, None)? {
write!(f, "{:?}\n", row?)?;
Expand Down
5 changes: 2 additions & 3 deletions tests/sql/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Schema-related tests, using an in-memory database against golden files in tests/sql/chema/
use toydb::error::Result;
use toydb::sql::engine::{Engine as _, Transaction as _};
use toydb::sql::types::schema::Catalog as _;
use toydb::sql::engine::{Catalog as _, Engine as _, Transaction as _};

use goldenfile::Mint;
use std::io::Write;
Expand Down Expand Up @@ -29,7 +28,7 @@ macro_rules! test_schema {

write!(f, "Storage:")?;
let txn = engine.begin()?;
for table in txn.scan_tables()? {
for table in txn.list_tables()? {
write!(f, "\n{}\n", table)?;
for row in txn.scan(&table.name, None)? {
write!(f, "{:?}\n", row?)?;
Expand Down

0 comments on commit 1c7aeac

Please sign in to comment.