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