$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include <dirent.h> 00002 #include <cassert> 00003 #include <iostream> 00004 #include <memory> 00005 #include <string> 00006 00007 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h" 00008 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h" 00009 //#include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h" 00010 //#include "artdaq-database/JsonDocument/JSONDocument.h" 00011 00012 // usage: 00013 // otsdaq_flatten_system_aliases <baseFlatVersion> <pathToSwapIn (optional)> 00014 // 00015 // if baseFlatVersion is invalid or temporary nothing is saved in the new db 00016 // (Note: this can be used to swap dbs using pathToSwapIn) 00017 00018 using namespace ots; 00019 00020 void FixNewTableFields(int argc, char* argv[]) 00021 { 00022 std::cout << "=================================================\n"; 00023 std::cout << "=================================================\n"; 00024 std::cout << "=================================================\n"; 00025 __COUT__ << "\nFixing new table fields!" << __E__; 00026 00027 std::cout << "\n\nusage: Two arguments:\n\t <pathToSwapIn (optional)> \n\n" 00028 << "\t Default values: pathToSwapIn = \"\" \n\n" 00029 << __E__; 00030 00031 std::cout << "\n\nNote: This assumes artdaq db file type interface. " 00032 << "The current database/ will be moved to database_<linuxtime>/ " 00033 << "unless a pathToSwapIn is specified, in which case the path will " 00034 << "be copied overwriting database/ \n\n" 00035 << __E__; 00036 00037 std::cout << "argc = " << argc << __E__; 00038 for(int i = 0; i < argc; i++) 00039 std::cout << "argv[" << i << "] = " << argv[i] << __E__; 00040 00041 if(argc > 2) 00042 { 00043 std::cout << "Error! Must provide at most one parameter.\n\n" << __E__; 00044 return; 00045 } 00046 00047 // determine if "h"elp was first parameter 00048 std::string pathToSwapIn = ""; 00049 if(argc >= 2) 00050 pathToSwapIn = argv[1]; 00051 00052 if(pathToSwapIn == "-h" || pathToSwapIn == "--help") 00053 { 00054 std::cout 00055 << "Recognized parameter 1 as a 'help' option. Usage was printed. Exiting." 00056 << __E__; 00057 return; 00058 } 00059 00060 __COUTV__(pathToSwapIn); 00061 00062 // return; 00063 //============================================================================== 00064 // Define environment variables 00065 // Note: normally these environment variables are set by StartOTS.sh 00066 00067 // These are needed by 00068 // otsdaq/otsdaq-core/ConfigurationDataFormats/ConfigurationInfoReader.cc [207] 00069 setenv("CONFIGURATION_TYPE", "File", 1); // Can be File, Database, DatabaseTest 00070 setenv("CONFIGURATION_DATA_PATH", 00071 (std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(), 00072 1); 00073 setenv( 00074 "TABLE_INFO_PATH", (std::string(getenv("USER_DATA")) + "/TableInfo").c_str(), 1); 00076 00077 // Some configuration plug-ins use getenv("SERVICE_DATA_PATH") in init() so define it 00078 setenv("SERVICE_DATA_PATH", 00079 (std::string(getenv("USER_DATA")) + "/ServiceData").c_str(), 00080 1); 00081 00082 // Some configuration plug-ins use getenv("OTSDAQ_LIB") and 00083 // getenv("OTSDAQ_UTILITIES_LIB") in init() so define it to a non-sense place is ok 00084 setenv("OTSDAQ_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); 00085 setenv("OTSDAQ_UTILITIES_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); 00086 00087 // Some configuration plug-ins use getenv("OTS_MAIN_PORT") in init() so define it 00088 setenv("OTS_MAIN_PORT", "2015", 1); 00089 00090 // also xdaq envs for XDAQContextTable 00091 setenv("XDAQ_CONFIGURATION_DATA_PATH", 00092 (std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(), 00093 1); 00094 setenv("XDAQ_CONFIGURATION_XML", "otsConfigurationNoRU_CMake", 1); 00096 00097 //============================================================================== 00098 // get prepared with initial source db 00099 00100 // ConfigurationManager instance immediately loads active groups 00101 __COUT__ << "Loading active Aliases..." << __E__; 00102 ConfigurationManagerRW cfgMgrInst("flatten_admin"); 00103 ConfigurationManagerRW* cfgMgr = &cfgMgrInst; 00104 00105 // create set of groups to persist 00106 // include active context 00107 // include active backbone 00108 // include active iterate group 00109 // include active config group 00110 // (keep key translation separate activeGroupKeys) 00111 // include all groups with system aliases 00112 00113 // for group in set 00114 // load/activate group and flatten tables to flatVersion to new DB 00115 // save new version to modifiedTables 00116 // save group with flatVersion key to new DB 00117 // save new key to groupSet 00118 // ++flatVersion 00119 00120 // reload the active backbone (using activeGroupKeys) 00121 // modify group aliases and table aliases properly based on groupSet and 00122 // modifiedTables save new backbone with flatVersion to new DB 00123 00124 // backup the file ConfigurationManager::ACTIVE_GROUPS_FILENAME with time 00125 // and change the ConfigurationManager::ACTIVE_GROUPS_FILENAME 00126 // to reflect new group names/keys 00127 00128 /* map<<groupName, origKey>, newKey> */ 00129 std::map<std::pair<std::string, TableGroupKey>, TableGroupKey> groupSet; 00130 /* <tableName, <origVersion, newVersion> >*/ 00131 std::map<std::pair<std::string, TableVersion>, TableVersion> modifiedTables; 00132 std::map<std::string, std::pair<TableGroupKey, TableGroupKey>> activeGroupKeys; 00133 std::map<std::pair<std::string, TableGroupKey>, std::string> groupErrors; 00134 00135 std::string activeBackboneGroupName = ""; 00136 std::string activeContextGroupName = ""; 00137 std::string activeIterateGroupName = ""; 00138 std::string activeConfigGroupName = ""; 00139 00140 std::string nowTime = std::to_string(time(0)); 00141 00142 std::string thenTime = ""; 00143 if(pathToSwapIn != "") // get target then time 00144 { 00145 thenTime = pathToSwapIn.substr(pathToSwapIn.rfind('_') + 1); 00146 __COUT__ << "thenTime = " << thenTime << __E__; 00147 // return; 00148 } 00149 00150 // add active groups to set 00151 std::map<std::string, std::pair<std::string, TableGroupKey>> activeGroupsMap = 00152 cfgMgr->getActiveTableGroups(); 00153 00154 for(const auto& activeGroup : activeGroupsMap) 00155 { 00156 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>, TableGroupKey>( 00157 std::pair<std::string, TableGroupKey>(activeGroup.second.first, 00158 activeGroup.second.second), 00159 TableGroupKey())); 00160 activeGroupKeys.insert( 00161 std::pair<std::string, std::pair<TableGroupKey, TableGroupKey>>( 00162 activeGroup.second.first, 00163 std::pair<TableGroupKey, TableGroupKey>(activeGroup.second.second, 00164 TableGroupKey()))); 00165 00166 if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE) 00167 { 00168 activeBackboneGroupName = activeGroup.second.first; 00169 __COUT__ << "found activeBackboneGroupName = " << activeBackboneGroupName 00170 << __E__; 00171 } 00172 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_CONTEXT) 00173 { 00174 activeContextGroupName = activeGroup.second.first; 00175 __COUT__ << "found activeContextGroupName = " << activeContextGroupName 00176 << __E__; 00177 } 00178 else if(activeGroup.first == ConfigurationManager::ACTIVE_GROUP_NAME_ITERATE) 00179 { 00180 activeIterateGroupName = activeGroup.second.first; 00181 __COUT__ << "found activeIterateGroupName = " << activeIterateGroupName 00182 << __E__; 00183 } 00184 else if(activeGroup.first == 00185 ConfigurationManager::ACTIVE_GROUP_NAME_CONFIGURATION) 00186 { 00187 activeConfigGroupName = activeGroup.second.first; 00188 __COUT__ << "found activeConfigGroupName = " << activeConfigGroupName 00189 << __E__; 00190 } 00191 } 00192 00193 // add system alias groups to set 00194 const std::string groupAliasesTableName = 00195 ConfigurationManager::GROUP_ALIASES_TABLE_NAME; 00196 std::map<std::string, TableVersion> activeVersions = cfgMgr->getActiveVersions(); 00197 if(activeVersions.find(groupAliasesTableName) == activeVersions.end()) 00198 { 00199 __SS__ << "\nActive version of " << groupAliasesTableName << " missing! " 00200 << groupAliasesTableName 00201 << " is a required member of the Backbone configuration group." 00202 << "\n\nLikely you need to activate a valid Backbone group." << __E__; 00203 __SS_THROW__; 00204 } 00205 00206 std::vector<std::pair<std::string, ConfigurationTree>> aliasNodePairs = 00207 cfgMgr->getNode(groupAliasesTableName).getChildren(); 00208 for(auto& groupPair : aliasNodePairs) 00209 groupSet.insert(std::pair<std::pair<std::string, TableGroupKey>, TableGroupKey>( 00210 std::pair<std::string, TableGroupKey>( 00211 groupPair.second.getNode("GroupName").getValueAsString(), 00212 TableGroupKey(groupPair.second.getNode("GroupKey").getValueAsString())), 00213 TableGroupKey())); 00214 00215 __COUT__ << "Identified groups:" << __E__; 00216 for(auto& group : groupSet) 00217 __COUT__ << group.first.first << " " << group.first.second << __E__; 00218 __COUT__ << __E__; 00219 __COUT__ << __E__; 00220 00221 // return; 00222 //============================================================================== 00223 // prepare to manipulate directories 00224 std::string currentDir = getenv("ARTDAQ_DATABASE_URI"); 00225 00226 if(currentDir.find("filesystemdb://") != 0) 00227 { 00228 __SS__ << "filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << __E__; 00229 __SS_THROW__; 00230 } 00231 00232 currentDir = currentDir.substr(std::string("filesystemdb://").length()); 00233 while(currentDir.length() && 00234 currentDir[currentDir.length() - 1] == '/') // remove trailing '/'s 00235 currentDir = currentDir.substr(0, currentDir.length() - 1); 00236 std::string moveToDir = currentDir + "_" + nowTime; 00237 00238 if(pathToSwapIn != "") 00239 { 00240 DIR* dp; 00241 if((dp = opendir(pathToSwapIn.c_str())) == 0) 00242 { 00243 __COUT__ << "ERROR:(" << errno << "). Can't open directory: " << pathToSwapIn 00244 << __E__; 00245 exit(0); 00246 } 00247 closedir(dp); 00248 } 00249 00250 // handle directory swap 00251 __COUT__ << "Copying current directory: \t" << currentDir << __E__; 00252 __COUT__ << "\t... to: \t\t" << moveToDir << __E__; 00253 // return; 00254 rename(currentDir.c_str(), moveToDir.c_str()); 00255 00256 if(pathToSwapIn != "") // move the swap in directory in 00257 { 00258 __COUT__ << "Swapping in directory: \t" << pathToSwapIn << __E__; 00259 __COUT__ << "\t.. to: \t\t" << currentDir << __E__; 00260 rename(pathToSwapIn.c_str(), currentDir.c_str()); 00261 00262 // also swap in active groups file 00263 // check if original active file exists 00264 std::string activeGroupsFile = 00265 ConfigurationManager::ACTIVE_GROUPS_FILENAME + "." + thenTime; 00266 FILE* fp = fopen(activeGroupsFile.c_str(), "r"); 00267 if(fp) 00268 { 00269 __COUT__ << "Swapping active groups file: \t" << activeGroupsFile << __E__; 00270 __COUT__ << "\t.. to: \t\t" << ConfigurationManager::ACTIVE_GROUPS_FILENAME 00271 << __E__; 00272 rename(activeGroupsFile.c_str(), 00273 ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str()); 00274 } 00275 00276 __COUT__ << "Path swapped in. Done." << __E__; 00277 return; 00278 } 00279 else // copy the bkup back 00280 std::system(("cp -r " + moveToDir + " " + currentDir).c_str()); 00281 00282 // return; 00283 00284 //============= 00285 // now ready to save each member table of active groups 00286 // to new version fixing column names. 00287 00288 // int flatVersion = 0; 00289 00290 // ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance(false); 00291 // //true for File interface, false for artdaq database; 00292 TableView* cfgView; 00293 TableBase* config; 00294 00295 bool errDetected = false; 00296 std::string accumulateErrors = ""; 00297 int count = 0; // for number of groups converted successfully 00298 00299 std::map<std::string /*name*/, TableVersion> memberMap; 00300 std::map<std::string /*name*/, std::string /*alias*/> groupAliases; 00301 std::string groupComment; 00302 // std::string groupAuthor; 00303 // std::string groupCreateTime; 00304 // time_t groupCreateTime_t; 00305 // TableBase* groupMetadataTable = cfgMgr->getMetadataTable(); 00306 00307 // //don't do anything more if flatVersion is not persistent 00308 // if(TableVersion(flatVersion).isInvalid() || 00309 // TableVersion(flatVersion).isTemporaryVersion()) 00310 // { 00311 // __COUT__<< "\n\nflatVersion " << TableVersion(flatVersion) << 00312 // " is an invalid or temporary version. Skipping to end!" << __E__; 00313 // goto CLEAN_UP; 00314 // } 00315 00316 for(auto& groupPair : groupSet) 00317 { 00318 errDetected = false; 00319 00320 __COUT__ << "****************************" << __E__; 00321 __COUT__ << "Loading members for " << groupPair.first.first << "(" 00322 << groupPair.first.second << ")" << __E__; 00323 00324 //========================= 00325 // load group, group metadata, and tables from original DB 00326 try 00327 { 00328 cfgMgr->loadTableGroup(groupPair.first.first, 00329 groupPair.first.second, 00330 false /*doActivate*/, 00331 &memberMap /*memberMap*/, 00332 0 /*progressBar*/, 00333 &accumulateErrors, 00334 &groupComment, 00335 0, //&groupAuthor, 00336 0, //&groupCreateTime, 00337 false /*doNotLoadMember*/, 00338 0 /*groupTypeString*/, 00339 &groupAliases); 00340 } 00341 catch(std::runtime_error& e) 00342 { 00343 __COUT__ << "Error was caught loading members for " << groupPair.first.first 00344 << "(" << groupPair.first.second << ")" << __E__; 00345 __COUT__ << e.what() << __E__; 00346 errDetected = true; 00347 } 00348 catch(...) 00349 { 00350 __COUT__ << "Error was caught loading members for " << groupPair.first.first 00351 << "(" << groupPair.first.second << ")" << __E__; 00352 errDetected = true; 00353 } 00354 00355 //========================= 00356 00357 // note error if any (loading) failure 00358 if(errDetected) 00359 { 00360 // power on if group failed 00361 // and record error 00362 00363 groupErrors.insert( 00364 std::pair<std::pair<std::string, TableGroupKey>, std::string>( 00365 std::pair<std::string, TableGroupKey>(groupPair.first.first, 00366 groupPair.first.second), 00367 "Error caught loading the group.")); 00368 continue; 00369 } 00370 00371 //========================= 00372 // save group and its tables with new key and versions! 00373 try 00374 { 00375 __COUT__ << "Before member map: " << StringMacros::mapToString(memberMap) 00376 << __E__; 00377 00378 // saving tables 00379 for(auto& memberPair : memberMap) 00380 { 00381 __COUT__ << memberPair.first << ":v" << memberPair.second << __E__; 00382 00383 // check if table has already been modified by a previous group 00384 // (i.e. two groups using the same version of a table) 00385 if(modifiedTables.find(std::pair<std::string, TableVersion>( 00386 memberPair.first, memberPair.second)) != modifiedTables.end()) 00387 { 00388 __COUT__ << "Table was already modified!" << __E__; 00389 memberPair.second = 00390 modifiedTables[std::pair<std::string, TableVersion>( 00391 memberPair.first, memberPair.second)]; 00392 __COUT__ << "\t to...\t" << memberPair.first << ":v" 00393 << memberPair.second << __E__; 00394 continue; 00395 } 00396 00397 // save new version, and then record new version in map 00398 00399 // first copy to new column names 00400 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns( 00401 memberPair.first /*table name*/, memberPair.second /*source version*/ 00402 ); 00403 00404 // then save temporary to persistent version 00405 TableVersion persistentVersion = cfgMgr->saveNewTable( 00406 memberPair.first /*table name*/, temporaryVersion); 00407 00408 // //change the version of the active view to flatVersion and 00409 // save it config = 00410 // cfgMgr->getTableByName(memberPair.first); cfgView = 00411 // config->getViewP(); 00412 // cfgView->setVersion(TableVersion(flatVersion)); 00413 // theInterface_->saveActiveVersion(config); 00414 // 00415 // //set it back for the table so that future groups can 00416 // re-use cached version 00417 // cfgView->setVersion(memberPair.second); 00419 00420 // save new version to modifiedTables 00421 modifiedTables.insert( 00422 std::pair<std::pair<std::string, TableVersion>, TableVersion>( 00423 std::pair<std::string, TableVersion>(memberPair.first, 00424 memberPair.second), 00425 persistentVersion)); 00426 00427 memberPair.second = persistentVersion; // change version in the member 00428 // map 00429 00430 __COUT__ << "\t to...\t" << memberPair.first << ":v" << memberPair.second 00431 << __E__; 00432 } // end table member loop 00433 00434 // now save new group 00435 __COUT__ << "After member map: " << StringMacros::mapToString(memberMap) 00436 << __E__; 00437 00438 // return; 00439 00440 TableGroupKey newGroupKey = 00441 cfgMgr->saveNewTableGroup(groupPair.first.first /*groupName*/, 00442 memberMap, 00443 groupComment, 00444 &groupAliases); 00445 00446 // 00447 // 00448 // 00449 // //Note: this code copies actions in 00450 // ConfigurationManagerRW::saveNewTableGroup 00451 // 00452 // //add meta data 00453 // __COUTV__(StringMacros::mapToString(groupAliases)); 00454 // __COUTV__(groupComment); 00455 // __COUTV__(groupAuthor); 00456 // __COUTV__(groupCreateTime); 00457 // sscanf(groupCreateTime.c_str(),"%ld",&groupCreateTime_t); 00458 // __COUTV__(groupCreateTime_t); 00459 // 00460 // //to compensate for unusual errors upstream, make sure the 00461 // metadata table has one row 00462 // while(groupMetadataTable->getViewP()->getNumberOfRows() > 1) 00463 // groupMetadataTable->getViewP()->deleteRow(0); 00464 // if(groupMetadataTable->getViewP()->getNumberOfRows() == 0) 00465 // groupMetadataTable->getViewP()->addRow(); 00466 // 00467 // //columns are uid,comment,author,time 00468 // //ConfigurationManager::METADATA_COL_ALIASES TODO 00469 // groupMetadataTable->getViewP()->setValue( 00470 // StringMacros::mapToString(groupAliases, 00471 // "," /*primary delimiter*/,":" /*secondary 00472 // delimeter*/), 00473 // 0,ConfigurationManager::METADATA_COL_ALIASES); 00474 // groupMetadataTable->getViewP()->setValue(groupComment 00475 //,0,ConfigurationManager::METADATA_COL_COMMENT); 00476 // groupMetadataTable->getViewP()->setValue(groupAuthor 00477 //,0,ConfigurationManager::METADATA_COL_AUTHOR); 00478 // groupMetadataTable->getViewP()->setValue(groupCreateTime_t 00479 //,0,ConfigurationManager::METADATA_COL_TIMESTAMP); 00480 // 00481 // //set version of metadata table 00482 // groupMetadataTable->getViewP()->setVersion(TableVersion(flatVersion)); 00483 // theInterface_->saveActiveVersion(groupMetadataTable); 00484 // 00485 // //force groupMetadataTable_ to be a member for the group 00486 // memberMap[groupMetadataTable->getTableName()] = 00487 // groupMetadataTable->getViewVersion(); 00488 // 00489 // //memberMap should now consist of members with new flat version, 00490 // so save group theInterface_->saveTableGroup(memberMap, 00491 // TableGroupKey::getFullGroupString( 00492 // groupPair.first.first, 00493 // TableGroupKey(flatVersion))); 00494 // 00495 00496 // and modify groupSet and activeGroupKeys keys 00497 groupPair.second = TableGroupKey(newGroupKey); 00498 00499 // if this is an active group, save key change 00500 if(activeGroupKeys.find(groupPair.first.first) != activeGroupKeys.end() && 00501 activeGroupKeys[groupPair.first.first].first == groupPair.first.second) 00502 activeGroupKeys[groupPair.first.first].second = 00503 TableGroupKey(newGroupKey); 00504 } 00505 catch(std::runtime_error& e) 00506 { 00507 __COUT__ << "Error was caught saving group " << groupPair.first.first << " (" 00508 << groupPair.first.second << ") " << __E__; 00509 __COUT__ << e.what() << __E__; 00510 00511 groupErrors.insert( 00512 std::pair<std::pair<std::string, TableGroupKey>, std::string>( 00513 std::pair<std::string, TableGroupKey>(groupPair.first.first, 00514 groupPair.first.second), 00515 "Error caught saving the group.")); 00516 } 00517 catch(...) 00518 { 00519 __COUT__ << "Error was caught saving group " << groupPair.first.first << " (" 00520 << groupPair.first.second << ") " << __E__; 00521 00522 groupErrors.insert( 00523 std::pair<std::pair<std::string, TableGroupKey>, std::string>( 00524 std::pair<std::string, TableGroupKey>(groupPair.first.first, 00525 groupPair.first.second), 00526 "Error caught saving the group.")); 00527 } 00528 //========================= 00529 00530 ++count; 00531 } // end group loop 00532 00533 // record in readme of moveToDir 00534 { 00535 FILE* fp = fopen((moveToDir + "/README_fix_new_table_fields.txt").c_str(), "a"); 00536 if(!fp) 00537 __COUT__ << "\tError opening README file!" << __E__; 00538 else 00539 { 00540 time_t rawtime; 00541 struct tm* timeinfo; 00542 char buffer[200]; 00543 00544 time(&rawtime); 00545 timeinfo = localtime(&rawtime); 00546 strftime(buffer, 200, "%b %d, %Y %I:%M%p %Z", timeinfo); 00547 00548 fprintf(fp, 00549 "This database %s \n\t is a backup of %s \n\t BEFORE forcing to new " 00550 "table fields \n\t and was created at this time \n\t %lu \t %s\n\n\n", 00551 currentDir.c_str(), 00552 moveToDir.c_str(), 00553 time(0), 00554 buffer); 00555 00556 fclose(fp); 00557 } 00558 } 00559 00560 // record in readme for currentDir 00561 { 00562 FILE* fp = fopen((currentDir + "/README_otsdaq_flatten.txt").c_str(), "a"); 00563 00564 if(!fp) 00565 __COUT__ << "\tError opening README file!" << __E__; 00566 else 00567 { 00568 time_t rawtime; 00569 struct tm* timeinfo; 00570 char buffer[200]; 00571 00572 time(&rawtime); 00573 timeinfo = localtime(&rawtime); 00574 strftime(buffer, 200, "%b %d, %Y %I:%M:%S%p %Z", timeinfo); 00575 00576 fprintf(fp, 00577 "This database %s \n\t was forced to new table fields \n\t at this " 00578 "time \n\t %lu \t %s\n\n\n", 00579 currentDir.c_str(), 00580 time(0), 00581 buffer); 00582 00583 fclose(fp); 00584 } 00585 } 00586 00587 // //print resulting all groups 00588 // 00589 // __COUT__ << "Resulting Groups:" << __E__; 00590 // for(const auto &group: groupSet) 00591 // __COUT__<< group.first.first << ": " << 00592 // group.first.second << " => " << group.second << __E__; 00593 // __COUT__ << "Resulting Groups end." << __E__; 00594 // 00595 // 00596 // //print resulting active groups 00597 // 00598 // __COUT__ << "Resulting Active Groups:" << __E__; 00599 // for(const auto &activeGroup: activeGroupKeys) 00600 // __COUT__<< activeGroup.first << ": " << 00601 // activeGroup.second.first << " => " << activeGroup.second.second << __E__; 00602 // 00603 // __COUT__<< activeBackboneGroupName << " is the " << 00604 // ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE << "." << __E__; 00605 // __COUT__ << "Resulting Active Groups end." << __E__; 00606 00607 // reload the active backbone (using activeGroupKeys) 00608 // modify group aliases and table aliases properly based on groupSet and 00609 // modifiedTables save new backbone with flatVersion to new DB 00610 00611 if(activeBackboneGroupName == "") 00612 { 00613 __COUT__ << "No active Backbone table identified." << __E__; 00614 goto CLEAN_UP; 00615 } 00616 00617 __COUT__ << "Modifying the active Backbone table to reflect new table versions and " 00618 "group keys." 00619 << __E__; 00620 00621 { // start active backbone group handling 00622 00623 cfgMgr->loadTableGroup(activeBackboneGroupName, 00624 activeGroupKeys[activeBackboneGroupName].second, 00625 true /*doActivate*/, 00626 &memberMap, 00627 0 /*progressBar*/, 00628 &accumulateErrors, 00629 &groupComment); 00630 00631 // modify Group Aliases Table and Version Aliases Table to point 00632 // at DEFAULT and flatVersion respectively 00633 00634 const std::string groupAliasesName = 00635 ConfigurationManager::GROUP_ALIASES_TABLE_NAME; 00636 const std::string versionAliasesName = 00637 ConfigurationManager::VERSION_ALIASES_TABLE_NAME; 00638 00639 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions(); 00640 00641 // modify Group Aliases Table 00642 if(activeMap.find(groupAliasesName) != activeMap.end()) 00643 { 00644 __COUT__ << "\n\nModifying " << groupAliasesName << __E__; 00645 00646 // now save new group 00647 __COUT__ << "Before member map: " << StringMacros::mapToString(memberMap) 00648 << __E__; 00649 00650 // save new Group Aliases table and Version Aliases table 00651 // first save new group aliases table 00652 __COUT__ << groupAliasesName << ":v" << memberMap[groupAliasesName] << __E__; 00653 00654 // first copy to new column names 00655 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns( 00656 groupAliasesName /*table name*/, 00657 memberMap[groupAliasesName] /*source version*/ 00658 ); 00659 00660 config = cfgMgr->getTableByName(groupAliasesName); 00661 config->setActiveView(temporaryVersion); 00662 cfgView = config->getViewP(); 00663 00664 unsigned int col1 = cfgView->findCol("GroupName"); 00665 unsigned int col2 = cfgView->findCol("GroupKey"); 00666 00667 cfgView->print(); 00668 00669 // change all key entries found to the new key and delete rows for groups not 00670 // found 00671 bool found; 00672 for(unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row) 00673 { 00674 found = false; 00675 for(const auto& group : groupSet) 00676 if(group.second.isInvalid()) 00677 continue; 00678 else if(cfgView->getDataView()[row][col1] == group.first.first && 00679 cfgView->getDataView()[row][col2] == 00680 group.first.second.toString()) 00681 { 00682 // found a matching group/key pair 00683 __COUT__ << "Changing row " << row << " for " 00684 << cfgView->getDataView()[row][col1] 00685 << " key=" << cfgView->getDataView()[row][col2] 00686 << " to NEW key=" << group.second << __E__; 00687 cfgView->setValue(group.second.toString(), row, col2); 00688 found = true; 00689 break; 00690 } 00691 00692 if(!found) // delete row 00693 cfgView->deleteRow(row--); 00694 } 00695 00696 cfgView->print(); 00697 00698 // then save temporary to persistent version 00699 TableVersion persistentVersion = 00700 cfgMgr->saveNewTable(groupAliasesName /*table name*/, temporaryVersion); 00701 00702 // //change the version of the active view to flatVersion and save it 00703 // config = cfgMgr->getTableByName(groupAliasesName); 00704 // cfgView = config->getViewP(); 00705 // cfgView->setVersion(TableVersion(flatVersion)); 00706 // theInterface_->saveActiveVersion(config); 00707 00708 memberMap[groupAliasesName] = 00709 persistentVersion; // change version in the member map 00710 00711 __COUT__ << "\t to...\t" << groupAliasesName << ":v" 00712 << memberMap[groupAliasesName] << __E__; 00713 00714 } // done modifying group aliases 00715 00716 // modify Version Aliases Table 00717 if(activeMap.find(versionAliasesName) != activeMap.end()) 00718 { 00719 __COUT__ << "\n\nModifying " << versionAliasesName << __E__; 00720 00721 // first save new version aliases table 00722 __COUT__ << versionAliasesName << ":v" << memberMap[versionAliasesName] 00723 << __E__; 00724 00725 // first copy to new column names 00726 TableVersion temporaryVersion = cfgMgr->copyViewToCurrentColumns( 00727 versionAliasesName /*table name*/, 00728 memberMap[versionAliasesName] /*source version*/ 00729 ); 00730 00731 config = cfgMgr->getTableByName(versionAliasesName); 00732 config->setActiveView(temporaryVersion); 00733 cfgView = config->getViewP(); 00734 unsigned int col1 = cfgView->findCol("TableName"); 00735 unsigned int col2 = cfgView->findCol("Version"); 00736 00737 // change all version entries to the new version and delete rows with no match 00738 bool found; 00739 for(unsigned int row = 0; row < cfgView->getNumberOfRows(); ++row) 00740 { 00741 found = false; 00742 for(const auto& table : modifiedTables) 00743 if(cfgView->getDataView()[row][col1] == table.first.first && 00744 cfgView->getDataView()[row][col2] == table.first.second.toString()) 00745 { 00746 // found a matching group/key pair 00747 __COUT__ << "Changing row " << row << " for " 00748 << cfgView->getDataView()[row][col1] 00749 << " version=" << cfgView->getDataView()[row][col2] 00750 << " to NEW version=" << table.second << __E__; 00751 cfgView->setValue(table.second.toString(), row, col2); 00752 found = true; 00753 break; 00754 } 00755 00756 if(!found) // delete row 00757 cfgView->deleteRow(row--); 00758 } 00759 00760 // then save temporary to persistent version 00761 TableVersion persistentVersion = 00762 cfgMgr->saveNewTable(versionAliasesName /*table name*/, temporaryVersion); 00763 00764 // //change the version of the active view to flatVersion and save it 00765 // config = cfgMgr->getTableByName(versionAliasesName); 00766 // cfgView = config->getViewP(); 00767 // cfgView->setVersion(TableVersion(flatVersion)); 00768 // theInterface_->saveActiveVersion(config); 00769 00770 memberMap[versionAliasesName] = 00771 persistentVersion; // change version in the member map 00772 00773 __COUT__ << "\t to...\t" << versionAliasesName << ":v" 00774 << memberMap[versionAliasesName] << __E__; 00775 00776 } // done modifying version aliases 00777 00778 // now save new group 00779 __COUT__ << "After member map: " << StringMacros::mapToString(memberMap) << __E__; 00780 00781 TableGroupKey newGroupKey = cfgMgr->saveNewTableGroup( 00782 activeBackboneGroupName /*groupName*/, 00783 memberMap, 00784 groupComment, 00785 0 /*groupAliases*/); // Do we need groupAliases for backbone here? 00786 00787 // TableGroupKey cfgMgr->saveNewTableGroup 00788 // theInterface_->saveTableGroup(memberMap, 00789 // TableGroupKey::getFullGroupString( 00790 // activeBackboneGroupName, 00791 // TableGroupKey(flatVersion))); 00792 00793 activeGroupKeys[activeBackboneGroupName].second = TableGroupKey(newGroupKey); 00794 00795 __COUT__ << "New to-be-active backbone group " << activeBackboneGroupName << ":v" 00796 << activeGroupKeys[activeBackboneGroupName].second << __E__; 00797 } // end active backbone group handling 00798 00799 // backup the file ConfigurationManager::ACTIVE_GROUPS_FILENAME with time 00800 // and change the ConfigurationManager::ACTIVE_GROUPS_FILENAME 00801 // to reflect new group names/keys 00802 00803 { 00804 __COUT__ << "Manipulating the Active Groups file..." << __E__; 00805 00806 // check if original active file exists 00807 FILE* fp = fopen(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), "r"); 00808 if(!fp) 00809 { 00810 __SS__ << "Original active groups file '" 00811 << ConfigurationManager::ACTIVE_GROUPS_FILENAME << "' not found." 00812 << __E__; 00813 goto CLEAN_UP; 00814 } 00815 00816 __COUT__ << "Backing up file: " << ConfigurationManager::ACTIVE_GROUPS_FILENAME 00817 << __E__; 00818 00819 fclose(fp); 00820 00821 std::string renameFile = 00822 ConfigurationManager::ACTIVE_GROUPS_FILENAME + "." + nowTime; 00823 rename(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), renameFile.c_str()); 00824 00825 __COUT__ << "Backup file name: " << renameFile << __E__; 00826 00827 TableGroupKey *theConfigurationTableGroupKey_, *theContextTableGroupKey_, 00828 *theBackboneTableGroupKey_, *theIterateTableGroupKey_; 00829 std::string theConfigurationTableGroup_, theContextTableGroup_, 00830 theBackboneTableGroup_, theIterateTableGroup_; 00831 00832 theConfigurationTableGroup_ = activeConfigGroupName; 00833 theConfigurationTableGroupKey_ = &(activeGroupKeys[activeConfigGroupName].second); 00834 00835 theContextTableGroup_ = activeContextGroupName; 00836 theContextTableGroupKey_ = &(activeGroupKeys[activeContextGroupName].second); 00837 00838 theBackboneTableGroup_ = activeBackboneGroupName; 00839 theBackboneTableGroupKey_ = &(activeGroupKeys[activeBackboneGroupName].second); 00840 00841 theIterateTableGroup_ = activeIterateGroupName; 00842 theIterateTableGroupKey_ = &(activeGroupKeys[activeIterateGroupName].second); 00843 00844 // the following is copied from ConfigurationManagerRW::activateTableGroup 00845 { 00846 __COUT__ << "Updating persistent active groups to " 00847 << ConfigurationManager::ACTIVE_GROUPS_FILENAME << " ..." << __E__; 00848 00849 std::string fn = ConfigurationManager::ACTIVE_GROUPS_FILENAME; 00850 FILE* fp = fopen(fn.c_str(), "w"); 00851 if(!fp) 00852 return; 00853 00854 fprintf(fp, "%s\n", theContextTableGroup_.c_str()); 00855 fprintf(fp, 00856 "%s\n", 00857 theContextTableGroupKey_ 00858 ? theContextTableGroupKey_->toString().c_str() 00859 : "-1"); 00860 fprintf(fp, "%s\n", theBackboneTableGroup_.c_str()); 00861 fprintf(fp, 00862 "%s\n", 00863 theBackboneTableGroupKey_ 00864 ? theBackboneTableGroupKey_->toString().c_str() 00865 : "-1"); 00866 fprintf(fp, "%s\n", theConfigurationTableGroup_.c_str()); 00867 fprintf(fp, 00868 "%s\n", 00869 theConfigurationTableGroupKey_ 00870 ? theConfigurationTableGroupKey_->toString().c_str() 00871 : "-1"); 00872 fprintf(fp, "%s\n", theIterateTableGroup_.c_str()); 00873 fprintf(fp, 00874 "%s\n", 00875 theIterateTableGroupKey_ 00876 ? theIterateTableGroupKey_->toString().c_str() 00877 : "-1"); 00878 fclose(fp); 00879 } 00880 } 00881 00882 // print resulting all groups 00883 00884 __COUT__ << "Resulting Groups:" << __E__; 00885 for(const auto& group : groupSet) 00886 __COUT__ << "\t" << group.first.first << ": " << group.first.second << " => " 00887 << group.second << __E__; 00888 __COUT__ << "Resulting Groups end." << __E__; 00889 00890 // print resulting active groups 00891 00892 __COUT__ << "Resulting Active Groups:" << __E__; 00893 for(const auto& activeGroup : activeGroupKeys) 00894 __COUT__ << "\t" << activeGroup.first << ": " << activeGroup.second.first 00895 << " => " << activeGroup.second.second << __E__; 00896 00897 __COUT__ << activeBackboneGroupName << " is the " 00898 << ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE << "." << __E__; 00899 __COUT__ << "Resulting Active Groups end." << __E__; 00900 00901 CLEAN_UP: 00902 //============================================================================== 00903 __COUT__ << "End of Flattening Active Table Groups!\n\n\n" << __E__; 00904 00905 __COUT__ << "****************************" << __E__; 00906 __COUT__ << "There were " << groupSet.size() << " groups considered, and there were " 00907 << groupErrors.size() << " errors found handling those groups." << __E__; 00908 __COUT__ << "The following errors were found handling the groups:" << __E__; 00909 for(auto& groupErr : groupErrors) 00910 __COUT__ << "\t" << groupErr.first.first << " " << groupErr.first.second << ": \t" 00911 << groupErr.second << __E__; 00912 __COUT__ << "End of errors.\n\n" << __E__; 00913 00914 __COUT__ << "Run the following to return to your previous database structure:" 00915 << __E__; 00916 __COUT__ << "\t otsdaq_fix_new_table_fiels " << moveToDir << "\n\n" << __E__; 00917 00918 return; 00919 } // end FixNewTableFields() 00920 00921 int main(int argc, char* argv[]) 00922 { 00923 FixNewTableFields(argc, argv); 00924 return 0; 00925 } 00926 // BOOST_AUTO_TEST_SUITE_END()