OpenSDN source code
resource_table.cc
Go to the documentation of this file.
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>
12 
14  rm_(rm), dirty_(false),
15  resource_table_(static_cast<ResourceTable *>(rm->resource_table(type))) {
16 }
17 
19 }
20 
21 bool ResourceKey::operator<(const ResourceKey &rhs) const {
22  return IsLess(rhs);
23 }
24 
27 }
28 
30 }
31 
33  rm_(rm) {
34 }
35 
37 }
38 
39 
41 }
42 
44  assert(key_data_map_.size() == 0);
45 }
46 
48  key_data_map_.insert(std::pair<KeyPtr, DataPtr>(key, data));
49 }
50 
52  key_data_map_.erase(key);
53 }
54 
56  KeyDataMapIter it = key_data_map_.find(key);
57  if (it == key_data_map_.end()) {
58  return DataPtr();
59  }
60  return (*it).second;
61 }
62 
64  return (FindKeyPtr(key).get());
65 }
66 
67 // Walk all the etries remove keys are not usable.
69  for (KeyDataMapIter it = key_data_map_.begin();
70  it != key_data_map_.end();) {
71  KeyPtr key = it->first;
72  if (key->dirty()) {
73  it++;
74  rm_->Release(key);
75  } else {
76  it++;
77  }
78  }
79 }
80 
81 // Allocate the resource and mark the key usable
83  KeyDataMapIter it = key_data_map_.find(key);
85  if (it == key_data_map_.end()) {
86  data = AllocateData(key);
87  InsertKey(key, data);
88  key->reset_dirty();
89  } else {
90  data = (*it).second;
91  (*it).first->reset_dirty();
92  }
93  return data;
94 }
virtual ~ResourceBackupEndKey()
ResourceBackupEndKey(ResourceManager *rm)
virtual ~ResourceData()
ResourceData(ResourceManager *rm)
bool operator<(const ResourceKey &rhs) const
ResourceKey(ResourceManager *rm, Resource::Type type)
virtual bool IsLess(const ResourceKey &rhs) const =0
virtual ~ResourceKey()
void Release(KeyPtr key)
boost::shared_ptr< ResourceData > DataPtr
virtual ~ResourceTable()
DataPtr FindKeyPtr(KeyPtr key)
boost::shared_ptr< ResourceData > DataPtr
void InsertKey(KeyPtr key, DataPtr data)
ResourceManager * rm_
KeyDataMap::iterator KeyDataMapIter
void DeleteKey(KeyPtr key)
ResourceTable::DataPtr Allocate(KeyPtr key)
ResourceData * FindKey(KeyPtr key)
virtual DataPtr AllocateData(KeyPtr key)=0
KeyDataMap key_data_map_
ResourceTable(ResourceManager *rm)
boost::shared_ptr< ResourceKey > KeyPtr
@ INVALID
Definition: globals.h:150
uint8_t type
Definition: load_balance.h:2