Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include <boost/uuid/uuid_io.hpp> 7 : #include <cmn/agent_cmn.h> 8 : #include <cmn/agent.h> 9 : #include <resource_manager/resource_manager.h> 10 : #include <resource_manager/resource_table.h> 11 : #include <resource_manager/index_resource.h> 12 : 13 1277 : ResourceKey::ResourceKey(ResourceManager *rm, Resource::Type type) : 14 1277 : rm_(rm), dirty_(false), 15 1277 : resource_table_(static_cast<ResourceTable *>(rm->resource_table(type))) { 16 1277 : } 17 : 18 1277 : ResourceKey::~ResourceKey() { 19 1277 : } 20 : 21 0 : bool ResourceKey::operator<(const ResourceKey &rhs) const { 22 0 : return IsLess(rhs); 23 : } 24 : 25 0 : ResourceBackupEndKey::ResourceBackupEndKey(ResourceManager *rm) : 26 0 : ResourceKey(rm, Resource::INVALID) { 27 0 : } 28 : 29 0 : ResourceBackupEndKey::~ResourceBackupEndKey() { 30 0 : } 31 : 32 1277 : ResourceData::ResourceData(ResourceManager *rm) : 33 1277 : rm_(rm) { 34 1277 : } 35 : 36 1277 : ResourceData::~ResourceData() { 37 1277 : } 38 : 39 : 40 12 : ResourceTable::ResourceTable(ResourceManager *rm) : rm_(rm) { 41 12 : } 42 : 43 12 : ResourceTable::~ResourceTable() { 44 12 : assert(key_data_map_.size() == 0); 45 12 : } 46 : 47 1277 : void ResourceTable::InsertKey(KeyPtr key, DataPtr data) { 48 1277 : key_data_map_.insert(std::pair<KeyPtr, DataPtr>(key, data)); 49 1277 : } 50 : 51 1277 : void ResourceTable::DeleteKey(KeyPtr key) { 52 1277 : key_data_map_.erase(key); 53 1277 : } 54 : 55 1277 : ResourceTable::DataPtr ResourceTable::FindKeyPtr(KeyPtr key) { 56 1277 : KeyDataMapIter it = key_data_map_.find(key); 57 1277 : if (it == key_data_map_.end()) { 58 0 : return DataPtr(); 59 : } 60 1277 : return (*it).second; 61 : } 62 : 63 0 : ResourceData* ResourceTable::FindKey(KeyPtr key) { 64 0 : return (FindKeyPtr(key).get()); 65 : } 66 : 67 : // Walk all the etries remove keys are not usable. 68 0 : void ResourceTable::FlushStale() { 69 0 : for (KeyDataMapIter it = key_data_map_.begin(); 70 0 : it != key_data_map_.end();) { 71 0 : KeyPtr key = it->first; 72 0 : if (key->dirty()) { 73 0 : it++; 74 0 : rm_->Release(key); 75 : } else { 76 0 : it++; 77 : } 78 0 : } 79 0 : } 80 : 81 : // Allocate the resource and mark the key usable 82 1277 : ResourceTable::DataPtr ResourceTable::Allocate(KeyPtr key) { 83 1277 : KeyDataMapIter it = key_data_map_.find(key); 84 1277 : ResourceManager::DataPtr data = DataPtr(); 85 1277 : if (it == key_data_map_.end()) { 86 1277 : data = AllocateData(key); 87 1277 : InsertKey(key, data); 88 1277 : key->reset_dirty(); 89 : } else { 90 0 : data = (*it).second; 91 0 : (*it).first->reset_dirty(); 92 : } 93 2554 : return data; 94 0 : }