1 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationBase.h"
2 #include "otsdaq-core/ConfigurationDataFormats/ConfigurationInfoReader.h"
12 #define __MF_SUBJECT__ "ConfigurationBase"
14 #define __MF_HDR__ __COUT_HDR_FL__ << getConfigurationName() << ": "
22 ConfigurationBase::ConfigurationBase(std::string configurationName,
23 std::string *accumulatedExceptions)
24 : MAX_VIEWS_IN_CACHE (5)
25 , configurationName_ (configurationName)
26 , activeConfigurationView_(0)
32 std::string returnedExceptions = configurationInfoReader.read(
this);
34 if(returnedExceptions !=
"")
35 __MOUT_ERR__ << returnedExceptions << std::endl;
37 if(accumulatedExceptions) *accumulatedExceptions += std::string(
"\n") + returnedExceptions;
41 __SS__ <<
"Failure in configurationInfoReader.read(this)" << std::endl;
42 __MOUT_ERR__ <<
"\n" << ss.str();
43 if(accumulatedExceptions) *accumulatedExceptions += std::string(
"\n") +
52 getMockupViewP()->init();
54 catch(std::runtime_error& e)
56 if(accumulatedExceptions) *accumulatedExceptions += std::string(
"\n") + e.what();
65 ConfigurationBase::ConfigurationBase(
void)
66 : MAX_VIEWS_IN_CACHE (1)
67 , configurationName_ (
"")
68 , activeConfigurationView_(0)
73 ConfigurationBase::~ConfigurationBase(
void)
78 std::string ConfigurationBase::getTypeId()
80 return typeid(
this).name();
90 void ConfigurationBase::reset(
bool keepTemporaryVersions)
94 if(keepTemporaryVersions)
97 configurationViews_.clear();
101 void ConfigurationBase::print(std::ostream &out)
const
104 if(!activeConfigurationView_)
106 __MOUT_ERR__ <<
"ERROR: No active view set" << std::endl;
109 activeConfigurationView_->print(out);
117 if(!isStored(version))
119 configurationViews_[version].copy(mockupConfigurationView_,
121 mockupConfigurationView_.getAuthor());
123 if(!isStored(version))
125 __SS__ <<
"\nsetupMockupView() IMPOSSIBLE ERROR: trimCache() is deleting the latest view version " <<
126 version <<
"!" << std::endl;
127 __MOUT_ERR__ <<
"\n" << ss.str();
128 throw std::runtime_error(ss.str());
133 __SS__ <<
"\nsetupMockupView() ERROR: View to fill with mockup already exists: " << version
134 <<
". Cannot overwrite!" << std::endl;
135 __MOUT_ERR__ <<
"\n" << ss.str();
136 throw std::runtime_error(ss.str());
146 void ConfigurationBase::trimCache(
unsigned int trimSize)
150 if(trimSize == (
unsigned int)-1)
151 trimSize = MAX_VIEWS_IN_CACHE;
154 while(getNumberOfStoredViews() > trimSize)
157 time_t stalestTime = -1;
159 for(
auto &viewPair : configurationViews_)
160 if(!viewPair.first.isTemporaryVersion())
162 if(stalestTime == -1 ||
163 viewPair.second.getLastAccessTime() < stalestTime)
165 versionToDelete = viewPair.first;
166 stalestTime = viewPair.second.getLastAccessTime();
171 if(versionToDelete.isInvalid())
173 __SS__ <<
"Can NOT have a stored view with an invalid version!";
174 throw std::runtime_error(ss.str());
177 eraseView(versionToDelete);
188 if(targetVersion.isInvalid())
190 for(
auto it = configurationViews_.begin(); it != configurationViews_.end(); )
192 if(it->first.isTemporaryVersion())
194 __MOUT__ <<
"Trimming temporary version: " << it->first << std::endl;
195 if(activeConfigurationView_ &&
196 getViewVersion() == it->first)
198 configurationViews_.erase(it++);
204 else if(targetVersion.isTemporaryVersion())
206 __MOUT__ <<
"Trimming temporary version: " << targetVersion << std::endl;
207 eraseView(targetVersion);
212 __SS__ <<
"Temporary trim target was a persistent version: " <<
213 targetVersion << std::endl;
214 __MOUT_ERR__ <<
"\n" << ss.str();
215 throw std::runtime_error(ss.str());
231 auto needleIt = configurationViews_.find(needleVersion);
232 if(needleIt == configurationViews_.end())
235 __SS__ <<
"needleVersion does not exist: " <<
236 needleVersion << std::endl;
237 __MOUT_ERR__ <<
"\n" << ss.str();
238 throw std::runtime_error(ss.str());
242 unsigned int rows = needleView->getNumberOfRows();
243 unsigned int cols = needleView->getNumberOfColumns();
248 for(
auto &viewPair: configurationViews_)
250 if(viewPair.first == needleVersion)
continue;
251 if(viewPair.first == ignoreVersion)
continue;
253 if(viewPair.second.getNumberOfRows() != rows)
256 if(viewPair.second.getSourceColumnSize() != cols ||
257 viewPair.second.getSourceColumnMismatch() != 0)
263 auto srcColNameIt = viewPair.second.getSourceColumnNames().begin();
264 for(
unsigned int col=0; match &&
265 col<viewPair.second.getSourceColumnNames().size()-3;++col,srcColNameIt++)
267 needleView->getColumnsInfo()[col].getName())
270 for(
unsigned int row=0;match && row<rows;++row)
272 for(
unsigned int col=0;col<cols-2;++col)
273 if(viewPair.second.getDataView()[row][col] !=
274 needleView->getDataView()[row][col])
275 { match =
false;
break; }
279 __MOUT_INFO__ <<
"Duplicate version found: " << viewPair.first << std::endl;
280 return viewPair.first;
291 if(configurationViews_.find(temporaryVersion) == configurationViews_.end())
293 __SS__ <<
"ERROR: Temporary view version " << temporaryVersion <<
" doesn't exists!" << std::endl;
294 __MOUT_ERR__ <<
"\n" << ss.str();
295 throw std::runtime_error(ss.str());
297 if(version.isInvalid())
299 __SS__ <<
"ERROR: Attempting to create an invalid version " << version <<
300 "! Did you really run out of versions? (this should never happen)" << std::endl;
301 __MOUT_ERR__ <<
"\n" << ss.str();
302 throw std::runtime_error(ss.str());
305 if(configurationViews_.find(version) != configurationViews_.end())
306 __MOUT_WARN__ <<
"WARNING: View version " << version <<
" already exists! Overwriting." << std::endl;
308 configurationViews_[version].copy(configurationViews_[temporaryVersion],
310 configurationViews_[temporaryVersion].getAuthor());
311 setActiveView(version);
312 eraseView(temporaryVersion);
318 return (configurationViews_.find(version) != configurationViews_.end());
324 if(!isStored(version))
327 if(activeConfigurationView_ &&
328 getViewVersion() == version)
331 configurationViews_.erase(version);
337 const std::string& ConfigurationBase::getConfigurationName(
void)
const
339 return configurationName_;
343 const std::string& ConfigurationBase::getConfigurationDescription(
void)
const
345 return configurationDescription_;
351 return getView().getVersion();
358 bool ConfigurationBase::latestAndMockupColumnNumberMismatch(
void)
const
360 std::set<ConfigurationVersion> retSet = getStoredVersions();
361 if(retSet.size() && !retSet.rbegin()->isTemporaryVersion())
363 return configurationViews_.find(*(retSet.rbegin()))->second.getNumberOfColumns() !=
364 mockupConfigurationView_.getNumberOfColumns();
371 std::set<ConfigurationVersion> ConfigurationBase::getStoredVersions(
void)
const
373 std::set<ConfigurationVersion> retSet;
374 for(
auto &configs:configurationViews_)
375 retSet.emplace(configs.first);
383 unsigned int ConfigurationBase::getNumberOfStoredViews(
void)
const
386 for(
auto &viewPair : configurationViews_)
387 if(viewPair.first.isTemporaryVersion())
continue;
388 else if(viewPair.first.isInvalid())
397 __MOUT__ <<
"There is an invalid version now!.. where did it come from?" << std::endl;
406 if(!activeConfigurationView_)
408 __SS__ <<
"activeConfigurationView_ pointer is null! (...likely the active view was not setup properly. Check your system setup.)";
409 throw std::runtime_error(ss.str());
411 return *activeConfigurationView_;
417 if(!activeConfigurationView_)
419 __SS__ <<
"activeConfigurationView_ pointer is null! (...likely the active view was not setup properly. Check your system setup.)";
420 throw std::runtime_error(ss.str());
422 return activeConfigurationView_;
428 return &mockupConfigurationView_;
432 void ConfigurationBase::setConfigurationName(
const std::string &configurationName)
434 configurationName_ = configurationName;
438 void ConfigurationBase::setConfigurationDescription(
const std::string &configurationDescription)
440 configurationDescription_ = configurationDescription;
446 void ConfigurationBase::deactivate()
448 activeConfigurationView_ = 0;
453 bool ConfigurationBase::isActive()
455 return activeConfigurationView_?
true:
false;
461 if(!isStored(version))
464 __SS__ <<
"\nsetActiveView() ERROR: View with version " << version <<
465 " has never been stored before!" << std::endl;
466 __MOUT_ERR__ <<
"\n" << ss.str();
467 throw std::runtime_error(ss.str());
470 activeConfigurationView_ = &configurationViews_[version];
472 if(configurationViews_[version].getVersion() != version)
474 __SS__ <<
"Something has gone very wrong with the version handling!" << std::endl;
475 throw std::runtime_error(ss.str());
492 throw(std::runtime_error)
495 if(sourceView.getNumberOfColumns() !=
496 mockupConfigurationView_.getNumberOfColumns())
498 __SS__ <<
"Error! Number of Columns of source view must match destination mock-up view." <<
499 "Dimension of source is [" << sourceView.getNumberOfColumns() <<
500 "] and of destination mockup is [" <<
501 mockupConfigurationView_.getNumberOfColumns() <<
"]." << std::endl;
502 throw std::runtime_error(ss.str());
506 if(!destinationVersion.isInvalid() &&
507 configurationViews_.find(sourceView.getVersion()) != configurationViews_.end())
509 __SS__ <<
"Error! Asked to copy a view with a conflicting version: " <<
510 sourceView.getVersion() << std::endl;
511 throw std::runtime_error(ss.str());
518 __MOUT__ <<
"Copying from " << sourceView.getTableName() <<
"_v" <<
519 sourceView.getVersion() <<
" to " << getConfigurationName() <<
"_v" <<
520 destinationVersion << std::endl;
524 configurationViews_[destinationVersion].copy(sourceView,destinationVersion,author);
528 __MOUT_ERR__ <<
"Failed to copy from " << sourceView.getTableName() <<
"_v" <<
529 sourceView.getVersion() <<
" to " << getConfigurationName() <<
"_v" <<
530 destinationVersion << std::endl;
531 __MOUT_WARN__ <<
"Deleting the failed destination version " <<
532 destinationVersion << std::endl;
533 eraseView(destinationVersion);
536 return destinationVersion;
549 __MOUT__ <<
"Configuration: " <<
550 getConfigurationName()<< std::endl;
552 __MOUT__ <<
"Num of Views: " <<
553 configurationViews_.size() <<
" (Temporary Views: " <<
554 (configurationViews_.size() - getNumberOfStoredViews()) <<
")" << std::endl;
557 if(tmpVersion.isInvalid()) tmpVersion = ConfigurationVersion::getNextTemporaryVersion();
558 while(isStored(tmpVersion) &&
559 !(tmpVersion = ConfigurationVersion::getNextTemporaryVersion(tmpVersion)).isInvalid());
560 if(isStored(tmpVersion) || tmpVersion.isInvalid())
562 __MOUT_ERR__ <<
"Invalid destination temporary version: " <<
563 destTemporaryViewVersion <<
". Expected next temporary version < " << tmpVersion << std::endl;
564 throw std::runtime_error(
"Invalid temporary version destination");
567 if(sourceViewVersion == ConfigurationVersion::INVALID ||
568 configurationViews_.find(sourceViewVersion) == configurationViews_.end())
570 if(sourceViewVersion != -1)
572 __MOUT_ERR__ <<
"ERROR: sourceViewVersion " << sourceViewVersion <<
" not found" << std::endl;
573 throw std::runtime_error(
"Invalid source version. Version requested is not stored (yet?) or does not exist.");
575 __MOUT__ <<
"Using Mock-up view" << std::endl;
576 configurationViews_[tmpVersion].copy(mockupConfigurationView_,
578 mockupConfigurationView_.getAuthor());
584 configurationViews_[tmpVersion].copy(configurationViews_[sourceViewVersion],
586 configurationViews_[sourceViewVersion].getAuthor());
590 __MOUT_WARN__ <<
"createTemporaryView() Source view failed init(). " <<
591 "This is being ignored (hopefully the new copy is being fixed)." << std::endl;
607 if(configurationViews_.size() != 0 && configurationViews_.begin()->first.isTemporaryVersion())
609 ConfigurationVersion::getNextTemporaryVersion(configurationViews_.begin()->first);
611 tmpVersion = ConfigurationVersion::getNextTemporaryVersion();
614 if(isStored(tmpVersion) || tmpVersion.isInvalid() || !tmpVersion.isTemporaryVersion())
616 __MOUT_ERR__ <<
"Invalid destination temporary version: " <<
617 tmpVersion << std::endl;
618 throw std::runtime_error(
"Invalid temporary version found");
632 if(configurationViews_.size() != 0 && !configurationViews_.rbegin()->first.isTemporaryVersion())
634 ConfigurationVersion::getNextVersion(configurationViews_.rbegin()->first);
636 tmpVersion = ConfigurationVersion::getNextVersion();
639 if(isStored(tmpVersion) || tmpVersion.isInvalid() || tmpVersion.isTemporaryVersion())
641 __MOUT_ERR__ <<
"Invalid destination next version: " <<
642 tmpVersion << std::endl;
643 throw std::runtime_error(
"Invalid next version found");
654 if(!temporaryVersion.isTemporaryVersion() ||
655 !isStored(temporaryVersion))
657 __MOUT_ERR__ << getConfigurationName() <<
":: Error! Temporary version not found!" << std::endl;
658 throw std::runtime_error(
"Invalid temporary version");
660 return &configurationViews_[temporaryVersion];
668 std::string ConfigurationBase::convertToCaps(std::string& str,
bool isConfigName)
669 throw(std::runtime_error)
672 unsigned int configPos = (
unsigned int)std::string::npos;
673 if(isConfigName && (configPos = str.find(
"Configuration")) !=
674 str.size() - strlen(
"Configuration"))
675 str +=
"Configuration";
682 std::string capsStr =
"";
683 for(
unsigned int c=0;c<str.size();++c)
684 if(str[c] >=
'A' && str[c] <=
'Z')
688 (c && str[c-1] >=
'a' && str[c-1] <=
'z') ||
689 (c && str[c-1] >=
'A' && str[c-1] <=
'Z' &&
690 c+1 < str.size() && str[c+1] >=
'a' && str[c+1] <=
'z')
695 else if(str[c] >=
'a' && str[c] <=
'z')
696 capsStr += char(str[c] -32);
697 else if(str[c] >=
'0' && str[c] <=
'9')
700 throw std::runtime_error(std::string(
"ConfigurationBase::convertToCaps::") +
701 "Invalid character found in name (allowed: A-Z, a-z, 0-9):" +