From 915a187354df285b605157e7d33c8d01d41cf25e Mon Sep 17 00:00:00 2001 From: Nischal Sheth Date: Tue, 28 Feb 2017 12:56:12 -0800 Subject: [PATCH] Allow search string in introspect to be a perl-style regex Change-Id: I8ef12a4fe15416e20ef4d97fe284d947c1d85c56 Closes-Bug: 1664757 --- src/ifmap/ifmap_server.cc | 24 +++++++++------------- src/ifmap/ifmap_server_show.cc | 37 ++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/ifmap/ifmap_server.cc b/src/ifmap/ifmap_server.cc index 3b44582fed9..3ee10ef966a 100644 --- a/src/ifmap/ifmap_server.cc +++ b/src/ifmap/ifmap_server.cc @@ -9,6 +9,7 @@ #include #include +#include #include "base/logging.h" #include "base/task_annotations.h" @@ -37,6 +38,8 @@ #include "sandesh/sandesh.h" #include "control-node/sandesh/control_node_types.h" +using boost::regex; +using boost::regex_search; using std::make_pair; class IFMapServer::IFMapVmSubscribe : public Task { @@ -339,15 +342,12 @@ IFMapNode *IFMapServer::GetVmNodeByUuid(const std::string &vm_uuid) { void IFMapServer::FillClientMap(IFMapServerShowClientMap *out_map, const std::string &search_string) { + regex search_expr(search_string); out_map->set_table_count(client_map_.size()); - if (search_string.empty()) { - out_map->clients.reserve(client_map_.size()); - } for (ClientMap::const_iterator iter = client_map_.begin(); iter != client_map_.end(); ++iter) { IFMapClient *client = iter->second; - if (!search_string.empty() && - (client->identifier().find(search_string) == std::string::npos)) { + if (!regex_search(client->identifier(), search_expr)) { continue; } IFMapServerClientMapShowEntry entry; @@ -363,15 +363,12 @@ void IFMapServer::FillClientMap(IFMapServerShowClientMap *out_map, void IFMapServer::FillIndexMap(IFMapServerShowIndexMap *out_map, const std::string &search_string) { + regex search_expr(search_string); out_map->set_table_count(index_map_.size()); - if (search_string.empty()) { - out_map->clients.reserve(index_map_.size()); - } for (IndexMap::const_iterator iter = index_map_.begin(); iter != index_map_.end(); ++iter) { IFMapClient *client = iter->second; - if (!search_string.empty() && - (client->identifier().find(search_string) == std::string::npos)) { + if (!regex_search(client->identifier(), search_expr)) { continue; } IFMapServerIndexMapShowEntry entry; @@ -392,15 +389,12 @@ const std::string IFMapServer::ClientHistoryInfo::history_created_at_str() const void IFMapServer::FillClientHistory(IFMapServerClientHistoryList *out_list, const std::string &search_string) { + regex search_expr(search_string); out_list->set_table_count(client_history_.size()); - if (search_string.empty()) { - out_list->clients.reserve(client_history_.size()); - } for (ClientHistory::const_iterator iter = client_history_.begin(); iter != client_history_.end(); ++iter) { ClientHistoryInfo info = *iter; - if (!search_string.empty() && - (info.client_name.find(search_string) == std::string::npos)) { + if (!regex_search(info.client_name, search_expr)) { continue; } IFMapServerClientHistoryEntry entry; diff --git a/src/ifmap/ifmap_server_show.cc b/src/ifmap/ifmap_server_show.cc index 47d936d325b..b8133a3914f 100644 --- a/src/ifmap/ifmap_server_show.cc +++ b/src/ifmap/ifmap_server_show.cc @@ -8,6 +8,7 @@ #include #include +#include #include "base/logging.h" #include "db/db.h" #include "db/db_graph.h" @@ -34,6 +35,8 @@ #include +using boost::regex; +using boost::regex_search; using namespace boost::assign; using namespace std; using namespace pugi; @@ -165,7 +168,6 @@ class ShowIFMapTable { bool ShowIFMapTable::TableToBuffer(const IFMapTableShowReq *request, IFMapTable *table, IFMapServer *server, const string &last_node_name, ShowData *show_data) { - DBEntryBase *src = NULL; if (last_node_name.length()) { // If the last_node_name is set, it was the last node printed in the @@ -181,20 +183,20 @@ bool ShowIFMapTable::TableToBuffer(const IFMapTableShowReq *request, } bool buffer_full = false; - string search_string = request->get_search_string(); + regex search_expr(request->get_search_string()); DBTablePartBase *partition = table->GetTablePartition(0); if (!src) { src = partition->GetFirst(); } for (; src != NULL; src = partition->GetNext(src)) { IFMapNode *src_node = static_cast(src); - if (!search_string.empty() && - (src_node->ToString().find(search_string) == string::npos)) { + if (!regex_search(src_node->ToString(), search_expr)) { continue; } IFMapNodeShowInfo dest; IFMapNodeCopier copyNode(&dest, src, server); show_data->send_buffer.push_back(dest); + // If we have picked up enough nodes for this round... if (show_data->send_buffer.size() == kMaxElementsPerRound) { // Save the values needed for the next round. When we come @@ -479,7 +481,8 @@ class ShowIFMapLinkTable { } static bool IncludeLink(DBEntryBase *src, const string &search_string, - const string &metadata); + const regex &search_expr, const string &metadata, + const regex &metadata_expr); static void CopyNode(IFMapLinkShowInfo *dest, DBEntryBase *src, IFMapServer *server); static bool BufferStageCommon(const IFMapLinkTableShowReq *request, @@ -507,23 +510,25 @@ class ShowIFMapLinkTable { }; bool ShowIFMapLinkTable::IncludeLink(DBEntryBase *src, - const string &search_string, const string &metadata) { + const string &search_string, const regex &search_expr, + const string &metadata, const regex &metadata_expr) { IFMapLink *link = static_cast(src); IFMapNode *left = link->left(); IFMapNode *right = link->right(); - // If we do not find the search string in the names of either of the 2 ends, - // do not include the link + // If we do not find the search string in the names of either of the + // two ends, do not include the link. if (!search_string.empty() && - (!left || (left->ToString().find(search_string) == string::npos)) && - (!right || (right->ToString().find(search_string) == string::npos))) { + (!left || !regex_search(left->ToString(), search_expr)) && + (!right || !regex_search(right->ToString(), search_expr))) { return false; } - // If the metadata does not match, do not include the link - if (!metadata.empty() && - (link->metadata().find(metadata) == string::npos)) { + + // If the metadata does not match, do not include the link. + if (!metadata.empty() && !regex_search(link->metadata(), metadata_expr)) { return false; } + return true; } @@ -619,6 +624,8 @@ bool ShowIFMapLinkTable::BufferStageCommon(const IFMapLinkTableShowReq *request, IFMapLinkTable *table = static_cast( sctx->ifmap_server()->database()->FindTable("__ifmap_metadata__.0")); if (table) { + regex search_expr(request->get_search_string()); + regex metadata_expr(request->get_metadata()); ShowData *show_data = static_cast(data); show_data->send_buffer.reserve(kMaxElementsPerRound); show_data->table_size = table->Size(); @@ -631,8 +638,8 @@ bool ShowIFMapLinkTable::BufferStageCommon(const IFMapLinkTableShowReq *request, src = partition->GetFirst(); } for (; src != NULL; src = partition->GetNext(src)) { - if (IncludeLink(src, request->get_search_string(), - request->get_metadata())) { + if (IncludeLink(src, request->get_search_string(), search_expr, + request->get_metadata(), metadata_expr)) { IFMapLinkShowInfo dest; CopyNode(&dest, src, sctx->ifmap_server()); show_data->send_buffer.push_back(dest);