LCOV - code coverage report
Current view: top level - dbe/src/internal - rwdacc.cpp (source / functions) Coverage Total Hit
Test: code.result Lines: 4.4 % 275 12
Test Date: 2026-07-12 15:23:06 Functions: 14.3 % 14 2

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

Generated by: LCOV version 2.0-1