From 95a96cffc3007fe6ec7eec79ed997ff8c9e67e13 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 19 Dec 2021 14:43:35 +0800 Subject: [PATCH] mgr: refactor DaemonState::set_metadata() restructure DaemonState::set_metadata() using for_each_pair() for better readability. Signed-off-by: Kefu Chai --- src/mgr/DaemonState.h | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/mgr/DaemonState.h b/src/mgr/DaemonState.h index d5866299b1470..556439afea461 100644 --- a/src/mgr/DaemonState.h +++ b/src/mgr/DaemonState.h @@ -167,27 +167,29 @@ class DaemonState devices.clear(); devices_bypath.clear(); metadata = m; - auto p = m.find("device_ids"); - if (p != m.end()) { + if (auto found = m.find("device_ids"); found != m.end()) { + auto& device_ids = found->second; std::map paths; // devname -> id or path - auto devs = get_str_map(p->second, ",; "); - auto q = m.find("device_paths"); - if (q != m.end()) { - get_str_map(q->second, &paths, ",; "); + if (auto found = m.find("device_paths"); found != m.end()) { + get_str_map(found->second, &paths, ",; "); } - for (auto& i : devs) { - if (i.second.size()) { // skip blank ids - devices[i.second] = i.first; // id -> devname - auto j = paths.find(i.first); - if (j != paths.end()) { - devices_bypath[i.second] = j->second; // id -> path + for_each_pair( + device_ids, ",; ", + [&paths, this](std::string_view devname, std::string_view id) { + // skip blank ids + if (id.empty()) { + return; } - } - } + // id -> devname + devices.emplace(id, devname); + if (auto path = paths.find(std::string(id)); path != paths.end()) { + // id -> path + devices_bypath.emplace(id, path->second); + } + }); } - p = m.find("hostname"); - if (p != m.end()) { - hostname = p->second; + if (auto found = m.find("hostname"); found != m.end()) { + hostname = found->second; } } -- 2.39.5