use std::path::Path;
use crate::api::{RedDBError, RedDBResult};
pub use reddb_file::{
EmbeddedRdbManifest, EmbeddedRdbOpen, EmbeddedRdbSuperblock, EMBEDDED_RDB_MANIFEST_0_OFFSET,
EMBEDDED_RDB_MANIFEST_1_OFFSET, EMBEDDED_RDB_MANIFEST_SLOT_SIZE,
EMBEDDED_RDB_MANIFEST_ZONE_END, EMBEDDED_RDB_SUPERBLOCK_0_OFFSET,
EMBEDDED_RDB_SUPERBLOCK_1_OFFSET, EMBEDDED_RDB_SUPERBLOCK_SIZE,
};
pub struct EmbeddedRdbArtifact;
pub(crate) struct EmbeddedCommitSession(reddb_file::embedded::EmbeddedGroupCommitSession);
impl EmbeddedCommitSession {
pub(crate) fn fence_interrupted_write(&self) {
self.0.fence_interrupted_write();
}
pub(crate) fn check(&self) -> RedDBResult<()> {
map_result(self.0.check())
}
pub(crate) fn append(
&self,
payloads: &[Vec<u8>],
options: reddb_file::embedded::EmbeddedGroupCommitOptions,
) -> RedDBResult<EmbeddedRdbOpen> {
map_result(self.0.append(payloads, options))
}
pub(crate) fn enqueue(
&self,
payloads: &[Vec<u8>],
options: reddb_file::embedded::EmbeddedGroupCommitOptions,
) -> RedDBResult<PendingEmbeddedAppend> {
map_result(self.0.enqueue(payloads, options)).map(PendingEmbeddedAppend)
}
}
pub(crate) struct PendingEmbeddedAppend(reddb_file::embedded::PendingEmbeddedAppend);
impl PendingEmbeddedAppend {
pub(crate) fn wait(&self) -> RedDBResult<EmbeddedRdbOpen> {
map_result(self.0.wait())
}
}
impl EmbeddedRdbArtifact {
pub(crate) fn group_commit_session(path: impl AsRef<Path>) -> EmbeddedCommitSession {
EmbeddedCommitSession(reddb_file::EmbeddedRdbArtifact::group_commit_session(path))
}
pub fn create(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
map_result(reddb_file::EmbeddedRdbArtifact::create(path))
}
pub fn create_with_snapshot(
path: impl AsRef<Path>,
snapshot: &[u8],
) -> RedDBResult<EmbeddedRdbOpen> {
map_result(reddb_file::EmbeddedRdbArtifact::create_with_snapshot(
path, snapshot,
))
}
pub fn open(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
map_result(reddb_file::EmbeddedRdbArtifact::open(path))
}
pub fn open_strict_manifest(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
map_result(reddb_file::EmbeddedRdbArtifact::open_strict_manifest(path))
}
pub fn read_snapshot(open: &EmbeddedRdbOpen) -> RedDBResult<Option<Vec<u8>>> {
map_result(reddb_file::EmbeddedRdbArtifact::read_snapshot(open))
}
pub fn write_snapshot(path: impl AsRef<Path>, snapshot: &[u8]) -> RedDBResult<EmbeddedRdbOpen> {
map_result(reddb_file::EmbeddedRdbArtifact::write_snapshot(
path, snapshot,
))
}
pub fn wal_payloads_encoded_len(payloads: &[Vec<u8>]) -> RedDBResult<u64> {
map_result(reddb_file::EmbeddedRdbArtifact::wal_payloads_encoded_len(
payloads,
))
}
pub fn write_snapshot_with_wal_capacity(
path: impl AsRef<Path>,
snapshot: &[u8],
min_wal_bytes: u64,
) -> RedDBResult<EmbeddedRdbOpen> {
map_result(
reddb_file::EmbeddedRdbArtifact::write_snapshot_with_wal_capacity(
path,
snapshot,
min_wal_bytes,
),
)
}
pub fn read_wal_payloads(open: &EmbeddedRdbOpen) -> RedDBResult<Vec<Vec<u8>>> {
map_result(reddb_file::EmbeddedRdbArtifact::read_wal_payloads(open))
}
pub fn append_wal_payloads(
path: impl AsRef<Path>,
payloads: &[Vec<u8>],
) -> RedDBResult<EmbeddedRdbOpen> {
map_result(reddb_file::EmbeddedRdbArtifact::append_wal_payloads(
path, payloads,
))
}
}
fn map_result<T>(result: reddb_file::RdbFileResult<T>) -> RedDBResult<T> {
result.map_err(map_error)
}
fn map_error(err: reddb_file::RdbFileError) -> RedDBError {
match err {
reddb_file::RdbFileError::InvalidOperation(msg) => RedDBError::InvalidOperation(msg),
reddb_file::RdbFileError::Io(err) => RedDBError::Io(err),
err @ reddb_file::RdbFileError::ZoneUnrecoverable { .. } => {
RedDBError::InvalidOperation(err.to_string())
}
}
}