#![cfg_attr(docsrs, feature(doc_cfg))]

mod expr;
mod query;
mod row;
mod schema;
mod sub_table;
mod table;
mod val;

use std::{fmt::Debug, future::Future};

pub use expr::{Expr, Op, Order};
use hipstr::HipByt;
pub use query::Query;
pub use row::{AsyncRow, Row};
pub use schema::{Field, Index, Schema};
pub use sub_table::SubTable;
pub use table::Table;
pub use val::Val;

/// 全局 ID 类型
pub type Id = u64;

/// 列名 Column name
pub type Col = HipByt<'static>;

/// 列偏移量 Column offset in Row
pub type ColIdx = u16;

/// 子表键 (用于路由定位子表)
/// SubTable key for routing to sub-table partition
pub type SubTableKey = Row;

/// 异步记录 Async record
#[derive(Debug)]
pub struct AsyncItem<R: AsyncRow> {
  pub sub_table: SubTableKey,
  pub id: Id,
  pub row: R,
}

pub trait IdGen: Send + Sync {
  type Error: Debug + Send + Sync;
  fn get(&self) -> impl Future<Output = Result<Id, Self::Error>> + Send;
}

pub trait Engine: Sized + Send + Sync {
  type Error: Debug + Send + Sync;
  type Gen: IdGen;
  type Table: Table;

  fn id_gen(&self) -> &Self::Gen;

  /// 打开或创建表 Open or create table
  fn open<F, Fut>(
    &self,
    name: &[u8],
    create: F,
  ) -> impl Future<Output = Result<Self::Table, Self::Error>> + Send
  where
    F: FnOnce() -> Fut + Send,
    Fut: Future<Output = Schema> + Send;
}
