tdaq-develop-2025-02-12
ConfigurationInterface.cc
1 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
2 #include "otsdaq/ConfigurationInterface/MakeConfigurationInterface.h"
3 
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/MessageFacility/MessageFacility.h"
6 
7 #include <dirent.h>
8 #include <cassert>
9 #include <iostream>
10 #include <typeinfo>
11 
12 using namespace ots;
13 
14 #define DEBUG_CONFIGURATION true
15 
16 //==============================================================================
17 ConfigurationInterface* ConfigurationInterface::theInstance_ = nullptr;
18 ConfigurationInterface::CONFIGURATION_MODE ConfigurationInterface::theMode_ =
19  ConfigurationInterface::CONFIGURATION_MODE::DO_NOT_CREATE;
20 bool ConfigurationInterface::theVersionTrackingEnabled_ = true;
21 
22 const std::string ConfigurationInterface::GROUP_METADATA_TABLE_NAME =
23  "TableGroupMetadata";
24 
25 //==============================================================================
27 
28 //==============================================================================
29 ConfigurationInterface* ConfigurationInterface::getInstance(
30  ConfigurationInterface::CONFIGURATION_MODE mode /* = DO_NOT_CREATE */)
31 {
32  if(mode == CONFIGURATION_MODE::DO_NOT_CREATE)
33  {
34  if(theInstance_ == nullptr)
35  std::cout
36  << __COUT_HDR_FL__
37  << "WARNING -- returning a nullptr ConfigurationInterface::theInstance_"
38  << __E__;
39  return theInstance_;
40  }
41 
42  auto instanceType = (mode == CONFIGURATION_MODE::XML_FILE) ? "File" : "Database";
43  if(theMode_ != mode)
44  {
45  delete theInstance_;
46  theInstance_ = nullptr;
47  }
48  if(theInstance_ == nullptr)
49  {
50  theInstance_ = makeConfigurationInterface(instanceType);
51  }
52 
53  theMode_ = mode;
54  return theInstance_;
55 } //end getInstance()
56 
57 //==============================================================================
58 bool ConfigurationInterface::isVersionTrackingEnabled()
59 {
60  return ConfigurationInterface::theVersionTrackingEnabled_;
61 }
62 
63 //==============================================================================
64 void ConfigurationInterface::setVersionTrackingEnabled(bool setValue)
65 {
66  ConfigurationInterface::theVersionTrackingEnabled_ = setValue;
67 }
68 
69 //==============================================================================
70 const ConfigurationInterface::CONFIGURATION_MODE& ConfigurationInterface::getMode()
71 {
72  return ConfigurationInterface::theMode_;
73 }
74 
75 //==============================================================================
83  TableVersion temporaryVersion,
84  TableVersion newVersion)
85 {
86  if(!temporaryVersion.isTemporaryVersion() || !table->isStored(temporaryVersion))
87  {
88  std::cout << __COUT_HDR_FL__
89  << "Invalid temporary version number: " << temporaryVersion
90  << std::endl;
91  return TableVersion(); // return INVALID
92  }
93 
94  if(!ConfigurationInterface::isVersionTrackingEnabled()) // tracking is OFF, so always
95  // save to same version
96  newVersion = TableVersion::SCRATCH;
97 
98  bool rewriteableExists = false;
99 
100  std::set<TableVersion> versions = getVersions(table);
101  if(newVersion == TableVersion::INVALID)
102  {
103  if(versions
104  .size() && // 1 more than last version, if any non-scratch versions exist
105  *(versions.rbegin()) != TableVersion(TableVersion::SCRATCH))
106  newVersion = TableVersion::getNextVersion(*(versions.rbegin()));
107  else if(versions.size() >
108  1) // if scratch exists, take 1 more than second to last version
109  newVersion = TableVersion::getNextVersion(*(--(versions.rbegin())));
110  else
111  newVersion = TableVersion::DEFAULT;
112  std::cout << __COUT_HDR_FL__ << "Next available version number is " << newVersion
113  << std::endl;
114  //
115  // //for sanity check, compare with config's idea of next version
116  // TableVersion baseNextVersion = table->getNextVersion();
117  // if(newVersion <= baseNextVersion)
118  // newVersion = TableVersion::getNextVersion(baseNextVersion);
119  //
120  // std::cout << __COUT_HDR_FL__ << "After considering baseNextVersion, " <<
121  // baseNextVersion <<
122  // ", next available version number is " << newVersion << std::endl;
123  }
124  else if(versions.find(newVersion) != versions.end())
125  {
126  std::cout << __COUT_HDR_FL__ << "newVersion(" << newVersion << ") already exists!"
127  << std::endl;
128  rewriteableExists = newVersion == TableVersion::SCRATCH;
129 
130  // throw error if version already exists and this is not the rewriteable version
131  if(!rewriteableExists || ConfigurationInterface::isVersionTrackingEnabled())
132  {
133  __SS__ << ("New version already exists!") << std::endl;
134  std::cout << __COUT_HDR_FL__ << ss.str();
135  __SS_THROW__;
136  }
137  }
138 
139  std::cout << __COUT_HDR_FL__ << "Version number to save is " << newVersion
140  << std::endl;
141 
142  // copy to new version
143  table->changeVersionAndActivateView(temporaryVersion, newVersion);
144 
145  // save to disk
146  // only allow overwrite if version tracking is disabled AND the rewriteable version
147  // already exists.
148  saveActiveVersion(
149  table, !ConfigurationInterface::isVersionTrackingEnabled() && rewriteableExists);
150 
151  return newVersion;
152 } //end saveNewVersion()
ConfigurationInterface(void)
Protected constructor.
TableVersion saveNewVersion(TableBase *configuration, TableVersion temporaryVersion, TableVersion newVersion=TableVersion())
static TableVersion getNextVersion(const TableVersion &version=TableVersion())
bool isTemporaryVersion(void) const