CatapultServer  v0.5.0.1 (Elephant)
FileBlockStorage.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BlockStorage.h"
23 #include "IndexFile.h"
24 #include "RawFile.h"
25 #include <string>
26 
27 namespace catapult { namespace io {
28 
30  enum class FileBlockStorageMode {
32  Hash_Index,
33 
35  None
36  };
37 
39  class FileBlockStorage final : public PrunableBlockStorage {
40  public:
43  explicit FileBlockStorage(const std::string& dataDirectory, FileBlockStorageMode mode = FileBlockStorageMode::Hash_Index);
44 
45  public:
46  // LightBlockStorage
47  Height chainHeight() const override;
48  model::HashRange loadHashesFrom(Height height, size_t maxHashes) const override;
49  void saveBlock(const model::BlockElement& blockElement) override;
50  void dropBlocksAfter(Height height) override;
51 
52  // BlockStorage
53  std::shared_ptr<const model::Block> loadBlock(Height height) const override;
54  std::shared_ptr<const model::BlockElement> loadBlockElement(Height height) const override;
55  std::pair<std::vector<uint8_t>, bool> loadBlockStatementData(Height height) const override;
56 
57  // PrunableBlockStorage
58  void purge() override;
59 
60  private:
61  void requireHeight(Height height, const char* description) const;
62 
63  private:
64  class HashFile final {
65  public:
66  explicit HashFile(const std::string& dataDirectory);
67 
68  model::HashRange loadHashesFrom(Height height, size_t numHashes) const;
69  void save(Height height, const Hash256& hash);
70  void reset();
71 
72  private:
73  const std::string& m_dataDirectory;
74 
75  // used for caching inside save()
77  std::unique_ptr<RawFile> m_pCachedHashFile;
78  };
79 
80  std::string m_dataDirectory;
82 
85  };
86 }}
catapult::io::IndexFile::get
uint64_t get() const
Gets the index value.
Definition: IndexFile.cpp:36
catapult::io::FileBlockStorage
File-based block storage.
Definition: FileBlockStorage.h:39
catapult::io::FileBlockStorage::loadBlockStatementData
std::pair< std::vector< uint8_t >, bool > loadBlockStatementData(Height height) const override
Returns the optional block statement data at height.
Definition: FileBlockStorage.cpp:300
MemoryUtils.h
catapult::io::BufferedOutputFileStream
Provides a buffered output stream around raw file.
Definition: BufferedFileStream.h:32
catapult::io::RawFile
Wrapper for low-level i/o operations on files.
Definition: RawFile.h:49
catapult::io::OpenMode::Read_Write
Open file for both reading and writing. Removes the file if it exists.
catapult::io::IndexFile
Index file containing a uint64_t value.
Definition: IndexFile.h:28
catapult::model::BlockElement::EntityHash
Hash256 EntityHash
Entity hash.
Definition: Elements.h:66
catapult::io::FileBlockStorageMode::Hash_Index
Maintain hash-based index.
catapult::Height
utils::BaseValue< uint64_t, Height_tag > Height
Definition: src/catapult/types.h:85
BlockStorage.h
catapult::io::FileBlockStorage::purge
void purge() override
Purges all blocks from storage.
Definition: FileBlockStorage.cpp:317
catapult::io::OpenMode::Read_Only
Open file in read-only mode.
catapult::io::RawFile::seek
void seek(uint64_t position)
Definition: RawFile.cpp:320
catapult::model::BlockElement::Block
const model::Block & Block
Block entity.
Definition: Elements.h:63
catapult::io::FileBlockStorage::dropBlocksAfter
void dropBlocksAfter(Height height) override
Drops all blocks after height.
Definition: FileBlockStorage.cpp:245
catapult::io::FileBlockStorage::HashFile
Definition: FileBlockStorage.h:64
catapult::io::FileBlockStorage::m_dataDirectory
std::string m_dataDirectory
Definition: FileBlockStorage.h:80
boost
Definition: IoThreadPool.h:25
catapult::io::FileBlockStorage::m_indexFile
IndexFile m_indexFile
Definition: FileBlockStorage.h:84
catapult::io::PrunableBlockStorage
Interface that allows saving, loading and pruning blocks.
Definition: BlockStorage.h:63
catapult::io::OpenMode
OpenMode
Defines mode of opening the file.
Definition: RawFile.h:28
catapult::io::FileBlockStorage::HashFile::reset
void reset()
Definition: FileBlockStorage.cpp:155
catapult::model::EntityRange
Represents a range of entities.
Definition: EntityRange.h:31
catapult::io::Read32
auto Read32(TIo &input)
Reads value from input.
Definition: PodIoUtils.h:74
BlockElementSerializer.h
m_rawFile
RawFile & m_rawFile
Definition: FileBlockStorage.cpp:193
RawFile.h
catapult::utils::BasicBaseValue::unwrap
constexpr ValueType unwrap() const
Unwraps this value and returns the underlying raw value.
Definition: BaseValue.h:53
catapult::io::FileBlockStorage::m_hashFile
HashFile m_hashFile
Definition: FileBlockStorage.h:83
catapult::utils::BaseValue< uint64_t, Height_tag >
catapult::io::WriteBlockStatement
void WriteBlockStatement(const model::BlockStatement &blockStatement, OutputStream &outputStream)
Writes blockStatement into outputStream.
Definition: BlockStatementSerializer.cpp:65
catapult::utils::MutableRawBuffer
BasicRawBuffer< uint8_t > MutableRawBuffer
A mutable binary buffer.
Definition: RawBuffer.h:65
catapult::io::FileBlockStorage::HashFile::m_cachedDirectoryId
uint64_t m_cachedDirectoryId
Definition: FileBlockStorage.h:76
FileBlockStorage.h
catapult::io::FileBlockStorage::loadBlock
std::shared_ptr< const model::Block > loadBlock(Height height) const override
Returns the block at height.
Definition: FileBlockStorage.cpp:282
CATAPULT_THROW_RUNTIME_ERROR_1
#define CATAPULT_THROW_RUNTIME_ERROR_1(MESSAGE, PARAM1)
Macro used to throw a catapult runtime error with a single parameter.
Definition: exceptions.h:171
catapult::io::FileBlockStorageMode
FileBlockStorageMode
File block storage modes.
Definition: FileBlockStorage.h:30
IndexFile.h
catapult::io::InputStream
Reader interface.
Definition: Stream.h:27
BufferedFileStream.h
catapult::model::EntityRange::PrepareFixed
static EntityRange PrepareFixed(size_t numElements, uint8_t **ppRangeData=nullptr)
Definition: EntityRange.h:265
catapult::io::FileBlockStorage::requireHeight
void requireHeight(Height height, const char *description) const
Definition: FileBlockStorage.cpp:327
catapult::io::FileBlockStorage::loadHashesFrom
model::HashRange loadHashesFrom(Height height, size_t maxHashes) const override
Returns a range of at most maxHashes hashes starting at height.
Definition: FileBlockStorage.cpp:201
catapult::io::FileBlockStorageMode::None
None.
catapult::io::FileBlockStorage::saveBlock
void saveBlock(const model::BlockElement &blockElement) override
Saves blockElement.
Definition: FileBlockStorage.cpp:214
catapult::io::BufferedOutputFileStream::flush
void flush() override
Definition: BufferedFileStream.cpp:33
catapult::io::FileBlockStorage::FileBlockStorage
FileBlockStorage(const std::string &dataDirectory, FileBlockStorageMode mode=FileBlockStorageMode::Hash_Index)
Definition: FileBlockStorage.cpp:164
catapult::model::BlockHeader::Height
catapult::Height Height
Height of a block.
Definition: Block.h:41
catapult::io::FileBlockStorage::HashFile::m_pCachedHashFile
std::unique_ptr< RawFile > m_pCachedHashFile
Definition: FileBlockStorage.h:77
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::io::PurgeDirectory
void PurgeDirectory(const std::string &directory)
Deletes everything in directory but does not delete the directory itself.
Definition: FilesystemUtils.cpp:26
catapult::io::FileBlockStorage::HashFile::save
void save(Height height, const Hash256 &hash)
Definition: FileBlockStorage.cpp:144
catapult::model::HashRange
EntityRange< Hash256 > HashRange
An entity range composed of hashes.
Definition: RangeTypes.h:35
catapult::io::ReadBlockElement
std::shared_ptr< model::BlockElement > ReadBlockElement(InputStream &inputStream)
Definition: BlockElementSerializer.cpp:110
catapult::io::WriteBlockElement
void WriteBlockElement(const model::BlockElement &blockElement, OutputStream &outputStream)
Writes blockElement into outputStream.
Definition: BlockElementSerializer.cpp:51
catapult::io::IndexFile::set
void set(uint64_t value)
Sets the index value to value.
Definition: IndexFile.cpp:41
catapult::io::FileBlockStorage::HashFile::HashFile
HashFile(const std::string &dataDirectory)
Definition: FileBlockStorage.cpp:101
catapult::model::BlockElement
Processing element for a block composed of a block and metadata.
Definition: Elements.h:55
catapult::io::FileBlockStorage::chainHeight
Height chainHeight() const override
Gets the number of blocks.
Definition: FileBlockStorage.cpp:197
SPRINTF
#define SPRINTF
Definition: FileBlockStorage.cpp:45
BlockStatementSerializer.h
catapult::io::IndexFile::exists
bool exists() const
true if the index file exists.
Definition: IndexFile.cpp:32
catapult::io::FileBlockStorage::HashFile::loadHashesFrom
model::HashRange loadHashesFrom(Height height, size_t numHashes) const
Definition: FileBlockStorage.cpp:123
CATAPULT_THROW_INVALID_ARGUMENT
#define CATAPULT_THROW_INVALID_ARGUMENT(MESSAGE)
Macro used to throw a catapult invalid argument.
Definition: exceptions.h:179
catapult::io::OpenMode::Read_Append
Open file for both reading and writing.
catapult
Definition: AddressExtractionExtension.cpp:28
m_dataDirectory
config::CatapultDataDirectory m_dataDirectory
Definition: FileSpoolingExtension.cpp:44
catapult::Hash256_Size
constexpr size_t Hash256_Size
Definition: src/catapult/types.h:34
PodIoUtils.h
catapult::io::FileBlockStorage::HashFile::m_dataDirectory
const std::string & m_dataDirectory
Definition: FileBlockStorage.h:73
catapult::utils::ByteArray< Hash256_Size, Hash256_tag >
catapult::io::FileBlockStorage::loadBlockElement
std::shared_ptr< const model::BlockElement > loadBlockElement(Height height) const override
Returns the block element (owning a block) at height.
Definition: FileBlockStorage.cpp:288
FilesystemUtils.h
catapult::io::FileBlockStorage::m_mode
FileBlockStorageMode m_mode
Definition: FileBlockStorage.h:81
catapult::io::OutputStream
Writer interface.
Definition: Stream.h:41
catapult::utils::RawBuffer
BasicRawBuffer< const uint8_t > RawBuffer
A const binary buffer.
Definition: RawBuffer.h:62
catapult::model::BlockElement::OptionalStatement
std::shared_ptr< const BlockStatement > OptionalStatement
Definition: Elements.h:79