Skip to content

Commit

Permalink
db: Make reusable base class for index databases.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Jun 5, 2018
1 parent 9b0ec1a commit e5af5fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/txdb.cpp
Expand Up @@ -415,8 +415,12 @@ bool CCoinsViewDB::Upgrade() {
return !ShutdownRequested();
}

BaseIndexDB::BaseIndexDB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
CDBWrapper(path, n_cache_size, f_memory, f_wipe, f_obfuscate)
{}

TxIndexDB::TxIndexDB(size_t n_cache_size, bool f_memory, bool f_wipe) :
CDBWrapper(GetDataDir() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
BaseIndexDB(GetDataDir() / "indexes" / "txindex", n_cache_size, f_memory, f_wipe)
{}

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

bool TxIndexDB::ReadBestBlock(CBlockLocator& locator) const
bool BaseIndexDB::ReadBestBlock(CBlockLocator& locator) const
{
bool success = Read(DB_BEST_BLOCK, locator);
if (!success) {
Expand All @@ -442,7 +446,7 @@ bool TxIndexDB::ReadBestBlock(CBlockLocator& locator) const
return success;
}

bool TxIndexDB::WriteBestBlock(const CBlockLocator& locator)
bool BaseIndexDB::WriteBestBlock(const CBlockLocator& locator)
{
return Write(DB_BEST_BLOCK, locator);
}
Expand Down
21 changes: 14 additions & 7 deletions src/txdb.h
Expand Up @@ -123,6 +123,19 @@ class CBlockTreeDB : public CDBWrapper
bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
};

class BaseIndexDB : public CDBWrapper
{
public:
BaseIndexDB(const fs::path& path, size_t n_cache_size,
bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);

/// Read block locator of the chain that the index is in sync with.
bool ReadBestBlock(CBlockLocator& locator) const;

/// Write block locator of the chain that the index is in sync with.
bool WriteBestBlock(const CBlockLocator& locator);
};

/**
* Access to the txindex database (indexes/txindex/)
*
Expand All @@ -132,7 +145,7 @@ class CBlockTreeDB : public CDBWrapper
* and block index entries may not be flushed to disk until after this database
* is updated.
*/
class TxIndexDB : public CDBWrapper
class TxIndexDB : public BaseIndexDB
{
public:
explicit TxIndexDB(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
Expand All @@ -144,12 +157,6 @@ class TxIndexDB : public CDBWrapper
/// Write a batch of transaction positions to the DB.
bool WriteTxs(const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos);

/// Read block locator of the chain that the txindex is in sync with.
bool ReadBestBlock(CBlockLocator& locator) const;

/// Write block locator of the chain that the txindex is in sync with.
bool WriteBestBlock(const CBlockLocator& locator);

/// Migrate txindex data from the block tree DB, where it may be for older nodes that have not
/// been upgraded yet to the new database.
bool MigrateData(CBlockTreeDB& block_tree_db, const CBlockLocator& best_locator);
Expand Down

0 comments on commit e5af5fc

Please sign in to comment.