use crate::PackageId;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use thiserror::Error;
#[cfg(not(feature = "hyperapp"))]
mod kv_sync;
#[cfg(not(feature = "hyperapp"))]
pub use kv_sync::{open, open_raw, remove_db};
#[cfg(feature = "hyperapp")]
mod kv_async;
#[cfg(feature = "hyperapp")]
pub use kv_async::{open, open_raw, remove_db};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KvRequest {
pub package_id: PackageId,
pub db: String,
pub action: KvAction,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum KvAction {
Open,
RemoveDb,
Set { key: Vec<u8>, tx_id: Option<u64> },
Delete { key: Vec<u8>, tx_id: Option<u64> },
Get(Vec<u8>),
BeginTx,
Commit { tx_id: u64 },
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum KvResponse {
Ok,
BeginTx { tx_id: u64 },
Get(Vec<u8>),
Err(KvError),
}
#[derive(Clone, Debug, Serialize, Deserialize, Error)]
pub enum KvError {
#[error("db [{0}, {1}] does not exist")]
NoDb(PackageId, String),
#[error("key not found")]
KeyNotFound,
#[error("no transaction {0} found")]
NoTx(u64),
#[error("no write capability for requested DB")]
NoWriteCap,
#[error("no read capability for requested DB")]
NoReadCap,
#[error("request to open or remove DB with mismatching package ID")]
MismatchingPackageId,
#[error("failed to generate capability for new DB")]
AddCapFailed,
#[error("kv got a malformed request that either failed to deserialize or was missing a required blob")]
MalformedRequest,
#[error("RocksDB internal error: {0}")]
RocksDBError(String),
#[error("IO error: {0}")]
IOError(String),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KvCapabilityParams {
pub kind: KvCapabilityKind,
pub db_key: (PackageId, String),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum KvCapabilityKind {
Read,
Write,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Kv<K, V> {
pub package_id: PackageId,
pub db: String,
pub timeout: u64,
_marker: PhantomData<(K, V)>,
}