OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
resource_table.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/uuid/uuid_io.hpp>
6 #include <cmn/agent_cmn.h>
7 #include <cmn/agent.h>
11 
13  rm_(rm), dirty_(false),
14  resource_table_(static_cast<ResourceTable *>(rm->resource_table(type))) {
15 }
16 
18 }
19 
20 bool ResourceKey::operator<(const ResourceKey &rhs) const {
21  return IsLess(rhs);
22 }
23 
26 }
27 
29 }
30 
32  rm_(rm) {
33 }
34 
36 }
37 
38 
40 }
41 
43  assert(key_data_map_.size() == 0);
44 }
45 
47  key_data_map_.insert(std::pair<KeyPtr, DataPtr>(key, data));
48 }
49 
51  key_data_map_.erase(key);
52 }
53 
55  KeyDataMapIter it = key_data_map_.find(key);
56  if (it == key_data_map_.end()) {
57  return DataPtr();
58  }
59  return (*it).second;
60 }
61 
63  return (FindKeyPtr(key).get());
64 }
65 
66 // Walk all the etries remove keys are not usable.
68  for (KeyDataMapIter it = key_data_map_.begin();
69  it != key_data_map_.end();) {
70  KeyPtr key = it->first;
71  if (key->dirty()) {
72  it++;
73  rm_->Release(key);
74  } else {
75  it++;
76  }
77  }
78 }
79 
80 // Allocate the resource and mark the key usable
82  KeyDataMapIter it = key_data_map_.find(key);
84  if (it == key_data_map_.end()) {
85  data = AllocateData(key);
86  InsertKey(key, data);
87  key->reset_dirty();
88  } else {
89  data = (*it).second;
90  (*it).first->reset_dirty();
91  }
92  return data;
93 }
virtual ~ResourceKey()
virtual DataPtr AllocateData(KeyPtr key)=0
void Release(KeyPtr key)
virtual bool IsLess(const ResourceKey &rhs) const =0
boost::shared_ptr< ResourceKey > KeyPtr
boost::shared_ptr< ResourceData > DataPtr
ResourceTable(ResourceManager *rm)
ResourceManager * rm_
ResourceData * FindKey(KeyPtr key)
virtual ~ResourceData()
uint8_t type
Definition: load_balance.h:109
void InsertKey(KeyPtr key, DataPtr data)
bool operator<(const ResourceKey &rhs) const
virtual ~ResourceBackupEndKey()
virtual ~ResourceTable()
DataPtr FindKeyPtr(KeyPtr key)
boost::shared_ptr< ResourceData > DataPtr
ResourceData(ResourceManager *rm)
ResourceBackupEndKey(ResourceManager *rm)
ResourceTable::DataPtr Allocate(KeyPtr key)
KeyDataMap key_data_map_
void DeleteKey(KeyPtr key)
KeyDataMap::iterator KeyDataMapIter
ResourceKey(ResourceManager *rm, Resource::Type type)