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