DDS  ver. 2.0
KeyValueManager.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef __DDS__KeyValueManager__
6 #define __DDS__KeyValueManager__
7 
8 // DDS
9 #include "DeleteKeyCmd.h"
10 #include "Topology.h"
11 #include "UpdateKeyCmd.h"
12 // STD
13 #include <map>
14 #include <mutex>
15 
16 namespace dds
17 {
18  namespace commander_cmd
19  {
20  enum class EKeyUpdateResult
21  {
22  Correct,
25  };
26 
32 
39  {
40  typedef std::shared_ptr<SKeyValueRecord> ptr_t;
41  typedef std::weak_ptr<SKeyValueRecord> wptr_t;
42  // task ID --> STaskKeyValue
43  typedef std::multimap<uint64_t, SKeyValueRecord::ptr_t> taskMap_t;
44  // key --> STaskKeyValue
45  // typedef std::map<std::string, SKeyValueRecord::ptr_t> keyMap_t;
46 
48  ~SKeyValueRecord();
49 
50  EKeyUpdateResult updateKeyValue(const protocol_api::SUpdateKeyCmd& _cmd,
51  protocol_api::SUpdateKeyCmd& _serverCmd);
52  void deleteKeyValue();
53  std::string getKeyValueString() const;
54 
55  friend std::ostream& operator<<(std::ostream& _stream, const SKeyValueRecord& _value);
56 
57  private:
58  mutable std::mutex m_mutex;
59  protocol_api::SUpdateKeyCmd m_keyValue;
60  bool m_deleted;
61  };
62 
63  inline std::ostream& operator<<(std::ostream& _stream, const SKeyValueRecord& _value)
64  {
65  std::lock_guard<std::mutex> lock(_value.m_mutex);
66 
67  return _stream << "SKeyValueRecord cmd: " << _value.m_keyValue << " deleted: " << _value.m_deleted;
68  }
69 
73  {
74  typedef std::shared_ptr<SPropertyRecord> ptr_t;
75  typedef std::weak_ptr<SPropertyRecord> wptr_t;
76  // property --> SPropertyRecord
77  typedef std::map<std::string, SPropertyRecord::ptr_t> propertyMap_t;
78 
80  ~SPropertyRecord();
81 
82  void addKeyValueRecord(uint64_t _taskID, SKeyValueRecord::ptr_t _keyValueRecord);
83 
84  EKeyUpdateResult updateKeyValue(const protocol_api::SUpdateKeyCmd& _cmd,
85  protocol_api::SUpdateKeyCmd& _serverCmd);
86 
87  std::string getKeyValueString() const;
88 
89  friend std::ostream& operator<<(std::ostream& _stream, const SPropertyRecord& _value);
90 
91  private:
93  };
94 
95  inline std::ostream& operator<<(std::ostream& _stream, const SPropertyRecord& _value)
96  {
97  _stream << "SPropertyRecord\n";
98  for (const auto& v : _value.m_taskMap)
99  {
100  _stream << " " << v.first << " --> " << *(v.second) << "\n";
101  }
102  return _stream;
103  }
104 
115  {
116  public:
118  ~CKeyValueManager();
119 
120  void initWithTopology(const topology_api::CTopology& _topology);
121  void updateWithTopology(const topology_api::CTopology& _topology,
122  const topology_api::CTopology::HashSet_t& _removedTasks,
123  const topology_api::CTopology::HashSet_t& _addedTasks);
124  EKeyUpdateResult updateKeyValue(const protocol_api::SUpdateKeyCmd& _cmd,
125  protocol_api::SUpdateKeyCmd& _serverCmd);
126  void deleteKeyValue(uint64_t _taskID);
127 
128  std::string getKeyValueString(const std::string _propertyID) const;
129  std::string getPropertyString() const;
130 
131  friend std::ostream& operator<<(std::ostream& _stream, const CKeyValueManager& _value);
132 
133  private:
134  void initWithTopologyImpl(const topology_api::CTopology& _topology,
135  const topology_api::CTopology::HashSet_t* _addedTasks);
136 
137  SPropertyRecord::propertyMap_t m_propertyMap;
138  // The map is used to query specific records by task ID, for example, to fetch or delete all records for a
139  // specific task ID.
140  SKeyValueRecord::taskMap_t m_taskMap;
141  };
142 
143  inline std::ostream& operator<<(std::ostream& _stream, const CKeyValueManager& _value)
144  {
145  _stream << "CKeyValueManager\n";
146  _stream << "[Property map]\n";
147  for (const auto& v : _value.m_propertyMap)
148  {
149  _stream << " " << v.first << " --> " << *(v.second) << "\n";
150  }
151  _stream << "[Task map]\n";
152  for (const auto& v : _value.m_taskMap)
153  {
154  _stream << " " << v.first << " --> " << *(v.second) << "\n";
155  }
156 
157  return _stream;
158  }
159  }
160 }
161 
162 #endif /* defined(__DDS__KeyValueManager__) */
std::multimap< uint64_t, SKeyValueRecord::ptr_t > taskMap_t
Definition: KeyValueManager.h:43
Definition: UpdateKeyCmd.h:14
TODO: Key-Value has to provide a possibility to properly react on the errors. If Key-Value was not sa...
Definition: KeyValueManager.h:38
EKeyUpdateResult
Definition: KeyValueManager.h:20
std::map< std::string, SPropertyRecord::ptr_t > propertyMap_t
Definition: KeyValueManager.h:77
std::set< uint64_t > HashSet_t
Definition: Topology.h:63
Definition: dds-agent/src/AgentConnectionManager.h:18
Definition: Topology.h:42
Key-value manager for the DDS commander.
Definition: KeyValueManager.h:114
std::weak_ptr< SPropertyRecord > wptr_t
Definition: KeyValueManager.h:75
Container for the key-value records with the same property ID.
Definition: KeyValueManager.h:72
std::ostream & operator<<(std::ostream &_stream, const SKeyValueRecord &_value)
Definition: KeyValueManager.h:63
std::shared_ptr< SPropertyRecord > ptr_t
Definition: KeyValueManager.h:74
std::weak_ptr< SKeyValueRecord > wptr_t
Definition: KeyValueManager.h:41
std::shared_ptr< SKeyValueRecord > ptr_t
Definition: KeyValueManager.h:40