Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "base/task.h" 6 : #include "db/db.h" 7 : #include "db/db_partition.h" 8 : #include "db/db_table.h" 9 : #include "db/db_table_walker.h" 10 : #include "db/db_table_walk_mgr.h" 11 : #include "tbb/task_scheduler_init.h" 12 : 13 : using namespace std; 14 : 15 : int DB::partition_count_; 16 : 17 : // factory map is declared as a local static variable in order to avoid 18 : // static initialization order dependencies. 19 290031 : DB::FactoryMap *DB::factories() { 20 290031 : static FactoryMap factory_map; 21 290045 : return &factory_map; 22 : } 23 : 24 1447 : void DB::RegisterFactory(const std::string &prefix, CreateFunction create_fn) { 25 1447 : DB::factories()->insert(make_pair(prefix, create_fn)); 26 1447 : } 27 : 28 9 : void DB::ClearFactoryRegistry() { 29 9 : DB::factories()->clear(); 30 9 : } 31 : 32 40899978 : int DB::PartitionCount() { 33 : 34 : // Initialize static partition_count_. 35 40899978 : if (!partition_count_) { 36 173 : partition_count_ = TaskScheduler::GetInstance()->HardwareThreadCount(); 37 : } 38 40902703 : return partition_count_; 39 : } 40 : 41 : // For unit testing only. 42 4 : void DB::SetPartitionCount(int partition_count) { 43 4 : partition_count_ = partition_count; 44 4 : } 45 : 46 27306 : DB::DB(int task_id) : task_id_(task_id) { 47 27306 : if (task_id == -1) 48 19185 : task_id_ = TaskScheduler::GetInstance()->GetTaskId("db::DBTable"); 49 27306 : walker_.reset(new DBTableWalker(task_id_)); 50 27306 : walk_mgr_.reset(new DBTableWalkMgr()); 51 135480 : for (int i = 0; i < PartitionCount(); i++) { 52 108174 : partitions_.push_back(new DBPartition(this, i)); 53 : } 54 27306 : } 55 : 56 27306 : DB::~DB() { 57 27306 : Clear(); 58 27306 : } 59 : 60 2342393 : DBPartition *DB::GetPartition(int index) { 61 2342393 : return partitions_[index]; 62 : } 63 : 64 6255522 : const DBPartition *DB::GetPartition(int index) const { 65 6255522 : return partitions_[index]; 66 : } 67 : 68 1511631 : DBTableBase *DB::FindTable(const string &name) { 69 1511631 : TableMap::iterator loc = tables_.find(name); 70 1511382 : if (loc != tables_.end()) { 71 1456129 : DBTableBase *tbl_base = loc->second; 72 1456121 : return tbl_base; 73 : } 74 55295 : return NULL; 75 : } 76 : 77 0 : DB::iterator DB::FindTableIter(const string &name) { 78 0 : return tables_.find(name); 79 : } 80 : 81 1219621 : void DB::AddTable(DBTableBase *tbl_base) { 82 : pair<TableMap::iterator, bool> result = 83 1219621 : tables_.insert(make_pair(tbl_base->name(), tbl_base)); 84 1219621 : assert(result.second); 85 1219621 : } 86 : 87 288591 : void DB::RemoveTable(DBTableBase *tbl_base) { 88 288591 : tables_.erase(tbl_base->name()); 89 288591 : } 90 : 91 1665399 : bool DB::IsDBQueueEmpty() const { 92 7799777 : for (int i = 0; i < PartitionCount(); i++) { 93 6255522 : if (!GetPartition(i)->IsDBQueueEmpty()) return false; 94 : } 95 : 96 1544255 : return true; 97 : } 98 : 99 288590 : DBTableBase *DB::CreateTable(const string &name) { 100 288590 : FactoryMap *factory_map = factories(); 101 288591 : string prefix = name; 102 527833 : while (prefix.size()) { 103 527805 : FactoryMap::iterator loc = factory_map->find(prefix); 104 527570 : if (loc != factory_map->end()) { 105 288531 : DBTableBase *tbl_base = (loc->second)(this, name); 106 288564 : std::scoped_lock lock(mutex_); 107 288621 : tables_.insert(make_pair(name, tbl_base)); 108 288621 : return tbl_base; 109 288621 : } 110 239144 : size_t index = prefix.find('.'); 111 239187 : if (index == string::npos) { 112 0 : break; 113 : } 114 239187 : if (index == (prefix.length()-1)) { 115 0 : break; 116 : } 117 239228 : prefix = prefix.substr(index+1); 118 : } 119 0 : return NULL; 120 288621 : } 121 : 122 15 : DBGraph *DB::GetGraph(const std::string &name) { 123 15 : GraphMap::iterator loc = graph_map_.find(name); 124 15 : if (loc != graph_map_.end()) { 125 15 : return loc->second; 126 : } 127 0 : return NULL; 128 : } 129 : 130 3 : void DB::SetGraph(const std::string &name, DBGraph *graph) { 131 : pair<GraphMap::iterator, bool> result = 132 3 : graph_map_.insert(make_pair(name, graph)); 133 3 : assert(result.second); 134 3 : } 135 : 136 10 : void DB::SetQueueDisable(bool disable) { 137 44 : for (int i = 0; i < PartitionCount(); i++) { 138 34 : partitions_[i]->SetQueueDisable(disable); 139 : } 140 10 : } 141 : 142 35361 : void DB::Clear() { 143 35361 : STLDeleteElements(&tables_); 144 35361 : STLDeleteValues(&partitions_); 145 35361 : }