reddb_server/storage/
embedded.rs1use std::path::Path;
9
10use crate::api::{RedDBError, RedDBResult};
11
12pub use reddb_file::{
13 EmbeddedRdbManifest, EmbeddedRdbOpen, EmbeddedRdbSuperblock, EMBEDDED_RDB_MANIFEST_0_OFFSET,
14 EMBEDDED_RDB_MANIFEST_1_OFFSET, EMBEDDED_RDB_MANIFEST_SLOT_SIZE,
15 EMBEDDED_RDB_MANIFEST_ZONE_END, EMBEDDED_RDB_SUPERBLOCK_0_OFFSET,
16 EMBEDDED_RDB_SUPERBLOCK_1_OFFSET, EMBEDDED_RDB_SUPERBLOCK_SIZE,
17};
18
19pub struct EmbeddedRdbArtifact;
20
21impl EmbeddedRdbArtifact {
22 pub fn create(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
23 map_result(reddb_file::EmbeddedRdbArtifact::create(path))
24 }
25
26 pub fn create_with_snapshot(
27 path: impl AsRef<Path>,
28 snapshot: &[u8],
29 ) -> RedDBResult<EmbeddedRdbOpen> {
30 map_result(reddb_file::EmbeddedRdbArtifact::create_with_snapshot(
31 path, snapshot,
32 ))
33 }
34
35 pub fn open(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
36 map_result(reddb_file::EmbeddedRdbArtifact::open(path))
37 }
38
39 pub fn open_strict_manifest(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
40 map_result(reddb_file::EmbeddedRdbArtifact::open_strict_manifest(path))
41 }
42
43 pub fn read_snapshot(open: &EmbeddedRdbOpen) -> RedDBResult<Option<Vec<u8>>> {
44 map_result(reddb_file::EmbeddedRdbArtifact::read_snapshot(open))
45 }
46
47 pub fn write_snapshot(path: impl AsRef<Path>, snapshot: &[u8]) -> RedDBResult<EmbeddedRdbOpen> {
48 map_result(reddb_file::EmbeddedRdbArtifact::write_snapshot(
49 path, snapshot,
50 ))
51 }
52
53 pub fn wal_payloads_encoded_len(payloads: &[Vec<u8>]) -> RedDBResult<u64> {
54 map_result(reddb_file::EmbeddedRdbArtifact::wal_payloads_encoded_len(
55 payloads,
56 ))
57 }
58
59 pub fn write_snapshot_with_wal_capacity(
60 path: impl AsRef<Path>,
61 snapshot: &[u8],
62 min_wal_bytes: u64,
63 ) -> RedDBResult<EmbeddedRdbOpen> {
64 map_result(
65 reddb_file::EmbeddedRdbArtifact::write_snapshot_with_wal_capacity(
66 path,
67 snapshot,
68 min_wal_bytes,
69 ),
70 )
71 }
72
73 pub fn read_wal_payloads(open: &EmbeddedRdbOpen) -> RedDBResult<Vec<Vec<u8>>> {
74 map_result(reddb_file::EmbeddedRdbArtifact::read_wal_payloads(open))
75 }
76
77 pub fn append_wal_payloads(
78 path: impl AsRef<Path>,
79 payloads: &[Vec<u8>],
80 ) -> RedDBResult<EmbeddedRdbOpen> {
81 map_result(reddb_file::EmbeddedRdbArtifact::append_wal_payloads(
82 path, payloads,
83 ))
84 }
85}
86
87fn map_result<T>(result: reddb_file::RdbFileResult<T>) -> RedDBResult<T> {
88 result.map_err(map_error)
89}
90
91fn map_error(err: reddb_file::RdbFileError) -> RedDBError {
92 match err {
93 reddb_file::RdbFileError::InvalidOperation(msg) => RedDBError::InvalidOperation(msg),
94 reddb_file::RdbFileError::Io(err) => RedDBError::Io(err),
95 err @ reddb_file::RdbFileError::ZoneUnrecoverable { .. } => {
98 RedDBError::InvalidOperation(err.to_string())
99 }
100 }
101}