ConfigDataTree.h
Go to the documentation of this file.
1 #ifndef _DBUTILS_DAQCONFIG_CONFIGDATATREE_H
2 #define _DBUTILS_DAQCONFIG_CONFIGDATATREE_H
3 
4 #include <DatabaseUtils/DAQConfig/ConfigDataSelector.h>
5 #include <DatabaseUtils/SimpleParameterSet.h>
6 #include <NovaDatabase/Table.h>
7 #include <boost/algorithm/string/replace.hpp>
8 #include <boost/lexical_cast.hpp>
9 #include <boost/shared_ptr.hpp>
10 #include <boost/tuple/tuple.hpp>
11 #include <string>
12 #include <map>
13 #include <vector>
14 #include <iostream>
15 #include <stdint.h>
16 
17 namespace dbutils {
18 namespace daqconfig {
19 
20 /**
21  * Class to handle fetching of DAQ configuration data from the database.
22  *
23  * @author Kurt Biery
24  * @version $Revision: 1.12.28.1 $ $Date: 2019/09/27 00:07:03 $
25  */
26 
28 
29  public:
30 
34 
35  typedef std::map<std::string, std::string> ConfigValueMap;
36  typedef boost::shared_ptr<ConfigValueMap> ConfigValueMapPtr;
37  typedef std::map<std::string, ConfigValueMapPtr> ConfigDataContainer;
38  typedef std::vector<std::string> ConfigValueList;
39  typedef boost::shared_ptr<ConfigValueList> ConfigValueListPtr;
40  typedef std::map<std::string, ConfigValueListPtr> ConfigValueListMap;
41 
42  // **********************
43  // *** Constructor(s) ***
44  // **********************
45 
46  ConfigDataTree(const std::string& tableDefinitionFilename,
47  const std::vector<int64_t>& subsystemConfigIdList,
48  const ConfigDataSelector& selector,
49  std::vector<std::string> dataColumnNameList);
50 
51  ConfigDataTree(const std::string& tableDefinitionFilename,
52  const std::vector<int64_t>& subsystemConfigIdList);
53 
54  // *******************
55  // *** Accessor(s) ***
56  // *******************
57 
58  bool hasValidData() const {return _hasValidData;}
59 
60  std::vector<std::string> getSelectorColumnNames() const
61  {
63  }
65  {
66  ConfigValueMap::const_iterator iter =
67  _selectorDefaultValueMap.find(columnName);
68  if (iter == _selectorDefaultValueMap.end()) {
69  return NO_DEFAULT_KEY_STRING;
70  }
71  else {
72  return iter->second;
73  }
74  }
75  std::vector<std::string>
77  {
78  ConfigValueListMap::const_iterator iter =
79  _distinctSelectorValueMap.find(columnName);
80  if (iter == _distinctSelectorValueMap.end()) {
81  std::vector<std::string> emptyList;
82  return emptyList;
83  }
84  else {
85  return *(iter->second);
86  }
87  }
88  std::vector<std::string> getDataColumnNames() const
89  {
90  return _dataColumnNameList;
91  }
92  std::vector<std::string> getDataColumnTypes() const
93  {
94  return _dataColumnTypeList;
95  }
97 
98  template<class T>
99  bool getTrueValue(const std::string& dataColumnName, T& resultValue);
100 
101  template<class T, class U>
102  bool getTrueValueFor1Key(const U& primarySelectionValue,
103  const std::string& dataColumnName,
104  T& resultValue);
105 
106  template<class T, class U, class V>
107  bool getTrueValueFor2Keys(const U& primarySelectionValue,
108  const V& secondarySelectionValue,
109  const std::string& dataColumnName,
110  T& resultValue);
111 
112  template<class T, class U, class V, class W>
113  bool getTrueValueFor3Keys(const U& primarySelectionValue,
114  const V& secondarySelectionValue,
115  const W& tertiarySelectionValue,
116  const std::string& dataColumnName,
117  T& resultValue);
118 
119  template<class T, class U>
120  bool getEffectiveValueFor1Key(const U& primarySelectionValue,
121  const std::string& dataColumnName,
122  T& resultValue);
123 
124  template<class T, class U, class V>
125  bool getEffectiveValueFor2Keys(const U& primarySelectionValue,
126  const V& secondarySelectionValue,
127  const std::string& dataColumnName,
128  T& resultValue);
129 
130  template<class T, class U, class V, class W>
131  bool getEffectiveValueFor3Keys(const U& primarySelectionValue,
132  const V& secondarySelectionValue,
133  const W& tertiarySelectionValue,
134  const std::string& dataColumnName,
135  T& resultValue);
136 
137  // *******************
138  // *** Modifier(s) ***
139  // *******************
140 
141  bool saveValues(const std::vector<dbutils::SimpleParameterSet>& newDataList) const;
142 
143  private:
144 
145  void _initialize(boost::shared_ptr<nova::database::Table> dbTablePtr,
146  const std::vector<int64_t>& subsystemConfigIdList,
147  const ConfigDataSelector& selector,
148  std::vector<std::string> dataColumnNameList,
149  int debugFlag);
150 
151  template<class T>
153  const std::string& dataColumnName,
154  T& value);
155 
156  // ********************
157  // *** Private data ***
158  // ********************
159 
163  ConfigValueMap _selectorDefaultValueMap;
164  ConfigValueListMap _distinctSelectorValueMap;
165  std::vector<std::string> _dataColumnNameList;
166  std::vector<std::string> _dataColumnTypeList;
168  ConfigDataContainer _resultSet;
169 
170  std::vector<std::string> _keyElementsForDefaults;
171  std::vector<bool> _noKeyElementForDefaults;
172 };
173 
174 /**
175  * Fetches the requested value...
176  */
177 template<class T>
178 bool ConfigDataTree::
179 getTrueValue(const std::string& dataColumnName, T& resultValue)
180 {
182  return _getValueFromContainer(key, dataColumnName, resultValue);
183 }
184 
185 /**
186  * Fetches the requested value...
187  */
188 template<class T, class U>
189 bool ConfigDataTree::
190 getTrueValueFor1Key(const U& primarySelectionValue,
191  const std::string& dataColumnName, T& resultValue)
192 {
193  std::string key =
194  boost::lexical_cast<std::string>(primarySelectionValue);
195  return _getValueFromContainer(key, dataColumnName, resultValue);
196 }
197 
198 /**
199  * Fetches the requested value...
200  */
201 template<class T, class U, class V>
202 bool ConfigDataTree::
203 getTrueValueFor2Keys(const U& primarySelectionValue,
204  const V& secondarySelectionValue,
205  const std::string& dataColumnName,
206  T& resultValue)
207 {
208  std::string key =
209  boost::lexical_cast<std::string>(primarySelectionValue) +
211  boost::lexical_cast<std::string>(secondarySelectionValue);
212  return _getValueFromContainer(key, dataColumnName, resultValue);
213 }
214 
215 /**
216  * Fetches the requested value...
217  */
218 template<class T, class U, class V, class W>
219 bool ConfigDataTree::
220 getTrueValueFor3Keys(const U& primarySelectionValue,
221  const V& secondarySelectionValue,
222  const W& tertiarySelectionValue,
223  const std::string& dataColumnName,
224  T& resultValue)
225 {
226  std::string key =
227  boost::lexical_cast<std::string>(primarySelectionValue) +
229  boost::lexical_cast<std::string>(secondarySelectionValue) +
231  boost::lexical_cast<std::string>(tertiarySelectionValue);
232  return _getValueFromContainer(key, dataColumnName, resultValue);
233 }
234 
235 /**
236  * Fetches the requested value...
237  */
238 template<class T, class U>
239 bool ConfigDataTree::
240 getEffectiveValueFor1Key(const U& primarySelectionValue,
241  const std::string& dataColumnName, T& resultValue)
242 {
243  std::string key =
244  boost::lexical_cast<std::string>(primarySelectionValue);
245 
246  if (!_getValueFromContainer(key, dataColumnName, resultValue)) {
247  if (_keyElementsForDefaults.size() < 1) {return false;}
248 
249  key = _keyElementsForDefaults[0];
250  if (_noKeyElementForDefaults[0]) {
252  boost::lexical_cast<std::string>
253  (primarySelectionValue));
254  }
255 
256  return _getValueFromContainer(key, dataColumnName, resultValue);
257  }
258  return true;
259 }
260 
261 /**
262  * Fetches the requested value...
263  */
264 template<class T, class U, class V>
265 bool ConfigDataTree::
266 getEffectiveValueFor2Keys(const U& primarySelectionValue,
267  const V& secondarySelectionValue,
268  const std::string& dataColumnName,
269  T& resultValue)
270 {
271  std::string key =
272  boost::lexical_cast<std::string>(primarySelectionValue) +
274  boost::lexical_cast<std::string>(secondarySelectionValue);
275 
276  if (! _getValueFromContainer(key, dataColumnName, resultValue)) {
277  if (_keyElementsForDefaults.size() < 2) {return false;}
278 
279  std::string secondKeyElement = _keyElementsForDefaults[1];
280  if (_noKeyElementForDefaults[1]) {
281  boost::replace_all(secondKeyElement, NO_DEFAULT_KEY_STRING,
282  boost::lexical_cast<std::string>
283  (secondarySelectionValue));
284  }
285  key = boost::lexical_cast<std::string>(primarySelectionValue) +
286  KEY_SEPARATOR_STRING + secondKeyElement;
287 
288  if (! _getValueFromContainer(key, dataColumnName, resultValue)) {
289  std::string firstKeyElement = _keyElementsForDefaults[0];
290  if (_noKeyElementForDefaults[0]) {
291  boost::replace_all(firstKeyElement, NO_DEFAULT_KEY_STRING,
292  boost::lexical_cast<std::string>
293  (primarySelectionValue));
294  }
295  key = firstKeyElement + KEY_SEPARATOR_STRING + secondKeyElement;
296 
297  return _getValueFromContainer(key, dataColumnName,
298  resultValue);
299  }
300  }
301  return true;
302 }
303 
304 /**
305  * Fetches the requested value...
306  */
307 template<class T, class U, class V, class W>
308 bool ConfigDataTree::
309 getEffectiveValueFor3Keys(const U& primarySelectionValue,
310  const V& secondarySelectionValue,
311  const W& tertiarySelectionValue,
312  const std::string& dataColumnName,
313  T& resultValue)
314 {
315  std::string key =
316  boost::lexical_cast<std::string>(primarySelectionValue) +
318  boost::lexical_cast<std::string>(secondarySelectionValue) +
320  boost::lexical_cast<std::string>(tertiarySelectionValue);
321 
322  if (! _getValueFromContainer(key, dataColumnName, resultValue)) {
323  if (_keyElementsForDefaults.size() < 3) {return false;}
324 
325  std::string thirdKeyElement = _keyElementsForDefaults[2];
326  if (_noKeyElementForDefaults[2]) {
327  boost::replace_all(thirdKeyElement, NO_DEFAULT_KEY_STRING,
328  boost::lexical_cast<std::string>
329  (tertiarySelectionValue));
330  }
331  key = boost::lexical_cast<std::string>(primarySelectionValue) +
333  boost::lexical_cast<std::string>(secondarySelectionValue) +
334  KEY_SEPARATOR_STRING + thirdKeyElement;
335 
336  if (! _getValueFromContainer(key, dataColumnName, resultValue)) {
337  std::string secondKeyElement = _keyElementsForDefaults[1];
338  if (_noKeyElementForDefaults[1]) {
339  boost::replace_all(secondKeyElement, NO_DEFAULT_KEY_STRING,
340  boost::lexical_cast<std::string>
341  (secondarySelectionValue));
342  }
343  key = boost::lexical_cast<std::string>(primarySelectionValue) +
344  KEY_SEPARATOR_STRING + secondKeyElement +
345  KEY_SEPARATOR_STRING + thirdKeyElement;
346 
347  if (! _getValueFromContainer(key, dataColumnName,
348  resultValue)) {
349  std::string firstKeyElement = _keyElementsForDefaults[0];
350  if (_noKeyElementForDefaults[0]) {
351  boost::replace_all(firstKeyElement, NO_DEFAULT_KEY_STRING,
352  boost::lexical_cast<std::string>
353  (primarySelectionValue));
354  }
355  key = firstKeyElement +
356  KEY_SEPARATOR_STRING + secondKeyElement +
357  KEY_SEPARATOR_STRING + thirdKeyElement;
358 
359  return _getValueFromContainer(key, dataColumnName,
360  resultValue);
361  }
362  }
363  }
364  return true;
365 }
366 
367 /**
368  *
369  */
370 template<class T>
371 bool ConfigDataTree::
373  const std::string& dataColumnName,
374  T& value)
375 {
376  ConfigDataContainer::const_iterator iter1 = _resultSet.find(key);
377  if (iter1 == _resultSet.end()) {return false;}
378 
379  ConfigValueMapPtr mapPtr = _resultSet[key];
380  ConfigValueMap::const_iterator iter2 = mapPtr->find(dataColumnName);
381  if (iter2 == mapPtr->end()) {return false;}
382 
383  value = boost::lexical_cast<T>(iter2->second);
384  return true;
385 }
386 
387 } // end of namespace daqconfig
388 } // end of namespace dbutils
389 
390 #endif
std::vector< std::string > _keyElementsForDefaults
bool getTrueValueFor1Key(const U &primarySelectionValue, const std::string &dataColumnName, T &resultValue)
bool getEffectiveValueFor2Keys(const U &primarySelectionValue, const V &secondarySelectionValue, const std::string &dataColumnName, T &resultValue)
std::vector< std::string > getDataColumnTypes() const
std::vector< bool > _noKeyElementForDefaults
std::vector< std::string > _dataColumnNameList
std::vector< std::string > getDataColumnNames() const
std::vector< std::string > _dataColumnTypeList
std::vector< std::string > getDistinctSelectorColumnValues(const std::string &columnName) const
ConfigValueListMap _distinctSelectorValueMap
std::map< std::string, std::string > ConfigValueMap
boost::shared_ptr< ConfigValueMap > ConfigValueMapPtr
bool replace_all(std::string &in, std::string const &from, std::string const &to)
Replace all occurrences of from in string with to.
bool getTrueValue(const std::string &dataColumnName, T &resultValue)
const XML_Char int const XML_Char * value
Definition: expat.h:331
bool getEffectiveValueFor1Key(const U &primarySelectionValue, const std::string &dataColumnName, T &resultValue)
bool saveValues(const std::vector< dbutils::SimpleParameterSet > &newDataList) const
void _initialize(boost::shared_ptr< nova::database::Table > dbTablePtr, const std::vector< int64_t > &subsystemConfigIdList, const ConfigDataSelector &selector, std::vector< std::string > dataColumnNameList, int debugFlag)
std::string getSelectorValueForDefault(std::string columnName) const
bool getTrueValueFor2Keys(const U &primarySelectionValue, const V &secondarySelectionValue, const std::string &dataColumnName, T &resultValue)
std::vector< std::string > getSelectorColumnNames() const
std::map< std::string, ConfigValueListPtr > ConfigValueListMap
static const std::string NO_DEFAULT_KEY_STRING
bool _getValueFromContainer(const std::string &key, const std::string &dataColumnName, T &value)
static const std::string KEY_FOR_SYSTEM_WIDE_PARAM
bool getTrueValueFor3Keys(const U &primarySelectionValue, const V &secondarySelectionValue, const W &tertiarySelectionValue, const std::string &dataColumnName, T &resultValue)
ConfigDataTree(const std::string &tableDefinitionFilename, const std::vector< int64_t > &subsystemConfigIdList, const ConfigDataSelector &selector, std::vector< std::string > dataColumnNameList)
std::string getTableName() const
std::map< std::string, ConfigValueMapPtr > ConfigDataContainer
double T
Definition: Xdiff_gwt.C:5
std::vector< std::string > getSelectionColumns() const
static const std::string KEY_SEPARATOR_STRING
#define W(x)
std::vector< std::string > ConfigValueList
boost::shared_ptr< ConfigValueList > ConfigValueListPtr
bool getEffectiveValueFor3Keys(const U &primarySelectionValue, const V &secondarySelectionValue, const W &tertiarySelectionValue, const std::string &dataColumnName, T &resultValue)
enum BeamMode string