mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-19 20:20:00 +01:00
refactor: inline constant return value of CDBWrapper::WriteBatch
`WriteBatch` can only ever return `true` - its errors are handled by throwing a `throw dbwrapper_error` instead. The boolean return value is quite confusing, especially since it's symmetric with `CDBWrapper::Read`, which catches the exceptions and returns a boolean instead. We're removing the constant return value and inlining `true` for its usages.
This commit is contained in:
@@ -274,7 +274,7 @@ CDBWrapper::~CDBWrapper()
|
||||
DBContext().options.env = nullptr;
|
||||
}
|
||||
|
||||
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
|
||||
void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
|
||||
{
|
||||
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug);
|
||||
double mem_before = 0;
|
||||
@@ -288,7 +288,6 @@ bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
|
||||
LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
|
||||
m_name, mem_before, mem_after);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t CDBWrapper::DynamicMemoryUsage() const
|
||||
|
||||
@@ -234,7 +234,8 @@ public:
|
||||
{
|
||||
CDBBatch batch(*this);
|
||||
batch.Write(key, value);
|
||||
return WriteBatch(batch, fSync);
|
||||
WriteBatch(batch, fSync);
|
||||
return true;
|
||||
}
|
||||
|
||||
//! @returns filesystem path to the on-disk data.
|
||||
@@ -259,10 +260,11 @@ public:
|
||||
{
|
||||
CDBBatch batch(*this);
|
||||
batch.Erase(key);
|
||||
return WriteBatch(batch, fSync);
|
||||
WriteBatch(batch, fSync);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteBatch(CDBBatch& batch, bool fSync = false);
|
||||
void WriteBatch(CDBBatch& batch, bool fSync = false);
|
||||
|
||||
// Get an estimate of LevelDB memory usage (in bytes).
|
||||
size_t DynamicMemoryUsage() const;
|
||||
|
||||
@@ -262,7 +262,7 @@ bool BaseIndex::Commit()
|
||||
ok = CustomCommit(batch);
|
||||
if (ok) {
|
||||
GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
|
||||
ok = GetDB().WriteBatch(batch);
|
||||
GetDB().WriteBatch(batch);
|
||||
}
|
||||
}
|
||||
if (!ok) {
|
||||
|
||||
@@ -338,7 +338,7 @@ bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block)
|
||||
// But since this creates new references to the filter, the position should get updated here
|
||||
// atomically as well in case Commit fails.
|
||||
batch.Write(DB_FILTER_POS, m_next_filter_pos);
|
||||
if (!m_db->WriteBatch(batch)) return false;
|
||||
m_db->WriteBatch(batch);
|
||||
|
||||
// Update cached header to the previous block hash
|
||||
m_last_header = *Assert(ReadFilterHeader(block.height - 1, *Assert(block.prev_hash)));
|
||||
|
||||
@@ -263,7 +263,7 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_db->WriteBatch(batch)) return false;
|
||||
m_db->WriteBatch(batch);
|
||||
|
||||
if (!ReverseBlock(block)) {
|
||||
return false; // failure cause logged internally
|
||||
|
||||
@@ -46,7 +46,8 @@ bool TxIndex::DB::WriteTxs(const std::vector<std::pair<Txid, CDiskTxPos>>& v_pos
|
||||
for (const auto& [txid, pos] : v_pos) {
|
||||
batch.Write(std::make_pair(DB_TXINDEX, txid.ToUint256()), pos);
|
||||
}
|
||||
return WriteBatch(batch);
|
||||
WriteBatch(batch);
|
||||
return true;
|
||||
}
|
||||
|
||||
TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
|
||||
|
||||
@@ -88,7 +88,8 @@ bool BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFi
|
||||
for (const CBlockIndex* bi : blockinfo) {
|
||||
batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), CDiskBlockIndex{bi});
|
||||
}
|
||||
return WriteBatch(batch, true);
|
||||
WriteBatch(batch, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlockTreeDB::WriteFlag(const std::string& name, bool fValue)
|
||||
|
||||
@@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
|
||||
// Remove key3 before it's even been written
|
||||
batch.Erase(key3);
|
||||
|
||||
BOOST_CHECK(dbw.WriteBatch(batch));
|
||||
dbw.WriteBatch(batch);
|
||||
|
||||
BOOST_CHECK(dbw.Read(key, res));
|
||||
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
|
||||
|
||||
@@ -149,9 +149,9 @@ bool CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashB
|
||||
batch.Write(DB_BEST_BLOCK, hashBlock);
|
||||
|
||||
LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() * (1.0 / 1048576.0));
|
||||
bool ret = m_db->WriteBatch(batch);
|
||||
m_db->WriteBatch(batch);
|
||||
LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
|
||||
return ret;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t CCoinsViewDB::EstimateSize() const
|
||||
|
||||
Reference in New Issue
Block a user