DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
confaccessor.cpp
Go to the documentation of this file.
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
7#include "dbe/Sorting.hpp"
8#include "dbe/Exceptions.hpp"
9#include "dbe/dbaccessor.hpp"
10#include "dbe/confaccessor.hpp"
11#include "dbe/messenger.hpp"
12
14#include "dbe/change_enum.hpp"
15#include "dbe/config_api.hpp"
17#include "dbe/version.hpp"
18
20
21#include <QFileInfo>
22#include <QStringList>
23#include <QVector>
24
25#include <unordered_set>
26#include <functional>
27#include <memory>
28#include <vector>
29#include <stack>
30
31using namespace dunedaq::conffwk;
32
34
35//------------------------------------------------------------------------------------------
37{
38 static confaccessor me;
39 return me;
40}
41
42//------------------------------------------------------------------------------------------
43
44//------------------------------------------------------------------------------------------
46 : CallId ( nullptr ),
48 { } ),
49 sequenced_command_stack ( new QUndoStack() ),
50 editconfig ( new datahandler() ),
51 coreconfig ( nullptr ),
52 this_change_enabled ( false ),
53 this_total_objects ( 0 )
54{}
55
65
66//------------------------------------------------------------------------------------------
67
68//------------------------------------------------------------------------------------------
70{
72 return ref().this_change_enabled;
73}
74
80
86
87//------------------------------------------------------------------------------------------
88
89//------------------------------------------------------------------------------------------
90void dbe::confaccessor::force_emit_object_created ( QString const & src, dref const obj )
91{
92 t_lock l ( force_mut );
93 emit object_created ( src, obj );
94}
95
96void dbe::confaccessor::force_emit_object_renamed ( QString const & src, dref const obj )
97{
98 t_lock l ( force_mut );
99 emit object_renamed ( src, obj );
100}
101
102void dbe::confaccessor::force_emit_object_deleted ( QString const & src, dref const obj )
103{
104 t_lock l ( force_mut );
105 emit object_deleted ( src, obj );
106}
107
108void dbe::confaccessor::force_emit_object_changed ( QString const & src, dref const obj )
109{
110 t_lock l ( force_mut );
111 emit object_changed ( src, obj );
112}
113
114//------------------------------------------------------------------------------------------
115
116//------------------------------------------------------------------------------------------
117void dbe::confaccessor::GetFileCache ( QList<QStringList> & FileCache )
118{
119 IncludedFileCache = FileCache;
120}
121
122//------------------------------------------------------------------------------------------
123
124//------------------------------------------------------------------------------------------
126{
127 return ref().this_dblocation;
128}
129
131{
132 return ref().this_resource_location;
133}
134
135//------------------------------------------------------------------------------------------
136
137//------------------------------------------------------------------------------------------
143
149
155
156//------------------------------------------------------------------------------------------
157
158//------------------------------------------------------------------------------------------
160{
161 return ref().this_total_objects;
162}
163
165{
166 ref().this_total_objects += i;
167}
168
170{
171 ref().this_total_objects = i;
172}
173
174//------------------------------------------------------------------------------------------
175
176//------------------------------------------------------------------------------------------
177
178template<>
179void dbe::confaccessor::set_dbinfo<dbe::dbinfo::oks> ( QString const & location )
180{
181 static QString const implementation ( "oksconflibs" );
182
183 this_dblocation = location;
184 this_resource_location = implementation + ":" + location;
185
186 setenabled();
187}
188
189template<>
191{
192 this_dblocation = location;
193 static QString const implementation ( "roksconflibs" );
194 this_resource_location = implementation + ":" + location;
195 t_lock l ( this_change_enabled_mutex );
196 this_change_enabled = true;
197}
198
199template<>
200void dbe::confaccessor::set_dbinfo<dbe::dbinfo::rdb> ( QString const & location )
201{
202 static QString const implementation ( "rdbconfig" );
203 this_resource_location = implementation + ":" + location;
204 this_dblocation = this_resource_location;
205 t_lock l ( this_change_enabled_mutex );
206 this_change_enabled = true;
207}
208
209void dbe::confaccessor::setdbinfo ( QString const & location, dbinfo const itype )
210{
211 switch ( itype )
212 {
213
214 case dbinfo::oks:
215 ref().set_dbinfo<dbinfo::oks> ( location );
216 break;
217
218 case dbinfo::roks:
219 ref().set_dbinfo<dbinfo::roks> ( location );
220 break;
221
222 case dbinfo::rdb:
223 ref().set_dbinfo<dbinfo::rdb> ( location );
224 break;
225
226 default:
227 ref().setenabled();
228 break;
229 }
230}
231
232void dbe::confaccessor::setdblocation ( QString const & Implementation )
233{
234 ref().this_resource_location = Implementation;
235}
236
238{
239 return dbaccessor::dbptr().get() != nullptr;
240}
241
242//------------------------------------------------------------------------------------------
243
244//------------------------------------------------------------------------------------------
245bool dbe::confaccessor::check_file_rw ( QString const & fn )
246{
247 try
248 {
249 if ( not fn.endsWith ( ".schema.xml" ) )
250 {
251 return dbaccessor::dbptr()->is_writable ( fn.toStdString() );
252 }
253 }
254 catch ( dunedaq::conffwk::Generic const & ex )
255 {
256 ERROR ( "Not possible to operate on file", dbe::config::errors::parse ( ex ),
257 "\n\nCheck filename:", fn.toStdString() );
258 }
259
260 return false;
261}
262
263//------------------------------------------------------------------------------------------
265{
266 if ( ref().get_commands().get() )
267 {
268 ref().get_commands()->clear();
269 t_internal_changes_stack cleared{ };
270 ref().get_internal_change_stack()->swap ( cleared );
271 }
272}
273
274//------------------------------------------------------------------------------------------
275
276//------------------------------------------------------------------------------------------
278{
279 dbaccessor::dbptr()->abort();
280}
281
282
283std::list<std::string> dbe::confaccessor::uncommitted_files() {
284 std::list<std::string> dbs;
285 dbaccessor::dbptr()->get_updated_dbs(dbs);
286 return dbs;
287}
288//------------------------------------------------------------------------------------------
289
290//------------------------------------------------------------------------------------------
291bool dbe::confaccessor::load(bool subscribeToChanges)
292{
293 try
294 {
296
297 if ( dbholder::database == nullptr )
298 {
300
301 if ( subscribeToChanges == true ) {
302 ref().CallId = dbholder::database->subscribe (
305 reinterpret_cast<void *> ( dbholder::database ) );
306 }
307 }
308 else
309 {
311 delete dbholder::database;
313 }
314
315 dbholder::database->prefetch_all_data();
316
318 return true;
319 }
320 catch ( dunedaq::conffwk::Exception const & e )
321 {
322 FAIL ( "Database loading failed", dbe::config::errors::parse ( e ).c_str() );
323 return false;
324 }
325
326}
327
328std::list<std::string> dbe::confaccessor::save ( const QString & CommitMessage )
329{
330 try
331 {
332 std::list<std::string> tobecommited{};
333
334 if ( ref().is_database_loaded() )
335 {
336 dbaccessor::dbptr()->get_updated_dbs ( tobecommited );
337 dbaccessor::dbptr()->commit ( CommitMessage.toStdString() );
338 confaccessor::ref().db_committed(tobecommited, CommitMessage.toStdString());
339 }
340
341 return tobecommited;
342 }
343 catch ( dunedaq::conffwk::Exception const & e )
344 {
345 // throw daq::dbe::CouldNotCommitChanges ( ERS_HERE, dbe::config::errors::parse ( e ) );
346 throw daq::dbe::CouldNotCommitChanges ( ERS_HERE, "confaccessor", e );
347 }
348}
349
350//------------------------------------------------------------------------------------------
351
352//------------------------------------------------------------------------------------------
354{
355 QString DUNEDAQ_DB_PATH = getenv ( "DUNEDAQ_DB_PATH" );
356 QString GUI_DATA = getenv ( "OKS_GUI_INIT_DATA" );
357
358 QStringList CONFIG_DATABASE = GUI_DATA.split ( ":", Qt::SkipEmptyParts );
359 QStringList DUNEDAQ_DB_PATH_SPLIT = DUNEDAQ_DB_PATH.split ( ":", Qt::SkipEmptyParts );
360
361 // We need to read the current configuration ( to retrieve parameters affecting dbe )
362
363 std::vector<std::string> full_path_names;
364
365 for ( QString & FileName : CONFIG_DATABASE )
366 {
367
368 for ( QString & PATH : DUNEDAQ_DB_PATH_SPLIT )
369 {
370 QFileInfo CheckFile ( PATH + "/" + FileName );
371
372 if ( CheckFile.exists() and CheckFile.isFile() )
373 {
374 QString fullpathname = PATH + "/" + FileName;
375 full_path_names.push_back ( fullpathname.toStdString() );
376 }
377 }
378 }
379
380 confaccessor::ref().coreconfig = new ui::config::info ( full_path_names );
381
382}
383
385{
386 return IncludedFileCache;
387}
388
389//------------------------------------------------------------------------------------------
390
391//------------------------------------------------------------------------------------------
392void dbe::confaccessor::addfile ( std::string const & db, std::string const & fn )
393{
394 if ( not enabled() )
395 {
396 throw daq::dbe::ChangeNotAllowed ( ERS_HERE );
397 }
398
399 dbaccessor::dbptr()->add_include ( db, fn );
400 emit IncludeFileDone();
401}
402
403void dbe::confaccessor::removefile ( std::string const & db, std::string const & fn )
404{
405 if ( not enabled() )
406 {
407 throw daq::dbe::ChangeNotAllowed ( ERS_HERE );
408 }
409
410 dbaccessor::dbptr()->remove_include ( db, fn );
411 emit RemoveFileDone();
412}
413
414//------------------------------------------------------------------------------------------
415
416//------------------------------------------------------------------------------------------
417void dbe::confaccessor::CallbackFunction ( std::vector<ConfigurationChange *> const &
418 changes,
419 void * parameter )
420{
421 dbe::confaccessor::ref().docallback ( changes, parameter );
422}
423
424void dbe::confaccessor::docallback ( std::vector<ConfigurationChange *> const & changes,
425 void * parameter )
426{
427 Q_UNUSED ( parameter );
428
429 t_lock l ( mut_changes );
430 external_change_stack.clear();
431
432 for ( ConfigurationChange * Change : changes )
433 {
434 const auto& cl = Change->get_class_name();
435 const auto& mod = Change->get_modified_objs();
436 const auto& cre = Change->get_created_objs();
437 const auto& rem = Change->get_removed_objs();
438
439 external_change_stack.push_back ( { cl, mod, cre, rem } );
440 }
441
443}
444
445//------------------------------------------------------------------------------------------
446
447//------------------------------------------------------------------------------------------
449{
450 if ( dbaccessor::dbptr().get() && ref().CallId != nullptr )
451 {
452 dbaccessor::dbptr()->unsubscribe ( ref().CallId );
453 }
454}
455
462
463//------------------------------------------------------------------------------------------
464
#define ERS_HERE
Definition cptr.hpp:55
T * get()
Definition cptr.hpp:88
std::lock_guard< t_mutex > t_lock
static int get_total_objects()
datahandler * editconfig
QList< QStringList > IncludedFileCache
void force_emit_object_created(QString const &, dref const)
static cptr< datahandler > gethandler()
cptr< t_internal_changes_stack > t_internal_changes_stack_cptr
void GetFileCache(QList< QStringList > &FileCache)
void object_created(QString const &source, dref const &obj)
static void setdblocation(const QString &Implementation)
void removefile(std::string const &db, std::string const &fn)
static t_undo_stack_cptr get_commands()
static void CallbackFunction(const std::vector< dunedaq::conffwk::ConfigurationChange * > &changes, void *parameter)
static void increase_total_objects(int const i)
static bool is_database_loaded()
cptr< t_undo_stack > t_undo_stack_cptr
void ExternalChangesAccepted()
static void unsubscribe()
t_external_changes_stack external_change_stack
static std::list< std::string > uncommitted_files()
std::stack< t_internal_change, std::vector< t_internal_change > > t_internal_changes_stack
void db_committed(const std::list< std::string > &files, const std::string &msg)
t_undo_stack * sequenced_command_stack
std::shared_ptr< t_internal_changes_stack > internal_change_stack
static bool enabled()
void force_emit_object_deleted(QString const &, dref const)
QList< QStringList > GetIncludedFileCache() const
static void abort()
static cptr< ui::config::info > guiconfig()
static void set_total_objects(int const i)
ui::config::info * coreconfig
static void init()
static void clear_commands()
void force_emit_object_renamed(QString const &, dref const)
void force_emit_object_changed(QString const &, dref const)
void object_deleted(QString const &source, dref const &obj)
void object_renamed(QString const &source, dref const &obj)
static bool check_file_rw(const QString &FileName)
void object_changed(QString const &source, dref const &obj)
static QString dbfullname()
static t_internal_changes_stack_cptr get_internal_change_stack()
static bool load(bool subscribeToChanges=true)
QString this_resource_location
t_mutex this_change_enabled_mutex
static std::list< std::string > save(QString const &)
void docallback(std::vector< dunedaq::conffwk::ConfigurationChange * > const &changes, void *parameter)
void set_dbinfo(QString const &p)
static void setdbinfo(QString const &location, dbinfo const itype=dbinfo::oks)
static confaccessor & ref()
dunedaq::conffwk::Configuration::CallbackId CallId
void addfile(std::string const &db, std::string const &fn)
static QString db_implementation_name()
static cptr< dunedaq::conffwk::Configuration > dbptr()
static cptr< dunedaq::conffwk::Configuration > database_concurrent_ptr
dbe::types::common::type_lock t_lock
static dunedaq::conffwk::Configuration * database
static dbe::types::common::type_mutex database_lock
Describes changes inside a class returned by the notification mechanism.
Definition Change.hpp:42
Defines base class for cache of template objects.
char const *const dbe_lib_internal_version
conffwk entry point
#define ERROR(...)
Definition messenger.hpp:93
#define FAIL(...)
std::string const parse(ers::Issue const &)
dbinfo
Definition dbinfo.hpp:20
config_object_description dref
msgpack::object obj
#define dbe_compiled_version
Definition version.hpp:22