Skip to content

Commit e5af5fc

Browse files
committed
db: Make reusable base class for index databases.
1 parent 9b0ec1a commit e5af5fc

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/txdb.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,12 @@ bool CCoinsViewDB::Upgrade() {
415415
return !ShutdownRequested();
416416
}
417417

418+
BaseIndexDB::BaseIndexDB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
419+
CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
420+
{}
421+
418422
TxIndexDB::TxIndexDB(size_t n_cache_size, bool f_memory, bool f_wipe) :
419-
CDBWrapper(GetDataDir() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
423+
BaseIndexDB(GetDataDir() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
420424
{}
421425

422426
bool TxIndexDB::ReadTxPos(const uint256 &txid, CDiskTxPos& pos) const
@@ -433,7 +437,7 @@ bool TxIndexDB::WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_po
433437
return WriteBatch(batch);
434438
}
435439

436-
bool TxIndexDB::ReadBestBlock(CBlockLocator& locator) const
440+
bool BaseIndexDB::ReadBestBlock(CBlockLocator& locator) const
437441
{
438442
bool success = Read(DB_BEST_BLOCK, locator);
439443
if (!success) {
@@ -442,7 +446,7 @@ bool TxIndexDB::ReadBestBlock(CBlockLocator& locator) const
442446
return success;
443447
}
444448

445-
bool TxIndexDB::WriteBestBlock(const CBlockLocator& locator)
449+
bool BaseIndexDB::WriteBestBlock(const CBlockLocator& locator)
446450
{
447451
return Write(DB_BEST_BLOCK, locator);
448452
}

src/txdb.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ class CBlockTreeDB : public CDBWrapper
123123
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
124124
};
125125

126+
class BaseIndexDB : public CDBWrapper
127+
{
128+
public:
129+
BaseIndexDB(const fs::path& path, size_t n_cache_size,
130+
bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
131+
132+
/// Read block locator of the chain that the index is in sync with.
133+
bool ReadBestBlock(CBlockLocator& locator) const;
134+
135+
/// Write block locator of the chain that the index is in sync with.
136+
bool WriteBestBlock(const CBlockLocator& locator);
137+
};
138+
126139
/**
127140
* Access to the txindex database (indexes/txindex/)
128141
*
@@ -132,7 +145,7 @@ class CBlockTreeDB : public CDBWrapper
132145
* and block index entries may not be flushed to disk until after this database
133146
* is updated.
134147
*/
135-
class TxIndexDB : public CDBWrapper
148+
class TxIndexDB : public BaseIndexDB
136149
{
137150
public:
138151
explicit TxIndexDB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
@@ -144,12 +157,6 @@ class TxIndexDB : public CDBWrapper
144157
/// Write a batch of transaction positions to the DB.
145158
bool WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos);
146159

147-
/// Read block locator of the chain that the txindex is in sync with.
148-
bool ReadBestBlock(CBlockLocator& locator) const;
149-
150-
/// Write block locator of the chain that the txindex is in sync with.
151-
bool WriteBestBlock(const CBlockLocator& locator);
152-
153160
/// Migrate txindex data from the block tree DB, where it may be for older nodes that have not
154161
/// been upgraded yet to the new database.
155162
bool MigrateData(CBlockTreeDB& block_tree_db, const CBlockLocator& best_locator);

0 commit comments

Comments
 (0)