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