chore(categorizer): add category resolver interface

This commit is contained in:
Marcus Holland-Moritz 2023-12-17 09:07:03 +01:00
parent 6228677672
commit e2606226bd
8 changed files with 161 additions and 13 deletions

View File

@ -380,6 +380,7 @@ list(
src/dwarfs/file_scanner.cpp src/dwarfs/file_scanner.cpp
src/dwarfs/file_stat.cpp src/dwarfs/file_stat.cpp
src/dwarfs/file_type.cpp src/dwarfs/file_type.cpp
src/dwarfs/filesystem_block_category_resolver.cpp
src/dwarfs/filesystem_extractor.cpp src/dwarfs/filesystem_extractor.cpp
src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_v2.cpp
src/dwarfs/filesystem_writer.cpp src/dwarfs/filesystem_writer.cpp

View File

@ -31,6 +31,7 @@
#include <span> #include <span>
#include <string_view> #include <string_view>
#include "dwarfs/category_resolver.h"
#include "dwarfs/inode_fragments.h" #include "dwarfs/inode_fragments.h"
namespace boost::program_options { namespace boost::program_options {
@ -124,7 +125,7 @@ class categorizer_job {
std::unique_ptr<impl> impl_; std::unique_ptr<impl> impl_;
}; };
class categorizer_manager { class categorizer_manager : public category_resolver {
public: public:
categorizer_manager(logger& lgr); categorizer_manager(logger& lgr);
@ -136,12 +137,13 @@ class categorizer_manager {
return impl_->job(path); 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); return impl_->category_name(c);
} }
std::optional<fragment_category::value_type> std::optional<fragment_category::value_type>
category_value(std::string_view name) const { category_value(std::string_view name) const override {
return impl_->category_value(name); return impl_->category_value(name);
} }

View File

@ -28,17 +28,17 @@
namespace dwarfs { namespace dwarfs {
class categorizer_manager; class category_resolver;
class category_parser { class category_parser {
public: public:
category_parser(std::shared_ptr<categorizer_manager> catmgr); category_parser(std::shared_ptr<category_resolver const> resolver);
std::vector<fragment_category::value_type> parse(std::string_view arg) const; std::vector<fragment_category::value_type> parse(std::string_view arg) const;
std::string to_string(fragment_category::value_type const& val) const; std::string to_string(fragment_category::value_type const& val) const;
private: private:
std::shared_ptr<categorizer_manager> catmgr_; std::shared_ptr<category_resolver const> resolver_;
}; };
} // namespace dwarfs } // namespace dwarfs

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <optional>
#include <string_view>
#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<fragment_category::value_type>
category_value(std::string_view name) const = 0;
};
} // namespace dwarfs

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#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<std::string> categories);
std::string_view
category_name(fragment_category::value_type c) const override;
std::optional<fragment_category::value_type>
category_value(std::string_view name) const override;
private:
std::vector<std::string> categories_;
std::unordered_map<std::string_view, fragment_category::value_type>
category_map_;
};
} // namespace dwarfs

View File

@ -49,6 +49,7 @@ struct iovec_read_buf;
struct file_stat; struct file_stat;
struct vfs_stat; struct vfs_stat;
class category_resolver;
class filesystem_writer; class filesystem_writer;
class history; class history;
class logger; class logger;

View File

@ -23,19 +23,20 @@
#include <folly/String.h> #include <folly/String.h>
#include "dwarfs/categorizer.h"
#include "dwarfs/category_parser.h" #include "dwarfs/category_parser.h"
#include "dwarfs/category_resolver.h"
namespace dwarfs { namespace dwarfs {
category_parser::category_parser(std::shared_ptr<categorizer_manager> catmgr) category_parser::category_parser(
: catmgr_{catmgr} {} std::shared_ptr<category_resolver const> resolver)
: resolver_{resolver} {}
std::vector<fragment_category::value_type> std::vector<fragment_category::value_type>
category_parser::parse(std::string_view arg) const { category_parser::parse(std::string_view arg) const {
if (!catmgr_) { if (!resolver_) {
throw std::runtime_error( throw std::runtime_error(
"cannot configure category-specific options without any categorizers"); "cannot configure category-specific options without any categories");
} }
std::vector<fragment_category::value_type> rv; std::vector<fragment_category::value_type> rv;
@ -45,7 +46,7 @@ category_parser::parse(std::string_view arg) const {
rv.reserve(categories.size()); rv.reserve(categories.size());
for (auto const& name : categories) { for (auto const& name : categories) {
if (auto val = catmgr_->category_value(name)) { if (auto val = resolver_->category_value(name)) {
rv.emplace_back(*val); rv.emplace_back(*val);
} else { } else {
throw std::range_error(fmt::format("unknown category: '{}'", name)); throw std::range_error(fmt::format("unknown category: '{}'", name));
@ -57,7 +58,7 @@ category_parser::parse(std::string_view arg) const {
std::string std::string
category_parser::to_string(fragment_category::value_type const& val) const { 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 } // namespace dwarfs

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <folly/container/Enumerate.h>
#include "dwarfs/filesystem_block_category_resolver.h"
namespace dwarfs {
filesystem_block_category_resolver::filesystem_block_category_resolver(
std::vector<std::string> 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<fragment_category::value_type>
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