Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CassandraMigrationBackend.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/CassandraBackend.hpp"
23#include "data/cassandra/SettingsProvider.hpp"
24#include "data/cassandra/Types.hpp"
25#include "migration/MigratiorStatus.hpp"
26#include "migration/cassandra/impl/CassandraMigrationSchema.hpp"
27#include "migration/cassandra/impl/Spec.hpp"
28#include "util/log/Logger.hpp"
29
30#include <boost/asio/spawn.hpp>
31
32#include <cstdint>
33#include <string>
34#include <utility>
35
36namespace migration::cassandra {
37
43 util::Logger log_{"Migration"};
44 data::cassandra::SettingsProvider settingsProvider_;
45 impl::CassandraMigrationSchema migrationSchema_;
46
47public:
54 : data::cassandra::CassandraBackend{auto{settingsProvider}, false /* not readonly */}
55 , settingsProvider_(std::move(settingsProvider))
56 , migrationSchema_{settingsProvider_}
57 {
58 }
59
69 template <impl::TableSpec TableDesc>
70 void
72 std::int64_t const start,
73 std::int64_t const end,
74 auto const& callback,
75 boost::asio::yield_context yield
76 )
77 {
78 LOG(log_.debug()) << "Travsering token range: " << start << " - " << end
79 << " ; table: " << TableDesc::kTABLE_NAME;
80 // for each table we only have one prepared statement
81 static auto kSTATEMENT_PREPARED =
82 migrationSchema_.getPreparedFullScanStatement(handle_, TableDesc::kTABLE_NAME, TableDesc::kPARTITION_KEY);
83
84 auto const statement = kSTATEMENT_PREPARED.bind(start, end);
85
86 auto const res = this->executor_.read(yield, statement);
87 if (not res) {
88 LOG(log_.error()) << "Could not fetch data from table: " << TableDesc::kTABLE_NAME << " range: " << start
89 << " - " << end << ";" << res.error();
90 return;
91 }
92
93 auto const& results = res.value();
94 if (not results.hasRows()) {
95 LOG(log_.debug()) << "No rows returned - table: " << TableDesc::kTABLE_NAME << " range: " << start << " - "
96 << end;
97 return;
98 }
99
100 for (auto const& row : std::apply(
101 [&](auto... args) { return data::cassandra::extract<decltype(args)...>(results); },
102 typename TableDesc::Row{}
103 )) {
104 callback(row);
105 }
106 }
107};
108} // namespace migration::cassandra
Implements BackendInterface for Cassandra/ScyllaDB.
Definition CassandraBackend.hpp:71
Provides settings for BasicCassandraBackend.
Definition SettingsProvider.hpp:35
Statement bind(Args &&... args) const
Bind the given arguments and produce a ready to execute Statement.
Definition Statement.hpp:171
The backend for the migration. It is a subclass of the CassandraBackend and provides the migration sp...
Definition CassandraMigrationBackend.hpp:42
void migrateInTokenRange(std::int64_t const start, std::int64_t const end, auto const &callback, boost::asio::yield_context yield)
Scan a table in a token range and call the callback for each row.
Definition CassandraMigrationBackend.hpp:71
CassandraMigrationBackend(data::cassandra::SettingsProvider settingsProvider)
Construct a new Cassandra Migration Backend object. The backend is not readonly.
Definition CassandraMigrationBackend.hpp:53
The schema for the migration process. It contains the prepared statements only used for the migration...
Definition CassandraMigrationSchema.hpp:37
data::cassandra::PreparedStatement getPreparedFullScanStatement(data::cassandra::Handle const &handler, std::string const &tableName, std::string const &key)
Get the prepared statement for the full scan of a table.
Definition CassandraMigrationSchema.hpp:60
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:215
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:200
impl::ResultExtractor< Types... > extract(Handle::ResultType const &result)
Extracts the results into series of std::tuple<Types...> by creating a simple wrapper with an STL inp...
Definition Handle.hpp:329
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70