#pragma once #include "data/BackendInterface.hpp" #include "data/CassandraBackend.hpp" #include "data/KeyspaceBackend.hpp" #include "data/LedgerCacheInterface.hpp" #include "data/cassandra/SettingsProvider.hpp" #include "util/config/ConfigDefinition.hpp" #include "util/log/Logger.hpp" #include #include #include #include #include namespace data { /** * @brief A factory function that creates the backend based on a config. * * @param config The clio config to use * @param cache The ledger cache to use * @return A shared_ptr with the selected implementation */ inline std::shared_ptr makeBackend(util::config::ClioConfigDefinition const& config, data::LedgerCacheInterface& cache) { using namespace cassandra::impl; static util::Logger const log{"Backend"}; // NOLINT(readability-identifier-naming) LOG(log.info()) << "Constructing BackendInterface"; auto const readOnly = config.get("read_only"); auto const type = config.get("database.type"); std::shared_ptr backend = nullptr; if (boost::iequals(type, "cassandra")) { auto const cfg = config.getObject("database." + type); if (providerFromString(cfg.getValueView("provider").asString()) == Provider::Keyspace) { backend = std::make_shared( data::cassandra::SettingsProvider{cfg}, cache, readOnly ); } else { backend = std::make_shared( data::cassandra::SettingsProvider{cfg}, cache, readOnly ); } } if (!backend) throw std::runtime_error("Invalid database type"); auto const rng = backend->hardFetchLedgerRangeNoThrow(); if (rng) backend->setRange(rng->minSequence, rng->maxSequence); LOG(log.info()) << "Constructed BackendInterface Successfully"; return backend; } } // namespace data