Skip to content

Commit d4cad11

Browse files
authored
chore: Refactor RdbLoad (dragonflydb#564)
Specifically, pass OpaqueObj to parsing routines so that they could fill it instead of creating it. In addition, this change improves the interface between generic_family and RdbLoad code: it removes reliance on the internal Item class. Signed-off-by: Roman Gershman <[email protected]> Signed-off-by: Roman Gershman <[email protected]>
1 parent 08803e6 commit d4cad11

File tree

3 files changed

+151
-100
lines changed

3 files changed

+151
-100
lines changed

src/server/generic_family.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ class RdbRestoreValue : protected RdbLoaderBase {
134134
std::optional<RdbLoaderBase::OpaqueObj> RdbRestoreValue::Parse(std::string_view payload) {
135135
InMemSource source(payload);
136136
src_ = &source;
137-
if (auto type_id = FetchType(); type_id && rdbIsObjectType(type_id.value())) {
138-
io::Result<OpaqueObj> io_res = ReadObj(type_id.value()); // load the type from the input stream
139-
if (!io_res) {
137+
if (io::Result<uint8_t> type_id = FetchType(); type_id && rdbIsObjectType(type_id.value())) {
138+
OpaqueObj obj;
139+
error_code ec = ReadObj(type_id.value(), &obj); // load the type from the input stream
140+
if (ec) {
140141
LOG(ERROR) << "failed to load data for type id " << (unsigned int)type_id.value();
141142
return std::nullopt;
142143
}
143-
return std::optional<OpaqueObj>(std::move(io_res.value()));
144+
145+
return std::optional<OpaqueObj>(std::move(obj));
144146
} else {
145147
LOG(ERROR) << "failed to load type id from the input stream or type id is invalid";
146148
return std::nullopt;
@@ -149,20 +151,20 @@ std::optional<RdbLoaderBase::OpaqueObj> RdbRestoreValue::Parse(std::string_view
149151

150152
bool RdbRestoreValue::Add(std::string_view data, std::string_view key, DbSlice& db_slice,
151153
DbIndex index, uint64_t expire_ms) {
152-
auto value_to_load = Parse(data);
153-
if (!value_to_load) {
154+
auto opaque_res = Parse(data);
155+
if (!opaque_res) {
154156
return false;
155157
}
156-
Item item{
157-
.key = std::string(key), .val = std::move(value_to_load.value()), .expire_ms = expire_ms};
158+
158159
PrimeValue pv;
159-
if (auto ec = Visit(item, &pv); ec) {
160+
if (auto ec = FromOpaque(*opaque_res, &pv); ec) {
160161
// we failed - report and exit
161162
LOG(WARNING) << "error while trying to save data: " << ec;
162163
return false;
163164
}
165+
164166
DbContext context{.db_index = index, .time_now_ms = GetCurrentTimeMs()};
165-
auto [it, added] = db_slice.AddOrSkip(context, key, std::move(pv), item.expire_ms);
167+
auto [it, added] = db_slice.AddOrSkip(context, key, std::move(pv), expire_ms);
166168
return added;
167169
}
168170

0 commit comments

Comments
 (0)