refactor: return OpResult in DbSlice::AddOrFind instead of throwing std::bad_alloc (#2427)

* return OpResult in AddOrFind instead of throwing bad_alloc
* small refactor
This commit is contained in:
Kostas Kyrimis 2024-01-23 14:16:03 +02:00 committed by GitHub
parent 1074bcb30b
commit 517be2005e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 209 additions and 213 deletions

View file

@ -2364,18 +2364,19 @@ void RdbLoader::LoadItemsBuffer(DbIndex db_ind, const ItemsBuf& ib) {
if (item->expire_ms > 0 && db_cntx.time_now_ms >= item->expire_ms)
continue;
try {
auto res = db_slice.AddOrUpdate(db_cntx, item->key, std::move(pv), item->expire_ms);
res.it->first.SetSticky(item->is_sticky);
if (!res.is_new) {
LOG(WARNING) << "RDB has duplicated key '" << item->key << "' in DB " << db_ind;
}
} catch (const std::bad_alloc&) {
auto op_res = db_slice.AddOrUpdate(db_cntx, item->key, std::move(pv), item->expire_ms);
if (!op_res) {
LOG(ERROR) << "OOM failed to add key '" << item->key << "' in DB " << db_ind;
ec_ = RdbError(errc::out_of_memory);
stop_early_ = true;
break;
}
auto& res = *op_res;
res.it->first.SetSticky(item->is_sticky);
if (!res.is_new) {
LOG(WARNING) << "RDB has duplicated key '" << item->key << "' in DB " << db_ind;
}
}
for (auto* item : ib) {