Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/BdbAdminTools/BdbCloseDbs.cc

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: BdbCloseDbs.cc,v 1.4 2002/06/24 19:11:06 becla Exp $
00004 //
00005 // Description:
00006 //      Transient class. Changes clustering hint internal information
00007 //      such that next time clustering hint will start with new databases
00008 //      and will never come back to existing ones.
00009 //
00010 // Environment:
00011 //      Software developed for the BaBar Detector at the SLAC B-Factory
00012 //
00013 // Author List:
00014 //      Jacek Becla                Original Author
00015 //
00016 // Copyright Information:
00017 //      Copyright (C) 1999      Stanford Linear Accelerator Center
00018 //
00019 //------------------------------------------------------------------------------
00020 
00021 //-----------------------
00022 // This Class's Header --
00023 //-----------------------
00024 #include "BdbAdminTools/BdbCloseDbs.hh"
00025 
00026 
00027 //-------------------------------
00028 // Collaborating Class Headers --
00029 //-------------------------------
00030 #include "BdbApplication/BdbDomain.hh"
00031 #include "BdbApplication/BdbDebug.hh"
00032 #include "BdbApplication/BdbErrorSignal.hh"
00033 #include "BdbAccess/BdbGenericDomain.hh"
00034 #include "BdbClustering/BdbClustHAccess.hh"
00035 
00036 #include "ooMap.h"
00037 
00038 
00039 // global, for simplicity
00040 char* clustName = 0;  // 0 -> this will pick up all clusters
00041 char authLevel  = 0;  // 0 -> this will pick up all auth levels
00042 char* authName  = 0;  // 0 -> this will pick up all auth names
00043 char* compName  = 0;  // 0 -> this will pick up all components
00044 
00045 
00046 d_Boolean verbose     = d_True;
00047 d_Boolean reallyClose = d_True;
00048 
00049 
00050 BdbCloseDbs::BdbCloseDbs(d_Boolean verbose, d_Boolean reallyClose)
00051     : _reallyClose(reallyClose)
00052 {
00053     if ( verbose ) {
00054         stream.attach(1); // stream becomes cout
00055     }
00056     _locatorMgr = new BdbClustHAccess("evs", d_False, 0);
00057 }
00058 
00059 
00060 
00061 BdbCloseDbs::~BdbCloseDbs()
00062 {
00063     delete _locatorMgr;
00064 }
00065 
00066 
00067 
00068 
00069 
00070 
00071 /**
00072  **  BdbCloseDbs::run()
00073  **
00074  **  The function processes all containers in all clustering hint databases
00075  **  in evs domain.
00076  **/
00077 
00078 int 
00079 BdbCloseDbs::run()
00080 {
00081     BdbApplicationOrDomain* app = BdbApplicationOrDomain::activeInstance();
00082     ooMode openMode;
00083     // initialize transaction
00084 
00085     if( ! app->isAuthorized( BdbDomain::System ) ) {
00086        cerr << "You must have system authorization to use this"
00087             << " utility."
00088             << endl;
00089        return( -1 ) ;
00090     }
00091 
00092     app->startUpdate("t");
00093     openMode = oocUpdate;
00094 
00095     
00096     if ( 0 != clustName ) {
00097         char dbName[64];
00098         sprintf(dbName, "evs_clustHintConfig_%s", clustName);
00099         if ( BdbcSuccess != runInOneDb(dbName, BdbcUpdate) ) {
00100             stream << "failed for db " << dbName << ", aborting all changes" << endl;
00101             app->abort("t");
00102             return BdbcError;
00103         }
00104     } else {
00105         // do it for every clustering hint database
00106         ooItr(ooDBObj) dbItr;
00107         if ( BdbcSuccess != app->fd().contains(dbItr) ) {
00108             app->commit("t");
00109             return BdbcError;
00110         }
00111 
00112         while ( dbItr.next() ) {
00113             const char* dbName = dbItr.name();
00114             if ( 0 != strstr(dbName, "clustHintConfig") &&  0 != strstr(dbName, "evs_") ) {
00115                 if ( BdbcSuccess != runInOneDb(dbName, BdbcUpdate) ) {
00116                     cerr << "failed for db " << dbName << ", aborting all changes" << endl;
00117                     app->abort("t");
00118                     return BdbcError;
00119                 }
00120             }
00121         }
00122     }
00123     
00124 
00125     app->commit("t");
00126 
00127     return BdbcSuccess;
00128 }
00129 
00130 
00131 
00132 BdbStatus
00133 BdbCloseDbs::runInOneDb(const char* dbName, ooMode openMode)
00134 {
00135     stream << "\n ====> Running in db: " << dbName << endl;
00136     
00137     // open database
00138     BdbHandle(BdbDBObj) dbH;
00139     if ( BdbcSuccess != dbH.open(BdbApplicationOrDomain::activeInstance()->fd(), dbName, openMode) ) {
00140         stream << "Cannot open clustering hint database " << dbName << endl;
00141         return BdbcError;
00142     }
00143 
00144     // initialize iterator over all container
00145     ooItr(ooContObj) contItr;
00146     if ( dbH.contains(contItr, openMode) != BdbcSuccess ) {
00147         stream << "Cannot initialize cont iterator" << endl;
00148         return BdbcError;
00149     }
00150 
00151     // iterator over all containers in the database and
00152     // make the appropriete action (depending on container name)
00153     while ( contItr.next() ) {
00154         const char* contName = contItr.name();        
00155         if ( ! strncmp(contName, "evs_", 4) ) {
00156             if ( skipIt(contName) ) {
00157                 continue;
00158             }   
00159             stream << "Closing in container:    \"" << contName << "\"" << endl;
00160             BdbHandle(BdbContObj) contH = contItr;
00161             if ( runInOneCont(contH) != BdbcSuccess ) {
00162                 stream << "Closing dbs in container: \"" << contName 
00163                        << "\" failed, aborting." << endl;
00164                 return BdbcError;
00165             }
00166         } else {
00167             stream << "Ignoring container:      \"" << contName << "\"" << endl;
00168         }
00169     }
00170 
00171     return BdbcSuccess;
00172 }
00173 
00174 
00175 
00176 /**
00177  **  The function decides whether the container should
00178  **  be skipped or not (whether it is of the right type
00179  **  (auth level, auth name, component name)
00180  **/
00181 d_Boolean
00182 BdbCloseDbs::skipIt(const char* contName)
00183 {
00184     if ( 0 != authLevel ) { // pick only a subset with one authLevel
00185         char s[4];
00186         s[0] = '_';
00187         s[1] = authLevel;
00188         s[2] = '_';
00189         s[3] = '\0';
00190         if ( 0 == strstr(contName, s) ) {
00191             stream  << "Skipping " << contName 
00192                     << ", does not contain " << s << endl;
00193             return d_True;
00194         }
00195     }
00196     
00197     if ( 0 != authName ) { // pick only a subset with one authName
00198         char* s = new char[strlen(authName)+3];
00199         sprintf(s, "_%s_", authName);
00200         const char* s1 = strstr(contName, s);
00201         delete [] s;
00202         if ( 0 == s1 ) {
00203             stream  << "Skipping " << contName 
00204                     << ", does not contain " << s << endl;
00205             return d_True;
00206         }
00207     }
00208 
00209     if ( 0 != compName ) { // pick only a subset with one componentName
00210         char* s = new char[strlen(compName)+3];
00211         sprintf(s, "_%s_", compName);
00212         const char* s1 = strstr(contName, s);
00213         delete [] s;
00214         if ( 0 == s1 ) {
00215             stream  << "Skipping " << contName 
00216                     << ", does not contain " << s << endl;
00217             return d_True;
00218         }
00219     }
00220 
00221     return d_False;
00222 }
00223 
00224 
00225 
00226 BdbStatus 
00227 BdbCloseDbs::runInOneCont(BdbHandle(BdbContObj)& contH)
00228 {
00229     const char* pcName = contH.name();
00230     if ( 0 == pcName ) {
00231         stream << "Unable to find param cont name for " << BdbPrintOID(contH) << endl;
00232         return BdbcError;
00233     }
00234     int len = strlen(pcName);
00235     if ( len < 4 ) {
00236         stream << "Incorrect paramContName: " << pcName << endl;
00237         return BdbcError;
00238     }
00239     len -=3;// skip "_v1" at the end
00240     char* paramContName = new char [len+1];
00241     strncpy(paramContName, pcName, len);
00242     *(paramContName+len) = '\0';
00243     
00244     
00245     // iterate over all named objects in the container
00246     // each object corresponds to one stream
00247 
00248     ooItr(ooObj) objI;
00249     if ( contH.getNameObj(objI) != BdbcSuccess ) {
00250         stream << "Cannot initialize container iterator" << endl;
00251         return BdbcError;
00252     }
00253 
00254     while ( objI.next() ) {
00255         stream << "Closing for stream:      \"" << objI.getObjName(contH) << "\"" << endl;
00256         if ( strcmp(objI.typeName(), "BdbPCompParam_001") ) {
00257             stream << "Unexpected object of type " << objI.typeName() << " expected" << endl;
00258             return BdbcError;
00259         }
00260         BdbHandle(BdbPCompParam_001) h = (BdbHandle(BdbPCompParam_001)) objI;
00261         int i, max = h->_activeDbs.size();
00262         for (i=0 ; i<max ; i++) {
00263             int id = h->_activeDbs[i];
00264             ooRef(ooDBObj) dbRef;
00265             dbRef.set_DB(id);
00266             const char* name = dbRef.name();
00267             if ( 0 != name ) {
00268                 stream << "Closing db \"" << name << "\" [" << id << "]" << endl;
00269             } else {
00270                 stream << "Closing db [" << id << "]" << endl;
00271             }
00272             // delete the counter   
00273             ooHandle(ooContObj) cH;
00274             if ( BdbcSuccess != _locatorMgr->locateDbPageCounterCont(id, cH, oocUpdate) ) {
00275                 stream << "Unable to locate page counter for " << id << endl;
00276             } else {
00277                 if ( _reallyClose ) {
00278                     stream << "Deleting " << cH.name() << endl;
00279                     ooDelete(cH);
00280                 }
00281             }
00282             // now register the db as full
00283             BdbHandle(BdbPFullDbsReg) theReg;
00284             if ( BdbcSuccess != _locatorMgr->locateFullDbsReg(theReg, BdbcUpdate) ) {
00285                 stream << "Failed to locate full dbs registry" << endl;
00286                 return BdbcError;
00287             }
00288             if ( _reallyClose ) {
00289                 if ( BdbcSuccess != theReg->add(id) ) {
00290                     stream << "Failed to register " << id << " on the list of full dbs" << endl;
00291                     return BdbcError;
00292                 }
00293                 stream << id << " registered on the list of full dbs" << endl;
00294             }
00295         }
00296     }
00297 
00298     if ( _reallyClose ) {
00299         // delete the container containing BdbPCompParam(s)
00300         const char* n = contH.name();
00301         if ( BdbcSuccess != ooDelete(contH) ) {
00302             stream << "Failed to delete " << n << endl;
00303             return BdbcError;
00304         }
00305         stream << "Container " << n << " deleted" << endl;
00306 
00307 
00308         // delete the registry of dbs with extra pages
00309         BdbHandle(BdbContObj) dbwepH;
00310         if ( BdbcSuccess == _locatorMgr->locateDbsWEPRegistry(dbwepH, paramContName, oocUpdate) ) {
00311             const char* n = dbwepH.name();
00312             if ( BdbcSuccess != ooDelete(dbwepH) ) {
00313                 stream << "Failed to delete dbsWEPRegistry for " << pcName << endl;
00314                 return BdbcError;
00315             }
00316             stream << "Container " << n << " deleted" << endl;
00317         }
00318     }
00319 
00320     return BdbcSuccess;
00321 }
00322 
00323 
00324 
00325 
00326 
00327 int
00328 processArgs(int argc, char** argv)
00329 {
00330     d_Boolean all   = d_False;
00331     d_Boolean other = d_False; // -authLevel, authName, -compName or -clustName
00332     
00333     for (int i=1 ; i<argc ; i++) {
00334         if ( !strcmp(argv[i], "-quiet") ) {
00335             verbose = d_False;
00336         } else if ( !strcmp(argv[i], "-all") ) {
00337             if ( other ) {
00338                 cerr << "-all and [-authLevel | -authName | -compName | -clustName]"
00339                      << " are mutually exclusive" << endl;
00340                 return 1;
00341             }
00342             all       = d_True;
00343             clustName = 0;
00344             authLevel = 0;
00345             authName  = 0;
00346             compName  = 0;
00347         } else if ( !strcmp(argv[i], "-authLevel") ) {
00348             if ( all ) {
00349                 cerr << "-all and -authLevel are mutually exclusive" << endl;
00350                 return 1;
00351             }
00352             other = d_True;
00353             i++;
00354             if ( i >= argc ) {
00355                 cerr << "Missing authorization level name after -authLevel" << endl;
00356                 return 1;
00357             }
00358             if ( argv[i][0] != 's' && argv[i][0] != 'g' && argv[i][0] != 'u' ) {
00359                 cerr << "Expected argument after -authLevel: s  g  u" << endl;
00360                 return 1;
00361             }
00362             authLevel = argv[i][0];
00363         } else if ( !strcmp(argv[i], "-authName") ) {
00364             if ( all ) {
00365                 cerr << "-all and -authName are mutually exclusive" << endl;
00366                 return 1;
00367             }
00368             other = d_True;
00369             i++;
00370             if ( i >= argc ) {
00371                 cerr << "Missing authorization name after -authName" << endl;
00372                 return 1;
00373             }
00374             authName = new char[strlen(argv[i])+1];
00375             strcpy(authName, argv[i]);
00376         } else if ( !strcmp(argv[i], "-compName") ) {
00377             if ( all ) {
00378                 cerr << "-all and -compName are mutually exclusive" << endl;
00379                 return 1;
00380             }
00381             other = d_True;
00382             i++;
00383             if ( i >= argc ) {
00384                 cerr << "Missing component name after -compName" << endl;
00385                 return 1;
00386             }
00387             compName = new char[strlen(argv[i])+1];
00388             strcpy(compName, argv[i]);
00389         } else if ( !strcmp(argv[i], "-clustName") ) {
00390             if ( all ) {
00391                 cerr << "-all and -clustName are mutually exclusive" << endl;
00392                 return 1;
00393             }
00394             other = d_True;
00395             i++;
00396             if ( i >= argc ) {
00397                 cerr << "Missing cluster name after -clustName" << endl;
00398                 return 1;
00399             }
00400             clustName = new char[strlen(argv[i])+1];
00401             strcpy(clustName, argv[i]);
00402         } else {
00403             cerr << "Incorrect argument: " << argv[i] 
00404                  << ", expected -authLevel, -authName, -compName, -clustName" << endl;
00405             return 1;
00406         }
00407     }
00408     cout << "Arguments ok" << endl;
00409     
00410     return 0;         
00411 }
00412 
00413 
00414 void printHelp()
00415 {
00416     cout << "\nBdbCloseDbs. This utility permanently closes databases.\n"
00417          << "Expected arguments:\n"
00418          << "   [-all] [-quiet] [-authLevel s/g/u] [-authName authName] [-compName compName] [-clustName clusterName]\n"
00419          << "   If you specify -all, you cannot specify any of -authLevel, -authName, -compName, -clustName\n"
00420          << "Examples:\n"
00421          << "     1) to close all isPhysicsEvents dbs (all components):\n"
00422          << "           BdbCloseDbs -authLevel g -authName isPhysicsEvents\n"
00423          << "     2) to close all \"sim\":\n"
00424          << "           BdbCloseDbs -compName sim\n"
00425          << "     3) to close all \"aod dbs in isMultiHadron:\n"
00426          << "           BdbCloseDbs -compName aod -authLevel g -authName isMultiHadron\n"
00427          << "     4) to close all \"calibEvents\" databases:\n"
00428          << "           BdbCloseDbs -authLevel g -authName calibEvents\n"
00429          << "     5) to close all dbs in the cluster \"cl1\":\n"
00430          << "           BdbCloseDbs -clustName cl1\n"  << endl;
00431 }
00432 
00433 int main(int argc, char* argv[])
00434 {    
00435     if ( argc == 1 ) {
00436         printHelp();
00437         return 1;
00438     }
00439     
00440     if ( 0 != processArgs(argc, argv) ) {
00441         return 1;
00442     }
00443 
00444     // Start a domain (any domain)
00445     BdbGenericDomain domain( "events" ) ;
00446     domain.activate() ;
00447 
00448     BdbCloseDbs theClass(verbose, reallyClose);
00449     if ( BdbcSuccess != theClass.run() ) {
00450         domain.deactivate() ;
00451         return 1;
00452     }
00453 
00454     domain.deactivate() ;
00455     return 0;    
00456 }
00457 

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002