Skip to content
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

Merged
merged 4 commits into from
Jan 23, 2024

Conversation

kostasrim
Copy link
Contributor

resolve comments in #2379

  • return OpResult in AddOrFind instead of throwing bad_alloc

@kostasrim kostasrim self-assigned this Jan 16, 2024
@kostasrim kostasrim requested a review from chakaz January 16, 2024 19:13
@kostasrim kostasrim changed the title refactor(): return OpResult in DbSlice::AddOrFind instead of throwing std::bad_alloc refactor: return OpResult in DbSlice::AddOrFind instead of throwing std::bad_alloc Jan 16, 2024
if (!op_res) {
return op_res.status();
}
auto add_res = std::move(*op_res);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto add_res = std::move(*op_res);
auto& add_res = *op_res;

Comment on lines 332 to 334
if (!add_res.is_new) {
if (add_res.it->second.ObjType() != OBJ_STRING) {
return OpStatus::WRONG_TYPE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;

if (!op_result) {
return op_result.status();
}
auto res = std::move(*op_result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto res = std::move(*op_result);
auto& res = *op_result;

@@ -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) {
Copy link
Collaborator

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?

Copy link
Contributor Author

@kostasrim kostasrim Jan 18, 2024

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

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

std::string_view key,
PrimeValue obj,
uint64_t expire_at_ms,
bool force_update) noexcept(false) {
Copy link
Collaborator

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 :)

if (!op_result) {
return op_result.status();
}
auto add_res = std::move(*op_result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto add_res = std::move(*op_result);
auto* add_res = *op_result;

if (!op_res) {
return op_res.status();
}
auto res = std::move(*op_res);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto res = std::move(*op_res);
auto& res = *op_res;

}
auto res = std::move(*op_res);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto res = std::move(*op_res);
auto& res = *op_res;

}
auto add_res = std::move(*op_res);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto add_res = std::move(*op_res);
auto& add_res = *op_res;

Copy link
Collaborator

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 :)

@kostasrim kostasrim requested a review from chakaz January 18, 2024 18:33
.key = key})};
}
auto status = res.status();
CHECK_EQ(status == OpStatus::KEY_NOTFOUND || status == OpStatus::OUT_OF_MEMORY, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CHECK_EQ(status == OpStatus::KEY_NOTFOUND || status == OpStatus::OUT_OF_MEMORY, true);
CHECK(status == OpStatus::KEY_NOTFOUND || status == OpStatus::OUT_OF_MEMORY) << status;

@@ -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"));
Copy link
Collaborator

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.

Comment on lines 327 to 329
if (!op_res) {
return op_res.status();
}
Copy link
Collaborator

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?

@kostasrim kostasrim requested a review from chakaz January 23, 2024 11:10
Copy link
Collaborator

@chakaz chakaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome :)

@kostasrim kostasrim merged commit 517be20 into main Jan 23, 2024
10 checks passed
@kostasrim kostasrim deleted the add_or_find_ret branch January 23, 2024 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants