Skip to content

Commit

Permalink
Use cursored writes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgumberg committed Oct 1, 2024
1 parent a4900d2 commit 137024f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ MDBXWrapper::MDBXWrapper(const DBParams& params)

LogPrintf("Opening MDBX in %s\n", fs::PathToString(params.path));

DBContext().create_params.geometry.pagesize = 16384;
DBContext().create_params.geometry.pagesize = 4096;

// We need this because of some unpleasant (for us) passing around of the
// Chainstate between threads during initialization.
Expand Down Expand Up @@ -519,6 +519,7 @@ size_t MDBXWrapper::DynamicMemoryUsage() const

struct MDBXBatch::MDBXWriteBatchImpl {
mdbx::txn_managed txn;
mdbx::cursor_managed cur;
};

MDBXBatch::MDBXBatch (const CDBWrapperBase& _parent) : CDBBatchBase(_parent)
Expand All @@ -527,6 +528,7 @@ MDBXBatch::MDBXBatch (const CDBWrapperBase& _parent) : CDBBatchBase(_parent)
m_impl_batch = std::make_unique<MDBXWriteBatchImpl>();

m_impl_batch->txn = parent.DBContext().env.start_write();
m_impl_batch->cur = m_impl_batch->txn.open_cursor(parent.DBContext().map);
};

MDBXBatch::~MDBXBatch()
Expand All @@ -538,22 +540,22 @@ MDBXBatch::~MDBXBatch()

void MDBXBatch::CommitAndReset()
{
// Committing writes closes cursors
m_impl_batch->txn.commit();

auto &parent = static_cast<const MDBXWrapper&>(m_parent);
m_impl_batch->txn = parent.DBContext().env.start_write();
m_impl_batch->cur = m_impl_batch->txn.open_cursor(parent.DBContext().map);
}

void MDBXBatch::WriteImpl(Span<const std::byte> key, DataStream& value)
{
auto &parent = static_cast<const MDBXWrapper&>(m_parent);

mdbx::slice slKey(CharCast(key.data()), key.size());
value.Xor(m_parent.GetObfuscateKey());
mdbx::slice slValue(CharCast(value.data()), value.size());

try {
m_impl_batch->txn.put(parent.m_db_context->map, slKey, slValue, mdbx::put_mode::upsert);
m_impl_batch->cur.upsert(slKey, slValue);
}
catch (mdbx::error err) {
const std::string errmsg = "Fatal MDBX error: " + err.message();
Expand All @@ -564,10 +566,8 @@ void MDBXBatch::WriteImpl(Span<const std::byte> key, DataStream& value)

void MDBXBatch::EraseImpl(Span<const std::byte> key)
{
auto &parent = static_cast<const MDBXWrapper&>(m_parent);

mdbx::slice slKey(CharCast(key.data()), key.size());
m_impl_batch->txn.erase(parent.m_db_context->map, slKey);
m_impl_batch->cur.erase(slKey);
}

size_t MDBXBatch::SizeEstimate() const
Expand Down
6 changes: 4 additions & 2 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ bool CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashB
// since any size we choose that is a multiple of the page size
// we will end up committing before we hit the maximum number of
// writes at that page size.
if (batch.SizeEstimate() >= 4*m_options.batch_write_bytes) {
if (changed % 262144 == 0) {
LogDebug(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
m_db->WriteBatch(batch);
}
Expand All @@ -142,7 +142,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.SizeEstimate() * (1.0 / 1048576.0));
bool ret = m_db->WriteBatch(batch);

// We need to force a sync on the final write.
bool ret = m_db->WriteBatch(batch, true);
LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
return ret;
}
Expand Down

0 comments on commit 137024f

Please sign in to comment.