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 290565 : DB::FactoryMap *DB::factories() { 20 290565 : static FactoryMap factory_map; 21 290570 : return &factory_map; 22 : } 23 : 24 1511 : void DB::RegisterFactory(const std::string &prefix, CreateFunction create_fn) { 25 1511 : DB::factories()->insert(make_pair(prefix, create_fn)); 26 1511 : } 27 : 28 11 : void DB::ClearFactoryRegistry() { 29 11 : DB::factories()->clear(); 30 11 : } 31 : 32 41228824 : int DB::PartitionCount() { 33 : 34 : // Initialize static partition_count_. 35 41228824 : if (!partition_count_) { 36 175 : partition_count_ = TaskScheduler::GetInstance()->HardwareThreadCount(); 37 : } 38 41235275 : 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 27308 : DB::DB(int task_id) : task_id_(task_id) { 47 27308 : if (task_id == -1) 48 19187 : task_id_ = TaskScheduler::GetInstance()->GetTaskId("db::DBTable"); 49 27308 : walker_.reset(new DBTableWalker(task_id_)); 50 27308 : walk_mgr_.reset(new DBTableWalkMgr()); 51 135490 : for (int i = 0; i < PartitionCount(); i++) { 52 108182 : partitions_.push_back(new DBPartition(this, i)); 53 : } 54 27308 : } 55 : 56 27308 : DB::~DB() { 57 27308 : Clear(); 58 27308 : } 59 : 60 2365825 : DBPartition *DB::GetPartition(int index) { 61 2365825 : return partitions_[index]; 62 : } 63 : 64 6256299 : const DBPartition *DB::GetPartition(int index) const { 65 6256299 : return partitions_[index]; 66 : } 67 : 68 1542129 : DBTableBase *DB::FindTable(const string &name) { 69 1542129 : TableMap::iterator loc = tables_.find(name); 70 1541792 : if (loc != tables_.end()) { 71 1486554 : DBTableBase *tbl_base = loc->second; 72 1486561 : return tbl_base; 73 : } 74 55287 : return NULL; 75 : } 76 : 77 0 : DB::iterator DB::FindTableIter(const string &name) { 78 0 : return tables_.find(name); 79 : } 80 : 81 1219919 : void DB::AddTable(DBTableBase *tbl_base) { 82 : pair<TableMap::iterator, bool> result = 83 1219919 : tables_.insert(make_pair(tbl_base->name(), tbl_base)); 84 1219919 : assert(result.second); 85 1219919 : } 86 : 87 288998 : void DB::RemoveTable(DBTableBase *tbl_base) { 88 288998 : tables_.erase(tbl_base->name()); 89 288998 : } 90 : 91 1665611 : bool DB::IsDBQueueEmpty() const { 92 7800770 : for (int i = 0; i < PartitionCount(); i++) { 93 6256299 : if (!GetPartition(i)->IsDBQueueEmpty()) return false; 94 : } 95 : 96 1544471 : return true; 97 : } 98 : 99 289041 : DBTableBase *DB::CreateTable(const string &name) { 100 289041 : FactoryMap *factory_map = factories(); 101 289044 : string prefix = name; 102 528698 : while (prefix.size()) { 103 528689 : FactoryMap::iterator loc = factory_map->find(prefix); 104 528413 : if (loc != factory_map->end()) { 105 288991 : DBTableBase *tbl_base = (loc->second)(this, name); 106 289038 : std::scoped_lock lock(mutex_); 107 289078 : tables_.insert(make_pair(name, tbl_base)); 108 289078 : return tbl_base; 109 289078 : } 110 239530 : size_t index = prefix.find('.'); 111 239620 : if (index == string::npos) { 112 0 : break; 113 : } 114 239620 : if (index == (prefix.length()-1)) { 115 0 : break; 116 : } 117 239627 : prefix = prefix.substr(index+1); 118 : } 119 0 : return NULL; 120 289078 : } 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 35363 : void DB::Clear() { 143 35363 : STLDeleteElements(&tables_); 144 35363 : STLDeleteValues(&partitions_); 145 35363 : }