LCOV - code coverage report
Current view: top level - db - db_table.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 288 354 81.4 %
Date: 2026-11-16 02:06:09 Functions: 54 71 76.1 %
Legend: Lines: hit not hit

          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             : #include <vector>
       7             : #include <atomic>
       8             : 
       9             : #include <tbb/spin_rw_mutex.h>
      10             : 
      11             : #include <boost/bind/bind.hpp>
      12             : #include <boost/foreach.hpp>
      13             : #include <boost/dynamic_bitset.hpp>
      14             : #include <boost/type_traits.hpp>
      15             : 
      16             : #include "base/compiler.h"
      17             : #include "base/logging.h"
      18             : #include "base/task_annotations.h"
      19             : #include "base/time_util.h"
      20             : #include "db/db.h"
      21             : #include "db/db_partition.h"
      22             : #include "db/db_table.h"
      23             : #include "db/db_table_partition.h"
      24             : #include "db/db_table_walk_mgr.h"
      25             : #include "db/db_types.h"
      26             : 
      27             : class DBEntry;
      28             : class DBEntryBase;
      29             : 
      30             : using namespace std;
      31             : using namespace boost::placeholders;
      32             : 
      33       87252 : DBRequest::DBRequest() : oper(static_cast<DBOperation>(0)) {
      34       87246 : }
      35             : 
      36      115674 : DBRequest::~DBRequest() {
      37             : #if defined(__GNUC__)
      38             : #if (__GNUC_PREREQ(4, 2) > 0)
      39             :     boost::has_virtual_destructor<DBRequestKey>::type key_has_destructor;
      40             :     boost::has_virtual_destructor<DBRequestData>::type data_has_destructor;
      41      115674 :     assert(key_has_destructor && data_has_destructor);
      42             : #endif
      43             : #endif
      44      115672 : }
      45             : 
      46       41575 : void DBRequest::Swap(DBRequest *rhs) {
      47       41575 :     swap(oper, rhs->oper);
      48       41574 :     swap(key, rhs->key);
      49       41573 :     swap(data, rhs->data);
      50       41572 : }
      51             : 
      52             : // we need copy to be able to resize vector of atomics
      53             : // therefore we don't need the same value in both instances
      54             : template<typename _Tp>
      55             : struct AtomicWithCopy : public std::atomic<_Tp> {
      56             :     // Inherit constructors
      57             :     using std::atomic<_Tp>::atomic;
      58             : 
      59             :     // Bring in base class operators and methods
      60             :     using std::atomic<_Tp>::operator=;
      61             :     using std::atomic<_Tp>::load;
      62             :     using std::atomic<_Tp>::store;
      63             : 
      64             :     // Custom constructor to handle copy from another AtomicWithCopy (must perform a load/store)
      65        7276 :     AtomicWithCopy(const AtomicWithCopy& other) : std::atomic<_Tp>(other.load()) {}
      66             :     
      67             :     // Custom assignment operator
      68       12395 :     AtomicWithCopy& operator=(const AtomicWithCopy& other) {
      69       24790 :         this->store(other.load());
      70       12395 :         return *this;
      71             :     }
      72             : };
      73             : 
      74             : class DBTableBase::ListenerInfo {
      75             : public:
      76             :     typedef vector<ChangeCallback> CallbackList;
      77             :     typedef vector<string> NameList;
      78             :     typedef vector<AtomicWithCopy<uint64_t>> StateCountList;
      79             : 
      80       33187 :     explicit ListenerInfo(const string &table_name) :
      81       33187 :         db_state_accounting_(true) {
      82       33187 :         if (table_name.find("__ifmap_") != string::npos) {
      83             :             // TODO need to have unconditional DB state accounting
      84             :             // for now skipp DB State accounting for ifmap tables
      85       32101 :             db_state_accounting_ = false;
      86             :         }
      87       33187 :     }
      88             : 
      89       12413 :     DBTableBase::ListenerId Register(ChangeCallback callback,
      90             :         const string &name) {
      91       12413 :         tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
      92       12414 :         size_t i = bmap_.find_first();
      93       12413 :         if (i == bmap_.npos) {
      94       12394 :             i = callbacks_.size();
      95       12394 :             callbacks_.push_back(callback);
      96       12394 :             names_.push_back(name);
      97       12394 :             state_count_.resize(i + 1);
      98       12395 :             state_count_[i] = AtomicWithCopy<uint64_t>(0);
      99             :         } else {
     100          19 :             bmap_.reset(i);
     101          19 :             if (bmap_.none()) {
     102          19 :                 bmap_.clear();
     103             :             }
     104          19 :             callbacks_[i] = callback;
     105          19 :             names_[i] = name;
     106          19 :             state_count_[i] = 0;
     107             :         }
     108       12414 :         return i;
     109       12414 :     }
     110             : 
     111       12348 :     void Unregister(ListenerId listener) {
     112       12348 :         tbb::spin_rw_mutex::scoped_lock write_lock(rw_mutex_, true);
     113       12348 :         callbacks_[listener] = NULL;
     114       12347 :         names_[listener] = "";
     115             :         // During Unregister Listener should have cleaned up,
     116             :         // DB states from all the entries in this table.
     117       12347 :         assert(state_count_[listener] == 0);
     118       12347 :         if ((size_t) listener == callbacks_.size() - 1) {
     119       22794 :             while (!callbacks_.empty() && callbacks_.back() == NULL) {
     120       12223 :                 callbacks_.pop_back();
     121       12223 :                 names_.pop_back();
     122       12223 :                 state_count_.pop_back();
     123             :             }
     124       10571 :             if (bmap_.size() > callbacks_.size()) {
     125         810 :                 bmap_.resize(callbacks_.size());
     126             :             }
     127             :         } else {
     128        1776 :             if ((size_t) listener >= bmap_.size()) {
     129        1394 :                 bmap_.resize(listener + 1);
     130             :             }
     131        1776 :             bmap_.set(listener);
     132             :         }
     133       12347 :     }
     134             : 
     135             :     // concurrency: called from DBPartition task.
     136      120670 :     void RunNotify(DBTablePartBase *tpart, DBEntryBase *entry) {
     137      120670 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     138      120812 :         for (CallbackList::iterator iter = callbacks_.begin();
     139      522610 :              iter != callbacks_.end(); ++iter) {
     140      401806 :             if (*iter != NULL) {
     141      401097 :                 ChangeCallback cb = *iter;
     142      400828 :                 (cb)(tpart, entry);
     143      401472 :             }
     144             :         }
     145      120414 :     }
     146             : 
     147      231002 :     void AddToDBStateCount(ListenerId listener, int count) {
     148      231002 :         if (db_state_accounting_ && listener != DBTableBase::kInvalidId) {
     149      219674 :             tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     150      220038 :             state_count_[listener] += count;
     151      220257 :         }
     152      231450 :     }
     153             : 
     154       47501 :     uint64_t GetDBStateCount(ListenerId listener) {
     155       47501 :         assert(db_state_accounting_ && listener != DBTableBase::kInvalidId);
     156       47503 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     157       95160 :         return state_count_[listener];
     158       47557 :     }
     159             : 
     160           0 :     void FillListeners(vector<ShowTableListener> *listeners) const {
     161           0 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     162           0 :         ListenerId id = 0;
     163           0 :         for (CallbackList::const_iterator iter = callbacks_.begin();
     164           0 :              iter != callbacks_.end(); ++iter, ++id) {
     165           0 :             if (*iter != NULL) {
     166           0 :                 ShowTableListener item;
     167           0 :                 item.id = id;
     168           0 :                 item.name = names_[id];
     169           0 :                 item.state_count = state_count_[id];
     170           0 :                 listeners->push_back(item);
     171           0 :             }
     172             :         }
     173           0 :     }
     174             : 
     175       94077 :     bool empty() const {
     176       94077 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     177      188169 :         return callbacks_.empty();
     178       94074 :     }
     179             : 
     180           0 :     size_t size() const {
     181           0 :         tbb::spin_rw_mutex::scoped_lock read_lock(rw_mutex_, false);
     182           0 :         return (callbacks_.size() - bmap_.count());
     183           0 :     }
     184             : 
     185             : private:
     186             :     bool db_state_accounting_;
     187             :     CallbackList callbacks_;
     188             :     NameList names_;
     189             :     StateCountList state_count_;
     190             :     mutable tbb::spin_rw_mutex rw_mutex_;
     191             :     boost::dynamic_bitset<> bmap_;      // free list.
     192             : };
     193             : 
     194       33187 : DBTableBase::DBTableBase(DB *db, const string &name)
     195       33187 :         : db_(db), name_(name), info_(new ListenerInfo(name)),
     196       33187 :           enqueue_count_(0), input_count_(0), notify_count_(0) {
     197       33187 :     walker_count_ = 0;
     198       33187 :     walk_request_count_ = 0;
     199       33187 :     walk_complete_count_ = 0;
     200       33187 :     walk_cancel_count_ = 0;
     201       33187 :     walk_again_count_ = 0;
     202       33187 :     walk_count_ = 0;
     203       33187 : }
     204             : 
     205       33187 : DBTableBase::~DBTableBase() {
     206       33187 : }
     207             : 
     208       12414 : DBTableBase::ListenerId DBTableBase::Register(ChangeCallback callback,
     209             :     const string &name) {
     210       12414 :     return info_->Register(callback, name);
     211             : }
     212             : 
     213       12348 : void DBTableBase::Unregister(ListenerId listener) {
     214       12348 :     info_->Unregister(listener);
     215             :     // If a table is marked for deletion, then we may trigger the deletion
     216             :     // process when the last client is removed
     217       12348 :     if (info_->empty())
     218        9535 :         RetryDelete();
     219       12348 : }
     220             : 
     221       39239 : bool DBTableBase::Enqueue(DBRequest *req) {
     222       39239 :     DBTablePartBase *tpart = GetTablePartition(req->key.get());
     223       39239 :     DBPartition *partition = db_->GetPartition(tpart->index());
     224       39239 :     enqueue_count_++;
     225       39239 :     return partition->EnqueueRequest(tpart, NULL, req);
     226             : }
     227             : 
     228       28792 : void DBTableBase::EnqueueRemove(DBEntryBase *db_entry) {
     229       28792 :     DBTablePartBase *tpart = GetTablePartition(db_entry);
     230       28760 :     DBPartition *partition = db_->GetPartition(tpart->index());
     231       28758 :     partition->EnqueueRemove(tpart, db_entry);
     232       28819 : }
     233             : 
     234      120701 : void DBTableBase::RunNotify(DBTablePartBase *tpart, DBEntryBase *entry) {
     235      120701 :     notify_count_++;
     236      120701 :     info_->RunNotify(tpart, entry);
     237      120813 : }
     238             : 
     239      231221 : void DBTableBase::AddToDBStateCount(ListenerId listener, int count) {
     240      231221 :     info_->AddToDBStateCount(listener, count);
     241      231431 : }
     242             : 
     243       47521 : uint64_t DBTableBase::GetDBStateCount(ListenerId listener) {
     244       47521 :     return info_->GetDBStateCount(listener);
     245             : }
     246             : 
     247        1024 : bool DBTableBase::MayDelete() const {
     248        1024 :     if (HasListeners()) {
     249           0 :         return false;
     250             :     }
     251        1024 :     if (HasWalkers()) {
     252           0 :         return false;
     253             :     }
     254        1024 :     if (!empty()) {
     255           0 :         return false;
     256             :     }
     257             : 
     258        1024 :     return true;
     259             : }
     260             : 
     261       81733 : bool DBTableBase::HasListeners() const {
     262       81733 :     return !info_->empty();
     263             : }
     264             : 
     265           0 : size_t DBTableBase::GetListenerCount() const {
     266           0 :     return info_->size();
     267             : }
     268             : 
     269           0 : void DBTableBase::FillListeners(vector<ShowTableListener> *listeners) const {
     270           0 :     info_->FillListeners(listeners);
     271           0 : }
     272             : 
     273             : class DBTable::WalkWorker : public Task {
     274             : public:
     275             :     WalkWorker(TableWalker *walker, int db_partition_id);
     276             : 
     277             :     virtual bool Run();
     278             : 
     279           0 :     std::string Description() const { return "DBTable::WalkWorker"; }
     280             : 
     281             : private:
     282             :     // Store the last visited node to continue walk
     283             :     std::unique_ptr<DBRequestKey> walk_ctx_;
     284             : 
     285             :     // Table partition for which this worker was created
     286             :     DBTablePartition *tbl_partition_;
     287             : 
     288             :     TableWalker *walker_;
     289             : };
     290             : 
     291             : class DBTable::TableWalker {
     292             : public:
     293       33187 :     TableWalker(DBTable *table) : table_(table) {
     294       33187 :         pending_workers_ = 0;
     295       33187 :     }
     296             : 
     297             :     void StartWalk();
     298             : 
     299       64893 :     DBTable *table() {
     300       64893 :         return table_;
     301             :     }
     302             : 
     303        5856 :     void ClearWalkWorks() {
     304        5856 :         worker_tasks_.clear();
     305        5856 :     }
     306             : 
     307             :     DBTable *table_;
     308             :     // check whether iteration is completed on all Table Partition
     309             :     std::atomic<uint16_t> pending_workers_;
     310             :     // For debugging purpose. Few of the tasks in this list could has finished
     311             :     // executing and destroyed. List of workers are useful in debugging with
     312             :     // gdb/gcore to see the current state of the walk and walk_context
     313             :     std::list<Task *> worker_tasks_;
     314             : };
     315             : 
     316       57469 : bool DBTable::WalkWorker::Run() {
     317       57469 :     int count = 0;
     318       57469 :     DBRequestKey *key_resume = walk_ctx_.get();
     319       57466 :     DBTable *table = walker_->table();
     320       57461 :     int max_walk_entry_count = table->GetWalkIterationToYield();
     321             :     DBEntry *entry;
     322             : 
     323       57459 :     if (key_resume != NULL) {
     324       53760 :         std::unique_ptr<const DBEntryBase> start;
     325       53760 :         start = table->AllocEntry(key_resume);
     326             :         // Find matching or next in sort order
     327       53721 :         entry = tbl_partition_->lower_bound(start.get());
     328       53828 :     } else {
     329        3699 :         entry = tbl_partition_->GetFirst();
     330             :     }
     331       57433 :     if (entry == NULL) {
     332           3 :         goto walk_done;
     333             :     }
     334             : 
     335      114897 :     for (DBEntry *next = NULL; entry; entry = next) {
     336      111127 :         next = tbl_partition_->GetNext(entry);
     337      111202 :         if (count == max_walk_entry_count) {
     338             :             // store the context
     339       53791 :             walk_ctx_ = entry->GetDBRequestKey();
     340       53475 :             return false;
     341             :         }
     342             : 
     343             :         // Invoke walker function
     344       57411 :         bool more = table->InvokeWalkCb(tbl_partition_, entry);
     345       57480 :         if (!more) {
     346           0 :             break;
     347             :         }
     348             : 
     349       57480 :         db_walker_wait();
     350       57467 :         count++;
     351             :     }
     352             : 
     353        3770 : walk_done:
     354             :     // Check whether all other walks on the table is completed
     355        3773 :     long num_walkers_on_tpart = walker_->pending_workers_.fetch_sub(1);
     356        3773 :     if (num_walkers_on_tpart == 1) {
     357        1946 :         table->WalkDone();
     358             :     }
     359        3714 :     return true;
     360             : }
     361             : 
     362        3714 : DBTable::WalkWorker::WalkWorker(TableWalker *walker, int db_partition_id)
     363        3714 :     : Task(walker->table()->GetWalkerTaskId(), db_partition_id), walker_(walker) {
     364        3714 :     tbl_partition_ = static_cast<DBTablePartition *>
     365        3714 :         (walker_->table()->GetTablePartition(db_partition_id));
     366        3714 : }
     367             : 
     368        5856 : void DBTable::TableWalker::StartWalk() {
     369        5856 :     CHECK_CONCURRENCY("db::Walker");
     370        5856 :     assert(pending_workers_ == 0);
     371       19149 :     for (int i = 0; i < table_->PartitionCount(); i++) {
     372             :         DBTablePartition *partition = static_cast<DBTablePartition *>(
     373       13293 :             table_->GetTablePartition(i));
     374       13293 :         if (!partition->size()) continue;
     375        3714 :         worker_tasks_.push_back(new WalkWorker(this, i));
     376        3714 :         pending_workers_++;
     377             :     }
     378        5856 :     if (pending_workers_ == 0) {
     379        3910 :         table_->WalkDone();
     380             :     } else {
     381        1946 :         TaskScheduler *scheduler = TaskScheduler::GetInstance();
     382        5660 :         for (auto *task : worker_tasks_) scheduler->Enqueue(task);
     383             :     }
     384        5856 : }
     385             : 
     386             : ///////////////////////////////////////////////////////////
     387             : // Implementation of DBTable methods
     388             : ///////////////////////////////////////////////////////////
     389       33187 : DBTable::DBTable(DB *db, const string &name)
     390             :     : DBTableBase(db, name),
     391       33187 :       walker_(new TableWalker(this)),
     392       66374 :       walker_task_id_(db->task_id()) {
     393             : 
     394             :     static bool init_ = false;
     395             :     static int iter_to_yield_env_ = 0;
     396             : 
     397       33187 :     if (!init_) {
     398             :         // XXX To be used for testing purposes only.
     399          21 :         char *count_str = getenv("DB_ITERATION_TO_YIELD");
     400          21 :         if (count_str) {
     401          21 :             iter_to_yield_env_ = strtol(count_str, NULL, 0);
     402             :         } else {
     403           0 :             iter_to_yield_env_ = kIterationToYield;
     404             :         }
     405          21 :         init_ = true;
     406             :     }
     407       33187 :     max_walk_iteration_to_yield_ = iter_to_yield_env_;
     408       33187 : }
     409             : 
     410       33187 : DBTable::~DBTable() {
     411       33187 :     STLDeleteValues(&partitions_);
     412       33187 : }
     413             : 
     414       33187 : void DBTable::Init() {
     415       67556 :     for (int i = 0; i < PartitionCount(); i++) {
     416       34368 :         partitions_.push_back(AllocPartition(i));
     417             :     }
     418       33187 : }
     419             : 
     420       34321 : DBTablePartition *DBTable::AllocPartition(int index) {
     421       34321 :     return new DBTablePartition(this, index);
     422             : }
     423             : 
     424        5856 : void DBTable::StartWalk() {
     425        5856 :     CHECK_CONCURRENCY("db::Walker");
     426        5856 :     incr_walk_count();
     427        5856 :     walker_->StartWalk();
     428        5856 : }
     429             : 
     430        9659 : DBEntry *DBTable::Add(const DBRequest *req) {
     431        9659 :     return AllocEntry(req->key.get()).release();
     432             : }
     433             : 
     434        1296 : void DBTable::Change(DBEntryBase *entry) {
     435        1296 :     DBTablePartBase *tpart = GetTablePartition(entry);
     436        1296 :     tpart->Notify(entry);
     437        1296 : }
     438             : 
     439           0 : bool DBTable::OnChange(DBEntry *entry, const DBRequest *req) {
     440           0 :     return true;
     441             : }
     442             : 
     443           0 : bool DBTable::Delete(DBEntry *entry, const DBRequest *req) {
     444           0 :     return true;
     445             : }
     446             : 
     447       14365 : int DBTable::PartitionCount() const {
     448       14365 :     return DB::PartitionCount();
     449             : }
     450             : 
     451     1158303 : static size_t HashToPartition(size_t hash) {
     452     1158303 :     return hash % DB::PartitionCount();
     453             : }
     454             : 
     455     1249143 : DBTablePartBase *DBTable::GetTablePartition(const int index) {
     456     1249143 :     return partitions_[index];
     457             : }
     458             : 
     459           0 : const DBTablePartBase *DBTable::GetTablePartition(const int index) const {
     460           0 :     return partitions_[index];
     461             : }
     462             : 
     463       70474 : DBTablePartBase *DBTable::GetTablePartition(const DBRequestKey *key) {
     464       70474 :     int id = HashToPartition(Hash(key));
     465       70474 :     return GetTablePartition(id);
     466             : }
     467             : 
     468           0 : const DBTablePartBase *DBTable::GetTablePartition(
     469             :         const DBRequestKey *key) const {
     470           0 :     int id = HashToPartition(Hash(key));
     471           0 :     return GetTablePartition(id);
     472             : }
     473             : 
     474      978830 : DBTablePartBase *DBTable::GetTablePartition(const DBEntryBase *entry) {
     475      978830 :     const DBEntry *gentry = static_cast<const DBEntry *>(entry);
     476      978830 :     size_t id = HashToPartition(Hash(gentry));
     477      976293 :     return GetTablePartition(id);
     478             : }
     479             : 
     480           0 : const DBTablePartBase *DBTable::GetTablePartition(
     481             :         const DBEntryBase *entry) const {
     482           0 :     const DBEntry *gentry = static_cast<const DBEntry *>(entry);
     483           0 :     size_t id = HashToPartition(Hash(gentry));
     484           0 :     return GetTablePartition(id);
     485             : }
     486             : 
     487             : // Find DB Entry without taking lock. Calling routine must ensure its
     488             : // running in exclusion with DB task
     489         117 : DBEntry *DBTable::FindNoLock(const DBEntry *entry) {
     490         117 :     size_t id = HashToPartition(Hash(entry));
     491             :     DBTablePartition *tbl_partition =
     492         117 :         static_cast<DBTablePartition *>(GetTablePartition(id));
     493         117 :     return tbl_partition->FindNoLock(entry);
     494             : }
     495             : 
     496       25759 : DBEntry *DBTable::Find(const DBEntry *entry) {
     497       25759 :     size_t id = HashToPartition(Hash(entry));
     498             :     DBTablePartition *tbl_partition =
     499       25757 :         static_cast<DBTablePartition *>(GetTablePartition(id));
     500       25757 :     return tbl_partition->Find(entry);
     501             : }
     502             : 
     503           0 : const DBEntry *DBTable::Find(const DBEntry *entry) const {
     504           0 :     return const_cast<DBTable *>(this)->Find(entry);
     505             : }
     506             : 
     507             : // Find DB Entry without taking lock. Calling routine must ensure its
     508             : // running in exclusion with DB task
     509          36 : DBEntry *DBTable::FindNoLock(const DBRequestKey *key) {
     510          36 :     int id = HashToPartition(Hash(key));
     511             :     DBTablePartition *tbl_partition =
     512          36 :     static_cast<DBTablePartition *>(GetTablePartition(id));
     513          36 :     return tbl_partition->FindNoLock(key);
     514             : }
     515             : 
     516       85168 : DBEntry *DBTable::Find(const DBRequestKey *key, int id) {
     517       85168 :     if (id == -1)
     518       85168 :         id = HashToPartition(Hash(key));
     519             :     DBTablePartition *tbl_partition =
     520       85168 :     static_cast<DBTablePartition *>(GetTablePartition(id));
     521       85168 :     return tbl_partition->Find(key);
     522             : }
     523             : 
     524           0 : const DBEntry *DBTable::Find(const DBRequestKey *key, int id) const {
     525           0 :     return const_cast<DBTable *>(this)->Find(key, id);
     526             : }
     527             : 
     528             : //
     529             : // Concurrency: called from task that's mutually exclusive with db::DBTable
     530             : // or db::IFMapTable as applicable.
     531             : //
     532             : // Calculate the size across all partitions.
     533             : //
     534       27013 : size_t DBTable::Size() const {
     535       27013 :     size_t total = 0;
     536       27013 :     for (vector<DBTablePartition *>::const_iterator iter = partitions_.begin();
     537       74505 :          iter != partitions_.end(); iter++) {
     538       47494 :         total += (*iter)->size();
     539             :     }
     540       27010 :     return total;
     541             : }
     542             : 
     543       18648 : void DBTable::Input(DBTablePartition *tbl_partition, DBClient *client,
     544             :                     DBRequest *req) {
     545             :     DBRequestKey *key =
     546       18648 :         static_cast<DBRequestKey *>(req->key.get());
     547       18648 :     DBEntry *entry = NULL;
     548             : 
     549       18648 :     entry = tbl_partition->Find(key);
     550       18648 :     if (req->oper == DBRequest::DB_ENTRY_ADD_CHANGE) {
     551       13809 :         if (entry) {
     552       10513 :             if (OnChange(entry, req) || entry->IsDeleted()) {
     553             :                 // The entry may currently be marked as deleted.
     554         658 :                 entry->ClearDelete();
     555         658 :                 tbl_partition->Change(entry);
     556             :             }
     557             :         } else {
     558        3296 :             if ((entry = Add(req)) != NULL) {
     559        3296 :                 tbl_partition->Add(entry);
     560             :             }
     561             :         }
     562        4839 :     } else if (req->oper == DBRequest::DB_ENTRY_DELETE) {
     563        3438 :         if (entry) {
     564        3438 :             if (Delete(entry, req)) {
     565        3438 :                 tbl_partition->Delete(entry);
     566             :             }
     567             :         }
     568        1401 :     } else if (req->oper == DBRequest::DB_ENTRY_NOTIFY) {
     569        1401 :         if (entry) {
     570        1401 :             tbl_partition->Notify(entry);
     571             :         }
     572             :     } else {
     573           0 :         assert(0);
     574             :     }
     575       18648 : }
     576             : 
     577           2 : void DBTable::DBStateClear(DBTable *table, ListenerId id) {
     578           2 :     DBEntryBase *next = NULL;
     579             : 
     580           4 :     for (int i = 0; i < table->PartitionCount(); ++i) {
     581             :         DBTablePartition *partition = static_cast<DBTablePartition *>(
     582           2 :             table->GetTablePartition(i));
     583             : 
     584           2 :         for (DBEntryBase *entry = partition->GetFirst(); entry; entry = next) {
     585           0 :             next = partition->GetNext(entry);
     586           0 :             DBState *state = entry->GetState(table, id);
     587           0 :             if (state) {
     588           0 :                 entry->ClearState(table, id);
     589           0 :                 delete state;
     590             :             }
     591             :         }
     592             :     }
     593           2 : }
     594             : 
     595             : //
     596             : // Callback for table walk triggered by NotifyAllEntries.
     597             : //
     598           0 : bool DBTable::WalkCallback(DBTablePartBase *tpart, DBEntryBase *entry) {
     599           0 :     tpart->Notify(entry);
     600           0 :     return true;
     601             : }
     602             : 
     603             : //
     604             : // Callback for completion of table walk triggered by NotifyAllEntries.
     605             : //
     606           0 : void DBTable::WalkCompleteCallback(DBTableBase *tbl_base) {
     607           0 :     walk_ref_.reset();
     608           0 : }
     609             : 
     610             : //
     611             : // Concurrency: called from task that's mutually exclusive with db::DBTable
     612             : // or db::IFMapTable as applicable.
     613             : //
     614             : // Trigger notification of all entries to all listeners.
     615             : // Should be used sparingly e.g. to handle significant configuration change.
     616             : //
     617             : // The walk callback just turns around and puts the DBentryBase on the change
     618             : // list.
     619             : //
     620             : // If the walk is already running, it is allowed to complete and WalkAgain API
     621             : // is invoked to trigger walk on current walk completion.
     622             : //
     623           0 : void DBTable::NotifyAllEntries() {
     624           0 :     CHECK_CONCURRENCY("bgp::Config", "bgp::ConfigHelper", "bgp::RTFilter",
     625             :                       "db::DBTable");
     626             : 
     627           0 :     if (empty())
     628           0 :         return;
     629             : 
     630           0 :     if (walk_ref_ == NULL) {
     631             :         walk_ref_ =
     632           0 :             AllocWalker(boost::bind(&DBTable::WalkCallback, this, _1, _2),
     633           0 :                     boost::bind(&DBTable::WalkCompleteCallback, this, _2));
     634           0 :         WalkTable(walk_ref_);
     635             :     } else {
     636           0 :         WalkAgain(walk_ref_);
     637             :     }
     638             : }
     639             : 
     640        7362 : DBTable::DBTableWalkRef DBTable::AllocWalker(WalkFn walk_fn,
     641             :                                              WalkCompleteFn walk_complete) {
     642        7362 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     643        7362 :     return walk_mgr->AllocWalker(this, walk_fn, walk_complete);
     644             : }
     645             : 
     646        5454 : void DBTable::ReleaseWalker(DBTable::DBTableWalkRef &walk) {
     647        5454 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     648        5454 :     walk_mgr->ReleaseWalker(walk);
     649        5454 :     return;
     650             : }
     651             : 
     652        4188 : void DBTable::WalkTable(DBTable::DBTableWalkRef walk) {
     653        4188 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     654        4188 :     walk_mgr->WalkTable(walk);
     655        4188 :     return;
     656             : }
     657             : 
     658        3241 : void DBTable::WalkAgain(DBTable::DBTableWalkRef walk) {
     659        3241 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     660        3241 :     walk_mgr->WalkAgain(walk);
     661        3241 :     return;
     662             : }
     663             : 
     664       57504 : bool DBTable::InvokeWalkCb(DBTablePartBase *part, DBEntryBase *entry) {
     665       57504 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     666       57449 :     return walk_mgr->InvokeWalkCb(part, entry);
     667             : }
     668             : 
     669        5856 : void DBTable::WalkDone() {
     670        5856 :     incr_walk_complete_count();
     671        5856 :     walker_->ClearWalkWorks();
     672        5856 :     DBTableWalkMgr *walk_mgr = database()->GetWalkMgr();
     673        5856 :     return walk_mgr->WalkDone();
     674             : }

Generated by: LCOV version 1.14