$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h" 00002 #include "otsdaq-core/ConfigurationInterface/DatabaseConfigurationInterface.h" 00003 #include "otsdaq-core/ConfigurationInterface/FileConfigurationInterface.h" 00004 00005 #include "otsdaq-core/Macros/CoutMacros.h" 00006 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00007 00008 #include <dirent.h> 00009 #include <cassert> 00010 #include <iostream> 00011 #include <typeinfo> 00012 00013 using namespace ots; 00014 00015 #define DEBUG_CONFIGURATION true 00016 00017 //============================================================================== 00018 ConfigurationInterface* ConfigurationInterface::theInstance_ = 0; 00019 bool ConfigurationInterface::theMode_ = true; 00020 bool ConfigurationInterface::theVersionTrackingEnabled_ = true; 00021 00022 const std::string ConfigurationInterface::GROUP_METADATA_TABLE_NAME = 00023 "TableGroupMetadata"; 00024 00025 //============================================================================== 00026 ConfigurationInterface::ConfigurationInterface() {} 00027 00028 //============================================================================== 00029 ConfigurationInterface* ConfigurationInterface::getInstance(bool mode) 00030 { 00031 if(mode == true) 00032 { 00033 if(theInstance_ != 0 && 00034 dynamic_cast<FileConfigurationInterface*>(theInstance_) == 0) 00035 { 00036 delete theInstance_; 00037 theInstance_ = 0; 00038 } 00039 if(theInstance_ == 0) // && typeid(theInstance_) != 00040 // static_cast<DatabaseConfigurationInterface*> ) 00041 theInstance_ = new FileConfigurationInterface(); 00042 } 00043 else 00044 { 00045 if(theInstance_ != 0 && 00046 dynamic_cast<DatabaseConfigurationInterface*>(theInstance_) == 0) 00047 { 00048 delete theInstance_; 00049 theInstance_ = 0; 00050 } 00051 if(theInstance_ == 0) // && typeid(theInstance_) != 00052 // static_cast<DatabaseConfigurationInterface*> ) 00053 { 00054 theInstance_ = new DatabaseConfigurationInterface(); 00055 } 00056 } 00057 theMode_ = mode; 00058 return theInstance_; 00059 } 00060 00061 //============================================================================== 00062 bool ConfigurationInterface::isVersionTrackingEnabled() 00063 { 00064 return ConfigurationInterface::theVersionTrackingEnabled_; 00065 } 00066 00067 //============================================================================== 00068 void ConfigurationInterface::setVersionTrackingEnabled(bool setValue) 00069 { 00070 ConfigurationInterface::theVersionTrackingEnabled_ = setValue; 00071 } 00072 00073 //============================================================================== 00074 // saveNewVersion 00075 // If newVersion is 0, then save the temporaryVersion as the next positive version 00076 // number, 00077 // save using the interface, and return the new version number 00078 // If newVersion is non 0, attempt to save as given newVersion number, else throw 00079 // exception. return TableVersion::INVALID on failure 00080 TableVersion ConfigurationInterface::saveNewVersion(TableBase* configuration, 00081 TableVersion temporaryVersion, 00082 TableVersion newVersion) 00083 { 00084 if(!temporaryVersion.isTemporaryVersion() || 00085 !configuration->isStored(temporaryVersion)) 00086 { 00087 std::cout << __COUT_HDR_FL__ 00088 << "Invalid temporary version number: " << temporaryVersion 00089 << std::endl; 00090 return TableVersion(); // return INVALID 00091 } 00092 00093 if(!ConfigurationInterface::isVersionTrackingEnabled()) // tracking is OFF, so always 00094 // save to same version 00095 newVersion = TableVersion::SCRATCH; 00096 00097 bool rewriteableExists = false; 00098 00099 std::set<TableVersion> versions = getVersions(configuration); 00100 if(newVersion == TableVersion::INVALID) 00101 { 00102 if(versions 00103 .size() && // 1 more than last version, if any non-scratch versions exist 00104 *(versions.rbegin()) != TableVersion(TableVersion::SCRATCH)) 00105 newVersion = TableVersion::getNextVersion(*(versions.rbegin())); 00106 else if(versions.size() > 00107 1) // if scratch exists, take 1 more than second to last version 00108 newVersion = TableVersion::getNextVersion(*(--(versions.rbegin()))); 00109 else 00110 newVersion = TableVersion::DEFAULT; 00111 std::cout << __COUT_HDR_FL__ << "Next available version number is " << newVersion 00112 << std::endl; 00113 // 00114 // //for sanity check, compare with config's idea of next version 00115 // TableVersion baseNextVersion = configuration->getNextVersion(); 00116 // if(newVersion <= baseNextVersion) 00117 // newVersion = TableVersion::getNextVersion(baseNextVersion); 00118 // 00119 // std::cout << __COUT_HDR_FL__ << "After considering baseNextVersion, " << 00120 // baseNextVersion << 00121 // ", next available version number is " << newVersion << std::endl; 00122 } 00123 else if(versions.find(newVersion) != versions.end()) 00124 { 00125 std::cout << __COUT_HDR_FL__ << "newVersion(" << newVersion << ") already exists!" 00126 << std::endl; 00127 rewriteableExists = newVersion == TableVersion::SCRATCH; 00128 00129 // throw error if version already exists and this is not the rewriteable version 00130 if(!rewriteableExists || ConfigurationInterface::isVersionTrackingEnabled()) 00131 { 00132 __SS__ << ("New version already exists!") << std::endl; 00133 std::cout << __COUT_HDR_FL__ << ss.str(); 00134 __SS_THROW__; 00135 } 00136 } 00137 00138 std::cout << __COUT_HDR_FL__ << "Version number to save is " << newVersion 00139 << std::endl; 00140 00141 // copy to new version 00142 configuration->changeVersionAndActivateView(temporaryVersion, newVersion); 00143 00144 // save to disk 00145 // only allow overwrite if version tracking is disabled AND the rewriteable version 00146 // already exists. 00147 saveActiveVersion( 00148 configuration, 00149 !ConfigurationInterface::isVersionTrackingEnabled() && rewriteableExists); 00150 00151 return newVersion; 00152 }