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