mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 20:41:04 -04:00
chore(categorizer): add category resolver interface
This commit is contained in:
parent
6228677672
commit
e2606226bd
@ -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
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
#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> 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<fragment_category::value_type>
|
||||
category_value(std::string_view name) const {
|
||||
category_value(std::string_view name) const override {
|
||||
return impl_->category_value(name);
|
||||
}
|
||||
|
||||
|
@ -28,17 +28,17 @@
|
||||
|
||||
namespace dwarfs {
|
||||
|
||||
class categorizer_manager;
|
||||
class category_resolver;
|
||||
|
||||
class category_parser {
|
||||
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::string to_string(fragment_category::value_type const& val) const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<categorizer_manager> catmgr_;
|
||||
std::shared_ptr<category_resolver const> resolver_;
|
||||
};
|
||||
|
||||
} // namespace dwarfs
|
||||
|
41
include/dwarfs/category_resolver.h
Normal file
41
include/dwarfs/category_resolver.h
Normal 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
|
49
include/dwarfs/filesystem_block_category_resolver.h
Normal file
49
include/dwarfs/filesystem_block_category_resolver.h
Normal 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
|
@ -49,6 +49,7 @@ struct iovec_read_buf;
|
||||
struct file_stat;
|
||||
struct vfs_stat;
|
||||
|
||||
class category_resolver;
|
||||
class filesystem_writer;
|
||||
class history;
|
||||
class logger;
|
||||
|
@ -23,19 +23,20 @@
|
||||
|
||||
#include <folly/String.h>
|
||||
|
||||
#include "dwarfs/categorizer.h"
|
||||
#include "dwarfs/category_parser.h"
|
||||
#include "dwarfs/category_resolver.h"
|
||||
|
||||
namespace dwarfs {
|
||||
|
||||
category_parser::category_parser(std::shared_ptr<categorizer_manager> catmgr)
|
||||
: catmgr_{catmgr} {}
|
||||
category_parser::category_parser(
|
||||
std::shared_ptr<category_resolver const> resolver)
|
||||
: resolver_{resolver} {}
|
||||
|
||||
std::vector<fragment_category::value_type>
|
||||
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<fragment_category::value_type> 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
|
||||
|
53
src/dwarfs/filesystem_block_category_resolver.cpp
Normal file
53
src/dwarfs/filesystem_block_category_resolver.cpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user