#include "spark_data_directory.h" #include "lib/config/config.h" #include "lib/config/protos/system_config.pb.h" #include "lib/logging/logging.h" #include "lib/os/os.h" #include "lib/utils/file_utils.h" #include #include #include namespace bts::spark { SparkDataDirectory::SparkDataDirectory() : SparkDataDirectory(config::get().data_path()) {} SparkDataDirectory::SparkDataDirectory(const std::string &root) : root_(root) {} boost::filesystem::path SparkDataDirectory::root() const { return root_; } boost::filesystem::path SparkDataDirectory::config_json_dir() const { auto p = root_; if (p.string().ends_with('/') && p.has_parent_path()) { p = p.parent_path(); } if (p.has_parent_path()) { p = p.parent_path(); } return p / "conf/"; } boost::filesystem::path SparkDataDirectory::config_json_file( const std::string &name) const { return config_json_dir() / (name + ".json"); } boost::filesystem::path SparkDataDirectory::log_dir() const { return root_ / fmt::format("logging.{}/", os::get_application_name()); } boost::filesystem::path SparkDataDirectory::log_file() const { return log_dir() / "log"; } boost::filesystem::path SparkDataDirectory::log_cache_file() const { return log_dir() / "log_cache"; } boost::filesystem::path SparkDataDirectory::database_dir( const std::string &name) const { return root_ / "databases" / (name + "/"); } boost::filesystem::path SparkDataDirectory::database_file( const std::string &name) const { return root_ / "databases" / name / (name + ".sqlite"); } boost::filesystem::path SparkDataDirectory::database_file( const std::string &dir_name, const std::string &db_name) const { return root_ / "databases" / dir_name / (db_name + ".sqlite"); } std::optional SparkDataDirectory::load_config_json( const std::string &name) const { const auto json_file = config_json_file(name); if (!boost::filesystem::exists(json_file)) { return {}; } const std::string json_str = utils::read_file(json_file.string()); try { return nlohmann::json::parse(json_str); } catch (const nlohmann::json::exception &ex) { LOG(Gen, Warn, "Failed to parse {}. ex: {}", json_file.string(), std::string{ex.what()}); } return {}; } } // namespace bts::spark