mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-26 14:45:52 +00:00
144 lines
5.3 KiB
C++
144 lines
5.3 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of clio: https://github.com/XRPLF/clio
|
|
Copyright (c) 2023, the clio developers.
|
|
|
|
Permission to use, copy, modify, and distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include "data/cassandra/SettingsProvider.hpp"
|
|
|
|
#include "data/cassandra/Types.hpp"
|
|
#include "data/cassandra/impl/Cluster.hpp"
|
|
#include "util/Constants.hpp"
|
|
#include "util/config/Config.hpp"
|
|
|
|
#include <boost/json/conversion.hpp>
|
|
#include <boost/json/value.hpp>
|
|
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <ios>
|
|
#include <iterator>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <system_error>
|
|
|
|
namespace data::cassandra {
|
|
|
|
namespace impl {
|
|
inline Settings::ContactPoints
|
|
tag_invoke(boost::json::value_to_tag<Settings::ContactPoints>, boost::json::value const& value)
|
|
{
|
|
if (not value.is_object()) {
|
|
throw std::runtime_error("Feed entire Cassandra section to parse Settings::ContactPoints instead");
|
|
}
|
|
|
|
util::Config const obj{value};
|
|
Settings::ContactPoints out;
|
|
|
|
out.contactPoints = obj.valueOrThrow<std::string>("contact_points", "`contact_points` must be a string");
|
|
out.port = obj.maybeValue<uint16_t>("port");
|
|
|
|
return out;
|
|
}
|
|
|
|
inline Settings::SecureConnectionBundle
|
|
tag_invoke(boost::json::value_to_tag<Settings::SecureConnectionBundle>, boost::json::value const& value)
|
|
{
|
|
if (not value.is_string())
|
|
throw std::runtime_error("`secure_connect_bundle` must be a string");
|
|
return Settings::SecureConnectionBundle{value.as_string().data()};
|
|
}
|
|
} // namespace impl
|
|
|
|
SettingsProvider::SettingsProvider(util::Config const& cfg)
|
|
: config_{cfg}
|
|
, keyspace_{cfg.valueOr<std::string>("keyspace", "clio")}
|
|
, tablePrefix_{cfg.maybeValue<std::string>("table_prefix")}
|
|
, replicationFactor_{cfg.valueOr<uint16_t>("replication_factor", 3)}
|
|
, settings_{parseSettings()}
|
|
{
|
|
}
|
|
|
|
Settings
|
|
SettingsProvider::getSettings() const
|
|
{
|
|
return settings_;
|
|
}
|
|
|
|
std::optional<std::string>
|
|
SettingsProvider::parseOptionalCertificate() const
|
|
{
|
|
if (auto const certPath = config_.maybeValue<std::string>("certfile"); certPath) {
|
|
auto const path = std::filesystem::path(*certPath);
|
|
std::ifstream fileStream(path.string(), std::ios::in);
|
|
if (!fileStream) {
|
|
throw std::system_error(errno, std::generic_category(), "Opening certificate " + path.string());
|
|
}
|
|
|
|
std::string contents(std::istreambuf_iterator<char>{fileStream}, std::istreambuf_iterator<char>{});
|
|
if (fileStream.bad()) {
|
|
throw std::system_error(errno, std::generic_category(), "Reading certificate " + path.string());
|
|
}
|
|
|
|
return contents;
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
Settings
|
|
SettingsProvider::parseSettings() const
|
|
{
|
|
auto settings = Settings::defaultSettings();
|
|
if (auto const bundle = config_.maybeValue<Settings::SecureConnectionBundle>("secure_connect_bundle"); bundle) {
|
|
settings.connectionInfo = *bundle;
|
|
} else {
|
|
settings.connectionInfo =
|
|
config_.valueOrThrow<Settings::ContactPoints>("Missing contact_points in Cassandra config");
|
|
}
|
|
|
|
settings.threads = config_.valueOr<uint32_t>("threads", settings.threads);
|
|
settings.maxWriteRequestsOutstanding =
|
|
config_.valueOr<uint32_t>("max_write_requests_outstanding", settings.maxWriteRequestsOutstanding);
|
|
settings.maxReadRequestsOutstanding =
|
|
config_.valueOr<uint32_t>("max_read_requests_outstanding", settings.maxReadRequestsOutstanding);
|
|
settings.coreConnectionsPerHost =
|
|
config_.valueOr<uint32_t>("core_connections_per_host", settings.coreConnectionsPerHost);
|
|
settings.queueSizeIO = config_.maybeValue<uint32_t>("queue_size_io");
|
|
settings.writeBatchSize = config_.valueOr<std::size_t>("write_batch_size", settings.writeBatchSize);
|
|
|
|
auto const connectTimeoutSecond = config_.maybeValue<uint32_t>("connect_timeout");
|
|
if (connectTimeoutSecond)
|
|
settings.connectionTimeout = std::chrono::milliseconds{*connectTimeoutSecond * util::MILLISECONDS_PER_SECOND};
|
|
|
|
auto const requestTimeoutSecond = config_.maybeValue<uint32_t>("request_timeout");
|
|
if (requestTimeoutSecond)
|
|
settings.requestTimeout = std::chrono::milliseconds{*requestTimeoutSecond * util::MILLISECONDS_PER_SECOND};
|
|
|
|
settings.certificate = parseOptionalCertificate();
|
|
settings.username = config_.maybeValue<std::string>("username");
|
|
settings.password = config_.maybeValue<std::string>("password");
|
|
|
|
return settings;
|
|
}
|
|
|
|
} // namespace data::cassandra
|