GeneralDataReader.h
Go to the documentation of this file.
1 #ifndef _DBUTILS_DAQCONFIG_GENERALDATAREADER_H
2 #define _DBUTILS_DAQCONFIG_GENERALDATAREADER_H
3 
4 #include <NovaDatabase/Table.h>
5 #include <boost/lexical_cast.hpp>
6 #include <boost/thread/mutex.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <string>
9 #include <vector>
10 #include <map>
11 
12 namespace NOVADB = nova::database;
13 
14 namespace dbutils {
15 namespace daqconfig {
16 
17 template <class T, class U>
19  public:
20  T key;
21  U value;
22  bool hasValue;
23 };
24 
25 template <class T>
27 {
29  std::map<uint32_t, boost::shared_ptr<SimpleDataContainer<uint32_t, T> > > dataMap;
30  bool isComplete() {return (numberOfValidValues == dataMap.size());}
31 };
32 
33 /**
34  * Class to handle fetching of DAQ configuration data from the database.
35  *
36  * @author Kurt Biery
37  * @version $Revision: 1.10.26.1 $ $Date: 2019/09/27 00:07:03 $
38  */
39 
41 
42  public:
43 
44  // ********************
45  // *** Constructors ***
46  // ********************
47 
48  GeneralDataReader(const std::string& tableDefinitionFilename,
49  const uint64_t& subsystemConfigId,
50  const std::string* namesTableDefinitionFilename = 0);
51 
52  GeneralDataReader(const std::string& tableDefinitionFilename,
53  const uint64_t& subsystemConfigId,
54  const std::string& primarySelectionColumnName,
55  const std::string* namesTableDefinitionFilename = 0);
56 
57  GeneralDataReader(const std::string& tableDefinitionFilename,
58  const uint64_t& subsystemConfigId,
59  const std::string& primarySelectionColumnName,
60  const std::string& secondarySelectionColumnName,
61  const std::string* namesTableDefinitionFilename = 0);
62 
63  GeneralDataReader(const std::string& tableDefinitionFilename,
64  const uint64_t& subsystemConfigId,
65  const std::string& primarySelectionColumnName,
66  const std::string& secondarySelectionColumnName,
67  const std::string& tertiarySelectionColumnName,
68  const std::string* namesTableDefinitionFilename = 0);
69 
70 
71  GeneralDataReader(const std::string& tableDefinitionFilename,
72  const std::vector<int64_t>& subsystemConfigIdList,
73  const std::string* namesTableDefinitionFilename = 0);
74 
75  GeneralDataReader(const std::string& tableDefinitionFilename,
76  const std::vector<int64_t>& subsystemConfigIdList,
77  const std::string& primarySelectionColumnName,
78  const std::string* namesTableDefinitionFilename = 0);
79 
80  GeneralDataReader(const std::string& tableDefinitionFilename,
81  const std::vector<int64_t>& subsystemConfigIdList,
82  const std::string& primarySelectionColumnName,
83  const std::string& secondarySelectionColumnName,
84  const std::string* namesTableDefinitionFilename = 0);
85 
86  GeneralDataReader(const std::string& tableDefinitionFilename,
87  const std::vector<int64_t>& subsystemConfigIdList,
88  const std::string& primarySelectionColumnName,
89  const std::string& secondarySelectionColumnName,
90  const std::string& tertiarySelectionColumnName,
91  const std::string* namesTableDefinitionFilename = 0);
92 
93 
94  // ****************************
95  // *** Fetch a single value ***
96  // ****************************
97 
98  template<class T>
99  bool getValue(const std::string& columnName, T& resultValue);
100 
101  template<class T, class U>
102  bool getValueFor1Key(const std::string& columnName, T& resultValue,
103  const U& primarySelectionValue);
104 
105  template<class T, class U, class V>
106  bool getValueFor2Keys(const std::string& columnName, T& resultValue,
107  const U& primarySelectionValue,
108  const V& secondarySelectionValue);
109 
110  template<class T, class U, class V, class W>
111  bool getValueFor3Keys(const std::string& columnName, T& resultValue,
112  const U& primarySelectionValue,
113  const V& secondarySelectionValue,
114  const W& tertiarySelectionValue);
115 
116  // *****************************
117  // *** Fetch multiple values ***
118  // *****************************
119 
120  template<class T, class U>
121  bool getListOfValuesFor2Keys(const std::string& columnName,
122  std::vector<T>& resultValues,
123  const U& primarySelectionValue,
124  const uint32_t& secondarySelectionLowValue,
125  const uint32_t& secondarySelectionHighValue);
126 
127  template<class T, class U, class V>
128  bool getListOfValuesFor3Keys(const std::string& columnName,
129  std::vector<T>& resultValues,
130  const U& primarySelectionValue,
131  const V& secondarySelectionValue,
132  const uint32_t& tertiarySelectionLowValue,
133  const uint32_t& tertiarySelectionHighValue);
134 
135  template<class T, class U>
136  bool getDataSetFor2Keys(const std::string& columnName,
137  SimpleDataSet<T>& resultDataSet,
138  const U& primarySelectionValue,
139  const uint32_t& secondarySelectionLowValue,
140  const uint32_t& secondarySelectionHighValue);
141 
142  template<class T, class U, class V>
143  bool getDataSetFor3Keys(const std::string& columnName,
144  SimpleDataSet<T>& resultDataSet,
145  const U& primarySelectionValue,
146  const V& secondarySelectionValue,
147  const uint32_t& tertiarySelectionLowValue,
148  const uint32_t& tertiarySelectionHighValue);
149 
150  // ***********************************
151  // *** Fetch a hierarchy of values ***
152  // ***********************************
153 
154  template<class T, class U>
155  bool getSetOfValuesFor3Keys(const std::string& columnName,
156  std::vector<boost::shared_ptr<std::vector<T> > >&
157  resultValues,
158  const U& primarySelectionValue,
159  const uint32_t& secondarySelectionLowValue,
160  const uint32_t& secondarySelectionHighValue,
161  const uint32_t& tertiarySelectionLowValue,
162  const uint32_t& tertiarySelectionHighValue);
163 
164  template<class T, class U>
165  bool getDataSetMapFor3Keys(const std::string& columnName,
166  std::map<uint32_t, boost::shared_ptr<
167  SimpleDataSet<T> > >& twoLevelDataSet,
168  const U& primarySelectionValue,
169  const uint32_t& secondarySelectionLowValue,
170  const uint32_t& secondarySelectionHighValue,
171  const uint32_t& tertiarySelectionLowValue,
172  const uint32_t& tertiarySelectionHighValue);
173 
174  // *********************
175  // *** General tools ***
176  // *********************
177 
178  template<class TT>
179  static std::vector<std::pair<uint64_t, TT> >
180  remapValues(const std::vector<TT>& values,
181  const uint32_t& indexOffset);
182 
183  private:
184 
185  void _init(const std::string& tableDefinitionFilename,
186  const uint64_t& subsystemConfigId,
187  const std::string* namesTableDefinitionFilename);
188 
189  template<class T>
190  bool _getValueForAllConfigs(const std::string& columnName,
191  const std::vector<std::string>& selectionValues,
192  T& resultValue);
193 
194  template<class T>
195  bool _getValueForSingleConfig(const std::string& columnName,
196  const uint64_t& configId,
197  std::vector<std::string> selectionValues,
198  T& resultValue);
199 
200  template<class T>
201  bool _getDefaultValue(const std::string& columnName,
202  const uint64_t& configId,
203  const std::vector<std::string>& selectionValues,
204  T& resultValue);
205 
206  template<class T>
207  bool _getValueFromDB(const std::string& columnName,
208  const uint64_t& configId,
209  const std::vector<std::string>&
210  selectionValues,
211  T& resultValue);
212 
213  template<class T>
214  bool _getListOfValuesForAllConfigs(const std::string& columnName,
215  const std::vector<std::string>& selectionValues,
216  const uint32_t& fineGrainedSelectionLowValue,
217  const uint32_t& fineGrainedSelectionHighValue,
218  std::vector<T>& resultValues);
219 
220  template<class T> bool
221  _getListOfValuesForSingleConfig(const std::string& columnName,
222  const uint64_t& configId,
223  std::vector<std::string> selectionValues,
224  const uint32_t& fineGrainedSelectionLowValue,
225  const uint32_t& fineGrainedSelectionHighValue,
226  SimpleDataSet<T>& dataSet);
227 
228  template<class T> bool
229  _getListOfValuesFromDB(const std::string& columnName,
230  const uint64_t& configId,
231  std::vector<std::string> selectionValues,
232  const uint32_t& fineGrainedSelectionLowValue,
233  const uint32_t& fineGrainedSelectionHighValue,
234  SimpleDataSet<T>& dataSet);
235 
236  template<class T>
237  bool _getDataSetForAllConfigs(const std::string& columnName,
238  const std::vector<std::string>& selectionValues,
239  const uint32_t& fineGrainedSelectionLowValue,
240  const uint32_t& fineGrainedSelectionHighValue,
241  SimpleDataSet<T>& resultDataSet);
242 
243  template<class T>
244  bool _getSetOfValuesForAllConfigs(const std::string& columnName,
245  const std::vector<std::string>& selectionValues,
246  const uint32_t& firstLevelSelectionLowValue,
247  const uint32_t& firstLevelSelectionHighValue,
248  const uint32_t& secondLevelSelectionLowValue,
249  const uint32_t& secondLevelSelectionHighValue,
250  std::vector<boost::shared_ptr<std::vector<T> > >&
251  resultValues);
252 
253  template<class T> bool
254  _getSetOfValuesForSingleConfig(const std::string& columnName,
255  const uint64_t& configId,
256  std::vector<std::string> selectionValues,
257  const uint32_t& firstLevelSelectionLowValue,
258  const uint32_t& firstLevelSelectionHighValue,
259  const uint32_t& secondLevelSelectionLowValue,
260  const uint32_t& secondLevelSelectionHighValue,
261  std::map<uint32_t, boost::shared_ptr<
262  SimpleDataSet<T> > >& twoLevelDataSet);
263 
264  template<class T> bool
265  _getSetOfValuesFromDB(const std::string& columnName,
266  const uint64_t& configId,
267  std::vector<std::string> selectionValues,
268  const uint32_t& firstLevelSelectionLowValue,
269  const uint32_t& firstLevelSelectionHighValue,
270  const uint32_t& secondLevelSelectionLowValue,
271  const uint32_t& secondLevelSelectionHighValue,
272  std::map<uint32_t, boost::shared_ptr<
273  SimpleDataSet<T> > >& twoLevelDataSet);
274 
275  template<class T>
276  bool _getDataSetMapForAllConfigs(const std::string& columnName,
277  const std::vector<std::string>& selectionValues,
278  const uint32_t& firstLevelSelectionLowValue,
279  const uint32_t& firstLevelSelectionHighValue,
280  const uint32_t& secondLevelSelectionLowValue,
281  const uint32_t& secondLevelSelectionHighValue,
282  std::map<uint32_t, boost::shared_ptr<
283  SimpleDataSet<T> > >& twoLevelDataSet);
284 
285  bool _setValidityRanges(const std::vector<std::string>& selectionValues,
286  bool fillInDefaults = true);
287 
288  boost::shared_ptr<NOVADB::Table> _dbTablePtr;
290 
291  std::vector<std::string> _selectionColumns;
292  std::vector<int64_t> _namedConfigLineage;
293 
295 
297 };
298 
299 /**
300  * Fetches the requested value...
301  */
302 template<class T>
304 getValue(const std::string& columnName, T& resultValue)
305 {
306  boost::mutex::scoped_lock sl(_dataMutex);
307  std::vector<std::string> selectionValues;
308  return _getValueForAllConfigs(columnName, selectionValues, resultValue);
309 }
310 
311 /**
312  * Fetches the requested value...
313  */
314 template<class T, class U>
316 getValueFor1Key(const std::string& columnName, T& resultValue,
317  const U& primarySelectionValue)
318 {
319  boost::mutex::scoped_lock sl(_dataMutex);
320  std::vector<std::string> selectionValues;
321  selectionValues.push_back(boost::lexical_cast<std::string>
322  (primarySelectionValue));
323  return _getValueForAllConfigs(columnName, selectionValues, resultValue);
324 }
325 
326 /**
327  * Fetches the requested value...
328  */
329 template<class T, class U, class V>
331 getValueFor2Keys(const std::string& columnName, T& resultValue,
332  const U& primarySelectionValue,
333  const V& secondarySelectionValue)
334 {
335  boost::mutex::scoped_lock sl(_dataMutex);
336  std::vector<std::string> selectionValues;
337  selectionValues.push_back(boost::lexical_cast<std::string>
338  (primarySelectionValue));
339  selectionValues.push_back(boost::lexical_cast<std::string>
340  (secondarySelectionValue));
341  return _getValueForAllConfigs(columnName, selectionValues, resultValue);
342 }
343 
344 /**
345  * Fetches the requested value...
346  */
347 template<class T, class U, class V, class W>
349 getValueFor3Keys(const std::string& columnName, T& resultValue,
350  const U& primarySelectionValue,
351  const V& secondarySelectionValue,
352  const W& tertiarySelectionValue)
353 {
354  boost::mutex::scoped_lock sl(_dataMutex);
355  std::vector<std::string> selectionValues;
356  selectionValues.push_back(boost::lexical_cast<std::string>
357  (primarySelectionValue));
358  selectionValues.push_back(boost::lexical_cast<std::string>
359  (secondarySelectionValue));
360  selectionValues.push_back(boost::lexical_cast<std::string>
361  (tertiarySelectionValue));
362  return _getValueForAllConfigs(columnName, selectionValues, resultValue);
363 }
364 
365 /**
366  * Fetches the requested values...
367  */
368 template<class T, class U>
371  std::vector<T>& resultValues,
372  const U& primarySelectionValue,
373  const uint32_t& secondarySelectionLowValue,
374  const uint32_t& secondarySelectionHighValue)
375 {
376  boost::mutex::scoped_lock sl(_dataMutex);
377  std::vector<std::string> selectionValues;
378  selectionValues.push_back(boost::lexical_cast<std::string>
379  (primarySelectionValue));
380  return _getListOfValuesForAllConfigs(columnName, selectionValues,
381  secondarySelectionLowValue,
382  secondarySelectionHighValue,
383  resultValues);
384 }
385 
386 /**
387  * Fetches the requested values...
388  */
389 template<class T, class U, class V>
392  std::vector<T>& resultValues,
393  const U& primarySelectionValue,
394  const V& secondarySelectionValue,
395  const uint32_t& tertiarySelectionLowValue,
396  const uint32_t& tertiarySelectionHighValue)
397 {
398  boost::mutex::scoped_lock sl(_dataMutex);
399  std::vector<std::string> selectionValues;
400  selectionValues.push_back(boost::lexical_cast<std::string>
401  (primarySelectionValue));
402  selectionValues.push_back(boost::lexical_cast<std::string>
403  (secondarySelectionValue));
404  return _getListOfValuesForAllConfigs(columnName, selectionValues,
405  tertiarySelectionLowValue,
406  tertiarySelectionHighValue,
407  resultValues);
408 }
409 
410 /**
411  * Fetches the requested values...
412  */
413 template<class T, class U>
415 getDataSetFor2Keys(const std::string& columnName,
416  SimpleDataSet<T>& resultDataSet,
417  const U& primarySelectionValue,
418  const uint32_t& secondarySelectionLowValue,
419  const uint32_t& secondarySelectionHighValue)
420 {
421  boost::mutex::scoped_lock sl(_dataMutex);
422  std::vector<std::string> selectionValues;
423  selectionValues.push_back(boost::lexical_cast<std::string>
424  (primarySelectionValue));
425  return _getDataSetForAllConfigs(columnName, selectionValues,
426  secondarySelectionLowValue,
427  secondarySelectionHighValue,
428  resultDataSet);
429 }
430 
431 /**
432  * Fetches the requested values...
433  */
434 template<class T, class U, class V>
436 getDataSetFor3Keys(const std::string& columnName,
437  SimpleDataSet<T>& resultDataSet,
438  const U& primarySelectionValue,
439  const V& secondarySelectionValue,
440  const uint32_t& tertiarySelectionLowValue,
441  const uint32_t& tertiarySelectionHighValue)
442 {
443  boost::mutex::scoped_lock sl(_dataMutex);
444  std::vector<std::string> selectionValues;
445  selectionValues.push_back(boost::lexical_cast<std::string>
446  (primarySelectionValue));
447  selectionValues.push_back(boost::lexical_cast<std::string>
448  (secondarySelectionValue));
449  return _getDataSetForAllConfigs(columnName, selectionValues,
450  tertiarySelectionLowValue,
451  tertiarySelectionHighValue,
452  resultDataSet);
453 }
454 
455 /**
456  * Fetches the requested values...
457  */
458 template<class T, class U>
461  std::vector<boost::shared_ptr<std::vector<T> > >&
462  resultValues,
463  const U& primarySelectionValue,
464  const uint32_t& secondarySelectionLowValue,
465  const uint32_t& secondarySelectionHighValue,
466  const uint32_t& tertiarySelectionLowValue,
467  const uint32_t& tertiarySelectionHighValue)
468 {
469  boost::mutex::scoped_lock sl(_dataMutex);
470  std::vector<std::string> selectionValues;
471  selectionValues.push_back(boost::lexical_cast<std::string>
472  (primarySelectionValue));
473  return _getSetOfValuesForAllConfigs(columnName, selectionValues,
474  secondarySelectionLowValue,
475  secondarySelectionHighValue,
476  tertiarySelectionLowValue,
477  tertiarySelectionHighValue,
478  resultValues);
479 }
480 
481 
482 template<class TT>
483 std::vector<std::pair<uint64_t, TT> > GeneralDataReader::
484 remapValues(const std::vector<TT>& values,
485  const uint32_t& indexOffset)
486 {
487  std::vector<std::pair<uint64_t, TT> > resultList;
488  for (uint32_t idx = 0; idx < values.size(); ++idx) {
489  uint32_t bitIndex = idx + indexOffset;
490  TT value = values[idx];
491  bool found = false;
492 
493  for (uint32_t jdx = 0; jdx < resultList.size(); ++jdx) {
494  if (resultList[jdx].second == value) {
495  resultList[jdx].first |= ((uint64_t) 1) << bitIndex;
496  found = true;
497  break;
498  }
499  }
500 
501  if (! found) {
502  uint64_t workingBitmap = ((uint64_t) 1) << bitIndex;
503  resultList.push_back(std::make_pair(workingBitmap, value));
504  }
505  }
506  return resultList;
507 }
508 
509 /**
510  * Fetches the requested value...
511  */
512 template<class T>
515  const std::vector<std::string>& selectionValues,
516  T& resultValue)
517 {
518  for (uint32_t idx = 0; idx < _namedConfigLineage.size(); ++idx) {
519  if (_getValueForSingleConfig(columnName,
520  _namedConfigLineage[idx],
521  selectionValues,
522  resultValue)) {
523  return true;
524  }
525  }
526  return false;
527 }
528 
529 /**
530  * Fetches the requested value...
531  */
532 template<class T>
535  const uint64_t& configId,
536  std::vector<std::string> selectionValues,
537  T& resultValue)
538 {
539  if (_getValueFromDB(columnName, configId,
540  selectionValues, resultValue)) {
541  return true;
542  }
543  while (selectionValues.size() > 0) {
544  selectionValues.pop_back();
545  //if (selectionValues.size() == 0 && _globalDefault.size() > 0) {
546  // resultValue = boost::lexical_cast<T>(_globalDefault);
547  // return true;
548  //}
549  //else {
550  bool found = _getDefaultValue(columnName, configId,
551  selectionValues,
552  resultValue);
553  //if (_globalDefault.size() == 0 && selectionValues.size() == 0) {
554  // _globalDefault = boost::lexical_cast<std::string>(resultValue);
555  //}
556  if (found) {return true;}
557  //}
558  }
559  return false;
560 }
561 
562 /**
563  * Fetches the requested value...
564  */
565 template<class T>
567 _getDefaultValue(const std::string& columnName,
568  const uint64_t& configId,
569  const std::vector<std::string>& selectionValues,
570  T& resultValue)
571 {
572  return _getValueFromDB(columnName, configId,
573  selectionValues, resultValue);
574 }
575 
576 /**
577  * Fetches the requested value...
578  */
579 template<class T>
581 _getValueFromDB(const std::string& columnName,
582  const uint64_t& configId,
583  const std::vector<std::string>& selectionValues,
584  T& resultValue)
585 {
586  if (! _hasValidConnection) {return false;}
587  _dbTablePtr->Clear();
588  if (! _setValidityRanges(selectionValues)) {
589  return false;
590  }
591  _dbTablePtr->SetValidityRange("subsysCfgId", configId);
592  if (_dbTablePtr->LoadFromDB()) {
593  if (_dbTablePtr->NRow() == 1) {
594  NOVADB::Row* rowPtr = _dbTablePtr->GetRow(0);
595  NOVADB::Column* colPtr = rowPtr->Col(columnName);
596  if (colPtr == 0) {return false;}
597  return colPtr->Get(resultValue);
598  }
599  }
600  return false;
601 }
602 
603 /**
604  * Fetches the requested values...
605  */
606 template<class T>
609  const std::vector<std::string>& selectionValues,
610  const uint32_t& fineGrainedSelectionLowValue,
611  const uint32_t& fineGrainedSelectionHighValue,
612  std::vector<T>& resultValues)
613 {
614  // require there to be an exact match between the number of
615  // selection columns and the number of selection values
616  // (including the fine-grained one)
617  if (selectionValues.size() != (_selectionColumns.size() - 1)) {
618  return false;
619  }
620 
621  // verify that the requested number of values is reasonable
622  if (fineGrainedSelectionLowValue > fineGrainedSelectionHighValue) {
623  return false;
624  }
625 
626  // if the requested number of values is one, simply call the
627  // single-value method?
628 
629  // initialize the working list of values
630  SimpleDataSet<T> dataSet;
631  dataSet.numberOfValidValues = 0;
632  for (uint32_t rdx = fineGrainedSelectionLowValue;
633  rdx <= fineGrainedSelectionHighValue; ++rdx) {
634  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
635  dataPtr.reset(new SimpleDataContainer<uint32_t, T>());
636  dataPtr->key = rdx;
637  dataPtr->hasValue = false;
638  dataSet.dataMap[rdx] = dataPtr;
639  }
640 
641  // loop over named configurations until we've found all of
642  // the requested values
643  for (uint32_t idx = 0; idx < _namedConfigLineage.size(); ++idx) {
644  if (! _getListOfValuesForSingleConfig(columnName,
645  _namedConfigLineage[idx],
646  selectionValues,
647  fineGrainedSelectionLowValue,
648  fineGrainedSelectionHighValue,
649  dataSet)) {
650  return false;
651  }
652  if (dataSet.isComplete()) {
653  for (uint32_t rdx = fineGrainedSelectionLowValue;
654  rdx <= fineGrainedSelectionHighValue; ++rdx) {
655  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
656  dataPtr = dataSet.dataMap[rdx];
657  resultValues.push_back(dataPtr->value);
658  }
659  return true;
660  }
661  }
662  return false;
663 }
664 
665 /**
666  * Fetches the requested values...
667  */
668 template<class T>
671  const uint64_t& configId,
672  std::vector<std::string> selectionValues,
673  const uint32_t& fineGrainedSelectionLowValue,
674  const uint32_t& fineGrainedSelectionHighValue,
675  SimpleDataSet<T>& dataSet)
676 {
677  if (! _getListOfValuesFromDB(columnName, configId,
678  selectionValues,
679  fineGrainedSelectionLowValue,
680  fineGrainedSelectionHighValue,
681  dataSet)) {
682  return false;
683  }
684  if (dataSet.isComplete()) {return true;}
685 
686  selectionValues.push_back("bogusEntry");
687  while (selectionValues.size() > 0) {
688  selectionValues.pop_back();
689  T resultValue;
690  bool found = _getDefaultValue(columnName, configId,
691  selectionValues,
692  resultValue);
693  if (found) {
694  for (uint32_t rdx = fineGrainedSelectionLowValue;
695  rdx <= fineGrainedSelectionHighValue; ++rdx) {
696  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
697  dataPtr = dataSet.dataMap[rdx];
698  if (! dataPtr->hasValue) {
699  dataPtr->value = resultValue;
700  dataPtr->hasValue = true;
701  ++dataSet.numberOfValidValues;
702  }
703  }
704  break;
705  }
706  }
707  return true;
708 }
709 
710 /**
711  * Fetches the requested values...
712  */
713 template<class T>
716  const uint64_t& configId,
717  std::vector<std::string> selectionValues,
718  const uint32_t& fineGrainedSelectionLowValue,
719  const uint32_t& fineGrainedSelectionHighValue,
720  SimpleDataSet<T>& dataSet)
721 {
722  if (! _hasValidConnection) {return false;}
723  _dbTablePtr->Clear();
724  if (! _setValidityRanges(selectionValues, false)) {
725  return false;
726  }
727  std::string fineGrainedSelectionColumn =
728  _selectionColumns[_selectionColumns.size() - 1];
729  _dbTablePtr->SetValidityRange(fineGrainedSelectionColumn,
730  fineGrainedSelectionLowValue,
731  fineGrainedSelectionHighValue);
732  _dbTablePtr->SetValidityRange("subsysCfgId", configId);
733  //_dbTablePtr->SetVerbosity(1);
734  if (_dbTablePtr->LoadFromDB()) {
735  for (int32_t idx = 0; idx < _dbTablePtr->NRow(); ++idx) {
736  NOVADB::Row* rowPtr = _dbTablePtr->GetRow(idx);
737  T resultValue;
738  NOVADB::Column* colPtr = rowPtr->Col(columnName);
739  if (! colPtr->Get(resultValue)) {
740  return false;
741  }
742 
743  int32_t selectionValue;
744  colPtr = rowPtr->Col(fineGrainedSelectionColumn);
745  if (! colPtr->Get(selectionValue)) {
746  return false;
747  }
748 
749  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
750  dataPtr = dataSet.dataMap[selectionValue];
751  if (! dataPtr->hasValue) {
752  dataPtr->value = resultValue;
753  dataPtr->hasValue = true;
754  ++dataSet.numberOfValidValues;
755  }
756  }
757  return true;
758  }
759  return false;
760 }
761 
762 /**
763  * Fetches the requested values...
764  */
765 template<class T>
768  const std::vector<std::string>& selectionValues,
769  const uint32_t& fineGrainedSelectionLowValue,
770  const uint32_t& fineGrainedSelectionHighValue,
771  SimpleDataSet<T>& resultDataSet)
772 {
773  // require there to be an exact match between the number of
774  // selection columns and the number of selection values
775  // (including the fine-grained one)
776  if (selectionValues.size() != (_selectionColumns.size() - 1)) {
777  return false;
778  }
779 
780  // verify that the requested number of values is reasonable
781  if (fineGrainedSelectionLowValue > fineGrainedSelectionHighValue) {
782  return false;
783  }
784 
785  // if the requested number of values is one, simply call the
786  // single-value method?
787 
788  // initialize the resulting data set
789  resultDataSet.dataMap.clear();
790  resultDataSet.numberOfValidValues = 0;
791  for (uint32_t rdx = fineGrainedSelectionLowValue;
792  rdx <= fineGrainedSelectionHighValue; ++rdx) {
793  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
794  dataPtr.reset(new SimpleDataContainer<uint32_t, T>());
795  dataPtr->key = rdx;
796  dataPtr->hasValue = false;
797  resultDataSet.dataMap[rdx] = dataPtr;
798  }
799 
800  // loop over named configurations until we've found all of
801  // the requested values
802  for (uint32_t idx = 0; idx < _namedConfigLineage.size(); ++idx) {
803  if (! _getListOfValuesFromDB(columnName,
804  _namedConfigLineage[idx],
805  selectionValues,
806  fineGrainedSelectionLowValue,
807  fineGrainedSelectionHighValue,
808  resultDataSet)) {
809  return false;
810  }
811  }
812  return true;
813 }
814 
815 /**
816  * Fetches the requested values...
817  */
818 template<class T>
821  const std::vector<std::string>& selectionValues,
822  const uint32_t& firstLevelSelectionLowValue,
823  const uint32_t& firstLevelSelectionHighValue,
824  const uint32_t& secondLevelSelectionLowValue,
825  const uint32_t& secondLevelSelectionHighValue,
826  std::vector<boost::shared_ptr<std::vector<T> > >& resultValues)
827 {
828  // require there to be an exact match between the number of
829  // selection columns and the number of selection values
830  // (including the multiple fine-grained ones)
831  if (selectionValues.size() != (_selectionColumns.size() - 2)) {
832  return false;
833  }
834 
835  // verify that the requested number of values is reasonable
836  if (firstLevelSelectionLowValue > firstLevelSelectionHighValue) {
837  return false;
838  }
839  if (secondLevelSelectionLowValue > secondLevelSelectionHighValue) {
840  return false;
841  }
842 
843  // if the requested number of values is one, simply call the
844  // single-value method?
845 
846  // initialize the working list of values
847  std::map<uint32_t, boost::shared_ptr<SimpleDataSet<T> > > twoLevelDataSet;
848  for (uint32_t idx = firstLevelSelectionLowValue;
849  idx <= firstLevelSelectionHighValue; ++idx) {
850  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
851  dataSetPtr.reset(new SimpleDataSet<T>());
852  dataSetPtr->numberOfValidValues = 0;
853  twoLevelDataSet[idx] = dataSetPtr;
854  for (uint32_t jdx = secondLevelSelectionLowValue;
855  jdx <= secondLevelSelectionHighValue; ++jdx) {
856  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
857  dataPtr.reset(new SimpleDataContainer<uint32_t, T>());
858  dataPtr->key = jdx;
859  dataPtr->hasValue = false;
860  dataSetPtr->dataMap[jdx] = dataPtr;
861  }
862  }
863 
864  // loop over named configurations until we've found all of
865  // the requested values
866  for (uint32_t idx = 0; idx < _namedConfigLineage.size(); ++idx) {
867  if (! _getSetOfValuesForSingleConfig(columnName,
868  _namedConfigLineage[idx],
869  selectionValues,
870  firstLevelSelectionLowValue,
871  firstLevelSelectionHighValue,
872  secondLevelSelectionLowValue,
873  secondLevelSelectionHighValue,
874  twoLevelDataSet)) {
875  return false;
876  }
877 
878  bool totallyComplete = true;
879  for (uint32_t idx = firstLevelSelectionLowValue;
880  idx <= firstLevelSelectionHighValue; ++idx) {
881  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
882  dataSetPtr = twoLevelDataSet[idx];
883  if (! dataSetPtr->isComplete()) {
884  totallyComplete = false;
885  break;
886  }
887  }
888  if (totallyComplete) {
889  for (uint32_t idx = firstLevelSelectionLowValue;
890  idx <= firstLevelSelectionHighValue; ++idx) {
891  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
892  dataSetPtr = twoLevelDataSet[idx];
893  boost::shared_ptr<std::vector<T> > secondLevelValues;
894  secondLevelValues.reset(new std::vector<T>());
895 
896  for (uint32_t jdx = secondLevelSelectionLowValue;
897  jdx <= secondLevelSelectionHighValue; ++jdx) {
898  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
899  dataPtr = dataSetPtr->dataMap[jdx];
900  secondLevelValues->push_back(dataPtr->value);
901  }
902 
903  resultValues.push_back(secondLevelValues);
904  }
905  return true;
906  }
907  }
908  return false;
909 }
910 
911 /**
912  * Fetches the requested values...
913  */
914 template<class T>
917  const uint64_t& configId,
918  std::vector<std::string> selectionValues,
919  const uint32_t& firstLevelSelectionLowValue,
920  const uint32_t& firstLevelSelectionHighValue,
921  const uint32_t& secondLevelSelectionLowValue,
922  const uint32_t& secondLevelSelectionHighValue,
923  std::map<uint32_t, boost::shared_ptr<
924  SimpleDataSet<T> > >& twoLevelDataSet)
925 {
926  if (! _getSetOfValuesFromDB(columnName, configId,
927  selectionValues,
928  firstLevelSelectionLowValue,
929  firstLevelSelectionHighValue,
930  secondLevelSelectionLowValue,
931  secondLevelSelectionHighValue,
932  twoLevelDataSet)) {
933  return false;
934  }
935  bool totallyComplete = true;
936  for (uint32_t idx = firstLevelSelectionLowValue;
937  idx <= firstLevelSelectionHighValue; ++idx) {
938  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
939  dataSetPtr = twoLevelDataSet[idx];
940  if (! dataSetPtr->isComplete()) {
941  totallyComplete = false;
942  break;
943  }
944  }
945  if (totallyComplete) {return true;}
946 
947  for (uint32_t idx = firstLevelSelectionLowValue;
948  idx <= firstLevelSelectionHighValue; ++idx) {
949  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
950  dataSetPtr = twoLevelDataSet[idx];
951 
952  selectionValues.push_back(boost::lexical_cast<std::string>(idx));
953  T resultValue;
954  bool found = _getDefaultValue(columnName, configId,
955  selectionValues,
956  resultValue);
957  if (found) {
958  for (uint32_t jdx = secondLevelSelectionLowValue;
959  jdx <= secondLevelSelectionHighValue; ++jdx) {
960  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
961  dataPtr = dataSetPtr->dataMap[jdx];
962  if (! dataPtr->hasValue) {
963  dataPtr->value = resultValue;
964  dataPtr->hasValue = true;
965  ++dataSetPtr->numberOfValidValues;
966  }
967  }
968  }
969  selectionValues.pop_back();
970  }
971 
972  totallyComplete = true;
973  for (uint32_t idx = firstLevelSelectionLowValue;
974  idx <= firstLevelSelectionHighValue; ++idx) {
975  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
976  dataSetPtr = twoLevelDataSet[idx];
977  if (! dataSetPtr->isComplete()) {
978  totallyComplete = false;
979  break;
980  }
981  }
982  if (totallyComplete) {return true;}
983 
984  selectionValues.push_back("bogusEntry");
985  while (selectionValues.size() > 0) {
986  selectionValues.pop_back();
987  T resultValue;
988  bool found = _getDefaultValue(columnName, configId,
989  selectionValues,
990  resultValue);
991  if (found) {
992  for (uint32_t idx = firstLevelSelectionLowValue;
993  idx <= firstLevelSelectionHighValue; ++idx) {
994  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
995  dataSetPtr = twoLevelDataSet[idx];
996 
997  for (uint32_t jdx = secondLevelSelectionLowValue;
998  jdx <= secondLevelSelectionHighValue; ++jdx) {
999  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
1000  dataPtr = dataSetPtr->dataMap[jdx];
1001  if (! dataPtr->hasValue) {
1002  dataPtr->value = resultValue;
1003  dataPtr->hasValue = true;
1004  ++dataSetPtr->numberOfValidValues;
1005  }
1006  }
1007  }
1008  break;
1009  }
1010  }
1011  return true;
1012 }
1013 
1014 /**
1015  * Fetches the requested values...
1016  */
1017 template<class T>
1020  const uint64_t& configId,
1021  std::vector<std::string> selectionValues,
1022  const uint32_t& firstLevelSelectionLowValue,
1023  const uint32_t& firstLevelSelectionHighValue,
1024  const uint32_t& secondLevelSelectionLowValue,
1025  const uint32_t& secondLevelSelectionHighValue,
1026  std::map<uint32_t, boost::shared_ptr<
1027  SimpleDataSet<T> > >& twoLevelDataSet)
1028 {
1029  if (! _hasValidConnection) {return false;}
1030  _dbTablePtr->Clear();
1031  if (! _setValidityRanges(selectionValues, false)) {
1032  return false;
1033  }
1034  std::string firstLevelSelectionColumn =
1035  _selectionColumns[_selectionColumns.size() - 2];
1036  _dbTablePtr->SetValidityRange(firstLevelSelectionColumn,
1037  firstLevelSelectionLowValue,
1038  firstLevelSelectionHighValue);
1039  std::string secondLevelSelectionColumn =
1040  _selectionColumns[_selectionColumns.size() - 1];
1041  _dbTablePtr->SetValidityRange(secondLevelSelectionColumn,
1042  secondLevelSelectionLowValue,
1043  secondLevelSelectionHighValue);
1044  _dbTablePtr->SetValidityRange("subsysCfgId", configId);
1045  //_dbTablePtr->SetVerbosity(1);
1046  if (_dbTablePtr->LoadFromDB()) {
1047  for (int32_t idx = 0; idx < _dbTablePtr->NRow(); ++idx) {
1048  NOVADB::Row* rowPtr = _dbTablePtr->GetRow(idx);
1049 
1050  T resultValue;
1051  NOVADB::Column* colPtr = rowPtr->Col(columnName);
1052  if (! colPtr->Get(resultValue)) {
1053  return false;
1054  }
1055 
1056  int32_t firstSelectionValue;
1057  colPtr = rowPtr->Col(firstLevelSelectionColumn);
1058  if (! colPtr->Get(firstSelectionValue)) {
1059  return false;
1060  }
1061 
1062  int32_t secondSelectionValue;
1063  colPtr = rowPtr->Col(secondLevelSelectionColumn);
1064  if (! colPtr->Get(secondSelectionValue)) {
1065  return false;
1066  }
1067 
1068  boost::shared_ptr<SimpleDataSet<T> > dataSetPtr;
1069  dataSetPtr = twoLevelDataSet[firstSelectionValue];
1070 
1071  boost::shared_ptr<SimpleDataContainer<uint32_t, T> > dataPtr;
1072  dataPtr = dataSetPtr->dataMap[secondSelectionValue];
1073  if (! dataPtr->hasValue) {
1074  dataPtr->value = resultValue;
1075  dataPtr->hasValue = true;
1076  ++dataSetPtr->numberOfValidValues;
1077  }
1078  }
1079  return true;
1080  }
1081  return false;
1082 }
1083 
1084 } // end of namespace daqconfig
1085 } // end of namespace dbutils
1086 
1087 #endif
bool _getListOfValuesForSingleConfig(const std::string &columnName, const uint64_t &configId, std::vector< std::string > selectionValues, const uint32_t &fineGrainedSelectionLowValue, const uint32_t &fineGrainedSelectionHighValue, SimpleDataSet< T > &dataSet)
bool _getValueForSingleConfig(const std::string &columnName, const uint64_t &configId, std::vector< std::string > selectionValues, T &resultValue)
bool getSetOfValuesFor3Keys(const std::string &columnName, std::vector< boost::shared_ptr< std::vector< T > > > &resultValues, const U &primarySelectionValue, const uint32_t &secondarySelectionLowValue, const uint32_t &secondarySelectionHighValue, const uint32_t &tertiarySelectionLowValue, const uint32_t &tertiarySelectionHighValue)
bool _getSetOfValuesForSingleConfig(const std::string &columnName, const uint64_t &configId, std::vector< std::string > selectionValues, const uint32_t &firstLevelSelectionLowValue, const uint32_t &firstLevelSelectionHighValue, const uint32_t &secondLevelSelectionLowValue, const uint32_t &secondLevelSelectionHighValue, std::map< uint32_t, boost::shared_ptr< SimpleDataSet< T > > > &twoLevelDataSet)
bool _getValueForAllConfigs(const std::string &columnName, const std::vector< std::string > &selectionValues, T &resultValue)
bool getValueFor1Key(const std::string &columnName, T &resultValue, const U &primarySelectionValue)
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
bool getListOfValuesFor3Keys(const std::string &columnName, std::vector< T > &resultValues, const U &primarySelectionValue, const V &secondarySelectionValue, const uint32_t &tertiarySelectionLowValue, const uint32_t &tertiarySelectionHighValue)
bool _getSetOfValuesForAllConfigs(const std::string &columnName, const std::vector< std::string > &selectionValues, const uint32_t &firstLevelSelectionLowValue, const uint32_t &firstLevelSelectionHighValue, const uint32_t &secondLevelSelectionLowValue, const uint32_t &secondLevelSelectionHighValue, std::vector< boost::shared_ptr< std::vector< T > > > &resultValues)
bool getDataSetFor3Keys(const std::string &columnName, SimpleDataSet< T > &resultDataSet, const U &primarySelectionValue, const V &secondarySelectionValue, const uint32_t &tertiarySelectionLowValue, const uint32_t &tertiarySelectionHighValue)
bool getDataSetFor2Keys(const std::string &columnName, SimpleDataSet< T > &resultDataSet, const U &primarySelectionValue, const uint32_t &secondarySelectionLowValue, const uint32_t &secondarySelectionHighValue)
boost::shared_ptr< NOVADB::Table > _dbTablePtr
bool getValueFor3Keys(const std::string &columnName, T &resultValue, const U &primarySelectionValue, const V &secondarySelectionValue, const W &tertiarySelectionValue)
std::shared_ptr< T > shared_ptr
Definition: memory.h:15
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::map< uint32_t, boost::shared_ptr< SimpleDataContainer< uint32_t, T > > > dataMap
static std::vector< std::pair< uint64_t, TT > > remapValues(const std::vector< TT > &values, const uint32_t &indexOffset)
bool _getSetOfValuesFromDB(const std::string &columnName, const uint64_t &configId, std::vector< std::string > selectionValues, const uint32_t &firstLevelSelectionLowValue, const uint32_t &firstLevelSelectionHighValue, const uint32_t &secondLevelSelectionLowValue, const uint32_t &secondLevelSelectionHighValue, std::map< uint32_t, boost::shared_ptr< SimpleDataSet< T > > > &twoLevelDataSet)
bool Get(T &val) const
Definition: Column.h:53
bool getListOfValuesFor2Keys(const std::string &columnName, std::vector< T > &resultValues, const U &primarySelectionValue, const uint32_t &secondarySelectionLowValue, const uint32_t &secondarySelectionHighValue)
bool _getListOfValuesFromDB(const std::string &columnName, const uint64_t &configId, std::vector< std::string > selectionValues, const uint32_t &fineGrainedSelectionLowValue, const uint32_t &fineGrainedSelectionHighValue, SimpleDataSet< T > &dataSet)
std::vector< int64_t > _namedConfigLineage
std::vector< std::string > _selectionColumns
bool getValueFor2Keys(const std::string &columnName, T &resultValue, const U &primarySelectionValue, const V &secondarySelectionValue)
bool getValue(const std::string &columnName, T &resultValue)
bool _getListOfValuesForAllConfigs(const std::string &columnName, const std::vector< std::string > &selectionValues, const uint32_t &fineGrainedSelectionLowValue, const uint32_t &fineGrainedSelectionHighValue, std::vector< T > &resultValues)
bool _getDataSetForAllConfigs(const std::string &columnName, const std::vector< std::string > &selectionValues, const uint32_t &fineGrainedSelectionLowValue, const uint32_t &fineGrainedSelectionHighValue, SimpleDataSet< T > &resultDataSet)
double T
Definition: Xdiff_gwt.C:5
#define W(x)
bool _getDefaultValue(const std::string &columnName, const uint64_t &configId, const std::vector< std::string > &selectionValues, T &resultValue)
bool _getValueFromDB(const std::string &columnName, const uint64_t &configId, const std::vector< std::string > &selectionValues, T &resultValue)
Column & Col(int i)
Definition: Row.h:51
enum BeamMode string