CatapultServer  v0.5.0.1 (Elephant)
BlockStorageCache.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BlockStorage.h"
24 
25 namespace catapult { namespace io { struct CachedData; } }
26 
27 namespace catapult { namespace io {
28 
31  public:
33  explicit BlockStorageView(
34  const BlockStorage& storage,
36  const CachedData& cachedData);
37 
38  public:
40  Height chainHeight() const;
41 
43  model::HashRange loadHashesFrom(Height height, size_t maxHashes) const;
44 
46  std::shared_ptr<const model::Block> loadBlock(Height height) const;
47 
49  std::shared_ptr<const model::BlockElement> loadBlockElement(Height height) const;
50 
52  std::pair<std::vector<uint8_t>, bool> loadBlockStatementData(Height height) const;
53 
54  private:
55  void requireHeight(Height height, const char* description) const;
56 
57  private:
61  };
62 
65  public:
68  BlockStorage& storage,
69  PrunableBlockStorage& stagingStorage,
71  CachedData& cachedData);
72 
73  public:
75  void saveBlock(const model::BlockElement& blockElement);
76 
78  void saveBlocks(const std::vector<model::BlockElement>& blockElements);
79 
81  void dropBlocksAfter(Height height);
82 
84  void commit();
85 
86  private:
93  };
94 
98  public:
100  BlockStorageCache(std::unique_ptr<BlockStorage>&& pStorage, std::unique_ptr<PrunableBlockStorage>&& pStagingStorage);
101 
104 
105  public:
107  BlockStorageView view() const;
108 
111 
112  private:
113  std::unique_ptr<BlockStorage> m_pStorage;
114  std::unique_ptr<PrunableBlockStorage> m_pStagingStorage;
115  std::unique_ptr<CachedData> m_pCachedData;
117  };
118 }}
catapult::io::BlockStorageCache::BlockStorageCache
BlockStorageCache(std::unique_ptr< BlockStorage > &&pStorage, std::unique_ptr< PrunableBlockStorage > &&pStagingStorage)
Creates a new cache around pStorage that uses pStagingStorage for staging blocks in order to enable t...
Definition: BlockStorageCache.cpp:167
catapult::io::BlockStorage::loadBlock
virtual std::shared_ptr< const model::Block > loadBlock(Height height) const =0
Returns the block at height.
catapult::io::BlockStorageModifier::m_cachedData
CachedData & m_cachedData
Definition: BlockStorageCache.h:91
catapult::io::BlockStorageView::BlockStorageView
BlockStorageView(const BlockStorage &storage, utils::SpinReaderWriterLock::ReaderLockGuard &&readLock, const CachedData &cachedData)
Creates a view around storage and cache data (cachedData) with lock context readLock.
Definition: BlockStorageCache.cpp:72
MemoryUtils.h
catapult::io::BlockStorageView::m_readLock
utils::SpinReaderWriterLock::ReaderLockGuard m_readLock
Definition: BlockStorageCache.h:59
catapult::io::CachedData::block
std::shared_ptr< const model::Block > block(Height) const
Definition: BlockStorageCache.cpp:42
catapult::io::BlockStorageView::m_cachedData
const CachedData & m_cachedData
Definition: BlockStorageCache.h:60
catapult::io::BlockStorageModifier::saveBlocks
void saveBlocks(const std::vector< model::BlockElement > &blockElements)
Saves multiple block elements (blockElements).
Definition: BlockStorageCache.cpp:141
catapult::io::BlockStorageModifier::m_writeLock
utils::SpinReaderWriterLock::WriterLockGuard m_writeLock
Definition: BlockStorageCache.h:90
catapult::io::BlockStorageModifier::m_saveStartHeight
Height m_saveStartHeight
Definition: BlockStorageCache.h:92
catapult::io::BlockStorageCache::m_pStagingStorage
std::unique_ptr< PrunableBlockStorage > m_pStagingStorage
Definition: BlockStorageCache.h:114
catapult::io::BlockStorage::loadBlockStatementData
virtual std::pair< std::vector< uint8_t >, bool > loadBlockStatementData(Height height) const =0
Returns the optional block statement data at height.
catapult::io::BlockStorageModifier::BlockStorageModifier
BlockStorageModifier(BlockStorage &storage, PrunableBlockStorage &stagingStorage, utils::SpinReaderWriterLock::ReaderLockGuard &&readLock, CachedData &cachedData)
Creates a view around storage, stagingStorage and cache data (cachedData) with lock context readLock.
Definition: BlockStorageCache.cpp:124
catapult::io::BlockStorageModifier
A write only view on top of block storage.
Definition: BlockStorageCache.h:64
catapult::Height
utils::BaseValue< uint64_t, Height_tag > Height
Definition: src/catapult/types.h:85
BlockStorage.h
catapult::io::BlockStorage::loadBlockElement
virtual std::shared_ptr< const model::BlockElement > loadBlockElement(Height height) const =0
Returns the block element (owning a block) at height.
MoveBlockFiles.h
catapult::io::LightBlockStorage::loadHashesFrom
virtual model::HashRange loadHashesFrom(Height height, size_t maxHashes) const =0
Returns a range of at most maxHashes hashes starting at height.
catapult::io::BlockStorageView::loadBlock
std::shared_ptr< const model::Block > loadBlock(Height height) const
Returns the block at height.
Definition: BlockStorageCache.cpp:89
catapult::io::BlockStorageView::loadHashesFrom
model::HashRange loadHashesFrom(Height height, size_t maxHashes) const
Returns a range of at most maxHashes hashes starting at height.
Definition: BlockStorageCache.cpp:85
m_readLock
utils::SpinReaderWriterLock::ReaderLockGuard m_readLock
Definition: MemoryPtCache.cpp:258
catapult::io::BlockStorageView::loadBlockElement
std::shared_ptr< const model::BlockElement > loadBlockElement(Height height) const
Returns the block element (owning a block) at height.
Definition: BlockStorageCache.cpp:97
m_storage
const io::BlockStorageCache & m_storage
Definition: LocalChainApi.cpp:57
catapult::io::PrunableBlockStorage
Interface that allows saving, loading and pruning blocks.
Definition: BlockStorage.h:63
catapult::io::BlockStorageView
A read only view on top of block storage.
Definition: BlockStorageCache.h:30
catapult::model::EntityRange
Represents a range of entities.
Definition: EntityRange.h:31
catapult::io::BlockStorageCache::modifier
BlockStorageModifier modifier()
Gets a write only view of the storage.
Definition: BlockStorageCache.cpp:180
Elements.h
SpinReaderWriterLock.h
catapult::io::BlockStorageModifier::saveBlock
void saveBlock(const model::BlockElement &blockElement)
Saves a block element (blockElement).
Definition: BlockStorageCache.cpp:137
catapult::utils::BaseValue< uint64_t, Height_tag >
catapult::io::BlockStorageModifier::m_readLock
utils::SpinReaderWriterLock::ReaderLockGuard m_readLock
Definition: BlockStorageCache.h:89
catapult::io::BlockStorageModifier::dropBlocksAfter
void dropBlocksAfter(Height height)
Drops all blocks after height.
Definition: BlockStorageCache.cpp:146
catapult::utils::BasicSpinReaderWriterLock::acquireReader
ReaderLockGuard acquireReader()
Blocks until a reader lock can be acquired.
Definition: SpinReaderWriterLock.h:153
catapult::io::BlockStorageCache::view
BlockStorageView view() const
Gets a read only view of the storage.
Definition: BlockStorageCache.cpp:176
catapult::io::CachedData::update
void update(const std::shared_ptr< const model::BlockElement > &pBlockElement)
Definition: BlockStorageCache.cpp:56
catapult::io::LightBlockStorage::dropBlocksAfter
virtual void dropBlocksAfter(Height height)=0
Drops all blocks after height.
catapult::utils::BasicSpinReaderWriterLock::WriterLockGuard
A writer lock guard.
Definition: SpinReaderWriterLock.h:83
catapult::io::MoveBlockFiles
void MoveBlockFiles(PrunableBlockStorage &sourceStorage, BlockStorage &destinationStorage, Height startHeight)
Moves block files starting at startHeight from sourceStorage to destinationStorage.
Definition: MoveBlockFiles.cpp:28
catapult::io::BlockStorageView::requireHeight
void requireHeight(Height height, const char *description) const
Definition: BlockStorageCache.cpp:110
catapult::io::LightBlockStorage::chainHeight
virtual Height chainHeight() const =0
Gets the number of blocks.
m_writeLock
utils::SpinReaderWriterLock::WriterLockGuard m_writeLock
Definition: MemoryPtCache.cpp:259
catapult::io::BlockStorageCache
Definition: BlockStorageCache.h:97
catapult::io::BlockStorageCache::m_pCachedData
std::unique_ptr< CachedData > m_pCachedData
Definition: BlockStorageCache.h:115
catapult::io::BlockStorageView::chainHeight
Height chainHeight() const
Gets the number of blocks.
Definition: BlockStorageCache.cpp:81
catapult::io::BlockStorageModifier::m_storage
BlockStorage & m_storage
Definition: BlockStorageCache.h:87
catapult::io::CachedData::reset
void reset()
Definition: BlockStorageCache.cpp:60
catapult::utils::MoveOnly
A class that can be moved but not copied.
Definition: NonCopyable.h:43
catapult::io::BlockStorageView::m_storage
const BlockStorage & m_storage
Definition: BlockStorageCache.h:58
catapult::io::BlockStorageView::loadBlockStatementData
std::pair< std::vector< uint8_t >, bool > loadBlockStatementData(Height height) const
Returns the optional block statement data at height.
Definition: BlockStorageCache.cpp:105
catapult::model::BlockElement
Processing element for a block composed of a block and metadata.
Definition: Elements.h:55
catapult::utils::BasicSpinReaderWriterLock::ReaderLockGuard
A reader lock guard.
Definition: SpinReaderWriterLock.h:99
catapult::io::CachedData
Definition: BlockStorageCache.cpp:36
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::io::BlockStorageCache::~BlockStorageCache
~BlockStorageCache()
Destroys the cache.
catapult::io::CachedData::height
Height height() const
Definition: BlockStorageCache.cpp:38
catapult::io::BlockStorage
Interface for saving and loading blocks.
Definition: BlockStorage.h:50
catapult::io::BlockStorageCache::m_pStorage
std::unique_ptr< BlockStorage > m_pStorage
Definition: BlockStorageCache.h:113
m_pStorage
std::unique_ptr< BlockStorage > m_pStorage
Definition: AggregateBlockStorage.cpp:75
BlockStorageCache.h
catapult::io::CachedData::blockElement
std::shared_ptr< const model::BlockElement > blockElement(Height) const
Definition: BlockStorageCache.cpp:46
catapult::io::CachedData::m_pBlockElement
std::shared_ptr< const model::BlockElement > m_pBlockElement
Definition: BlockStorageCache.cpp:65
catapult::io::BlockStorageCache::m_lock
utils::SpinReaderWriterLock m_lock
Definition: BlockStorageCache.h:116
catapult::utils::BasicSpinReaderWriterLock
Definition: SpinReaderWriterLock.h:36
catapult::io::BlockStorageModifier::commit
void commit()
Commits all staged changes to the primary storage.
Definition: BlockStorageCache.cpp:151
catapult::io::BlockStorageModifier::m_stagingStorage
PrunableBlockStorage & m_stagingStorage
Definition: BlockStorageCache.h:88
catapult::io::CachedData::contains
bool contains(Height height) const
Definition: BlockStorageCache.cpp:51
catapult::io::LightBlockStorage::saveBlock
virtual void saveBlock(const model::BlockElement &blockElement)=0
Saves blockElement.