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
21pub(crate) struct EmbeddedCommitSession(reddb_file::embedded::EmbeddedGroupCommitSession);
22
23impl EmbeddedCommitSession {
24 pub(crate) fn fence_interrupted_write(&self) {
25 self.0.fence_interrupted_write();
26 }
27 pub(crate) fn check(&self) -> RedDBResult<()> {
28 map_result(self.0.check())
29 }
30 pub(crate) fn append(
31 &self,
32 payloads: &[Vec<u8>],
33 options: reddb_file::embedded::EmbeddedGroupCommitOptions,
34 ) -> RedDBResult<EmbeddedRdbOpen> {
35 map_result(self.0.append(payloads, options))
36 }
37 pub(crate) fn enqueue(
38 &self,
39 payloads: &[Vec<u8>],
40 options: reddb_file::embedded::EmbeddedGroupCommitOptions,
41 ) -> RedDBResult<PendingEmbeddedAppend> {
42 map_result(self.0.enqueue(payloads, options)).map(PendingEmbeddedAppend)
43 }
44}
45
46pub(crate) struct PendingEmbeddedAppend(reddb_file::embedded::PendingEmbeddedAppend);
47
48impl PendingEmbeddedAppend {
49 pub(crate) fn wait(&self) -> RedDBResult<EmbeddedRdbOpen> {
50 map_result(self.0.wait())
51 }
52}
53
54impl EmbeddedRdbArtifact {
55 pub(crate) fn group_commit_session(path: impl AsRef<Path>) -> EmbeddedCommitSession {
56 EmbeddedCommitSession(reddb_file::EmbeddedRdbArtifact::group_commit_session(path))
57 }
58 pub fn create(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
59 map_result(reddb_file::EmbeddedRdbArtifact::create(path))
60 }
61
62 pub fn create_with_snapshot(
63 path: impl AsRef<Path>,
64 snapshot: &[u8],
65 ) -> RedDBResult<EmbeddedRdbOpen> {
66 map_result(reddb_file::EmbeddedRdbArtifact::create_with_snapshot(
67 path, snapshot,
68 ))
69 }
70
71 pub fn open(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
72 map_result(reddb_file::EmbeddedRdbArtifact::open(path))
73 }
74
75 pub fn open_with_snapshot(
76 path: impl AsRef<Path>,
77 ) -> RedDBResult<(EmbeddedRdbOpen, Option<Vec<u8>>)> {
78 map_result(reddb_file::EmbeddedRdbArtifact::open_with_snapshot(path))
79 }
80
81 pub fn open_strict_manifest(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
82 map_result(reddb_file::EmbeddedRdbArtifact::open_strict_manifest(path))
83 }
84
85 pub fn read_snapshot(open: &EmbeddedRdbOpen) -> RedDBResult<Option<Vec<u8>>> {
86 map_result(reddb_file::EmbeddedRdbArtifact::read_snapshot(open))
87 }
88
89 pub fn write_snapshot(path: impl AsRef<Path>, snapshot: &[u8]) -> RedDBResult<EmbeddedRdbOpen> {
90 map_result(reddb_file::EmbeddedRdbArtifact::write_snapshot(
91 path, snapshot,
92 ))
93 }
94
95 pub fn wal_payloads_encoded_len(payloads: &[Vec<u8>]) -> RedDBResult<u64> {
96 map_result(reddb_file::EmbeddedRdbArtifact::wal_payloads_encoded_len(
97 payloads,
98 ))
99 }
100
101 pub fn write_snapshot_with_wal_capacity(
102 path: impl AsRef<Path>,
103 snapshot: &[u8],
104 min_wal_bytes: u64,
105 ) -> RedDBResult<EmbeddedRdbOpen> {
106 map_result(
107 reddb_file::EmbeddedRdbArtifact::write_snapshot_with_wal_capacity(
108 path,
109 snapshot,
110 min_wal_bytes,
111 ),
112 )
113 }
114
115 pub fn read_wal_payloads(open: &EmbeddedRdbOpen) -> RedDBResult<Vec<Vec<u8>>> {
116 map_result(reddb_file::EmbeddedRdbArtifact::read_wal_payloads(open))
117 }
118
119 pub fn append_wal_payloads(
120 path: impl AsRef<Path>,
121 payloads: &[Vec<u8>],
122 ) -> RedDBResult<EmbeddedRdbOpen> {
123 map_result(reddb_file::EmbeddedRdbArtifact::append_wal_payloads(
124 path, payloads,
125 ))
126 }
127}
128
129fn map_result<T>(result: reddb_file::RdbFileResult<T>) -> RedDBResult<T> {
130 result.map_err(map_error)
131}
132
133fn map_error(err: reddb_file::RdbFileError) -> RedDBError {
134 match err {
135 reddb_file::RdbFileError::InvalidOperation(msg) => RedDBError::InvalidOperation(msg),
136 reddb_file::RdbFileError::Io(err) => RedDBError::Io(err),
137 err @ (reddb_file::RdbFileError::ZoneUnrecoverable { .. }
140 | reddb_file::RdbFileError::ReadLimitExceeded { .. }) => {
141 RedDBError::InvalidOperation(err.to_string())
142 }
143 }
144}