-
Notifications
You must be signed in to change notification settings - Fork 876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: return OpResult in DbSlice::AddOrFind instead of throwing std::bad_alloc #2427
Conversation
src/server/bitops_family.cc
Outdated
if (!op_res) { | ||
return op_res.status(); | ||
} | ||
auto add_res = std::move(*op_res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto add_res = std::move(*op_res); | |
auto& add_res = *op_res; |
src/server/bitops_family.cc
Outdated
if (!add_res.is_new) { | ||
if (add_res.it->second.ObjType() != OBJ_STRING) { | ||
return OpStatus::WRONG_TYPE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!add_res.is_new) { | |
if (add_res.it->second.ObjType() != OBJ_STRING) { | |
return OpStatus::WRONG_TYPE; | |
if (!add_res.is_new && add_res.it->second.ObjType() != OBJ_STRING) { | |
return OpStatus::WRONG_TYPE; |
src/server/db_slice.cc
Outdated
if (!op_result) { | ||
return op_result.status(); | ||
} | ||
auto res = std::move(*op_result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto res = std::move(*op_result); | |
auto& res = *op_result; |
src/server/db_slice.cc
Outdated
@@ -495,7 +495,8 @@ OpResult<pair<PrimeConstIterator, unsigned>> DbSlice::FindFirstReadOnly(const Co | |||
return OpStatus::KEY_NOTFOUND; | |||
} | |||
|
|||
DbSlice::AddOrFindResult DbSlice::AddOrFind(const Context& cntx, string_view key) noexcept(false) { | |||
OpResult<DbSlice::AddOrFindResult> DbSlice::AddOrFind(const Context& cntx, | |||
string_view key) noexcept(false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we change this to noexcept(true)
now? or just noexcept
?
Or just remove this, as we never throw in other places anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just noexcept
will suffice but if we omit it completely it's also ok I guess. I keep conditional noexcept only when there is type deduction
src/server/db_slice.cc
Outdated
auto res = AddOrUpdateInternal(cntx, key, std::move(obj), expire_at_ms, false); | ||
OpResult<DbSlice::ItAndUpdater> DbSlice::AddNew(const Context& cntx, string_view key, | ||
PrimeValue obj, | ||
uint64_t expire_at_ms) noexcept(false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question here
src/server/db_slice.cc
Outdated
std::string_view key, | ||
PrimeValue obj, | ||
uint64_t expire_at_ms, | ||
bool force_update) noexcept(false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto, and in other places :)
src/server/generic_family.cc
Outdated
if (!op_result) { | ||
return op_result.status(); | ||
} | ||
auto add_res = std::move(*op_result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto add_res = std::move(*op_result); | |
auto* add_res = *op_result; |
src/server/hll_family.cc
Outdated
if (!op_res) { | ||
return op_res.status(); | ||
} | ||
auto res = std::move(*op_res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto res = std::move(*op_res); | |
auto& res = *op_res; |
src/server/hll_family.cc
Outdated
} | ||
auto res = std::move(*op_res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto res = std::move(*op_res); | |
auto& res = *op_res; |
src/server/hset_family.cc
Outdated
} | ||
auto add_res = std::move(*op_res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto add_res = std::move(*op_res); | |
auto& add_res = *op_res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tired of writing these, also in other places in this PR :)
src/server/db_slice.cc
Outdated
.key = key})}; | ||
} | ||
auto status = res.status(); | ||
CHECK_EQ(status == OpStatus::KEY_NOTFOUND || status == OpStatus::OUT_OF_MEMORY, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CHECK_EQ(status == OpStatus::KEY_NOTFOUND || status == OpStatus::OUT_OF_MEMORY, true); | |
CHECK(status == OpStatus::KEY_NOTFOUND || status == OpStatus::OUT_OF_MEMORY) << status; |
src/server/dragonfly_test.cc
Outdated
@@ -541,19 +541,19 @@ TEST_F(DflyEngineTest, Bug496) { | |||
db.RegisterOnChange([&cb_hits](DbIndex, const DbSlice::ChangeReq&) { cb_hits++; }); | |||
|
|||
{ | |||
auto res = db.AddOrFind({}, "key-1"); | |||
auto res = std::move(*db.AddOrFind({}, "key-1")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a proposal:
Maybe add V&& operator*() &&;
to OpResult
, and then we won't have to call std::move()
on what seems like rvalue types? We do it quite a lot, so I think it's worth it. That's also how std::optional
allows similar things.
src/server/bitops_family.cc
Outdated
if (!op_res) { | ||
return op_res.status(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a similar if
in many places. For std::status_code
we have RETURN_ON_ERR()
(see error.h
), so how about creating a RETURN_ON_BAD_STATUS()
macro?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome :)
resolve comments in #2379