Line data Source code
1 : /************************************************************
2 : * rwdacc.cpp
3 : *
4 : * Created on: Feb 4, 2016
5 : * Author: Leonidas Georgopoulos
6 : ************************************************************/
7 : #include "dbe/dbaccessor.hpp"
8 : #include "dbe/config_direct_access.hpp"
9 : #include "dbe/confobject_desc.hpp"
10 : #include "dbe/Exceptions.hpp"
11 : #include "dbe/messenger.hpp"
12 : #include "dbe/config_api_set.hpp"
13 : #include "dbe/Conversion.hpp"
14 :
15 : #include "conffwk/ConfigObject.hpp"
16 : #include "conffwk/Configuration.hpp"
17 : #include "conffwk/Errors.hpp"
18 : #include "conffwk/Schema.hpp"
19 : #include "ers/LocalContext.hpp"
20 : #include "logging/Logging.hpp"
21 :
22 : #include <QString>
23 : #include <QStringList>
24 :
25 : #include <functional>
26 : #include <map>
27 : #include <stdexcept>
28 : #include <string>
29 : #include <vector>
30 :
31 : using namespace dunedaq::conffwk;
32 :
33 : namespace dbe
34 : {
35 :
36 : namespace config
37 : {
38 :
39 : namespace api
40 : {
41 : //------------------------------------------------------------------------------------------
42 0 : ConfigObject rwdacc::create_object(std::string const & fn, std::string const & cn,
43 : std::string const & uid)
44 : {
45 0 : if (info::onclass::definition(cn, false).p_abstract)
46 : {
47 0 : ERROR("Create Object: Was not possible to create object ",
48 0 : "Abstract classes cannot hold objects", uid, "for abstract class" , cn);
49 :
50 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE);
51 : }
52 :
53 : // An empty object, if its creation does not succeed this object will be returned
54 0 : ConfigObject newobj;
55 :
56 0 : try
57 : {
58 0 : dbaccessor::dbptr()->create(fn, cn, uid,newobj);
59 : }
60 0 : catch (dunedaq::conffwk::Exception const & ex)
61 : {
62 0 : FAIL ( "Object creation failed", dbe::config::errors::parse ( ex ), "\n\nObject UID:",
63 0 : uid );
64 :
65 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE);
66 0 : }
67 :
68 0 : return newobj;
69 :
70 0 : }
71 : //------------------------------------------------------------------------------------------
72 :
73 : //------------------------------------------------------------------------------------------
74 : /**
75 : * Define a function to retrieve from a key-val container a values with of KEYTYPE keys and
76 : * convert them to result type RESTYPE
77 : */
78 : template<typename RESTYPE, typename KEYTYPE = std::string>
79 : using mapreader = std::function< RESTYPE (KEYTYPE const &) >;
80 : //------------------------------------------------------------------------------------------
81 :
82 : //------------------------------------------------------------------------------------------
83 0 : tref rwdacc::set_object(tref newobj,
84 : dbe::t_config_object_preimage::type_attrmap const & attributes,
85 : dbe::t_config_object_preimage::type_relmap const & relations)
86 : {
87 0 : auto relreader = [&relations ]( std::string const & name )
88 : {
89 0 : return relations.at(name);
90 0 : };
91 :
92 0 : auto attreader = [ & attributes ] ( std::string const & name )
93 : {
94 0 : return attributes.at(name);
95 0 : };
96 :
97 0 : if (not newobj.is_null())
98 : {
99 0 : dunedaq::conffwk::class_t ClassInfo = dbe::config::api::info::onclass::definition(
100 0 : newobj.class_name(),
101 0 : false);
102 0 : std::vector<dunedaq::conffwk::attribute_t> AttList = ClassInfo.p_attributes;
103 0 : std::vector<dunedaq::conffwk::relationship_t> RelList = ClassInfo.p_relationships;
104 :
105 0 : for (dunedaq::conffwk::attribute_t const & att : AttList)
106 : {
107 0 : try
108 : {
109 0 : if(att.p_is_multi_value) {
110 0 : switch(att.p_type) {
111 0 : case dunedaq::conffwk::bool_type:
112 0 : {
113 0 : auto data = dbe::convert::to<std::vector<bool>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
114 0 : set::noactions::attribute(newobj, att, data, true);
115 0 : }
116 0 : break;
117 0 : case dunedaq::conffwk::enum_type:
118 0 : {
119 0 : auto data = dbe::convert::to<std::vector<std::string>>(dbe::convert::to<QStringList>(attreader(att.p_name)));
120 0 : set::noactions::anenum(newobj, att, data, true);
121 0 : }
122 0 : break;
123 0 : case dunedaq::conffwk::date_type:
124 0 : {
125 0 : auto data = dbe::convert::to<std::vector<std::string>>(dbe::convert::to<QStringList>(attreader(att.p_name)));
126 0 : set::noactions::adate(newobj, att, data, true);
127 0 : }
128 0 : break;
129 0 : case dunedaq::conffwk::time_type:
130 0 : {
131 0 : auto data = dbe::convert::to<std::vector<std::string>>(dbe::convert::to<QStringList>(attreader(att.p_name)));
132 0 : set::noactions::atime(newobj, att, data, true);
133 0 : }
134 0 : break;
135 0 : case dunedaq::conffwk::string_type:
136 0 : {
137 0 : auto data = dbe::convert::to<std::vector<std::string>>(dbe::convert::to<QStringList>(attreader(att.p_name)));
138 0 : set::noactions::attribute(newobj, att, data, true);
139 0 : }
140 0 : break;
141 0 : case dunedaq::conffwk::class_type:
142 0 : {
143 0 : auto data = dbe::convert::to<std::vector<std::string>>(dbe::convert::to<QStringList>(attreader(att.p_name)));
144 0 : set::noactions::aclass(newobj, att, data, true);
145 0 : }
146 0 : break;
147 0 : case dunedaq::conffwk::float_type:
148 0 : {
149 0 : auto data = dbe::convert::to<std::vector<float>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
150 0 : set::noactions::attribute(newobj, att, data, true);
151 0 : }
152 0 : break;
153 0 : case dunedaq::conffwk::double_type:
154 0 : {
155 0 : auto data = dbe::convert::to<std::vector<double>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
156 0 : set::noactions::attribute(newobj, att, data, true);
157 0 : }
158 0 : break;
159 0 : case dunedaq::conffwk::s8_type:
160 0 : {
161 0 : auto data = dbe::convert::to<std::vector<int8_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
162 0 : set::noactions::attribute(newobj, att, data, true);
163 0 : }
164 0 : break;
165 0 : case dunedaq::conffwk::s16_type:
166 0 : {
167 0 : auto data = dbe::convert::to<std::vector<int16_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
168 0 : set::noactions::attribute(newobj, att, data, true);
169 0 : }
170 0 : break;
171 0 : case dunedaq::conffwk::s32_type:
172 0 : {
173 0 : auto data = dbe::convert::to<std::vector<int32_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
174 0 : set::noactions::attribute(newobj, att, data, true);
175 0 : }
176 0 : break;
177 0 : case dunedaq::conffwk::s64_type:
178 0 : {
179 0 : auto data = dbe::convert::to<std::vector<int64_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
180 0 : set::noactions::attribute(newobj, att, data, true);
181 0 : }
182 0 : break;
183 0 : case dunedaq::conffwk::u8_type:
184 0 : {
185 0 : auto data = dbe::convert::to<std::vector<u_int8_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
186 0 : set::noactions::attribute(newobj, att, data, true);
187 0 : }
188 0 : break;
189 0 : case dunedaq::conffwk::u16_type:
190 0 : {
191 0 : auto data = dbe::convert::to<std::vector<u_int16_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
192 0 : set::noactions::attribute(newobj, att, data, true);
193 0 : }
194 0 : break;
195 0 : case dunedaq::conffwk::u32_type:
196 0 : {
197 0 : auto data = dbe::convert::to<std::vector<u_int32_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
198 0 : set::noactions::attribute(newobj, att, data, true);
199 0 : }
200 0 : break;
201 0 : case dunedaq::conffwk::u64_type:
202 0 : {
203 0 : auto data = dbe::convert::to<std::vector<u_int64_t>>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
204 0 : set::noactions::attribute(newobj, att, data, true);
205 0 : }
206 0 : break;
207 : default:
208 : break;
209 : }
210 : } else {
211 0 : switch(att.p_type) {
212 0 : case dunedaq::conffwk::bool_type:
213 0 : {
214 0 : auto data = dbe::convert::to<bool>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
215 0 : set::noactions::attribute(newobj, att, data, true);
216 : }
217 0 : break;
218 0 : case dunedaq::conffwk::enum_type:
219 0 : {
220 0 : auto data = dbe::convert::to<std::string>(dbe::convert::to<QStringList>(attreader(att.p_name)));
221 0 : set::noactions::anenum(newobj, att, data, true);
222 0 : }
223 0 : break;
224 0 : case dunedaq::conffwk::date_type:
225 0 : {
226 0 : auto data = dbe::convert::to<std::string>(dbe::convert::to<QStringList>(attreader(att.p_name)));
227 0 : set::noactions::adate(newobj, att, data, true);
228 0 : }
229 0 : break;
230 0 : case dunedaq::conffwk::time_type:
231 0 : {
232 0 : auto data = dbe::convert::to<std::string>(dbe::convert::to<QStringList>(attreader(att.p_name)));
233 0 : set::noactions::atime(newobj, att, data, true);
234 0 : }
235 0 : break;
236 0 : case dunedaq::conffwk::string_type:
237 0 : {
238 0 : auto data = dbe::convert::to<std::string>(dbe::convert::to<QStringList>(attreader(att.p_name)));
239 0 : set::noactions::attribute(newobj, att, data, true);
240 0 : }
241 0 : break;
242 0 : case dunedaq::conffwk::class_type:
243 0 : {
244 0 : auto data = dbe::convert::to<std::string>(dbe::convert::to<QStringList>(attreader(att.p_name)));
245 0 : set::noactions::aclass(newobj, att, data, true);
246 0 : }
247 0 : break;
248 0 : case dunedaq::conffwk::float_type:
249 0 : {
250 0 : auto data = dbe::convert::to<float>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
251 0 : set::noactions::attribute(newobj, att, data, true);
252 : }
253 0 : break;
254 0 : case dunedaq::conffwk::double_type:
255 0 : {
256 0 : auto data = dbe::convert::to<double>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
257 0 : set::noactions::attribute(newobj, att, data, true);
258 : }
259 0 : break;
260 0 : case dunedaq::conffwk::s8_type:
261 0 : {
262 0 : auto data = dbe::convert::to<int8_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
263 0 : set::noactions::attribute(newobj, att, data, true);
264 : }
265 0 : break;
266 0 : case dunedaq::conffwk::s16_type:
267 0 : {
268 0 : auto data = dbe::convert::to<int16_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
269 0 : set::noactions::attribute(newobj, att, data, true);
270 : }
271 0 : break;
272 0 : case dunedaq::conffwk::s32_type:
273 0 : {
274 0 : auto data = dbe::convert::to<int32_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
275 0 : set::noactions::attribute(newobj, att, data, true);
276 : }
277 0 : break;
278 0 : case dunedaq::conffwk::s64_type:
279 0 : {
280 0 : auto data = dbe::convert::to<int64_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
281 0 : set::noactions::attribute(newobj, att, data, true);
282 : }
283 0 : break;
284 0 : case dunedaq::conffwk::u8_type:
285 0 : {
286 0 : auto data = dbe::convert::to<u_int8_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
287 0 : set::noactions::attribute(newobj, att, data, true);
288 : }
289 0 : break;
290 0 : case dunedaq::conffwk::u16_type:
291 0 : {
292 0 : auto data = dbe::convert::to<u_int16_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
293 0 : set::noactions::attribute(newobj, att, data, true);
294 : }
295 0 : break;
296 0 : case dunedaq::conffwk::u32_type:
297 0 : {
298 0 : auto data = dbe::convert::to<u_int32_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
299 0 : set::noactions::attribute(newobj, att, data, true);
300 : }
301 0 : break;
302 0 : case dunedaq::conffwk::u64_type:
303 0 : {
304 0 : auto data = dbe::convert::to<u_int64_t>(dbe::convert::to<QStringList>(attreader(att.p_name)), att.p_int_format);
305 0 : set::noactions::attribute(newobj, att, data, true);
306 : }
307 0 : break;
308 : default:
309 : break;
310 : }
311 : }
312 : }
313 0 : catch (std::out_of_range const & e)
314 : {
315 : // nothing to do just ignore this , it might be the case that the attribute was not defined
316 0 : }
317 : }
318 :
319 0 : for (dunedaq::conffwk::relationship_t const & rel : RelList)
320 : {
321 0 : try
322 : {
323 0 : auto data = relreader(rel.p_name);
324 0 : set::noactions::relation(newobj, rel, data);
325 0 : }
326 0 : catch (std::out_of_range const & e)
327 : {
328 : // nothing to do just ignore this , it might be the case that the relation was not defined
329 0 : }
330 : }
331 0 : }
332 :
333 0 : return newobj;
334 : }
335 : //------------------------------------------------------------------------------------------
336 :
337 : //------------------------------------------------------------------------------------------
338 0 : void rwdacc::destroy_object(ConfigObject & todelete)
339 : {
340 0 : try
341 : {
342 0 : dbaccessor::dbptr()->destroy_obj(todelete);
343 : }
344 0 : catch (dunedaq::conffwk::Exception const & ex)
345 : {
346 0 : WARN("Object deletion did not succeed for object" , errors::parse(ex).c_str(),
347 0 : todelete.UID().c_str());
348 :
349 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE);
350 0 : }
351 0 : }
352 : //------------------------------------------------------------------------------------------
353 :
354 : //------------------------------------------------------------------------------------------
355 5 : ConfigObject rwdacc::get_object(std::string const & classname,
356 : std::string const & objectname)
357 : {
358 5 : if (dbaccessor::is_loaded())
359 : {
360 5 : try
361 : {
362 5 : ConfigObject object;
363 5 : dbaccessor::dbptr()->get(classname, objectname, object);
364 5 : return object;
365 5 : }
366 0 : catch (dunedaq::conffwk::NotFound const & e)
367 : {
368 0 : TLOG_DEBUG(3) << "Object not found" ;
369 0 : }
370 0 : catch (dunedaq::conffwk::Generic const & e)
371 : {
372 0 : ERROR("Generic exception caught", dbe::config::errors::parse(e).c_str());
373 0 : TLOG_DEBUG(3) << "Generic Exception Caught" ;
374 0 : }
375 0 : catch (...)
376 : {
377 0 : ERROR("Unknown exception caught", "");
378 0 : TLOG_DEBUG(3) << "Unknown exception caught!" ;
379 0 : }
380 : }
381 0 : return
382 0 : {};
383 : }
384 : //------------------------------------------------------------------------------------------
385 :
386 : //------------------------------------------------------------------------------------------
387 2 : std::vector<ConfigObject> rwdacc::query_class(std::string const & classname,
388 : std::string const & query)
389 : {
390 2 : std::vector<ConfigObject> objects;
391 2 : if (dbaccessor::is_loaded())
392 : {
393 2 : try
394 : {
395 2 : dbaccessor::dbptr()->get(classname, objects, query);
396 : }
397 0 : catch (dunedaq::conffwk::NotFound const & e)
398 : {
399 0 : TLOG_DEBUG(3) << "Object not found" ;
400 0 : }
401 0 : catch (dunedaq::conffwk::Generic const & e)
402 : {
403 0 : ERROR("Generic exception caught", dbe::config::errors::parse(e).c_str());
404 0 : TLOG_DEBUG(3) << "Generic Exception Caught" ;
405 0 : }
406 0 : catch (...)
407 : {
408 0 : ERROR("Unknown exception caught", "");
409 0 : TLOG_DEBUG(3) << "Unknown exception caught!" ;
410 0 : }
411 : }
412 2 : return objects;
413 0 : }
414 : //------------------------------------------------------------------------------------------
415 :
416 : //------------------------------------------------------------------------------------------
417 0 : void rwdacc::rename_object(ConfigObject & object, std::string const & newname)
418 : {
419 0 : object.rename(newname);
420 0 : }
421 : //------------------------------------------------------------------------------------------
422 :
423 : }// namespace api
424 :
425 : } // namespace config
426 :
427 : } // namespace dbe
|