This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Konstantin Munichev <toobwn@gmail.com>
- Loading branch information
1 parent
88c4ad4
commit fafc095
Showing
16 changed files
with
531 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
|
||
namespace iroha { | ||
namespace ametsuchi { | ||
|
||
/** | ||
* Creates a block storage | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "ametsuchi/impl/flat_file_block_storage.hpp" | ||
|
||
#include <boost/filesystem.hpp> | ||
|
||
#include "backend/protobuf/block.hpp" | ||
#include "common/byteutils.hpp" | ||
#include "logger/logger.hpp" | ||
|
||
using namespace iroha::ametsuchi; | ||
|
||
FlatFileBlockStorage::FlatFileBlockStorage( | ||
std::unique_ptr<FlatFile> flat_file, | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> json_converter, | ||
logger::LoggerPtr log) | ||
: flat_file_storage_(std::move(flat_file)), | ||
json_converter_(std::move(json_converter)), | ||
log_(std::move(log)) {} | ||
|
||
FlatFileBlockStorage::~FlatFileBlockStorage() { | ||
log_->info("Remove {} temp directory", flat_file_storage_->directory()); | ||
boost::filesystem::remove_all(flat_file_storage_->directory()); | ||
} | ||
|
||
bool FlatFileBlockStorage::insert( | ||
std::shared_ptr<const shared_model::interface::Block> block) { | ||
return json_converter_->serialize(*block).match( | ||
[&](const expected::Value<std::string> &block_json) { | ||
return flat_file_storage_->add(block->height(), | ||
stringToBytes(block_json.value)); | ||
}, | ||
[this](const auto &error) { | ||
log_->warn("Error while block serialization: {}", error.error); | ||
return false; | ||
}); | ||
} | ||
|
||
bool FlatFileBlockStorage::insert(const shared_model::interface::Block &block) { | ||
return insert(clone(block)); | ||
} | ||
|
||
boost::optional<std::shared_ptr<const shared_model::interface::Block>> | ||
FlatFileBlockStorage::fetch( | ||
shared_model::interface::types::HeightType height) const { | ||
auto storage_block = flat_file_storage_->get(height); | ||
if (not storage_block) { | ||
return boost::none; | ||
} | ||
|
||
return json_converter_->deserialize(bytesToString(*storage_block)) | ||
.match( | ||
[&](expected::Value<std::unique_ptr<shared_model::interface::Block>> | ||
&block) { | ||
return boost::make_optional< | ||
std::shared_ptr<const shared_model::interface::Block>>( | ||
std::move(block.value)); | ||
}, | ||
[&](expected::Error<std::string> &error) | ||
-> boost::optional< | ||
std::shared_ptr<const shared_model::interface::Block>> { | ||
log_->warn("Error while block deserialization: {}", error.error); | ||
return boost::none; | ||
}); | ||
} | ||
|
||
size_t FlatFileBlockStorage::size() const { | ||
return flat_file_storage_->blockIdentifiers().size(); | ||
} | ||
|
||
void FlatFileBlockStorage::clear() { | ||
flat_file_storage_->dropAll(); | ||
} | ||
|
||
void FlatFileBlockStorage::forEach( | ||
iroha::ametsuchi::BlockStorage::FunctionType function) const { | ||
for (auto block_id : flat_file_storage_->blockIdentifiers()) { | ||
auto block = fetch(block_id); | ||
BOOST_ASSERT(block); | ||
function(*block); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_FLAT_FILE_BLOCK_STORAGE_HPP | ||
#define IROHA_FLAT_FILE_BLOCK_STORAGE_HPP | ||
|
||
#include "ametsuchi/block_storage.hpp" | ||
|
||
#include "ametsuchi/impl/flat_file/flat_file.hpp" | ||
#include "interfaces/iroha_internal/block_json_converter.hpp" | ||
#include "logger/logger_fwd.hpp" | ||
|
||
namespace iroha { | ||
namespace ametsuchi { | ||
class FlatFileBlockStorage : public BlockStorage { | ||
public: | ||
FlatFileBlockStorage( | ||
std::unique_ptr<FlatFile> flat_file, | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_converter, | ||
logger::LoggerPtr log); | ||
|
||
~FlatFileBlockStorage() override; | ||
|
||
bool insert( | ||
std::shared_ptr<const shared_model::interface::Block> block) override; | ||
|
||
bool insert(const shared_model::interface::Block &block) override; | ||
|
||
boost::optional<std::shared_ptr<const shared_model::interface::Block>> | ||
fetch(shared_model::interface::types::HeightType height) const override; | ||
|
||
size_t size() const override; | ||
|
||
void clear() override; | ||
|
||
void forEach(FunctionType function) const override; | ||
|
||
private: | ||
std::unique_ptr<FlatFile> flat_file_storage_; | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_converter_; | ||
logger::LoggerPtr log_; | ||
}; | ||
} // namespace ametsuchi | ||
} // namespace iroha | ||
|
||
#endif // IROHA_FLAT_FILE_BLOCK_STORAGE_HPP |
Oops, something went wrong.