-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2431 from Taraxa-project/migrations
Migrations
- Loading branch information
Showing
11 changed files
with
151 additions
and
19 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
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
32 changes: 32 additions & 0 deletions
32
libraries/core_libs/storage/include/storage/migration/migration_base.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
#include "storage/storage.hpp" | ||
|
||
namespace taraxa::storage::migration { | ||
class Base { | ||
public: | ||
Base(std::shared_ptr<DbStorage> db) : db_(std::move(db)), batch_(db_->createWriteBatch()) {} | ||
virtual ~Base() = default; | ||
virtual std::string id() = 0; | ||
// We need to specify version here, so in case of major version change(db reindex) we won't apply unneeded migrations | ||
virtual uint32_t dbVersion() = 0; | ||
|
||
bool isApplied() { return db_->lookup_int<bool>(id(), DB::Columns::migrations).has_value(); } | ||
void apply() { | ||
if (db_->getMajorVersion() != dbVersion()) { | ||
return; | ||
} | ||
migrate(); | ||
setApplied(); | ||
db_->commitWriteBatch(batch_); | ||
} | ||
|
||
protected: | ||
// Method with custom logic. All db changes should be made using `batch_` | ||
virtual void migrate() = 0; | ||
|
||
void setApplied() { db_->insert(batch_, DB::Columns::migrations, id(), true); } | ||
|
||
std::shared_ptr<DbStorage> db_; | ||
DB::Batch batch_; | ||
}; | ||
} // namespace taraxa::storage::migration |
19 changes: 19 additions & 0 deletions
19
libraries/core_libs/storage/include/storage/migration/migration_manager.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
#include "storage/migration/migration_base.hpp" | ||
|
||
namespace taraxa::storage::migration { | ||
class Manager { | ||
public: | ||
explicit Manager(std::shared_ptr<DbStorage> db, const addr_t& node_addr = {}); | ||
template <typename T> | ||
void registerMigration() { | ||
migrations_.push_back(std::make_shared<T>(db_)); | ||
} | ||
void applyAll(); | ||
|
||
private: | ||
std::shared_ptr<DbStorage> db_; | ||
std::vector<std::shared_ptr<migration::Base>> migrations_; | ||
LOG_OBJECTS_DEFINE | ||
}; | ||
} // namespace taraxa::storage::migration |
16 changes: 16 additions & 0 deletions
16
libraries/core_libs/storage/include/storage/migration/transaction_hashes.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
#include <libdevcore/Common.h> | ||
|
||
#include "final_chain/final_chain.hpp" | ||
#include "storage/migration/migration_base.hpp" | ||
#include "transaction/transaction.hpp" | ||
|
||
namespace taraxa::storage::migration { | ||
class TransactionHashes : public migration::Base { | ||
public: | ||
TransactionHashes(std::shared_ptr<DbStorage> db); | ||
std::string id() override; | ||
uint32_t dbVersion() override; | ||
void migrate() override; | ||
}; | ||
} // namespace taraxa::storage::migration |
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
20 changes: 20 additions & 0 deletions
20
libraries/core_libs/storage/src/migration/migration_manager.cpp
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,20 @@ | ||
#include "storage/migration/migration_manager.hpp" | ||
|
||
#include "storage/migration/transaction_hashes.hpp" | ||
|
||
namespace taraxa::storage::migration { | ||
Manager::Manager(std::shared_ptr<DbStorage> db, const addr_t& node_addr) : db_(db) { | ||
LOG_OBJECTS_CREATE("MIGRATIONS"); | ||
registerMigration<migration::TransactionHashes>(); | ||
} | ||
|
||
void Manager::applyAll() { | ||
for (const auto& m : migrations_) { | ||
if (!m->isApplied()) { | ||
LOG(log_nf_) << "Applying migration " << m->id(); | ||
m->apply(); | ||
LOG(log_nf_) << "Migration applied " << m->id(); | ||
} | ||
} | ||
} | ||
} // namespace taraxa::storage::migration |
37 changes: 37 additions & 0 deletions
37
libraries/core_libs/storage/src/migration/transaction_hashes.cpp
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,37 @@ | ||
#include "storage/migration/transaction_hashes.hpp" | ||
|
||
namespace taraxa::storage::migration { | ||
struct OldTransactionsHashes { | ||
std::string serialized_; | ||
size_t count_; | ||
|
||
explicit OldTransactionsHashes(std::string serialized) | ||
: serialized_(std::move(serialized)), count_(serialized_.size() / dev::h256::size) {} | ||
dev::h256 get(size_t i) const { | ||
return dev::h256(reinterpret_cast<const uint8_t*>(serialized_.data() + i * dev::h256::size), | ||
dev::h256::ConstructFromPointer); | ||
} | ||
size_t count() const { return count_; } | ||
}; | ||
|
||
TransactionHashes::TransactionHashes(std::shared_ptr<DbStorage> db) : migration::Base(db) {} | ||
|
||
std::string TransactionHashes::id() { return "TransactionHashes"; } | ||
|
||
uint32_t TransactionHashes::dbVersion() { return 1; } | ||
|
||
void TransactionHashes::migrate() { | ||
auto it = db_->getColumnIterator(DB::Columns::final_chain_transaction_hashes_by_blk_number); | ||
|
||
// Get and save data in new format for all blocks | ||
for (it->SeekToFirst(); it->Valid(); it->Next()) { | ||
::taraxa::TransactionHashes new_data; | ||
auto old_data = std::make_unique<OldTransactionsHashes>(it->value().ToString()); | ||
new_data.reserve(old_data->count()); | ||
for (size_t i = 0; i < new_data.capacity(); ++i) { | ||
new_data.emplace_back(old_data->get(i)); | ||
} | ||
db_->insert(batch_, DB::Columns::final_chain_transaction_hashes_by_blk_number, it->key(), dev::rlp(new_data)); | ||
} | ||
} | ||
} // namespace taraxa::storage::migration |
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