aboutsummaryrefslogtreecommitdiff
path: root/src/node
diff options
context:
space:
mode:
authorTheCharlatan <seb.kung@gmail.com>2024-07-23 22:18:15 +0200
committerTheCharlatan <seb.kung@gmail.com>2024-07-24 09:39:35 +0200
commit7aa8994c6fceae5cf8fb7e661371cdb19d2cb482 (patch)
tree954e69d64c0b9bedbf77a50088c9be41b7f10190 /src/node
parent910d38b22f575cba9a3325de3f4c5ac667d4a487 (diff)
refactor: Add FlatFileSeq member variables in BlockManager
Instead of constructing a new class every time a file operation is done, construct them once for each of the undo and block file when a new BlockManager is created. In future, this might make it easier to introduce an abstract block store.
Diffstat (limited to 'src/node')
-rw-r--r--src/node/blockstorage.cpp28
-rw-r--r--src/node/blockstorage.h8
2 files changed, 14 insertions, 22 deletions
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index c50625f58d..29b624cb12 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -734,7 +734,7 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
bool BlockManager::FlushUndoFile(int block_file, bool finalize)
{
FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
- if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
+ if (!m_undo_file_seq.Flush(undo_pos_old, finalize)) {
m_opts.notifications.flushError(_("Flushing undo file to disk failed. This is likely the result of an I/O error."));
return false;
}
@@ -756,7 +756,7 @@ bool BlockManager::FlushBlockFile(int blockfile_num, bool fFinalize, bool finali
assert(static_cast<int>(m_blockfile_info.size()) > blockfile_num);
FlatFilePos block_pos_old(blockfile_num, m_blockfile_info[blockfile_num].nSize);
- if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
+ if (!m_block_file_seq.Flush(block_pos_old, fFinalize)) {
m_opts.notifications.flushError(_("Flushing block file to disk failed. This is likely the result of an I/O error."));
success = false;
}
@@ -808,38 +808,28 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
std::error_code ec;
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
FlatFilePos pos(*it, 0);
- const bool removed_blockfile{fs::remove(BlockFileSeq().FileName(pos), ec)};
- const bool removed_undofile{fs::remove(UndoFileSeq().FileName(pos), ec)};
+ const bool removed_blockfile{fs::remove(m_block_file_seq.FileName(pos), ec)};
+ const bool removed_undofile{fs::remove(m_undo_file_seq.FileName(pos), ec)};
if (removed_blockfile || removed_undofile) {
LogPrint(BCLog::BLOCKSTORAGE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
}
}
}
-FlatFileSeq BlockManager::BlockFileSeq() const
-{
- return FlatFileSeq(m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
-}
-
-FlatFileSeq BlockManager::UndoFileSeq() const
-{
- return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
-}
-
AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
{
- return AutoFile{BlockFileSeq().Open(pos, fReadOnly)};
+ return AutoFile{m_block_file_seq.Open(pos, fReadOnly)};
}
/** Open an undo file (rev?????.dat) */
AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
{
- return AutoFile{UndoFileSeq().Open(pos, fReadOnly)};
+ return AutoFile{m_undo_file_seq.Open(pos, fReadOnly)};
}
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
{
- return BlockFileSeq().FileName(pos);
+ return m_block_file_seq.FileName(pos);
}
FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
@@ -919,7 +909,7 @@ FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int n
m_blockfile_info[nFile].nSize += nAddSize;
bool out_of_space;
- size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
+ size_t bytes_allocated = m_block_file_seq.Allocate(pos, nAddSize, out_of_space);
if (out_of_space) {
m_opts.notifications.fatalError(_("Disk space is too low!"));
return {};
@@ -965,7 +955,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
m_dirty_fileinfo.insert(nFile);
bool out_of_space;
- size_t bytes_allocated = UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
+ size_t bytes_allocated = m_undo_file_seq.Allocate(pos, nAddSize, out_of_space);
if (out_of_space) {
return FatalError(m_opts.notifications, state, _("Disk space is too low!"));
}
diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h
index a946b4ea94..8c6190cd02 100644
--- a/src/node/blockstorage.h
+++ b/src/node/blockstorage.h
@@ -166,9 +166,6 @@ private:
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
- FlatFileSeq BlockFileSeq() const;
- FlatFileSeq UndoFileSeq() const;
-
AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
/**
@@ -261,12 +258,17 @@ private:
const kernel::BlockManagerOpts m_opts;
+ const FlatFileSeq m_block_file_seq;
+ const FlatFileSeq m_undo_file_seq;
+
public:
using Options = kernel::BlockManagerOpts;
explicit BlockManager(const util::SignalInterrupt& interrupt, Options opts)
: m_prune_mode{opts.prune_target > 0},
m_opts{std::move(opts)},
+ m_block_file_seq{FlatFileSeq{m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kB */ : BLOCKFILE_CHUNK_SIZE}},
+ m_undo_file_seq{FlatFileSeq{m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE}},
m_interrupt{interrupt} {}
const util::SignalInterrupt& m_interrupt;