Skip to main content

reddb_server/storage/
embedded.rs

1//! Server-facing wrapper for the RedDB file artifact layer.
2//!
3//! The physical `.rdb` format, WAL, checkpoint, locking, and recovery
4//! implementation lives in `reddb-file`. This module keeps the historical
5//! `crate::storage::EmbeddedRdbArtifact` API and maps file-layer errors onto
6//! `RedDBError`.
7
8use std::path::Path;
9
10use crate::api::{RedDBError, RedDBResult};
11
12pub use reddb_file::{
13    EmbeddedRdbManifest, EmbeddedRdbOpen, EmbeddedRdbSuperblock, EMBEDDED_RDB_MANIFEST_OFFSET,
14    EMBEDDED_RDB_SUPERBLOCK_0_OFFSET, EMBEDDED_RDB_SUPERBLOCK_1_OFFSET,
15    EMBEDDED_RDB_SUPERBLOCK_SIZE,
16};
17
18pub struct EmbeddedRdbArtifact;
19
20impl EmbeddedRdbArtifact {
21    pub fn create(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
22        map_result(reddb_file::EmbeddedRdbArtifact::create(path))
23    }
24
25    pub fn create_with_snapshot(
26        path: impl AsRef<Path>,
27        snapshot: &[u8],
28    ) -> RedDBResult<EmbeddedRdbOpen> {
29        map_result(reddb_file::EmbeddedRdbArtifact::create_with_snapshot(
30            path, snapshot,
31        ))
32    }
33
34    pub fn open(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
35        map_result(reddb_file::EmbeddedRdbArtifact::open(path))
36    }
37
38    pub fn open_strict_manifest(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
39        map_result(reddb_file::EmbeddedRdbArtifact::open_strict_manifest(path))
40    }
41
42    pub fn read_snapshot(open: &EmbeddedRdbOpen) -> RedDBResult<Option<Vec<u8>>> {
43        map_result(reddb_file::EmbeddedRdbArtifact::read_snapshot(open))
44    }
45
46    pub fn write_snapshot(path: impl AsRef<Path>, snapshot: &[u8]) -> RedDBResult<EmbeddedRdbOpen> {
47        map_result(reddb_file::EmbeddedRdbArtifact::write_snapshot(
48            path, snapshot,
49        ))
50    }
51
52    pub fn wal_payloads_encoded_len(payloads: &[Vec<u8>]) -> RedDBResult<u64> {
53        map_result(reddb_file::EmbeddedRdbArtifact::wal_payloads_encoded_len(
54            payloads,
55        ))
56    }
57
58    pub fn write_snapshot_with_wal_capacity(
59        path: impl AsRef<Path>,
60        snapshot: &[u8],
61        min_wal_bytes: u64,
62    ) -> RedDBResult<EmbeddedRdbOpen> {
63        map_result(
64            reddb_file::EmbeddedRdbArtifact::write_snapshot_with_wal_capacity(
65                path,
66                snapshot,
67                min_wal_bytes,
68            ),
69        )
70    }
71
72    pub fn read_wal_payloads(open: &EmbeddedRdbOpen) -> RedDBResult<Vec<Vec<u8>>> {
73        map_result(reddb_file::EmbeddedRdbArtifact::read_wal_payloads(open))
74    }
75
76    pub fn append_wal_payloads(
77        path: impl AsRef<Path>,
78        payloads: &[Vec<u8>],
79    ) -> RedDBResult<EmbeddedRdbOpen> {
80        map_result(reddb_file::EmbeddedRdbArtifact::append_wal_payloads(
81            path, payloads,
82        ))
83    }
84}
85
86fn map_result<T>(result: reddb_file::RdbFileResult<T>) -> RedDBResult<T> {
87    result.map_err(map_error)
88}
89
90fn map_error(err: reddb_file::RdbFileError) -> RedDBError {
91    match err {
92        reddb_file::RdbFileError::InvalidOperation(msg) => RedDBError::InvalidOperation(msg),
93        reddb_file::RdbFileError::Io(err) => RedDBError::Io(err),
94    }
95}