[dbg] Hide implementations of shared components

This commit is contained in:
Ming-Hung Tsai 2021-02-03 17:11:24 +08:00
parent b9b04dc872
commit a05ac553b6
5 changed files with 165 additions and 130 deletions

View File

@ -6,7 +6,42 @@ using namespace std;
//----------------------------------------------------------------
namespace {
strings read_input(std::istream &in) {
class command_interpreter_impl : public command_interpreter {
public:
typedef std::shared_ptr<command_interpreter> ptr;
command_interpreter_impl(std::istream &in, std::ostream &out)
: in_(in),
out_(out),
exit_(false) {
}
void register_command(std::string const &str, command::ptr cmd) {
commands_.insert(make_pair(str, cmd));
}
void enter_main_loop() {
while (!exit_)
do_once();
}
void exit_main_loop() {
exit_ = true;
}
private:
void do_once();
std::istream &in_;
std::ostream &out_;
std::map <std::string, command::ptr> commands_;
bool exit_;
};
//--------------------------------
strings read_input(std::istream &in)
{
using namespace boost::algorithm;
std::string input;
@ -21,7 +56,8 @@ namespace {
//----------------------------------------------------------------
void command_interpreter::do_once() {
void command_interpreter_impl::do_once()
{
if (in_.eof())
throw runtime_error("input closed");
@ -42,3 +78,11 @@ void command_interpreter::do_once() {
}
//----------------------------------------------------------------
command_interpreter::ptr
dbg::create_command_interpreter(std::istream &in, std::ostream &out)
{
return command_interpreter::ptr(new command_interpreter_impl(in, out));
}
//----------------------------------------------------------------

View File

@ -26,33 +26,13 @@ namespace dbg {
public:
typedef std::shared_ptr<command_interpreter> ptr;
command_interpreter(std::istream &in, std::ostream &out)
: in_(in),
out_(out),
exit_(false) {
}
void register_command(std::string const &str, command::ptr cmd) {
commands_.insert(make_pair(str, cmd));
}
void enter_main_loop() {
while (!exit_)
do_once();
}
void exit_main_loop() {
exit_ = true;
}
private:
void do_once();
std::istream &in_;
std::ostream &out_;
std::map <std::string, command::ptr> commands_;
bool exit_;
virtual void register_command(std::string const &str, command::ptr cmd) = 0;
virtual void enter_main_loop() = 0;
virtual void exit_main_loop() = 0;
};
command_interpreter::ptr
create_command_interpreter(std::istream &in, std::ostream &out);
}
//----------------------------------------------------------------

View File

@ -8,6 +8,31 @@ using namespace std;
//----------------------------------------------------------------
namespace {
class abstract_formatter : public formatter {
typedef boost::optional<std::string> maybe_string;
void field(std::string const &name, std::string const &value) {
fields_.push_back(field_type(name, value));
}
void child(std::string const &name, formatter::ptr t) {
children_.push_back(field_type(name, t));
}
protected:
typedef boost::variant<std::string, ptr> value;
typedef boost::tuple<std::string, value> field_type;
std::vector<field_type> fields_;
std::vector<field_type> children_;
};
class xml_formatter : public abstract_formatter {
public:
void output(std::ostream &out, int depth = 0,
boost::optional<std::string> name = boost::none);
};
void indent(int depth, std::ostream &out) {
for (int i = 0; i < depth * 2; i++)
out << ' ';
@ -51,3 +76,11 @@ void xml_formatter::output(std::ostream &out,
}
//----------------------------------------------------------------
formatter::ptr
dbg::create_xml_formatter()
{
return formatter::ptr(new xml_formatter());
}
//----------------------------------------------------------------

View File

@ -17,26 +17,10 @@ namespace dbg {
typedef std::shared_ptr<formatter> ptr;
virtual ~formatter() {}
typedef boost::optional<std::string> maybe_string;
void field(std::string const &name, std::string const &value) {
fields_.push_back(field_type(name, value));
}
void child(std::string const &name, formatter::ptr t) {
children_.push_back(field_type(name, t));
}
virtual void field(std::string const &name, std::string const &value) = 0;
virtual void child(std::string const &name, formatter::ptr t) = 0;
virtual void output(std::ostream &out, int depth = 0,
boost::optional<std::string> name = boost::none) = 0;
protected:
typedef boost::variant<std::string, ptr> value;
typedef boost::tuple<std::string, value> field_type;
std::vector<field_type> fields_;
std::vector<field_type> children_;
};
template <typename T>
@ -45,13 +29,7 @@ namespace dbg {
t.field(name, boost::lexical_cast<std::string>(value));
}
//--------------------------------
class xml_formatter : public formatter {
public:
virtual void output(std::ostream &out, int depth = 0,
boost::optional<std::string> name = boost::none);
};
formatter::ptr create_xml_formatter();
}
//----------------------------------------------------------------

View File

@ -60,26 +60,26 @@ namespace {
class exit_handler : public dbg::command {
public:
exit_handler(command_interpreter &interpreter)
exit_handler(command_interpreter::ptr interpreter)
: interpreter_(interpreter) {
}
virtual void exec(strings const &args, ostream &out) {
out << "Goodbye!" << endl;
interpreter_.exit_main_loop();
interpreter_->exit_main_loop();
}
command_interpreter &interpreter_;
command_interpreter::ptr interpreter_;
};
class sm_root_show_traits : public persistent_data::sm_disk_detail::sm_root_traits {
public:
static void show(formatter &f, string const &key,
static void show(formatter::ptr f, string const &key,
persistent_data::sm_disk_detail::sm_root const &value) {
field(f, "nr_blocks", value.nr_blocks_);
field(f, "nr_allocated", value.nr_allocated_);
field(f, "bitmap_root", value.bitmap_root_);
field(f, "ref_count_root", value.ref_count_root_);
field(*f, "nr_blocks", value.nr_blocks_);
field(*f, "nr_allocated", value.nr_allocated_);
field(*f, "bitmap_root", value.bitmap_root_);
field(*f, "ref_count_root", value.ref_count_root_);
}
};
@ -90,47 +90,47 @@ namespace {
}
virtual void exec(strings const &args, ostream &out) {
xml_formatter f;
formatter::ptr f = create_xml_formatter();
thin_provisioning::superblock_detail::superblock const &sb = md_->sb_;
field(f, "csum", sb.csum_);
field(f, "flags", sb.flags_);
field(f, "blocknr", sb.blocknr_);
field(f, "uuid", sb.uuid_); // FIXME: delimit, and handle non-printable chars
field(f, "magic", sb.magic_);
field(f, "version", sb.version_);
field(f, "time", sb.time_);
field(f, "trans_id", sb.trans_id_);
field(f, "metadata_snap", sb.metadata_snap_);
field(*f, "csum", sb.csum_);
field(*f, "flags", sb.flags_);
field(*f, "blocknr", sb.blocknr_);
field(*f, "uuid", sb.uuid_); // FIXME: delimit, and handle non-printable chars
field(*f, "magic", sb.magic_);
field(*f, "version", sb.version_);
field(*f, "time", sb.time_);
field(*f, "trans_id", sb.trans_id_);
field(*f, "metadata_snap", sb.metadata_snap_);
sm_disk_detail::sm_root_disk const *d;
sm_disk_detail::sm_root v;
{
d = reinterpret_cast<sm_disk_detail::sm_root_disk const *>(sb.metadata_space_map_root_);
sm_disk_detail::sm_root_traits::unpack(*d, v);
formatter::ptr f2(new xml_formatter);
sm_root_show_traits::show(*f2, "value", v);
f.child("metadata_space_map_root", f2);
formatter::ptr f2 = create_xml_formatter();
sm_root_show_traits::show(f2, "value", v);
f->child("metadata_space_map_root", f2);
}
{
d = reinterpret_cast<sm_disk_detail::sm_root_disk const *>(sb.data_space_map_root_);
sm_disk_detail::sm_root_traits::unpack(*d, v);
formatter::ptr f2(new xml_formatter);
sm_root_show_traits::show(*f2, "value", v);
f.child("data_space_map_root", f2);
formatter::ptr f2 = create_xml_formatter();
sm_root_show_traits::show(f2, "value", v);
f->child("data_space_map_root", f2);
}
field(f, "data_mapping_root", sb.data_mapping_root_);
field(f, "device_details_root", sb.device_details_root_);
field(f, "data_block_size", sb.data_block_size_);
field(f, "metadata_block_size", sb.metadata_block_size_);
field(f, "metadata_nr_blocks", sb.metadata_nr_blocks_);
field(f, "compat_flags", sb.compat_flags_);
field(f, "compat_ro_flags", sb.compat_ro_flags_);
field(f, "incompat_flags", sb.incompat_flags_);
field(*f, "data_mapping_root", sb.data_mapping_root_);
field(*f, "device_details_root", sb.device_details_root_);
field(*f, "data_block_size", sb.data_block_size_);
field(*f, "metadata_block_size", sb.metadata_block_size_);
field(*f, "metadata_nr_blocks", sb.metadata_nr_blocks_);
field(*f, "compat_flags", sb.compat_flags_);
field(*f, "compat_ro_flags", sb.compat_ro_flags_);
field(*f, "incompat_flags", sb.incompat_flags_);
f.output(out, 0);
f->output(out, 0);
}
private:
@ -141,12 +141,12 @@ namespace {
public:
typedef thin_provisioning::device_tree_detail::device_details_traits value_trait;
static void show(formatter &f, string const &key,
static void show(formatter::ptr f, string const &key,
thin_provisioning::device_tree_detail::device_details const &value) {
field(f, "mapped_blocks", value.mapped_blocks_);
field(f, "transaction_id", value.transaction_id_);
field(f, "creation_time", value.creation_time_);
field(f, "snap_time", value.snapshotted_time_);
field(*f, "mapped_blocks", value.mapped_blocks_);
field(*f, "transaction_id", value.transaction_id_);
field(*f, "creation_time", value.creation_time_);
field(*f, "snap_time", value.snapshotted_time_);
}
};
@ -154,8 +154,8 @@ namespace {
public:
typedef uint64_traits value_trait;
static void show(formatter &f, string const &key, uint64_t const &value) {
field(f, key, boost::lexical_cast<string>(value));
static void show(formatter::ptr f, string const &key, uint64_t const &value) {
field(*f, key, boost::lexical_cast<string>(value));
}
};
@ -163,10 +163,10 @@ namespace {
public:
typedef thin_provisioning::mapping_tree_detail::block_traits value_trait;
static void show(formatter &f, string const &key,
static void show(formatter::ptr f, string const &key,
thin_provisioning::mapping_tree_detail::block_time const &value) {
field(f, "block", value.block_);
field(f, "time", value.time_);
field(*f, "block", value.block_);
field(*f, "time", value.time_);
}
};
@ -174,11 +174,11 @@ namespace {
public:
typedef persistent_data::sm_disk_detail::index_entry_traits value_trait;
static void show(formatter &f, string const &key,
static void show(formatter::ptr f, string const &key,
persistent_data::sm_disk_detail::index_entry const &value) {
field(f, "blocknr", value.blocknr_);
field(f, "nr_free", value.nr_free_);
field(f, "none_free_before", value.none_free_before_);
field(*f, "blocknr", value.blocknr_);
field(*f, "nr_free", value.nr_free_);
field(*f, "none_free_before", value.none_free_before_);
}
};
@ -210,23 +210,23 @@ namespace {
private:
template <typename ST>
void show_node(node_ref<typename ST::value_trait> n, ostream &out) {
xml_formatter f;
formatter::ptr f = create_xml_formatter();
field(f, "csum", n.get_checksum());
field(f, "blocknr", n.get_block_nr());
field(f, "type", n.get_type() == INTERNAL ? "internal" : "leaf");
field(f, "nr_entries", n.get_nr_entries());
field(f, "max_entries", n.get_max_entries());
field(f, "value_size", n.get_value_size());
field(*f, "csum", n.get_checksum());
field(*f, "blocknr", n.get_block_nr());
field(*f, "type", n.get_type() == INTERNAL ? "internal" : "leaf");
field(*f, "nr_entries", n.get_nr_entries());
field(*f, "max_entries", n.get_max_entries());
field(*f, "value_size", n.get_value_size());
for (unsigned i = 0; i < n.get_nr_entries(); i++) {
formatter::ptr f2(new xml_formatter);
formatter::ptr f2 = create_xml_formatter();
field(*f2, "key", n.key_at(i));
ST::show(*f2, "value", n.value_at(i));
f.child(boost::lexical_cast<string>(i), f2);
ST::show(f2, "value", n.value_at(i));
f->child(boost::lexical_cast<string>(i), f2);
}
f.output(out, 0);
f->output(out, 0);
}
metadata::ptr md_;
@ -259,19 +259,19 @@ namespace {
private:
void show_metadata_index(sm_disk_detail::metadata_index const *mdi, block_address nr_indexes, ostream &out) {
xml_formatter f;
field(f, "csum", to_cpu<uint32_t>(mdi->csum_));
field(f, "padding", to_cpu<uint32_t>(mdi->padding_));
field(f, "blocknr", to_cpu<uint64_t>(mdi->blocknr_));
formatter::ptr f = create_xml_formatter();
field(*f, "csum", to_cpu<uint32_t>(mdi->csum_));
field(*f, "padding", to_cpu<uint32_t>(mdi->padding_));
field(*f, "blocknr", to_cpu<uint64_t>(mdi->blocknr_));
sm_disk_detail::index_entry ie;
for (block_address i = 0; i < nr_indexes; i++) {
sm_disk_detail::index_entry_traits::unpack(*(mdi->index + i), ie);
formatter::ptr f2(new xml_formatter);
index_entry_show_traits::show(*f2, "value", ie);
f.child(boost::lexical_cast<string>(i), f2);
formatter::ptr f2 = create_xml_formatter();
index_entry_show_traits::show(f2, "value", ie);
f->child(boost::lexical_cast<string>(i), f2);
}
f.output(out, 0);
f->output(out, 0);
}
unsigned const ENTRIES_PER_BLOCK = (MD_BLOCK_SIZE - sizeof(persistent_data::sm_disk_detail::bitmap_header)) * 4;
@ -286,17 +286,17 @@ namespace {
try {
block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY, 1);
metadata::ptr md(new metadata(bm, false));
command_interpreter interp(cin, cout);
interp.register_command("hello", command::ptr(new hello));
interp.register_command("superblock", command::ptr(new show_superblock(md)));
interp.register_command("m1_node", command::ptr(new show_btree_node<uint64_show_traits>(md)));
interp.register_command("m2_node", command::ptr(new show_btree_node<block_show_traits>(md)));
interp.register_command("detail_node", command::ptr(new show_btree_node<device_details_show_traits>(md)));
interp.register_command("index_block", command::ptr(new show_index_block(md)));
interp.register_command("index_node", command::ptr(new show_btree_node<index_entry_show_traits>(md)));
interp.register_command("help", command::ptr(new help));
interp.register_command("exit", command::ptr(new exit_handler(interp)));
interp.enter_main_loop();
command_interpreter::ptr interp = create_command_interpreter(cin, cout);
interp->register_command("hello", command::ptr(new hello));
interp->register_command("superblock", command::ptr(new show_superblock(md)));
interp->register_command("m1_node", command::ptr(new show_btree_node<uint64_show_traits>(md)));
interp->register_command("m2_node", command::ptr(new show_btree_node<block_show_traits>(md)));
interp->register_command("detail_node", command::ptr(new show_btree_node<device_details_show_traits>(md)));
interp->register_command("index_block", command::ptr(new show_index_block(md)));
interp->register_command("index_node", command::ptr(new show_btree_node<index_entry_show_traits>(md)));
interp->register_command("help", command::ptr(new help));
interp->register_command("exit", command::ptr(new exit_handler(interp)));
interp->enter_main_loop();
} catch (std::exception &e) {
cerr << e.what() << endl;