Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDBToolkit.hh
Go to the documentation of this file.
1 #ifndef __JDB_JDBTOOLKIT__
2 #define __JDB_JDBTOOLKIT__
3 
4 #include <string>
5 #include <map>
6 
7 #include "JLang/JException.hh"
8 
9 #include "JDB/JDB.hh"
10 #include "JDB/JDetectors.hh"
11 #include "JDB/JCLBID.hh"
12 #include "JDB/JUPI.hh"
13 #include "JDB/JUPI_t.hh"
14 #include "JDB/JSelector.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JDATABASE {};
23 namespace JPP { using namespace JDATABASE; }
24 
25 /**
26  * Auxiliary classes and methods for database I/O.
27  */
28 namespace JDATABASE {
29 
32 
33 
34  /**
35  * Auxiliary class for mapping serial number and object identifier of detectors.
36  */
38  /**
39  * Initialise.
40  */
41  void initialise()
42  {
43  ResultSet& rs = JDB::get()->StreamDS(getTable<JDetectors>(), JSelector());
44 
45  for (JDetectors parameters; rs >> parameters; ) {
46  det2id[parameters.OID] = parameters.SERIALNUMBER;
47  id2det[parameters.SERIALNUMBER] = parameters.OID;
48  }
49 
50  rs.Close();
51  }
52 
53 
54  /**
55  * Get detector serial number.
56  *
57  * \param detid object identifier
58  * \return serial number
59  */
60  int operator()(const std::string& detid)
61  {
63 
64  if (p == det2id.end()) {
65 
66  initialise();
67 
68  p = det2id.find(detid);
69 
70  if (p == det2id.end()) {
71  THROW(JDatabaseException, "Invalid detector identifier " << detid);
72  }
73  }
74 
75  return p->second;
76  }
77 
78 
79  /**
80  * Get detector identifier.
81  *
82  * \param id serial number
83  * \return object identifier
84  */
85  inline std::string operator()(const int id)
86  {
88 
89  if (p == id2det.end()) {
90 
91  initialise();
92 
93  p = id2det.find(id);
94 
95  if (p == id2det.end()) {
96  THROW(JDatabaseException, "Invalid detector identifier " << id);
97  }
98  }
99 
100  return p->second;
101  }
102 
103  protected:
106  };
107 
108 
109  /**
110  * Auxiliary class for mapping UPI of central-logic board to module identifier.
111  */
112  struct JCLBIDHelper {
113  /**
114  * Initialise.
115  */
116  void initialise()
117  {
118  ResultSet& rs = JDB::get()->StreamDS(getTable<JCLBID>(), getSelector<JCLBID>());
119 
120  for (JCLBID parameters; rs >> parameters; ) {
121  upi2id[parameters.CLBUPI] = parameters.CLBID;
122  id2upi[parameters.CLBID] = parameters.CLBUPI;
123  }
124 
125  rs.Close();
126  }
127 
128 
129  /**
130  * Get module identifier.
131  *
132  * \param upi %UPI
133  * \return module identifier
134  */
135  int operator()(const JUPI_t& upi)
136  {
138 
139  if (p == upi2id.end()) {
140 
141  initialise();
142 
143  p = upi2id.find(upi);
144 
145  if (p == upi2id.end()) {
146  THROW(JDatabaseException, "Invalid UPI " << upi);
147  }
148  }
149 
150  return p->second;
151  }
152 
153 
154  /**
155  * Get %UPI.
156  *
157  * \param id module identifier
158  * \return %UPI
159  */
160  JUPI_t operator()(const int id)
161  {
163 
164  if (p == id2upi.end()) {
165 
166  initialise();
167 
168  p = id2upi.find(id);
169 
170  if (p == id2upi.end()) {
171  THROW(JDatabaseException, "Invalid module identifier " << id);
172  }
173  }
174 
175  return p->second;
176  }
177 
178  protected:
181  };
182 
183 
184  /**
185  * Auxiliary class for mapping %UPI of product to serial number.
186  */
187  struct JUPIHelper {
188  /**
189  * Initialise.
190  *
191  * \param pbs %PBS
192  */
193  void initialise(const JPBS_t& pbs)
194  {
195  ResultSet& rs = JDB::get()->StreamDS(getTable<JUPI>(), getSelector<JUPI>(pbs));
196 
197  for (JUPI parameters; rs >> parameters; ) {
198 
199  const JUPI_t upi(parameters.PBS,
200  parameters.VARIANT,
201  parameters.VERSION,
202  parameters.SERIALNUMBER);
203 
204  number2upi[pbs][parameters.SERIALNUMBER] = upi;
205  }
206 
207  rs.Close();
208  }
209 
210 
211  /**
212  * Get %UPI.
213  *
214  * \param pbs %PBS
215  * \param number serial number
216  * \return %UPI
217  */
218  JUPI_t operator()(const JPBS_t& pbs, const int number)
219  {
220  using namespace std;
221  using namespace JPP;
222 
223  map<JPBS_t, map<int, JUPI_t>>::const_iterator p = number2upi.find(pbs);
224 
225  if (p == number2upi.end()) {
226 
227  initialise(pbs);
228  p = number2upi.find(pbs);
229  }
230 
231  if (p != number2upi.end()) {
232 
233  map<int, JUPI_t>::const_iterator q = p->second.find(number);
234 
235  if (q != p->second.end()) {
236 
237  return q->second;
238 
239  } else {
240 
241  THROW(JValueOutOfRange, "Invalid serial number " << number);
242  }
243 
244  } else {
245 
246  THROW(JDatabaseException, "Invalid PBS " << pbs);
247  }
248  }
249 
250  protected:
252  };
253 
254 
255  /**
256  * Wrapper data structure for initialisation of fuction objects.
257  *
258  * The fuction objects will expand on the fly making a corresponding query to the database.\n
259  * To avoid making a query within another query, a given function object can <em>a priori</em>
260  * be initialised using the static method JDBToolkit::initialise.
261  */
262  struct JDBToolkit {
263  /**
264  * Initialise.
265  *
266  * \param helper database helper
267  * \param args values
268  */
269  template<class JHelper_t, class ...Args>
270  static void initialise(JHelper_t& helper, const Args& ...args)
271  {
272  helper.initialise(args...);
273  }
274  };
275 
276 
277  extern JDetectorsHelper getDetector; //!< Function object for mapping serial number to object identifier of detector and vice versa.
278  extern JCLBIDHelper getCLBID; //!< Function object for mapping %UPI of central-logic board to module identifier.
279  static JCLBIDHelper& getCLBUPI = getCLBID; //!< Function object for mapping module identifier to %UPI of central-logic board.
280  extern JUPIHelper getUPI; //!< Function object for mapping %PBS and serial number to %UPI.
281 }
282 
283 #endif
Exceptions.
static JCLBIDHelper & getCLBUPI
Function object for mapping module identifier to UPI of central-logic board.
Definition: JDBToolkit.hh:279
static void initialise(JHelper_t &helper, const Args &...args)
Initialise.
Definition: JDBToolkit.hh:270
Database exception.
Definition: JException.hh:648
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
Auxiliary class for specifying selection of database data.
Auxiliary class for mapping UPI of central-logic board to module identifier.
Definition: JDBToolkit.hh:112
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
std::map< std::string, int > det2id
Definition: JDBToolkit.hh:105
std::map< JUPI_t, int > upi2id
Definition: JDBToolkit.hh:179
int operator()(const JUPI_t &upi)
Get module identifier.
Definition: JDBToolkit.hh:135
void initialise(const JPBS_t &pbs)
Initialise.
Definition: JDBToolkit.hh:193
Auxiliary class for mapping UPI of product to serial number.
Definition: JDBToolkit.hh:187
std::map< int, JUPI_t > id2upi
Definition: JDBToolkit.hh:180
JUPI_t operator()(const int id)
Get UPI.
Definition: JDBToolkit.hh:160
std::map< JPBS_t, std::map< int, JUPI_t > > number2upi
Definition: JDBToolkit.hh:251
Auxiliary class for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:37
void initialise()
Initialise.
Definition: JDBToolkit.hh:116
Wrapper data structure for initialisation of fuction objects.
Definition: JDBToolkit.hh:262
void initialise()
Initialise.
Definition: JDBToolkit.hh:41
Product breakdown structure (PBS).
Definition: JPBS_t.hh:25
std::string operator()(const int id)
Get detector identifier.
Definition: JDBToolkit.hh:85
JUPI_t operator()(const JPBS_t &pbs, const int number)
Get UPI.
Definition: JDBToolkit.hh:218
JUPIHelper getUPI
Function object for mapping PBS and serial number to UPI.
Definition: JDBToolkit.cc:7
JCLBIDHelper getCLBID
Function object for mapping UPI of central-logic board to module identifier.
Definition: JDBToolkit.cc:6
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
int operator()(const std::string &detid)
Get detector serial number.
Definition: JDBToolkit.hh:60
std::map< int, std::string > id2det
Definition: JDBToolkit.hh:104
Template definition for getting table specific selector.
static JDB & get()
Get connection to database.
Definition: JDB.hh:225