$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 #include "otsdaq-core/ConfigurationInterface/ConfigurationInterface.h" 00007 #include "otsdaq-core/ConfigurationInterface/ConfigurationManagerRW.h" 00008 00009 // usage: 00010 // otsdaq_import_system_aliases <pathOfDatabaseToImport> <(optional) prependLabel> 00011 // 00012 // all system aliases are imported to current db and are prepended with a label 00013 // 00014 00015 using namespace ots; 00016 00017 void ImportSystemAliasTableGroups(int argc, char* argv[]) 00018 { 00019 __COUT__ << "=================================================\n"; 00020 __COUT__ << "=================================================\n"; 00021 __COUT__ << "=================================================\n"; 00022 __COUT__ << "Importing External System Aliases!" << std::endl; 00023 00024 std::string prependBaseName = "Imported"; 00025 const std::string groupAliasesTableName = 00026 ConfigurationManager::GROUP_ALIASES_TABLE_NAME; 00027 const std::string versionAliasesTableName = 00028 ConfigurationManager::VERSION_ALIASES_TABLE_NAME; 00029 00030 std::cout << "\n\nusage: Two arguments:\n\n\t otsdaq_import_system_aliases " 00031 "<path_to_import_database_folder> <path_to_active_groups_file> " 00032 "<import_prepend_base_name (optional)> \n\n" 00033 << "\t\t Default values: \n\t\t\timport_prepend_base_name = \"" 00034 << prependBaseName << "\" " 00035 << "\n\n" 00036 << "\t\tfor example:\n\n" 00037 << "\t\t\totsdaq_import_system_aliases " 00038 "~/databaseToImport/filesystemdb/test_db " 00039 "~/UserDataToImport/ServiceData/ActiveTableGroups.cfg" 00040 << __E__; 00041 00042 std::cout << "\n\n\tExample active groups file content:\n\n" 00043 << "testContext\n2\nTableEditWizBackbone\n3\ndefaultConfig\n1\n\n" 00044 << __E__; 00045 00046 std::cout << "\n\nNote: This assumes artdaq db file type interface. " 00047 << "The current database/ will be backed up to database_<linuxtime>/ " 00048 << "before importing the active groups.\n\n" 00049 << __E__; 00050 00051 __COUTV__(argc); 00052 for(int i = 0; i < argc; i++) 00053 std::cout << "argv[" << i << "] = " << argv[i] << std::endl; 00054 00055 if(argc < 3) 00056 { 00057 __COUT__ << "Must provide at least two parameters (the database and active " 00058 "groups files for the import).\n\n\n" 00059 << __E__; 00060 return; 00061 } 00062 00063 // remove trailing /'s 00064 while(strlen(argv[1]) && argv[1][strlen(argv[1]) - 1] == '/') 00065 argv[1][strlen(argv[1]) - 1] = '\0'; 00066 00067 // determine if "h"elp was first parameter 00068 std::string pathToImportDatabase = argv[1]; 00069 if(pathToImportDatabase.size() && 00070 (pathToImportDatabase[0] == 'h' || pathToImportDatabase[0] == '-')) 00071 { 00072 __COUT__ 00073 << "Recognized parameter 1. as a 'help' option. Usage was printed. Exiting." 00074 << std::endl; 00075 return; 00076 } 00077 00078 std::string pathToImportActiveGroups = ""; 00079 if(argc >= 3) 00080 pathToImportActiveGroups = argv[2]; 00081 if(argc >= 4) 00082 prependBaseName = argv[3]; 00083 00084 __COUTV__(pathToImportDatabase); 00085 __COUTV__(pathToImportActiveGroups); 00086 __COUTV__(prependBaseName); 00087 00088 //============================================================================== 00089 // Define environment variables 00090 // Note: normally these environment variables are set by StartOTS.sh 00091 00092 // These are needed by 00093 // otsdaq/otsdaq-core/ConfigurationDataFormats/ConfigurationInfoReader.cc [207] 00094 setenv("CONFIGURATION_TYPE", "File", 1); // Can be File, Database, DatabaseTest 00095 setenv("CONFIGURATION_DATA_PATH", 00096 (std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(), 00097 1); 00098 setenv( 00099 "TABLE_INFO_PATH", (std::string(getenv("USER_DATA")) + "/TableInfo").c_str(), 1); 00101 00102 // Some configuration plug-ins use getenv("SERVICE_DATA_PATH") in init() so define it 00103 setenv("SERVICE_DATA_PATH", 00104 (std::string(getenv("USER_DATA")) + "/ServiceData").c_str(), 00105 1); 00106 00107 // Some configuration plug-ins use getenv("OTSDAQ_LIB") and 00108 // getenv("OTSDAQ_UTILITIES_LIB") in init() so define it to a non-sense place is ok 00109 setenv("OTSDAQ_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); 00110 setenv("OTSDAQ_UTILITIES_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1); 00111 00112 // Some configuration plug-ins use getenv("OTS_MAIN_PORT") in init() so define it 00113 setenv("OTS_MAIN_PORT", "2015", 1); 00114 00115 // also xdaq envs for XDAQContextTable 00116 setenv("XDAQ_CONFIGURATION_DATA_PATH", 00117 (std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(), 00118 1); 00119 setenv("XDAQ_CONFIGURATION_XML", "otsConfigurationNoRU_CMake", 1); 00121 00122 //============================================================================== 00123 00124 // Steps: 00125 // 00126 // -- create empty map of import alias to original groupName & groupKey 00127 // -- create empty map of original groupName & groupKey to new groupKey 00128 // 00129 // -- create empty map of import alias to original tableName & tableVersion 00130 // -- create empty map of original tableName & tableVersion to new tableVersion 00131 // 00132 // -- in current-db extract info 00133 // - get set of existing group aliases 00134 // . load current aliases from current-db 00135 // - get set of existing table aliases 00136 // . load current aliases from current-db 00137 // 00138 // -- swap to import-db, clear cache, and create vector of groups to import 00139 // - get active groups from user data file 00140 // - get aliases from active backbone group 00141 // . check basename+alias for collision with existing group aliases and throw 00142 // error if collision 00143 // . add map record of basename+alias to original groupName & groupKey 00144 // . check basename+alias for collision with existing table aliases and throw 00145 // error if collision 00146 // . add map record of basename+alias to original tableName & tableVersion 00147 // 00148 // -- for each group in set 00149 // - swap to import-db 00150 // - load/activate group 00151 // - swap to current-db 00152 // - for each member table in group 00153 // . check map of original tableName & tableVersion to new tableName & 00154 // tableVersion, to prevent making a new table 00155 // . if needed, save active tables as next table version (using interface) 00156 // , add map record of original tableName & tableVersion to new tableName 00157 //& tableVersion 00158 // - save new group with associated table versions 00159 // . add map record of original groupName & groupKey to new groupName & 00160 // groupKey 00161 // 00162 // 00163 // -- in current-db after loop... 00164 // - destroy and reload active groups 00165 // -- insert new aliases for imported groups 00166 // - should be basename+alias connection to (hop through maps) new groupName & 00167 // groupKey 00168 // -- insert new aliases for imported tables 00169 // - should be basename+alias connection to (hop through maps) new tableName & 00170 // tableVersion 00171 // -- save new backbone tables and save new backbone group 00172 // -- backup the file ConfigurationManager::ACTIVE_GROUPS_FILENAME with time 00173 // -- activate the new backbone group 00174 // 00175 00176 //============================================================================== 00177 00178 // create objects 00179 00180 // std::map<std::string /*tableName*/, 00181 // TableVersion /*next persisten version*/> 00182 // tableNextVersionMap; 00183 00184 std::map<std::string /*importGroupAlias*/, 00185 /*original*/ std::pair<std::string /*groupName*/, TableGroupKey>> 00186 originalGroupAliasMap; 00187 00188 std::map</*original*/ std::pair<std::string /*groupName*/, TableGroupKey>, 00189 /*new*/ TableGroupKey> 00190 groupSet; 00191 00192 std::map<std::string /*importTableAlias*/, 00193 /*original*/ std::pair<std::string /*tableName*/, TableVersion>> 00194 originalTableAliasMap; 00195 00196 std::map</*original*/ std::pair<std::string /*tableName*/, TableVersion>, 00197 /*new*/ TableVersion> 00198 newTableVersionMap; 00199 00200 std::map</*original*/ std::pair<std::string, TableGroupKey>, 00201 std::string /*error string*/> 00202 groupErrors; 00203 00204 // get prepared with initial source db 00205 00206 // ConfigurationManager instance immediately loads active groups 00207 __COUT__ << "Getting started..." << std::endl; 00208 ConfigurationManagerRW cfgMgrInst("import_aliases"); 00209 ConfigurationManagerRW* cfgMgr = &cfgMgrInst; 00210 bool importedDbInPlace = false; 00211 00212 __COUT__ << "Configuration manager initialized." << __E__; 00213 00214 std::string nowTime = std::to_string(time(0)); 00215 std::string currentDir = getenv("ARTDAQ_DATABASE_URI"); 00216 00217 if(currentDir.find("filesystemdb://") != 0) 00218 { 00219 __SS__ << "filesystemdb:// was not found in $ARTDAQ_DATABASE_URI!" << std::endl; 00220 __COUT_ERR__ << "\n" << ss.str(); 00221 __SS_THROW__; 00222 } 00223 00224 currentDir = currentDir.substr(std::string("filesystemdb://").length()); 00225 while(currentDir.length() && 00226 currentDir[currentDir.length() - 1] == '/') // remove trailing '/'s 00227 currentDir = currentDir.substr(0, currentDir.length() - 1); 00228 00229 __COUTV__(currentDir); 00230 00231 std::string backupDir = currentDir + "_" + nowTime; 00232 std::string importDir = pathToImportDatabase + "_" + nowTime; 00233 std::string tmpCurrentDir = currentDir + "_tmp_" + nowTime; 00234 // std::string tmpImportDir = pathToImportDatabase + "_tmp_" + nowTime; 00235 00236 // -- get set of existing aliases 00237 std::map<std::string /*table name*/, 00238 std::map<std::string /*version alias*/, TableVersion /*aliased version*/>> 00239 existingTableAliases = cfgMgr->ConfigurationManager::getVersionAliases(); 00240 std::map<std::string /*alias*/, std::pair<std::string /*group name*/, TableGroupKey>> 00241 existingGroupAliases = cfgMgr->getActiveGroupAliases(); 00242 00243 // //NOT NEEDED -- just ask db on fly for next version // - fill map of table name 00244 // to next persistent table version 00245 // { 00246 // const std::map<std::string, TableInfo>& allCfgInfo = 00247 // cfgMgr->getAllConfigurationInfo(); for(auto& mapPair : allCfgInfo) 00248 // { 00249 // tableNextVersionMap[mapPair.first] = 00250 // mapPair.second.versions_.size()? 00251 // *(mapPair.second.versions_.rbegin()): 00252 // TableVersion(TableVersion::DEFAULT); 00253 // } 00254 // } 00255 // 00256 // __COUTV__(StringMacros::mapToString(tableNextVersionMap)); 00257 00258 // -- swap to import-db and clear cache 00259 { 00260 // back up current directory now 00261 __COUT__ << "Backing up current database at '" << currentDir << "' to '" 00262 << backupDir << "'" << __E__; 00263 std::system(("cp -r " + currentDir + " " + backupDir).c_str()); 00264 00265 __COUT__ << "Backing up current database at '" << pathToImportDatabase << "' to '" 00266 << importDir << "'" << __E__; 00267 std::system(("cp -r " + pathToImportDatabase + " " + importDir).c_str()); 00268 00269 cfgMgr->destroy(); 00270 00271 __COUT__ << "Swap to import-db" << std::endl; 00272 if(rename(currentDir.c_str(), tmpCurrentDir.c_str()) < 0) 00273 { 00274 __SS__ << "Problem!" << std::endl; 00275 __SS_THROW__; 00276 } 00277 if(rename(importDir.c_str(), currentDir.c_str()) < 0) 00278 { 00279 __SS__ << "Problem!" << std::endl; 00280 __SS_THROW__; 00281 } 00282 importedDbInPlace = true; 00283 } 00284 00285 try 00286 { 00287 // - get active groups from user data file 00288 cfgMgr->restoreActiveTableGroups(true /*throwErrors*/, pathToImportActiveGroups); 00289 00290 // add active groups to set 00291 std::map<std::string, std::pair<std::string, TableGroupKey>> activeGroupsMap = 00292 cfgMgr->getActiveTableGroups(); 00293 00294 for(const auto& activeGroup : activeGroupsMap) 00295 { 00296 if(activeGroup.second.second.isInvalid()) 00297 continue; 00298 if(activeGroup.second.first == "") 00299 continue; 00300 00301 __COUTV__(activeGroup.second.first); 00302 __COUTV__(activeGroup.second.second); 00303 00304 groupSet.insert( 00305 std::pair<std::pair<std::string, TableGroupKey>, TableGroupKey>( 00306 std::pair<std::string, TableGroupKey>(activeGroup.second.first, 00307 activeGroup.second.second), 00308 TableGroupKey())); 00309 } 00310 00311 // add system alias groups to set 00312 std::map<std::string, TableVersion> activeVersions = cfgMgr->getActiveVersions(); 00313 if(activeVersions.find(groupAliasesTableName) == activeVersions.end()) 00314 { 00315 __SS__ << "\nActive version of " << groupAliasesTableName << " missing! " 00316 << groupAliasesTableName 00317 << " is a required member of the Backbone configuration group." 00318 << "\n\nLikely you need to activate a valid Backbone group." 00319 << std::endl; 00320 __SS_THROW__; 00321 } 00322 if(activeVersions.find(versionAliasesTableName) == activeVersions.end()) 00323 { 00324 __SS__ << "\nActive version of " << versionAliasesTableName << " missing! " 00325 << versionAliasesTableName 00326 << " is a required member of the Backbone configuration group." 00327 << "\n\nLikely you need to activate a valid Backbone group." 00328 << std::endl; 00329 __SS_THROW__; 00330 } 00331 00332 std::vector<std::pair<std::string, ConfigurationTree>> aliasNodePairs = 00333 cfgMgr->getNode(groupAliasesTableName).getChildren(); 00334 std::string aliasName; 00335 for(auto& aliasPair : aliasNodePairs) 00336 { 00337 if(TableGroupKey(aliasPair.second.getNode("GroupKey").getValueAsString()) 00338 .isInvalid()) 00339 continue; 00340 if(aliasPair.second.getNode("GroupName").getValueAsString() == "") 00341 continue; 00342 00343 aliasName = aliasPair.second.getNode("GroupKeyAlias").getValueAsString(); 00344 if(aliasName == "") 00345 continue; 00346 00347 if(aliasName[0] >= 'a' && aliasName[0] <= 'z') // capitalize the name 00348 aliasName[0] -= 32; 00349 aliasName = prependBaseName + aliasName; 00350 __COUTV__(aliasName); 00351 __COUTV__(aliasPair.second.getNode("GroupName").getValueAsString()); 00352 __COUTV__(aliasPair.second.getNode("GroupKey").getValueAsString()); 00353 00354 // . check basename+alias for collision with existing group aliases 00355 // and throw error if collision 00356 if(existingGroupAliases.find(aliasName) != existingGroupAliases.end()) 00357 { 00358 __SS__ << "Conflicting group alias '" << aliasName << "' found!" << __E__; 00359 __SS_THROW__; 00360 } 00361 00362 groupSet.insert( 00363 std::pair<std::pair<std::string, TableGroupKey>, TableGroupKey>( 00364 std::pair<std::string, TableGroupKey>( 00365 aliasPair.second.getNode("GroupName").getValueAsString(), 00366 TableGroupKey( 00367 aliasPair.second.getNode("GroupKey").getValueAsString())), 00368 TableGroupKey())); 00369 00370 originalGroupAliasMap[aliasName] = std::pair<std::string, TableGroupKey>( 00371 aliasPair.second.getNode("GroupName").getValueAsString(), 00372 TableGroupKey(aliasPair.second.getNode("GroupKey").getValueAsString())); 00373 } // end group aliases loop 00374 00375 aliasNodePairs = cfgMgr->getNode(versionAliasesTableName).getChildren(); 00376 for(auto& aliasPair : aliasNodePairs) 00377 { 00378 if(TableVersion(aliasPair.second.getNode("Version").getValueAsString()) 00379 .isInvalid()) 00380 continue; 00381 if(aliasPair.second.getNode("TableName").getValueAsString() == "") 00382 continue; 00383 aliasName = aliasPair.second.getNode("VersionAlias").getValueAsString(); 00384 if(aliasName == "") 00385 continue; 00386 00387 if(aliasName[0] >= 'a' && aliasName[0] <= 'z') // capitalize the name 00388 aliasName[0] -= 32; 00389 aliasName = prependBaseName + aliasName; 00390 __COUTV__(aliasPair.second.getNode("TableName").getValueAsString()); 00391 __COUTV__(aliasName); 00392 __COUTV__(aliasPair.second.getNode("Version").getValueAsString()); 00393 00394 // . check basename+alias for collision with existing table aliases 00395 // and throw error if collision 00396 if(existingTableAliases.find(aliasName) != existingTableAliases.end()) 00397 { 00398 __SS__ << "Conflicting table version alias '" << aliasName << "' found!" 00399 << __E__; 00400 __SS_THROW__; 00401 } 00402 00403 originalTableAliasMap[aliasName] = std::pair<std::string, TableVersion>( 00404 aliasPair.second.getNode("TableName").getValueAsString(), 00405 TableVersion(aliasPair.second.getNode("Version").getValueAsString())); 00406 } // end table aliases loop 00407 } // end extract group set 00408 catch(const std::runtime_error& e) 00409 { 00410 __COUT_ERR__ << "There was a fatal error: " << e.what() << __E__; 00411 00412 __COUT__ << std::endl; 00413 __COUT__ << std::endl; 00414 __COUT__ << "Run the following to return to your previous database structure:" 00415 << std::endl; 00416 __COUT__ << "\t otsdaq_flatten_system_aliases -1 " << backupDir << "\n\n" 00417 << std::endl; 00418 __COUT__ << std::endl; 00419 __COUT__ << std::endl; 00420 return; 00421 } 00422 00423 __COUTV__(StringMacros::mapToString(existingGroupAliases)); 00424 __COUTV__(StringMacros::mapToString(existingTableAliases)); 00425 00426 __COUT__ << std::endl; 00427 __COUT__ << std::endl; 00428 __COUT__ << "Identified groups:" << std::endl; 00429 for(auto& group : groupSet) 00430 __COUT__ << "\t" << group.first.first << " " << group.first.second << std::endl; 00431 __COUT__ << std::endl; 00432 __COUT__ << std::endl; 00433 00434 __COUT__ << "Identified group aliases:" << std::endl; 00435 for(auto& groupAlias : originalGroupAliasMap) 00436 __COUT__ << "\t" << groupAlias.first << " ==> " << groupAlias.second.first << "-" 00437 << groupAlias.second.second << std::endl; 00438 __COUT__ << std::endl; 00439 __COUT__ << std::endl; 00440 00441 //============================================================================== 00442 // -- for each group in set 00443 00444 ConfigurationInterface* theInterface_ = ConfigurationInterface::getInstance( 00445 false); // true for File interface, false for artdaq database; 00446 TableView* cfgView; 00447 TableBase* config; 00448 TableVersion newVersion; 00449 TableGroupKey newKey; 00450 00451 bool errDetected; 00452 std::string accumulateErrors = ""; 00453 int count = 0; 00454 00455 std::map<std::string, TableVersion> memberMap; 00456 std::map<std::string /*name*/, std::string /*alias*/> groupAliases; 00457 std::string groupComment; 00458 std::string groupAuthor; 00459 std::string groupCreateTime; 00460 time_t groupCreateTime_t; 00461 TableBase* groupMetadataTable = cfgMgr->getMetadataTable(); 00462 00463 __COUT__ << "Proceeding with handling of identified groups..." << __E__; 00464 00465 // -- for each group in set 00466 for(auto& groupPair : groupSet) 00467 { 00468 cfgMgr->destroy(); 00469 00470 errDetected = false; 00471 00472 //- swap to import-db (first time already in import-db 00473 //- load/activate group 00474 00475 __COUTV__(importedDbInPlace); 00476 // -- swap to import-db 00477 if(!importedDbInPlace) 00478 { 00479 __COUT__ << "Swap to import-db" << std::endl; 00480 if(rename(currentDir.c_str(), tmpCurrentDir.c_str()) < 0) 00481 { 00482 __SS__ << "Problem!" << std::endl; 00483 __SS_THROW__; 00484 } 00485 if(rename(importDir.c_str(), currentDir.c_str()) < 0) 00486 { 00487 __SS__ << "Problem!" << std::endl; 00488 __SS_THROW__; 00489 } 00490 importedDbInPlace = true; 00491 } 00492 00493 // cfgMgr->restoreActiveTableGroups(true 00494 // /*throwErrors*/,pathToImportActiveGroups); 00495 00496 __COUT__ << "****************************" << std::endl; 00497 __COUT__ << "Loading members for " << groupPair.first.first << "(" 00498 << groupPair.first.second << ")" << std::endl; 00499 __COUTV__(count); 00500 00501 //========================= 00502 // load group, group metadata, and tables from original DB 00503 try 00504 { 00505 cfgMgr->loadTableGroup(groupPair.first.first, 00506 groupPair.first.second, 00507 true /*doActivate*/, 00508 &memberMap /*memberMap*/, 00509 0 /*progressBar*/, 00510 &accumulateErrors, 00511 &groupComment, 00512 &groupAuthor, 00513 &groupCreateTime, 00514 false /*doNotLoadMember*/, 00515 0 /*groupTypeString*/, 00516 &groupAliases); 00517 } 00518 catch(std::runtime_error& e) 00519 { 00520 __COUT__ << "Error was caught loading members for " << groupPair.first.first 00521 << "(" << groupPair.first.second << ")" << std::endl; 00522 __COUT__ << e.what() << std::endl; 00523 errDetected = true; 00524 } 00525 catch(...) 00526 { 00527 __COUT__ << "Error was caught loading members for " << groupPair.first.first 00528 << "(" << groupPair.first.second << ")" << std::endl; 00529 errDetected = true; 00530 } 00531 00532 //========================= 00533 00534 // if(count == 2) break; 00535 00536 // exit loop if any (loading) failure 00537 if(errDetected) 00538 { 00539 // goto CLEAN_UP; 00540 00541 // power on if group failed 00542 // and record error 00543 00544 groupErrors.insert( 00545 std::pair<std::pair<std::string, TableGroupKey>, std::string>( 00546 std::pair<std::string, TableGroupKey>(groupPair.first.first, 00547 groupPair.first.second), 00548 "Error caught loading the group.")); 00549 continue; 00550 } 00551 00552 // -- swap to current-db 00553 if(importedDbInPlace) 00554 { 00555 __COUT__ << "Swap to current-db" << std::endl; 00556 if(rename(currentDir.c_str(), importDir.c_str()) < 0) 00557 { 00558 __SS__ << "Problem!" << std::endl; 00559 __SS_THROW__; 00560 } 00561 if(rename(tmpCurrentDir.c_str(), currentDir.c_str()) < 0) 00562 { 00563 __SS__ << "Problem!" << std::endl; 00564 __SS_THROW__; 00565 } 00566 importedDbInPlace = false; 00567 } 00568 00569 //========================= 00570 // save group and its tables with new key and versions! 00571 try 00572 { 00573 // saving tables 00574 for(auto& memberPair : memberMap) 00575 { 00576 __COUT__ << memberPair.first << ":v" << memberPair.second << std::endl; 00577 00578 // check if table has already been modified by a previous group 00579 // (i.e. two groups using the same version of a table) 00580 if(newTableVersionMap.find(std::pair<std::string, TableVersion>( 00581 memberPair.first, memberPair.second)) != newTableVersionMap.end()) 00582 { 00583 __COUT__ << "Table was already modified!" << std::endl; 00584 memberPair.second = 00585 newTableVersionMap[std::pair<std::string, TableVersion>( 00586 memberPair.first, memberPair.second)]; 00587 __COUT__ << "\t to...\t" << memberPair.first << ":v" 00588 << memberPair.second << std::endl; 00589 continue; 00590 } 00591 00592 // change the version of the active view to next available version and 00593 // save it 00594 config = cfgMgr->getTableByName(memberPair.first); 00595 cfgView = config->getViewP(); 00596 // newVersion = theInterface_->saveNewVersion(config, temporaryVersion); 00597 newVersion = TableVersion::getNextVersion( 00598 theInterface_->findLatestVersion(config)); 00599 __COUTV__(newVersion); 00600 cfgView->setVersion(newVersion); 00601 theInterface_->saveActiveVersion(config); 00602 00603 // set it back for the table so that future groups can re-use cached 00604 // version 00605 // FIXME -- RAR note: I do not understand why this was important.. seems 00606 // like it will not help since the cache is destroyed for each group 00607 // cfgView->setVersion(memberPair.second); //IMPORTANT 00608 00609 // save new version to modifiedTables 00610 newTableVersionMap.insert( 00611 std::pair<std::pair<std::string, TableVersion>, TableVersion>( 00612 std::pair<std::string, TableVersion>(memberPair.first, 00613 memberPair.second), 00614 newVersion)); 00615 00616 memberPair.second = newVersion; // change version in the member map 00617 00618 __COUT__ << "\t to...\t" << memberPair.first << ":v" << memberPair.second 00619 << std::endl; 00620 } // end member map loop 00621 00622 __COUT__ << "Member map completed" << __E__; 00623 __COUTV__(StringMacros::mapToString(memberMap)); 00624 00625 // Note: this code copies actions in ConfigurationManagerRW::saveNewTableGroup 00626 00627 // add meta data 00628 __COUTV__(StringMacros::mapToString(groupAliases)); 00629 __COUTV__(groupComment); 00630 __COUTV__(groupAuthor); 00631 __COUTV__(groupCreateTime); 00632 sscanf(groupCreateTime.c_str(), "%ld", &groupCreateTime_t); 00633 __COUTV__(groupCreateTime_t); 00634 00635 // to compensate for unusual errors upstream, make sure the metadata table has 00636 // one row 00637 while(groupMetadataTable->getViewP()->getNumberOfRows() > 1) 00638 groupMetadataTable->getViewP()->deleteRow(0); 00639 if(groupMetadataTable->getViewP()->getNumberOfRows() == 0) 00640 groupMetadataTable->getViewP()->addRow(); 00641 00642 // columns are uid,comment,author,time 00643 // ConfigurationManager::METADATA_COL_ALIASES TODO 00644 groupMetadataTable->getViewP()->setValue( 00645 StringMacros::mapToString( 00646 groupAliases, "," /*primary delimiter*/, ":" /*secondary delimeter*/), 00647 0, 00648 ConfigurationManager::METADATA_COL_ALIASES); 00649 groupMetadataTable->getViewP()->setValue( 00650 groupComment, 0, ConfigurationManager::METADATA_COL_COMMENT); 00651 groupMetadataTable->getViewP()->setValue( 00652 groupAuthor, 0, ConfigurationManager::METADATA_COL_AUTHOR); 00653 groupMetadataTable->getViewP()->setValue( 00654 groupCreateTime_t, 0, ConfigurationManager::METADATA_COL_TIMESTAMP); 00655 00656 // set version of metadata table 00657 newVersion = TableVersion::getNextVersion( 00658 theInterface_->findLatestVersion(groupMetadataTable)); 00659 __COUTV__(newVersion); 00660 groupMetadataTable->getViewP()->setVersion(newVersion); 00661 theInterface_->saveActiveVersion(groupMetadataTable); 00662 00663 // force groupMetadataTable_ to be a member for the group 00664 memberMap[groupMetadataTable->getTableName()] = 00665 groupMetadataTable->getViewVersion(); 00666 00667 // memberMap should now consist of members with new flat version, so save 00668 // group 00669 newKey = TableGroupKey::getNextKey( 00670 theInterface_->findLatestGroupKey(groupPair.first.first)); 00671 00672 __COUTV__(newKey); 00673 00674 // memberMap should now consist of members with new flat version, so save 00675 theInterface_->saveTableGroup( 00676 memberMap, 00677 TableGroupKey::getFullGroupString(groupPair.first.first, newKey)); 00678 00679 // and modify groupSet and activeGroupKeys keys 00680 groupPair.second = newKey; 00681 } 00682 catch(std::runtime_error& e) 00683 { 00684 __COUT__ << "Error was caught saving group " << groupPair.first.first << " (" 00685 << groupPair.first.second << ") " << std::endl; 00686 __COUT__ << e.what() << std::endl; 00687 00688 groupErrors.insert( 00689 std::pair<std::pair<std::string, TableGroupKey>, std::string>( 00690 std::pair<std::string, TableGroupKey>(groupPair.first.first, 00691 groupPair.first.second), 00692 "Error caught saving the group.")); 00693 } 00694 catch(...) 00695 { 00696 __COUT__ << "Error was caught saving group " << groupPair.first.first << " (" 00697 << groupPair.first.second << ") " << std::endl; 00698 00699 groupErrors.insert( 00700 std::pair<std::pair<std::string, TableGroupKey>, std::string>( 00701 std::pair<std::string, TableGroupKey>(groupPair.first.first, 00702 groupPair.first.second), 00703 "Error caught saving the group.")); 00704 } 00705 //========================= 00706 00707 // increment 00708 ++count; 00709 } // end group loop 00710 00711 __COUT__ << "Completed group and table saving for " << count << " groups." << __E__; 00712 __COUT__ << "Created tables:" << std::endl; 00713 for(auto& tablePair : newTableVersionMap) 00714 __COUT__ << "\t" << tablePair.first.first << "-v" << tablePair.first.second 00715 << " ==> " << tablePair.second << std::endl; 00716 __COUT__ << std::endl; 00717 __COUT__ << std::endl; 00718 __COUT__ << "Created groups:" << std::endl; 00719 for(auto& group : groupSet) 00720 __COUT__ << "\t" << group.first.first << "(" << group.first.second << ") ==> " 00721 << group.second << std::endl; 00722 00723 // -- in current-db after loop... 00724 // -- swap to current-db 00725 if(importedDbInPlace) 00726 { 00727 __COUT__ << "Swap to current-db" << std::endl; 00728 if(rename(currentDir.c_str(), importDir.c_str()) < 0) 00729 { 00730 __SS__ << "Problem!" << std::endl; 00731 __SS_THROW__; 00732 } 00733 if(rename(tmpCurrentDir.c_str(), currentDir.c_str()) < 0) 00734 { 00735 __SS__ << "Problem!" << std::endl; 00736 __SS_THROW__; 00737 } 00738 importedDbInPlace = false; 00739 } 00740 00741 // record in readme for current-db 00742 { 00743 FILE* fp = fopen((currentDir + "/README_otsdaq_import.txt").c_str(), "a"); 00744 00745 if(!fp) 00746 __COUT__ << "\tError opening README file!" << std::endl; 00747 else 00748 { 00749 time_t rawtime; 00750 struct tm* timeinfo; 00751 char buffer[200]; 00752 00753 time(&rawtime); 00754 timeinfo = localtime(&rawtime); 00755 strftime(buffer, 200, "%b %d, %Y %I:%M:%S%p %Z", timeinfo); 00756 00757 fprintf(fp, 00758 "This database...\t %s \t received an import from...\t %s \t at this " 00759 "time \t %lu \t %s\n\n", 00760 currentDir.c_str(), 00761 pathToImportDatabase.c_str(), 00762 time(0), 00763 buffer); 00764 fclose(fp); 00765 } 00766 } 00767 00768 // -- in current-db after loop... 00769 // - destroy and reload active groups 00770 cfgMgr->destroy(); 00771 cfgMgr->restoreActiveTableGroups(true /*throwErrors*/); 00772 00773 // -- insert new aliases for imported groups 00774 // - should be basename+alias connection to (hop through maps) new groupName & 00775 // groupKey 00776 // -- insert new aliases for imported tables 00777 // - should be basename+alias connection to (hop through maps) new tableName & 00778 // tableVersion 00779 // -- save new backbone tables and save new backbone group 00780 00781 __COUT__ << "Modifying the active Backbone table to reflect new table versions and " 00782 "group keys." 00783 << std::endl; 00784 00785 try 00786 { 00787 // modify Group Aliases Table and Version Aliases Table to 00788 // include new groups and tables 00789 00790 std::string activeBackboneGroupName = 00791 cfgMgr->getActiveGroupName(ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE); 00792 cfgMgr->loadTableGroup( 00793 activeBackboneGroupName, 00794 cfgMgr->getActiveGroupKey(ConfigurationManager::ACTIVE_GROUP_NAME_BACKBONE), 00795 true, 00796 &memberMap, 00797 0, 00798 &accumulateErrors); 00799 00800 std::map<std::string, TableVersion> activeMap = cfgMgr->getActiveVersions(); 00801 00802 // modify Group Aliases Table 00803 if(activeMap.find(groupAliasesTableName) != activeMap.end()) 00804 { 00805 __COUT__ << "\n\nModifying " << groupAliasesTableName << std::endl; 00806 config = cfgMgr->getTableByName(groupAliasesTableName); 00807 cfgView = config->getViewP(); 00808 00809 unsigned int col0 = cfgView->findCol("GroupKeyAlias"); 00810 unsigned int col1 = cfgView->findCol("GroupName"); 00811 unsigned int col2 = cfgView->findCol("GroupKey"); 00812 unsigned int row; 00813 00814 cfgView->print(); 00815 00816 // -- insert new aliases for imported groups 00817 // - should be basename+alias connection to (hop through maps) new 00818 // groupName & groupKey 00819 for(auto& aliasPair : originalGroupAliasMap) 00820 { 00821 auto groupIt = groupSet.find(std::pair<std::string, TableGroupKey>( 00822 aliasPair.second.first, aliasPair.second.second)); 00823 00824 if(groupIt == groupSet.end()) 00825 { 00826 __COUT__ 00827 << "Error! Could not find the new entry for the original group " 00828 << aliasPair.second.first << "(" << aliasPair.second.second << ")" 00829 << __E__; 00830 continue; 00831 } 00832 row = cfgView->addRow("import_aliases", true /*incrementUniqueData*/); 00833 cfgView->setValue(aliasPair.first, row, col0); 00834 cfgView->setValue(aliasPair.second.first, row, col1); 00835 cfgView->setValue(groupIt->second.toString(), row, col2); 00836 } // end group alias edit 00837 00838 cfgView->print(); 00839 } 00840 00841 // modify Version Aliases Table 00842 if(activeMap.find(versionAliasesTableName) != activeMap.end()) 00843 { 00844 __COUT__ << "\n\nModifying " << versionAliasesTableName << std::endl; 00845 config = cfgMgr->getTableByName(versionAliasesTableName); 00846 cfgView = config->getViewP(); 00847 unsigned int col0 = cfgView->findCol("VersionAlias"); 00848 unsigned int col1 = cfgView->findCol("TableName"); 00849 unsigned int col2 = cfgView->findCol("Version"); 00850 00851 unsigned int row; 00852 00853 cfgView->print(); 00854 00855 // -- insert new aliases for imported tables 00856 // - should be basename+alias connection to (hop through maps) new 00857 // tableName & tableVersion 00858 for(auto& aliasPair : originalTableAliasMap) 00859 { 00860 auto tableIt = 00861 newTableVersionMap.find(std::pair<std::string, TableVersion>( 00862 aliasPair.second.first, aliasPair.second.second)); 00863 00864 if(tableIt == newTableVersionMap.end()) 00865 { 00866 __COUT__ 00867 << "Error! Could not find the new entry for the original table " 00868 << aliasPair.second.first << "(" << aliasPair.second.second << ")" 00869 << __E__; 00870 continue; 00871 } 00872 row = cfgView->addRow("import_aliases", true /*incrementUniqueData*/); 00873 cfgView->setValue(aliasPair.first, row, col0); 00874 cfgView->setValue(aliasPair.second.first, row, col1); 00875 cfgView->setValue(tableIt->second.toString(), row, col2); 00876 } // end group alias edit 00877 00878 cfgView->print(); 00879 } 00880 00881 // save new Group Aliases Table and Version Aliases Table 00882 00883 // change the version of the active view to flatVersion and save it 00884 config = cfgMgr->getTableByName(groupAliasesTableName); 00885 cfgView = config->getViewP(); 00886 newVersion = 00887 TableVersion::getNextVersion(theInterface_->findLatestVersion(config)); 00888 __COUTV__(newVersion); 00889 cfgView->setVersion(newVersion); 00890 theInterface_->saveActiveVersion(config); 00891 00892 memberMap[groupAliasesTableName] = newVersion; // change version in the member 00893 // map 00894 00895 __COUT__ << "\t to...\t" << groupAliasesTableName << ":v" 00896 << memberMap[groupAliasesTableName] << std::endl; 00897 00898 __COUT__ << versionAliasesTableName << ":v" << memberMap[versionAliasesTableName] 00899 << std::endl; 00900 // change the version of the active view to flatVersion and save it 00901 config = cfgMgr->getTableByName(versionAliasesTableName); 00902 cfgView = config->getViewP(); 00903 newVersion = 00904 TableVersion::getNextVersion(theInterface_->findLatestVersion(config)); 00905 __COUTV__(newVersion); 00906 cfgView->setVersion(newVersion); 00907 theInterface_->saveActiveVersion(config); 00908 00909 memberMap[versionAliasesTableName] = 00910 newVersion; // change version in the member map 00911 00912 __COUT__ << "\t to...\t" << versionAliasesTableName << ":v" 00913 << memberMap[versionAliasesTableName] << std::endl; 00914 00915 __COUT__ << "Backbone member map completed" << __E__; 00916 __COUTV__(StringMacros::mapToString(memberMap)); 00917 00918 newKey = TableGroupKey::getNextKey( 00919 theInterface_->findLatestGroupKey(activeBackboneGroupName)); 00920 00921 __COUTV__(newKey); 00922 00923 // memberMap should now consist of members with new flat version, so save 00924 theInterface_->saveTableGroup( 00925 memberMap, 00926 TableGroupKey::getFullGroupString(activeBackboneGroupName, newKey)); 00927 00928 std::string renameFile = 00929 ConfigurationManager::ACTIVE_GROUPS_FILENAME + "." + nowTime; 00930 rename(ConfigurationManager::ACTIVE_GROUPS_FILENAME.c_str(), renameFile.c_str()); 00931 00932 __COUT__ << "Backing up '" << ConfigurationManager::ACTIVE_GROUPS_FILENAME 00933 << "' to ... '" << renameFile << "'" << std::endl; 00934 00935 cfgMgr->activateTableGroup(activeBackboneGroupName, 00936 newKey); // and write to active group file 00937 00938 } // end try 00939 catch(const std::runtime_error& e) 00940 { 00941 __COUT_ERR__ << "There was a fatal error during backbone modification: " 00942 << e.what() << __E__; 00943 00944 goto CLEAN_UP; 00945 } 00946 00947 // print resulting all groups 00948 00949 std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Groups:" << std::endl; 00950 for(const auto& group : groupSet) 00951 __COUT__ << "\t" << group.first.first << ": " << group.first.second << " => " 00952 << group.second << std::endl; 00953 std::cout << "\n\n" << __COUT_HDR_FL__ << "Resulting Groups end." << std::endl; 00954 00955 CLEAN_UP: 00956 //============================================================================== 00957 __COUT__ << "End of Importing Active Table Groups!\n\n\n" << std::endl; 00958 00959 __COUT__ << "****************************" << std::endl; 00960 __COUT__ << "There were " << groupSet.size() << " groups considered, and there were " 00961 << groupErrors.size() << " errors found handling those groups." << std::endl; 00962 __COUT__ << "The following errors were found handling the groups:" << std::endl; 00963 for(auto& groupErr : groupErrors) 00964 __COUT__ << "\t" << groupErr.first.first << " " << groupErr.first.second << ": \t" 00965 << groupErr.second << std::endl; 00966 __COUT__ << "End of errors.\n\n" << std::endl; 00967 00968 __COUT__ << "Run the following to return to your previous database structure:" 00969 << std::endl; 00970 __COUT__ << "\t otsdaq_flatten_system_aliases -1 " << backupDir << "\n\n" 00971 << std::endl; 00972 00973 return; 00974 } 00975 00976 int main(int argc, char* argv[]) 00977 { 00978 ImportSystemAliasTableGroups(argc, argv); 00979 return 0; 00980 } 00981 // BOOST_AUTO_TEST_SUITE_END()