use crate::error::KernelResult;
use crate::transaction::TransactionId;
use crate::wal::LogSequenceNumber;
pub type PageId = u64;
pub type RowId = u64;
pub type TableId = u32;
pub type ColumnId = u16;
pub trait KernelStorage: Send + Sync {
fn read_page(&self, page_id: PageId) -> KernelResult<Vec<u8>>;
fn write_page(&self, page_id: PageId, data: &[u8]) -> KernelResult<LogSequenceNumber>;
fn allocate_page(&self) -> KernelResult<PageId>;
fn free_page(&self, page_id: PageId) -> KernelResult<()>;
fn sync(&self) -> KernelResult<()>;
fn durable_lsn(&self) -> LogSequenceNumber;
}
pub trait KernelTransaction: Send + Sync {
fn begin(&self) -> KernelResult<TransactionId>;
fn begin_with_isolation(
&self,
isolation: crate::transaction::IsolationLevel,
) -> KernelResult<TransactionId>;
fn commit(&self, txn_id: TransactionId) -> KernelResult<()>;
fn abort(&self, txn_id: TransactionId) -> KernelResult<()>;
fn is_active(&self, txn_id: TransactionId) -> bool;
fn snapshot_ts(&self, txn_id: TransactionId) -> KernelResult<u64>;
}
pub trait KernelCatalog: Send + Sync {
fn create_table(&self, name: &str, schema: &TableSchema) -> KernelResult<TableId>;
fn drop_table(&self, table_id: TableId) -> KernelResult<()>;
fn get_schema(&self, table_id: TableId) -> KernelResult<TableSchema>;
fn list_tables(&self) -> KernelResult<Vec<TableInfo>>;
fn rename_table(&self, table_id: TableId, new_name: &str) -> KernelResult<()>;
}
#[derive(Debug, Clone)]
pub struct TableSchema {
pub name: String,
pub columns: Vec<ColumnDef>,
pub primary_key: Vec<usize>,
}
#[derive(Debug, Clone)]
pub struct ColumnDef {
pub name: String,
pub data_type: DataType,
pub nullable: bool,
pub default_value: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataType {
Bool,
Int64,
Float64,
String,
Bytes,
Timestamp,
Null,
}
#[derive(Debug, Clone)]
pub struct TableInfo {
pub id: TableId,
pub name: String,
pub row_count: u64,
pub created_at: u64,
}
pub trait KernelRecovery: Send + Sync {
fn recover(&self) -> KernelResult<RecoveryStats>;
fn checkpoint(&self) -> KernelResult<LogSequenceNumber>;
fn last_checkpoint_lsn(&self) -> Option<LogSequenceNumber>;
}
#[derive(Debug, Clone, Default)]
pub struct RecoveryStats {
pub txns_redone: u64,
pub txns_undone: u64,
pub pages_recovered: u64,
pub duration_ms: u64,
}
pub trait KernelHealth: Send + Sync {
fn is_healthy(&self) -> bool;
fn health_info(&self) -> HealthInfo;
}
#[derive(Debug, Clone)]
pub struct HealthInfo {
pub operational: bool,
pub wal_size_bytes: u64,
pub active_txns: u64,
pub buffer_pool_usage: f64,
pub checkpoint_age_secs: u64,
}
impl Default for HealthInfo {
fn default() -> Self {
Self {
operational: true,
wal_size_bytes: 0,
active_txns: 0,
buffer_pool_usage: 0.0,
checkpoint_age_secs: 0,
}
}
}