pub mod db;
pub use crate::codec::kv::{KeyCodec, ValueCodec};
pub use db::{Db, RangeIter, Tree, WriteTxn};
use std::{fmt, str::FromStr};
use thiserror::Error;
#[repr(u64)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyEncodingId {
BeU64 = 0,
BeI64 = 1,
Utf8 = 2,
RawBytes = 3,
}
impl fmt::Display for KeyEncodingId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
KeyEncodingId::BeU64 => "be_u64",
KeyEncodingId::BeI64 => "be_i64",
KeyEncodingId::Utf8 => "utf8",
KeyEncodingId::RawBytes => "raw",
})
}
}
impl FromStr for KeyEncodingId {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"be_u64" => Ok(Self::BeU64),
"be_i64" => Ok(Self::BeI64),
"utf8" => Ok(Self::Utf8),
"raw" => Ok(Self::RawBytes),
other => Err(format!("unknown key encoding: {other}")),
}
}
}
impl TryFrom<u64> for KeyEncodingId {
type Error = String;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::BeU64),
1 => Ok(Self::BeI64),
2 => Ok(Self::Utf8),
3 => Ok(Self::RawBytes),
other => Err(format!("unknown key encoding id: {other}")),
}
}
}
pub type TreeId = u64;
#[derive(Clone, Copy, Debug)]
pub struct KeyLimits {
pub min_len: u32,
pub max_len: u32,
}
#[derive(Debug, Error)]
pub enum ApiError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("tree: {0}")]
Tree(String),
#[error("commit: {0}")]
Commit(String),
#[error("storage: {0}")]
Storage(String),
#[error("key type incompatible with tree encoding {expected}")]
IncompatibleKeyType {
expected: KeyEncodingId,
},
#[error("decode: {0}")]
Decode(String),
#[error("internal: {0}")]
Internal(String),
#[error("range request requires end >= start in key order")]
BadRangeBounds,
#[error("transaction aborted after exhausting retries")]
TxnAborted,
}
impl From<crate::bplustree::tree::TreeError> for ApiError {
fn from(e: crate::bplustree::tree::TreeError) -> Self {
ApiError::Tree(e.to_string())
}
}
impl From<crate::bplustree::tree::CommitError> for ApiError {
fn from(e: crate::bplustree::tree::CommitError) -> Self {
ApiError::Commit(e.to_string())
}
}
impl From<crate::storage::StorageError> for ApiError {
fn from(e: crate::storage::StorageError) -> Self {
ApiError::Storage(e.to_string())
}
}