Skip to content

Commit

Permalink
Merge "Allow search string in introspect to be a perl-style regex"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Mar 2, 2017
2 parents 028debc + 915a187 commit 63403b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
24 changes: 9 additions & 15 deletions src/ifmap/ifmap_server.cc
Expand Up @@ -9,6 +9,7 @@

#include <boost/asio/io_service.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>

#include "base/logging.h"
#include "base/task_annotations.h"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
37 changes: 22 additions & 15 deletions src/ifmap/ifmap_server_show.cc
Expand Up @@ -8,6 +8,7 @@

#include <boost/bind.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/regex.hpp>
#include "base/logging.h"
#include "db/db.h"
#include "db/db_graph.h"
Expand All @@ -34,6 +35,8 @@

#include <pugixml/pugixml.hpp>

using boost::regex;
using boost::regex_search;
using namespace boost::assign;
using namespace std;
using namespace pugi;
Expand Down Expand Up @@ -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
Expand All @@ -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<IFMapNode *>(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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<IFMapLink *>(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;
}

Expand Down Expand Up @@ -619,6 +624,8 @@ bool ShowIFMapLinkTable::BufferStageCommon(const IFMapLinkTableShowReq *request,
IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
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<ShowData *>(data);
show_data->send_buffer.reserve(kMaxElementsPerRound);
show_data->table_size = table->Size();
Expand All @@ -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);
Expand Down

0 comments on commit 63403b6

Please sign in to comment.