Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef _STAT_WALKER_H_ 7 : #define _STAT_WALKER_H_ 8 : 9 : #include <boost/function.hpp> 10 : #include <string> 11 : #include <map> 12 : #include "db_handler.h" 13 : 14 : /* This class provides a higher-level interface for DbHandler's StatTableInsert 15 : * It allows for nested stat strutures 16 : * The client calls "Push" to fill in a child node 17 : * The client calls "Pop" when the current node has no more children to fill 18 : * 19 : * This class will call StatTableInsert with the right tags and attribs when 20 : * "Pop" is called. 21 : */ 22 : class StatWalker { 23 : public: 24 : typedef boost::function<void( 25 : const uint64_t& timestamp, 26 : const std::string& statName, 27 : const std::string& statAttr, 28 : const DbHandler::TagMap & attribs_tag, 29 : const DbHandler::AttribMap & attribs)> StatTableInsertFn ; 30 : 31 : struct TagVal { 32 : std::pair<std::string,DbHandler::Var> prefix; 33 : DbHandler::Var val; 34 : }; 35 : typedef std::map<std::string,TagVal> TagMap; 36 : 37 : // Record a new child node 38 : // name : local name of this node attribute in the parent struct 39 : // tags : tags at this level. These tags will be inherited by children 40 : // attribs : attribs at this level. 41 : void Push(const std::string& name, 42 : const TagMap& tags, 43 : const DbHandler::AttribMap& attribs); 44 : 45 : // The current node's children have been completely processed. 46 : // Its time to call StatTableInsert 47 : void Pop(void); 48 : 49 3 : StatWalker(StatTableInsertFn fn, const uint64_t ×tamp, 50 3 : const std::string& statName, const TagMap& tags) : 51 3 : timestamp_(timestamp), 52 3 : stat_name_(statName), 53 3 : fn_(fn), 54 3 : top_tags_(tags) {} 55 : ~StatWalker(); 56 : private: 57 : struct StatNode { 58 : std::string name; 59 : TagMap tags; 60 : DbHandler::AttribMap attribs; 61 : }; 62 : 63 : // Utility function for aggregating StatWalker::TagMap into DbHandler::TagMap 64 : static void FillTag(DbHandler::TagMap *attribs_tag, 65 : const std::pair<const std::string, TagVal>* tag); 66 : 67 : const uint64_t timestamp_; 68 : const std::string stat_name_; 69 : const StatTableInsertFn fn_; 70 : const TagMap top_tags_; 71 : std::vector<StatNode> nodes_; 72 : }; 73 : 74 : #endif 75 :