Skip to content

Commit dcc1304

Browse files
committed
Merge pull request #5367
63d1ae5 Do all block index writes in a batch (Pieter Wuille)
2 parents d749230 + 63d1ae5 commit dcc1304

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

src/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class CDiskBlockIndex : public CBlockIndex
293293
hashPrev = 0;
294294
}
295295

296-
explicit CDiskBlockIndex(CBlockIndex* pindex) : CBlockIndex(*pindex) {
296+
explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
297297
hashPrev = (pprev ? pprev->GetBlockHash() : 0);
298298
}
299299

src/main.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,24 +1792,23 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
17921792
// First make sure all block and undo data is flushed to disk.
17931793
FlushBlockFile();
17941794
// Then update all block file information (which may refer to block and undo files).
1795-
bool fileschanged = false;
1796-
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
1797-
if (!pblocktree->WriteBlockFileInfo(*it, vinfoBlockFile[*it])) {
1798-
return state.Abort("Failed to write to block index");
1795+
{
1796+
std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
1797+
vFiles.reserve(setDirtyFileInfo.size());
1798+
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
1799+
vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
1800+
setDirtyFileInfo.erase(it++);
1801+
}
1802+
std::vector<const CBlockIndex*> vBlocks;
1803+
vBlocks.reserve(setDirtyBlockIndex.size());
1804+
for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
1805+
vBlocks.push_back(*it);
1806+
setDirtyBlockIndex.erase(it++);
1807+
}
1808+
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
1809+
return state.Abort("Files to write to block index database");
17991810
}
1800-
fileschanged = true;
1801-
setDirtyFileInfo.erase(it++);
1802-
}
1803-
if (fileschanged && !pblocktree->WriteLastBlockFile(nLastBlockFile)) {
1804-
return state.Abort("Failed to write to block index");
1805-
}
1806-
for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
1807-
if (!pblocktree->WriteBlockIndex(CDiskBlockIndex(*it))) {
1808-
return state.Abort("Failed to write to block index");
1809-
}
1810-
setDirtyBlockIndex.erase(it++);
18111811
}
1812-
pblocktree->Sync();
18131812
// Finally flush the chainstate (which may refer to block index entries).
18141813
if (!pcoinsTip->Flush())
18151814
return state.Abort("Failed to write to coin database");

src/txdb.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,10 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
6666
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
6767
}
6868

69-
bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
70-
{
71-
return Write(make_pair('b', blockindex.GetBlockHash()), blockindex);
72-
}
73-
74-
bool CBlockTreeDB::WriteBlockFileInfo(int nFile, const CBlockFileInfo &info) {
75-
return Write(make_pair('f', nFile), info);
76-
}
77-
7869
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {
7970
return Read(make_pair('f', nFile), info);
8071
}
8172

82-
bool CBlockTreeDB::WriteLastBlockFile(int nFile) {
83-
return Write('l', nFile);
84-
}
85-
8673
bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
8774
if (fReindexing)
8875
return Write('R', '1');
@@ -152,6 +139,18 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
152139
return true;
153140
}
154141

142+
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
143+
CLevelDBBatch batch;
144+
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
145+
batch.Write(make_pair('f', it->first), *it->second);
146+
}
147+
batch.Write('l', nLastFile);
148+
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
149+
batch.Write(make_pair('b', (*it)->GetBlockHash()), CDiskBlockIndex(*it));
150+
}
151+
return WriteBatch(batch, true);
152+
}
153+
155154
bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
156155
return Read(make_pair('t', txid), pos);
157156
}

src/txdb.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ class CBlockTreeDB : public CLevelDBWrapper
4848
CBlockTreeDB(const CBlockTreeDB&);
4949
void operator=(const CBlockTreeDB&);
5050
public:
51-
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
51+
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
5252
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
53-
bool WriteBlockFileInfo(int nFile, const CBlockFileInfo &fileinfo);
5453
bool ReadLastBlockFile(int &nFile);
55-
bool WriteLastBlockFile(int nFile);
5654
bool WriteReindexing(bool fReindex);
5755
bool ReadReindexing(bool &fReindex);
5856
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);

0 commit comments

Comments
 (0)