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(crate) fn try_open(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
76 map_result(reddb_file::EmbeddedRdbArtifact::try_open(path))
77 }
78
79 pub(crate) fn try_open_with_snapshot(
80 path: impl AsRef<Path>,
81 ) -> RedDBResult<(EmbeddedRdbOpen, Option<Vec<u8>>)> {
82 map_result(reddb_file::EmbeddedRdbArtifact::try_open_with_snapshot(
83 path,
84 ))
85 }
86
87 pub(crate) fn try_open_strict_manifest(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
88 map_result(reddb_file::EmbeddedRdbArtifact::try_open_strict_manifest(
89 path,
90 ))
91 }
92
93 pub fn open_with_snapshot(
94 path: impl AsRef<Path>,
95 ) -> RedDBResult<(EmbeddedRdbOpen, Option<Vec<u8>>)> {
96 map_result(reddb_file::EmbeddedRdbArtifact::open_with_snapshot(path))
97 }
98
99 pub fn open_strict_manifest(path: impl AsRef<Path>) -> RedDBResult<EmbeddedRdbOpen> {
100 map_result(reddb_file::EmbeddedRdbArtifact::open_strict_manifest(path))
101 }
102
103 pub fn read_snapshot(open: &EmbeddedRdbOpen) -> RedDBResult<Option<Vec<u8>>> {
104 map_result(reddb_file::EmbeddedRdbArtifact::read_snapshot(open))
105 }
106
107 pub fn write_snapshot(path: impl AsRef<Path>, snapshot: &[u8]) -> RedDBResult<EmbeddedRdbOpen> {
108 map_result(reddb_file::EmbeddedRdbArtifact::write_snapshot(
109 path, snapshot,
110 ))
111 }
112
113 pub fn wal_payloads_encoded_len(payloads: &[Vec<u8>]) -> RedDBResult<u64> {
114 map_result(reddb_file::EmbeddedRdbArtifact::wal_payloads_encoded_len(
115 payloads,
116 ))
117 }
118
119 pub fn write_snapshot_with_wal_capacity(
120 path: impl AsRef<Path>,
121 snapshot: &[u8],
122 min_wal_bytes: u64,
123 ) -> RedDBResult<EmbeddedRdbOpen> {
124 map_result(
125 reddb_file::EmbeddedRdbArtifact::write_snapshot_with_wal_capacity(
126 path,
127 snapshot,
128 min_wal_bytes,
129 ),
130 )
131 }
132
133 pub fn read_wal_payloads(open: &EmbeddedRdbOpen) -> RedDBResult<Vec<Vec<u8>>> {
134 map_result(reddb_file::EmbeddedRdbArtifact::read_wal_payloads(open))
135 }
136
137 pub fn append_wal_payloads(
138 path: impl AsRef<Path>,
139 payloads: &[Vec<u8>],
140 ) -> RedDBResult<EmbeddedRdbOpen> {
141 map_result(reddb_file::EmbeddedRdbArtifact::append_wal_payloads(
142 path, payloads,
143 ))
144 }
145}
146
147fn map_result<T>(result: reddb_file::RdbFileResult<T>) -> RedDBResult<T> {
148 result.map_err(map_error)
149}
150
151fn map_error(err: reddb_file::RdbFileError) -> RedDBError {
152 match err {
153 reddb_file::RdbFileError::InvalidOperation(msg) => RedDBError::InvalidOperation(msg),
154 reddb_file::RdbFileError::Io(err) => RedDBError::Io(err),
155 err @ (reddb_file::RdbFileError::ZoneUnrecoverable { .. }
158 | reddb_file::RdbFileError::ReadLimitExceeded { .. }) => {
159 RedDBError::InvalidOperation(err.to_string())
160 }
161 }
162}