From e2606226bd8b900f4490f3ad773f8b89d8ad9bd6 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 17 Dec 2023 09:07:03 +0100 Subject: [PATCH] chore(categorizer): add category resolver interface --- CMakeLists.txt | 1 + include/dwarfs/categorizer.h | 8 +-- include/dwarfs/category_parser.h | 6 +-- include/dwarfs/category_resolver.h | 41 ++++++++++++++ .../filesystem_block_category_resolver.h | 49 +++++++++++++++++ include/dwarfs/filesystem_v2.h | 1 + src/dwarfs/category_parser.cpp | 15 +++--- .../filesystem_block_category_resolver.cpp | 53 +++++++++++++++++++ 8 files changed, 161 insertions(+), 13 deletions(-) create mode 100644 include/dwarfs/category_resolver.h create mode 100644 include/dwarfs/filesystem_block_category_resolver.h create mode 100644 src/dwarfs/filesystem_block_category_resolver.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a98a067..752929fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -380,6 +380,7 @@ list( src/dwarfs/file_scanner.cpp src/dwarfs/file_stat.cpp src/dwarfs/file_type.cpp + src/dwarfs/filesystem_block_category_resolver.cpp src/dwarfs/filesystem_extractor.cpp src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_writer.cpp diff --git a/include/dwarfs/categorizer.h b/include/dwarfs/categorizer.h index 9774f7a4..7f881f89 100644 --- a/include/dwarfs/categorizer.h +++ b/include/dwarfs/categorizer.h @@ -31,6 +31,7 @@ #include #include +#include "dwarfs/category_resolver.h" #include "dwarfs/inode_fragments.h" namespace boost::program_options { @@ -124,7 +125,7 @@ class categorizer_job { std::unique_ptr impl_; }; -class categorizer_manager { +class categorizer_manager : public category_resolver { public: categorizer_manager(logger& lgr); @@ -136,12 +137,13 @@ class categorizer_manager { return impl_->job(path); } - std::string_view category_name(fragment_category::value_type c) const { + std::string_view + category_name(fragment_category::value_type c) const override { return impl_->category_name(c); } std::optional - category_value(std::string_view name) const { + category_value(std::string_view name) const override { return impl_->category_value(name); } diff --git a/include/dwarfs/category_parser.h b/include/dwarfs/category_parser.h index 607f5205..cc819bad 100644 --- a/include/dwarfs/category_parser.h +++ b/include/dwarfs/category_parser.h @@ -28,17 +28,17 @@ namespace dwarfs { -class categorizer_manager; +class category_resolver; class category_parser { public: - category_parser(std::shared_ptr catmgr); + category_parser(std::shared_ptr resolver); std::vector parse(std::string_view arg) const; std::string to_string(fragment_category::value_type const& val) const; private: - std::shared_ptr catmgr_; + std::shared_ptr resolver_; }; } // namespace dwarfs diff --git a/include/dwarfs/category_resolver.h b/include/dwarfs/category_resolver.h new file mode 100644 index 00000000..5c18a14d --- /dev/null +++ b/include/dwarfs/category_resolver.h @@ -0,0 +1,41 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#pragma once + +#include +#include + +#include "dwarfs/fragment_category.h" + +namespace dwarfs { + +class category_resolver { + public: + virtual ~category_resolver() = default; + + virtual std::string_view + category_name(fragment_category::value_type c) const = 0; + virtual std::optional + category_value(std::string_view name) const = 0; +}; + +} // namespace dwarfs diff --git a/include/dwarfs/filesystem_block_category_resolver.h b/include/dwarfs/filesystem_block_category_resolver.h new file mode 100644 index 00000000..8778f5b9 --- /dev/null +++ b/include/dwarfs/filesystem_block_category_resolver.h @@ -0,0 +1,49 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#pragma once + +#include +#include +#include + +#include "dwarfs/category_resolver.h" + +namespace dwarfs { + +class filesystem_block_category_resolver : public category_resolver { + public: + filesystem_block_category_resolver() = default; + explicit filesystem_block_category_resolver( + std::vector categories); + + std::string_view + category_name(fragment_category::value_type c) const override; + std::optional + category_value(std::string_view name) const override; + + private: + std::vector categories_; + std::unordered_map + category_map_; +}; + +} // namespace dwarfs diff --git a/include/dwarfs/filesystem_v2.h b/include/dwarfs/filesystem_v2.h index 44a1d856..d00a8b2f 100644 --- a/include/dwarfs/filesystem_v2.h +++ b/include/dwarfs/filesystem_v2.h @@ -49,6 +49,7 @@ struct iovec_read_buf; struct file_stat; struct vfs_stat; +class category_resolver; class filesystem_writer; class history; class logger; diff --git a/src/dwarfs/category_parser.cpp b/src/dwarfs/category_parser.cpp index 1e4e32ba..400f5650 100644 --- a/src/dwarfs/category_parser.cpp +++ b/src/dwarfs/category_parser.cpp @@ -23,19 +23,20 @@ #include -#include "dwarfs/categorizer.h" #include "dwarfs/category_parser.h" +#include "dwarfs/category_resolver.h" namespace dwarfs { -category_parser::category_parser(std::shared_ptr catmgr) - : catmgr_{catmgr} {} +category_parser::category_parser( + std::shared_ptr resolver) + : resolver_{resolver} {} std::vector category_parser::parse(std::string_view arg) const { - if (!catmgr_) { + if (!resolver_) { throw std::runtime_error( - "cannot configure category-specific options without any categorizers"); + "cannot configure category-specific options without any categories"); } std::vector rv; @@ -45,7 +46,7 @@ category_parser::parse(std::string_view arg) const { rv.reserve(categories.size()); for (auto const& name : categories) { - if (auto val = catmgr_->category_value(name)) { + if (auto val = resolver_->category_value(name)) { rv.emplace_back(*val); } else { throw std::range_error(fmt::format("unknown category: '{}'", name)); @@ -57,7 +58,7 @@ category_parser::parse(std::string_view arg) const { std::string category_parser::to_string(fragment_category::value_type const& val) const { - return std::string(catmgr_->category_name(val)); + return std::string(resolver_->category_name(val)); } } // namespace dwarfs diff --git a/src/dwarfs/filesystem_block_category_resolver.cpp b/src/dwarfs/filesystem_block_category_resolver.cpp new file mode 100644 index 00000000..a0e7ef07 --- /dev/null +++ b/src/dwarfs/filesystem_block_category_resolver.cpp @@ -0,0 +1,53 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#include + +#include "dwarfs/filesystem_block_category_resolver.h" + +namespace dwarfs { + +filesystem_block_category_resolver::filesystem_block_category_resolver( + std::vector categories) + : categories_{std::move(categories)} { + for (auto const& [i, name] : folly::enumerate(categories_)) { + if (!category_map_.emplace(name, i).second) { + throw std::runtime_error( + fmt::format("duplicate category name: '{}'", name)); + } + } +} + +std::string_view filesystem_block_category_resolver::category_name( + fragment_category::value_type c) const { + return categories_.at(c); +} + +std::optional +filesystem_block_category_resolver::category_value( + std::string_view name) const { + if (auto it = category_map_.find(name); it != category_map_.end()) { + return it->second; + } + return std::nullopt; +} + +} // namespace dwarfs