size_t RegSearch::Search( NodePtr root, 
                          const string& pattern,
                          int depth) 
{
    nodes.clear();
    size_t results = 0;
    try {
        RegPattern thePattern(pattern);
        regPattern = &thePattern;

        root->ForEach([=] ( NodePtr&& node ) -> void {
            this->AddTree(node,depth); });
    } catch ( RegError& e ) {
        SLOG_FROM(LOG_ERROR, "RegSearch::Search",
            "Invalid Regex: " << e.what()) 
        results = 0;
    } 

    /*
     * Now sort the results to the node which 
     * as the most total time spent in it is first
     */
    sort(nodes.begin(), nodes.end(), 
      [=] (const NodePtr& lhs, const NodePtr& rhs) -> bool 
      {
          return lhs->RunTime() > rhs->RunTime();
      });


    // The pointer is invalid once this stack frame
    // has been unwound
    regPattern = nullptr;
    return results;
}
void SearchCache::AddTree(NodePtr& node, int depth) {
    AddNode(node);
    --depth;
    if (depth != -1) {
        node->ForEach([=] ( NodePtr&& node ) -> void {
            this->AddTree(node, depth);
        });
    }
}
void RegSearch::AddTree(NodePtr& node, int depth) {
    if ( regPattern && regPattern->Search(node->Name()) ) {
        this->AddNode(node);
    } else {
        SLOG_FROM(LOG_VERBOSE,"RegSearch::Search",
           "Rejected Node: " << node->Name() )
    }

    --depth;
    if ( depth != 0 ) {
        node->ForEach([=] ( NodePtr&& node ) -> void {
            this->AddTree(node, depth);
        });
    }
}
void GCGV_ReqGetNodes::SortedNodes::AddTree(
    NodePtr root,
    SORT_BY sort,
    const std::string& filter,
    int depth)
{
    if (root->Name() == filter) {
        AddNode(root,sort);
    }

    --depth;
    if (depth != -1) {
        root->ForEach([=] (NodePtr&& node) -> void {
            this->AddTree(node,sort,filter,depth);
        });
    }
}