Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef ctrlplane_db_h 6 : #define ctrlplane_db_h 7 : 8 : #include <map> 9 : #include <vector> 10 : #include <mutex> 11 : 12 : #include <boost/function.hpp> 13 : 14 : #include "base/util.h" 15 : 16 : class DBGraph; 17 : class DBPartition; 18 : class DBTableBase; 19 : class DBTableWalker; 20 : class DBTableWalkMgr; 21 : 22 : // A database is a collection of tables. 23 : // The storage is implemented by a set of shards (DB Partitions). 24 : class DB { 25 : public: 26 : typedef boost::function<DBTableBase *(DB *, const std::string &)> 27 : CreateFunction; 28 : typedef std::map<std::string, DBTableBase *> TableMap; 29 : typedef TableMap::iterator iterator; 30 : typedef TableMap::const_iterator const_iterator; 31 : 32 : DB(int task_id = -1); 33 : ~DB(); 34 : 35 : // Get the partition with the specified id. 36 : DBPartition *GetPartition(int index); 37 : const DBPartition *GetPartition(int index) const; 38 : 39 : void AddTable(DBTableBase *tbl_base); 40 : 41 : // Table creation. 42 : DBTableBase *CreateTable(const std::string &name); 43 : DBTableBase *FindTable(const std::string &name); 44 : iterator FindTableIter(const std::string &name); 45 : void RemoveTable(DBTableBase *tbl_base); 46 : 47 : // Table walker 48 323 : DBTableWalker *GetWalker() { 49 323 : return walker_.get(); 50 : } 51 1666773 : DBTableWalkMgr *GetWalkMgr() { 52 1666773 : return walk_mgr_.get(); 53 : } 54 : 55 : DBGraph *GetGraph(const std::string &name); 56 : void SetGraph(const std::string &name, DBGraph *graph); 57 : void SetQueueDisable(bool disable); 58 : 59 : static void SetPartitionCount(int partition_count); 60 : static int PartitionCount(); 61 : static void RegisterFactory(const std::string &prefix, 62 : CreateFunction create_fn); 63 : static void ClearFactoryRegistry(); 64 : 65 : void Clear(); 66 : bool IsDBQueueEmpty() const; 67 : 68 : iterator begin() { return tables_.begin(); } 69 1222763 : iterator end() { return tables_.end(); } 70 8162 : iterator lower_bound(const std::string &name) { 71 8162 : return tables_.lower_bound(name); 72 : } 73 5 : const_iterator const_begin() { return tables_.begin(); } 74 5 : const_iterator const_end() { return tables_.end(); } 75 0 : const_iterator const_lower_bound(const std::string &name) { 76 0 : return tables_.lower_bound(name); 77 : } 78 : 79 2360840 : int task_id() const { return task_id_; } 80 : 81 : private: 82 : typedef std::map<std::string, CreateFunction> FactoryMap; 83 : typedef std::map<std::string, DBGraph *> GraphMap; 84 : 85 : static int partition_count_; 86 : static FactoryMap *factories(); 87 : 88 : std::mutex mutex_; 89 : int task_id_; 90 : std::vector<DBPartition *> partitions_; 91 : TableMap tables_; 92 : GraphMap graph_map_; 93 : std::unique_ptr<DBTableWalker> walker_; 94 : std::unique_ptr<DBTableWalkMgr> walk_mgr_; 95 : 96 : DISALLOW_COPY_AND_ASSIGN(DB); 97 : }; 98 : 99 : #endif