From a44bc233f57fd4e3a378bdbdbcb645d6a03d6e20 Mon Sep 17 00:00:00 2001 From: "Raphael S. Carvalho" Date: Wed, 3 Mar 2021 12:33:48 -0300 Subject: [PATCH] compaction: refactor mapping of compaction type to string This will make it easier to introduce new type and also to map type to string and vice-versa, using reverse lookup. Signed-off-by: Raphael S. Carvalho --- sstables/compaction.cc | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/sstables/compaction.cc b/sstables/compaction.cc index c6036243d1..42bef400af 100644 --- a/sstables/compaction.cc +++ b/sstables/compaction.cc @@ -74,27 +74,23 @@ namespace sstables { logging::logger clogger("compaction"); +static const std::unordered_map compaction_types = { + { compaction_type::Compaction, "COMPACTION" }, + { compaction_type::Cleanup, "CLEANUP" }, + { compaction_type::Validation, "VALIDATION" }, + { compaction_type::Scrub, "SCRUB" }, + { compaction_type::Index_build, "INDEX_BUILD" }, + { compaction_type::Reshard, "RESHARD" }, + { compaction_type::Upgrade, "UPGRADE" }, + { compaction_type::Reshape, "RESHAPE" }, +}; + sstring compaction_name(compaction_type type) { - switch (type) { - case compaction_type::Compaction: - return "COMPACTION"; - case compaction_type::Cleanup: - return "CLEANUP"; - case compaction_type::Validation: - return "VALIDATION"; - case compaction_type::Scrub: - return "SCRUB"; - case compaction_type::Index_build: - return "INDEX_BUILD"; - case compaction_type::Reshard: - return "RESHARD"; - case compaction_type::Upgrade: - return "UPGRADE"; - case compaction_type::Reshape: - return "RESHAPE"; - default: - throw std::runtime_error("Invalid Compaction Type"); + auto ret = compaction_types.find(type); + if (ret != compaction_types.end()) { + return ret->second; } + throw std::runtime_error("Invalid Compaction Type"); } static std::string_view to_string(compaction_type type) {