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 865 : DB::FactoryMap *DB::factories() { 20 865 : static FactoryMap factory_map; 21 865 : return &factory_map; 22 : } 23 : 24 57 : void DB::RegisterFactory(const std::string &prefix, CreateFunction create_fn) { 25 57 : DB::factories()->insert(make_pair(prefix, create_fn)); 26 57 : } 27 : 28 10 : void DB::ClearFactoryRegistry() { 29 10 : DB::factories()->clear(); 30 10 : } 31 : 32 238171 : int DB::PartitionCount() { 33 : 34 : // Initialize static partition_count_. 35 238171 : if (!partition_count_) { 36 21 : partition_count_ = TaskScheduler::GetInstance()->HardwareThreadCount(); 37 : } 38 238178 : return partition_count_; 39 : } 40 : 41 : // For unit testing only. 42 0 : void DB::SetPartitionCount(int partition_count) { 43 0 : partition_count_ = partition_count; 44 0 : } 45 : 46 262 : DB::DB(int task_id) : task_id_(task_id) { 47 262 : if (task_id == -1) 48 103 : task_id_ = TaskScheduler::GetInstance()->GetTaskId("db::DBTable"); 49 262 : walker_.reset(new DBTableWalker(task_id_)); 50 262 : walk_mgr_.reset(new DBTableWalkMgr()); 51 1310 : for (int i = 0; i < PartitionCount(); i++) { 52 1048 : partitions_.push_back(new DBPartition(this, i)); 53 : } 54 262 : } 55 : 56 262 : DB::~DB() { 57 262 : Clear(); 58 262 : } 59 : 60 22914 : DBPartition *DB::GetPartition(int index) { 61 22914 : return partitions_[index]; 62 : } 63 : 64 14576 : const DBPartition *DB::GetPartition(int index) const { 65 14576 : return partitions_[index]; 66 : } 67 : 68 21145 : DBTableBase *DB::FindTable(const string &name) { 69 21145 : TableMap::iterator loc = tables_.find(name); 70 21136 : if (loc != tables_.end()) { 71 19564 : DBTableBase *tbl_base = loc->second; 72 19564 : return tbl_base; 73 : } 74 1576 : return NULL; 75 : } 76 : 77 0 : DB::iterator DB::FindTableIter(const string &name) { 78 0 : return tables_.find(name); 79 : } 80 : 81 25470 : void DB::AddTable(DBTableBase *tbl_base) { 82 : pair<TableMap::iterator, bool> result = 83 25470 : tables_.insert(make_pair(tbl_base->name(), tbl_base)); 84 25470 : assert(result.second); 85 25470 : } 86 : 87 758 : void DB::RemoveTable(DBTableBase *tbl_base) { 88 758 : tables_.erase(tbl_base->name()); 89 758 : } 90 : 91 3695 : bool DB::IsDBQueueEmpty() const { 92 18203 : for (int i = 0; i < PartitionCount(); i++) { 93 14576 : if (!GetPartition(i)->IsDBQueueEmpty()) return false; 94 : } 95 : 96 3627 : return true; 97 : } 98 : 99 798 : DBTableBase *DB::CreateTable(const string &name) { 100 798 : FactoryMap *factory_map = factories(); 101 798 : string prefix = name; 102 1135 : while (prefix.size()) { 103 1135 : FactoryMap::iterator loc = factory_map->find(prefix); 104 1135 : if (loc != factory_map->end()) { 105 798 : DBTableBase *tbl_base = (loc->second)(this, name); 106 798 : std::scoped_lock lock(mutex_); 107 798 : tables_.insert(make_pair(name, tbl_base)); 108 798 : return tbl_base; 109 798 : } 110 337 : size_t index = prefix.find('.'); 111 337 : if (index == string::npos) { 112 0 : break; 113 : } 114 337 : if (index == (prefix.length()-1)) { 115 0 : break; 116 : } 117 337 : prefix = prefix.substr(index+1); 118 : } 119 0 : return NULL; 120 798 : } 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 0 : void DB::SetQueueDisable(bool disable) { 137 0 : for (int i = 0; i < PartitionCount(); i++) { 138 0 : partitions_[i]->SetQueueDisable(disable); 139 : } 140 0 : } 141 : 142 355 : void DB::Clear() { 143 355 : STLDeleteElements(&tables_); 144 355 : STLDeleteValues(&partitions_); 145 355 : }