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