LCOV - code coverage report
Current view: top level - db - db.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 73 86 84.9 %
Date: 2026-09-28 02:13:17 Functions: 16 19 84.2 %
Legend: Lines: hit not hit

          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             : 
      12             : using namespace std;
      13             : 
      14             : int DB::partition_count_;
      15             : 
      16             : // factory map is declared as a local static variable in order to avoid
      17             : // static initialization order dependencies.
      18        3580 : DB::FactoryMap *DB::factories() {
      19        3580 :     static FactoryMap factory_map;
      20        3580 :     return &factory_map;
      21             : }
      22             : 
      23          72 : void DB::RegisterFactory(const std::string &prefix, CreateFunction create_fn) {
      24          72 :     DB::factories()->insert(make_pair(prefix, create_fn));
      25          72 : }
      26             : 
      27          13 : void DB::ClearFactoryRegistry() {
      28          13 :     DB::factories()->clear();
      29          13 : }
      30             : 
      31      192212 : int DB::PartitionCount() {
      32             : 
      33             :     // Initialize static partition_count_.
      34      192212 :     if (!partition_count_) {
      35          28 :         partition_count_ = TaskScheduler::GetInstance()->HardwareThreadCount();
      36             :     }
      37      192212 :     return partition_count_;
      38             : }
      39             : 
      40             : // For unit testing only.
      41           0 : void DB::SetPartitionCount(int partition_count) {
      42           0 :     partition_count_ = partition_count;
      43           0 : }
      44             : 
      45         978 : DB::DB(int task_id) : task_id_(task_id) {
      46         978 :     if (task_id == -1)
      47         414 :         task_id_ = TaskScheduler::GetInstance()->GetTaskId("db::DBTable");
      48         978 :     walker_.reset(new DBTableWalker(task_id_));
      49         978 :     walk_mgr_.reset(new DBTableWalkMgr());
      50        4890 :     for (int i = 0; i < PartitionCount(); i++) {
      51        3912 :         partitions_.push_back(new DBPartition(this, i));
      52             :     }
      53         978 : }
      54             : 
      55         978 : DB::~DB() {
      56         978 :     Clear();
      57         978 : }
      58             : 
      59       14909 : DBPartition *DB::GetPartition(int index) {
      60       14909 :     return partitions_[index];
      61             : }
      62             : 
      63       65648 : const DBPartition *DB::GetPartition(int index) const {
      64       65648 :     return partitions_[index];
      65             : }
      66             : 
      67       41549 : DBTableBase *DB::FindTable(const string &name) {
      68       41549 :     TableMap::iterator loc = tables_.find(name);
      69       41540 :     if (loc != tables_.end()) {
      70       38459 :         DBTableBase *tbl_base = loc->second;
      71       38460 :         return tbl_base;
      72             :     }
      73        3080 :     return NULL;
      74             : }
      75             : 
      76           0 : DB::iterator DB::FindTableIter(const string &name) {
      77           0 :     return tables_.find(name);
      78             : }
      79             : 
      80       85620 : void DB::AddTable(DBTableBase *tbl_base) {
      81             :     pair<TableMap::iterator, bool> result =
      82       85620 :             tables_.insert(make_pair(tbl_base->name(), tbl_base));
      83       85620 :     assert(result.second);
      84       85620 : }
      85             : 
      86        3440 : void DB::RemoveTable(DBTableBase *tbl_base) {
      87        3440 :     tables_.erase(tbl_base->name());
      88        3440 : }
      89             : 
      90       16589 : bool DB::IsDBQueueEmpty() const {
      91       82001 :     for (int i = 0; i < PartitionCount(); i++) {
      92       65648 :         if (!GetPartition(i)->IsDBQueueEmpty()) return false;
      93             :     }
      94             : 
      95       16353 :     return true;
      96             : }
      97             : 
      98        3495 : DBTableBase *DB::CreateTable(const string &name) {
      99        3495 :     FactoryMap *factory_map = factories();
     100        3495 :     string prefix = name;
     101        4653 :     while (prefix.size()) {
     102        4653 :         FactoryMap::iterator loc = factory_map->find(prefix);
     103        4653 :         if (loc != factory_map->end()) {
     104        3495 :             DBTableBase *tbl_base = (loc->second)(this, name);
     105        3495 :             std::scoped_lock lock(mutex_);
     106        3495 :             tables_.insert(make_pair(name, tbl_base));
     107        3495 :             return tbl_base;
     108        3495 :         }
     109        1158 :         size_t index = prefix.find('.');
     110        1158 :         if (index == string::npos) {
     111           0 :             break;
     112             :         }
     113        1158 :         if (index == (prefix.length()-1)) {
     114           0 :             break;
     115             :         }
     116        1158 :         prefix = prefix.substr(index+1);
     117             :     }
     118           0 :     return NULL;
     119        3495 : }
     120             : 
     121          30 : DBGraph *DB::GetGraph(const std::string &name) {
     122          30 :     GraphMap::iterator loc = graph_map_.find(name);
     123          30 :     if (loc != graph_map_.end()) {
     124          30 :         return loc->second;
     125             :     }
     126           0 :     return NULL;
     127             : }
     128             : 
     129           6 : void DB::SetGraph(const std::string &name, DBGraph *graph) {
     130             :     pair<GraphMap::iterator, bool> result =
     131           6 :         graph_map_.insert(make_pair(name, graph));
     132           6 :     assert(result.second);
     133           6 : }
     134             : 
     135           0 : void DB::SetQueueDisable(bool disable) {
     136           0 :     for (int i = 0; i < PartitionCount(); i++) {
     137           0 :         partitions_[i]->SetQueueDisable(disable);
     138             :     }
     139           0 : }
     140             : 
     141        1385 : void DB::Clear() {
     142        1385 :     STLDeleteElements(&tables_);
     143        1385 :     STLDeleteValues(&partitions_);
     144        1385 : }

Generated by: LCOV version 1.14