//------------------------------------------------------------------------------ /* 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 #include #include #include #include #include #include #include #include #include #include #include #include #include namespace data::cassandra { namespace impl { inline Settings::ContactPoints tag_invoke(boost::json::value_to_tag, 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("contact_points", "`contact_points` must be a string"); out.port = obj.maybeValue("port"); return out; } inline Settings::SecureConnectionBundle tag_invoke(boost::json::value_to_tag, 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("keyspace", "clio")} , tablePrefix_{cfg.maybeValue("table_prefix")} , replicationFactor_{cfg.valueOr("replication_factor", 3)} , settings_{parseSettings()} { } Settings SettingsProvider::getSettings() const { return settings_; } std::optional SettingsProvider::parseOptionalCertificate() const { if (auto const certPath = config_.maybeValue("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{fileStream}, std::istreambuf_iterator{}); 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("secure_connect_bundle"); bundle) { settings.connectionInfo = *bundle; } else { settings.connectionInfo = config_.valueOrThrow("Missing contact_points in Cassandra config"); } settings.threads = config_.valueOr("threads", settings.threads); settings.maxWriteRequestsOutstanding = config_.valueOr("max_write_requests_outstanding", settings.maxWriteRequestsOutstanding); settings.maxReadRequestsOutstanding = config_.valueOr("max_read_requests_outstanding", settings.maxReadRequestsOutstanding); settings.coreConnectionsPerHost = config_.valueOr("core_connections_per_host", settings.coreConnectionsPerHost); settings.queueSizeIO = config_.maybeValue("queue_size_io"); settings.writeBatchSize = config_.valueOr("write_batch_size", settings.writeBatchSize); auto const connectTimeoutSecond = config_.maybeValue("connect_timeout"); if (connectTimeoutSecond) settings.connectionTimeout = std::chrono::milliseconds{*connectTimeoutSecond * util::MILLISECONDS_PER_SECOND}; auto const requestTimeoutSecond = config_.maybeValue("request_timeout"); if (requestTimeoutSecond) settings.requestTimeout = std::chrono::milliseconds{*requestTimeoutSecond * util::MILLISECONDS_PER_SECOND}; settings.certificate = parseOptionalCertificate(); settings.username = config_.maybeValue("username"); settings.password = config_.maybeValue("password"); return settings; } } // namespace data::cassandra