-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rdb burrow schema Signed-off-by: iceseer <[email protected]> Signed-off-by: Alexander Lednev <[email protected]> * burrow storage Signed-off-by: iceseer <[email protected]> Signed-off-by: Alexander Lednev <[email protected]> * rdb command executor Signed-off-by: iceseer <[email protected]> Signed-off-by: Alexander Lednev <[email protected]> * rdb burrow spec query executor Signed-off-by: iceseer <[email protected]> Signed-off-by: Alexander Lednev <[email protected]> * tests Signed-off-by: iceseer <[email protected]> Signed-off-by: Alexander Lednev <[email protected]>
- Loading branch information
Showing
15 changed files
with
895 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "ametsuchi/impl/rocksdb_burrow_storage.hpp" | ||
|
||
#include <cassert> | ||
#include <optional> | ||
|
||
#include "ametsuchi/impl/rocksdb_common.hpp" | ||
#include "common/obj_utils.hpp" | ||
#include "common/result.hpp" | ||
#include "common/to_lower.hpp" | ||
|
||
using namespace iroha::ametsuchi; | ||
using namespace iroha::expected; | ||
|
||
#define MAKE_LOWER_ON_STACK(name, source, sz) \ | ||
static_assert(sz > 0ull, "Unexpected size " #sz); \ | ||
assert(source.size() <= sz); \ | ||
char name##_buffer[sz]; \ | ||
auto name = toLower(source, name##_buffer); | ||
|
||
RocksdbBurrowStorage::RocksdbBurrowStorage( | ||
RocksDbCommon &common, | ||
std::string_view tx_hash, | ||
shared_model::interface::types::CommandIndexType cmd_index) | ||
: common_(common), tx_hash_(tx_hash), cmd_index_(cmd_index) {} | ||
|
||
Result<std::optional<std::string>, std::string> | ||
RocksdbBurrowStorage::getAccount(std::string_view address) { | ||
MAKE_LOWER_ON_STACK(address_lc, address, 128); | ||
RDB_TRY_GET_VALUE_OR_STR_ERR( | ||
opt_data, | ||
forCallEngineAccount<kDbOperation::kGet, kDbEntry::kCanExist>( | ||
common_, address_lc)); | ||
if (opt_data) | ||
return expected::makeValue(std::string(opt_data->data(), opt_data->size())); | ||
|
||
return std::nullopt; | ||
} | ||
|
||
Result<void, std::string> RocksdbBurrowStorage::updateAccount( | ||
std::string_view address, std::string_view account) { | ||
MAKE_LOWER_ON_STACK(address_lc, address, 128); | ||
common_.valueBuffer().assign(account.data(), account.size()); | ||
RDB_ERROR_CHECK_TO_STR( | ||
forCallEngineAccount<kDbOperation::kPut>(common_, address_lc)); | ||
return {}; | ||
} | ||
|
||
Result<void, std::string> RocksdbBurrowStorage::removeAccount( | ||
std::string_view address) { | ||
MAKE_LOWER_ON_STACK(address_lc, address, 128); | ||
RDB_ERROR_CHECK_TO_STR( | ||
forCallEngineAccount<kDbOperation::kDel, kDbEntry::kCanExist>( | ||
common_, address_lc)); | ||
|
||
auto const &[_, status] = | ||
common_.filterDelete(std::numeric_limits<uint64_t>::max(), | ||
RocksDBPort::ColumnFamilyType::kWsv, | ||
fmtstrings::kPathEngineStorage, | ||
address_lc); | ||
|
||
if (!status.ok() && !status.IsNotFound()) | ||
return expected::makeError(fmt::format( | ||
"Delete CallEngine storage with address '{}' failed.", address_lc)); | ||
|
||
return {}; | ||
} | ||
|
||
Result<std::optional<std::string>, std::string> | ||
RocksdbBurrowStorage::getStorage(std::string_view address, | ||
std::string_view key) { | ||
MAKE_LOWER_ON_STACK(address_lc, address, 128); | ||
|
||
std::string key_lc; | ||
toLowerAppend(key, key_lc); | ||
|
||
RDB_TRY_GET_VALUE_OR_STR_ERR( | ||
opt_value, | ||
forCallEngineStorage<kDbOperation::kGet, kDbEntry::kCanExist>( | ||
common_, address_lc, key_lc)); | ||
if (opt_value) | ||
return expected::makeValue( | ||
std::string(opt_value->data(), opt_value->size())); | ||
|
||
return std::nullopt; | ||
} | ||
|
||
Result<void, std::string> RocksdbBurrowStorage::setStorage( | ||
std::string_view address, std::string_view key, std::string_view value) { | ||
MAKE_LOWER_ON_STACK(address_lc, address, 128); | ||
|
||
std::string key_lc; | ||
toLowerAppend(key, key_lc); | ||
|
||
common_.valueBuffer().assign(value.data(), value.size()); | ||
RDB_ERROR_CHECK_TO_STR( | ||
forCallEngineStorage<kDbOperation::kPut>(common_, address_lc, key_lc)); | ||
return {}; | ||
} | ||
|
||
Result<void, std::string> RocksdbBurrowStorage::initCallId() { | ||
if (!call_id_cache_) { | ||
RDB_ERROR_CHECK_TO_STR( | ||
forCallEngineCallIds<kDbOperation::kCheck, kDbEntry::kMustNotExist>( | ||
common_, tx_hash_, cmd_index_)); | ||
RDB_TRY_GET_VALUE_OR_STR_ERR( | ||
opt_call_id, | ||
forCallEngineNextCallIds<kDbOperation::kGet, kDbEntry::kCanExist>( | ||
common_)); | ||
if (opt_call_id) | ||
call_id_cache_ = std::make_pair(*opt_call_id, 0ull); | ||
else | ||
call_id_cache_ = std::make_pair(0ull, 0ull); | ||
|
||
common_.encode(call_id_cache_->first); | ||
RDB_ERROR_CHECK_TO_STR(forCallEngineCallIds<kDbOperation::kPut>( | ||
common_, tx_hash_, cmd_index_)); | ||
|
||
common_.encode(call_id_cache_->first + 1ull); | ||
RDB_ERROR_CHECK_TO_STR( | ||
forCallEngineNextCallIds<kDbOperation::kPut>(common_)); | ||
} | ||
return {}; | ||
} | ||
|
||
Result<void, std::string> RocksdbBurrowStorage::storeLog( | ||
std::string_view address, | ||
std::string_view data, | ||
std::vector<std::string_view> topics) { | ||
if (!call_id_cache_) { | ||
RDB_ERROR_CHECK(initCallId()); | ||
} | ||
|
||
uint64_t log_idx = 0ull; | ||
RDB_TRY_GET_VALUE_OR_STR_ERR( | ||
opt_log_idx, | ||
forCallEngineNextLogIx<kDbOperation::kGet, kDbEntry::kCanExist>(common_)); | ||
if (opt_log_idx) | ||
log_idx = *opt_log_idx; | ||
|
||
common_.encode(log_idx + 1ull); | ||
RDB_ERROR_CHECK_TO_STR(forCallEngineNextLogIx<kDbOperation::kPut>(common_)); | ||
|
||
MAKE_LOWER_ON_STACK(address_lc, address, 128); | ||
common_.valueBuffer() = std::to_string(log_idx); | ||
common_.valueBuffer() += '#'; | ||
common_.valueBuffer() += address_lc; | ||
common_.valueBuffer() += '#'; | ||
common_.valueBuffer() += data; | ||
RDB_ERROR_CHECK_TO_STR(forCallEngineLogs<kDbOperation::kPut>( | ||
common_, call_id_cache_->first, call_id_cache_->second++)); | ||
|
||
for (uint64_t ix = 0; ix < topics.size(); ++ix) { | ||
auto const &topic = topics[ix]; | ||
common_.valueBuffer().assign(topic.data(), topic.size()); | ||
RDB_ERROR_CHECK_TO_STR( | ||
forCallEngineTopics<kDbOperation::kPut>(common_, log_idx, ix)); | ||
} | ||
|
||
return {}; | ||
} |
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,65 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_RDB_BURROW_STORAGE_HPP | ||
#define IROHA_RDB_BURROW_STORAGE_HPP | ||
|
||
#include "ametsuchi/burrow_storage.hpp" | ||
|
||
#include <string_view> | ||
#include <utility> | ||
|
||
#include "interfaces/common_objects/types.hpp" | ||
|
||
namespace iroha::ametsuchi { | ||
class RocksDbCommon; | ||
|
||
class RocksdbBurrowStorage : public BurrowStorage { | ||
public: | ||
RocksdbBurrowStorage( | ||
RocksDbCommon &common, | ||
std::string_view tx_hash, | ||
shared_model::interface::types::CommandIndexType cmd_index); | ||
|
||
expected::Result<std::optional<std::string>, std::string> getAccount( | ||
std::string_view address) override; | ||
|
||
expected::Result<void, std::string> updateAccount( | ||
std::string_view address, std::string_view account) override; | ||
|
||
expected::Result<void, std::string> removeAccount( | ||
std::string_view address) override; | ||
|
||
expected::Result<std::optional<std::string>, std::string> getStorage( | ||
std::string_view address, std::string_view key) override; | ||
|
||
expected::Result<void, std::string> setStorage( | ||
std::string_view address, | ||
std::string_view key, | ||
std::string_view value) override; | ||
|
||
expected::Result<void, std::string> storeLog( | ||
std::string_view address, | ||
std::string_view data, | ||
std::vector<std::string_view> topics) override; | ||
|
||
std::optional<size_t> getCallId() const { | ||
if (call_id_cache_) | ||
return call_id_cache_->first; | ||
return std::nullopt; | ||
} | ||
|
||
expected::Result<void, std::string> initCallId(); | ||
|
||
private: | ||
RocksDbCommon &common_; | ||
std::string_view tx_hash_; | ||
shared_model::interface::types::CommandIndexType cmd_index_; | ||
std::optional<std::pair<size_t, size_t>> call_id_cache_; | ||
}; | ||
|
||
} // namespace iroha::ametsuchi | ||
|
||
#endif // IROHA_RDB_BURROW_STORAGE_HPP |
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
Oops, something went wrong.