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

CdbRooInit.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: CdbRooInit.cc,v 1.1 2005/02/09 17:14:35 gapon Exp $
00004 //
00005 // Description:
00006 //      Class CdbRooInit.  Initialize Cdb.
00007 //
00008 // Environment:
00009 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00010 //
00011 // Author List:
00012 //     David Brown, Igor Gaponenko 4/26/02
00013 //
00014 // Copyright Information:
00015 //      Copyright (C) 2002              Lawrence Berkeley Laboratory
00016 //
00017 //------------------------------------------------------------------------
00018 
00019 #include "BaBar/BaBar.hh"
00020 
00021 #include "CdbRooModules/CdbRooInit.hh"
00022 
00023 #include "CdbBase/CdbEnvironment.hh"
00024 #include "CdbBase/CdbDatabase.hh"
00025 #include "CdbBase/CdbOrigin.hh"
00026 #include "CdbBase/CdbTransaction.hh"
00027 #include "CdbBase/CdbDebugStream.hh"
00028 
00029 #include "CdbRooReadonly/CdbRooReadonly.hh"
00030 
00031 #include "ErrLogger/ErrLog.hh"
00032 
00033 #include "BbrStdUtils/Tokenize.hh" 
00034 using namespace babar::String; 
00035 #include "BbrStdUtils/String.hh"
00036 #include <string>
00037 using std::endl;
00038 using std::string;
00039 
00040 namespace {
00041 
00042   // These are the minimally allowed and the default interval (in seconds) between
00043   // restarted transactions if the corresponding option is choosen. 
00044 
00045   const double MINIMAL_INTERVAL_BETWEEN_RESTARTS = 1.0;
00046   const double DEFAULT_INTERVAL_BETWEEN_RESTARTS = 300.0;
00047 };
00048 
00049 CdbRooInit::CdbRooInit( const char* const theName,
00050                         const char* const theDescription ) :
00051   AppModule( theName, theDescription ),
00052   _implementation( "Implementation", this ),
00053   _view( "View", this, "<local>::<recent>" ),
00054   _truncateTime( "TruncateTime", this, "+Infinity" ),
00055   _mainTransactionPolicy( "MainTransactionPolicy", this ),
00056   _mainTransactionRestartInterval( "MainTransactionRestartInterval", this, DEFAULT_INTERVAL_BETWEEN_RESTARTS ),
00057   _numEvents(0),
00058   _previousTimestamp(BdbTime::minusInfinity),
00059   _allowedOrigins("AllowedOrigins", this, "*" ),
00060   _myTransactionPtr(0)
00061 {
00062   commands( )->append( &_implementation );
00063   commands( )->append( &_view );
00064   commands( )->append( &_truncateTime );
00065   commands( )->append( &_mainTransactionPolicy );
00066   commands( )->append( &_mainTransactionRestartInterval );
00067   commands( )->append( &_allowedOrigins );
00068 
00069   // create the commands: first entry becomes default
00070   _implementation.addItem( "<autodetection>", CdbRooInit::AUTODETECTION );
00071   _implementation.addItem( "Readonly",        CdbRooInit::READONLY );
00072 
00073   _mainTransactionPolicy.addItem( "RestartedLongTransaction",  CdbRooInit::RESTARTED_LONG_MAIN_TRANSACTION );
00074   _mainTransactionPolicy.addItem( "LongTransaction",           CdbRooInit::LONG_MAIN_TRANSACTION );
00075   _mainTransactionPolicy.addItem( "ShortTransaction",          CdbRooInit::SHORT_MAIN_TRANSACTION );
00076   _mainTransactionPolicy.addItem( "NoTransaction",             CdbRooInit::NO_MAIN_TRANSACTION );
00077 
00078   // force linking of the CDB API implementation
00079   CdbRooReadonly::forceLoad();
00080 }
00081 
00082 CdbRooInit::~CdbRooInit( )
00083 {
00084     commitTransaction( );
00085 }
00086 
00087 AppResult
00088 CdbRooInit::beginJob(AbsEvent* anEvent)
00089 {
00090     _previousTimestamp = BdbTime::now( );
00091 
00092     if( CdbRooInit::NO_MAIN_TRANSACTION != _mainTransactionPolicy.value( )) {
00093 
00094         CDB_DEBUG_STREAM << "Starting main CDB trans in CdbRooInit::beginJob" << endl;
00095 
00096         if( CdbRooInit::RESTARTED_LONG_MAIN_TRANSACTION == _mainTransactionPolicy.value( )) {
00097 
00098             if( _mainTransactionRestartInterval.value( ) < MINIMAL_INTERVAL_BETWEEN_RESTARTS ) {
00099                 ErrMsg(fatal) << "An incorrect value of the \"MainTransactionRestartInterval\" = "
00100                               << _mainTransactionRestartInterval.value( ) << " detected by the module." << endl
00101                               << "    The parameter must be equal or greater than " << MINIMAL_INTERVAL_BETWEEN_RESTARTS << " seconds." << endl
00102                               << "    Note, that this is a fatal error. The application is considered misconfigured." << endmsg;
00103                 return AppResult::ERROR;
00104             }
00105             CDB_DEBUG_STREAM << "The main CDB transaction will be restarted every " << _mainTransactionRestartInterval.value( ) << " seconds at CdbRooInit::event()." << endl;
00106         }
00107         startTransaction( );
00108     }
00109 
00110   // Set requested defaults
00111   switch( _implementation.value( )){
00112 
00113   case READONLY:
00114 
00115     CdbEnvironment::setDefault("Roo",
00116                                "Readonly",
00117                                "<default>",
00118                                _view.value( ));
00119     break;
00120 
00121   case AUTODETECTION:
00122 
00123   default:
00124 
00125     // Rely on the CDB API "autodetection" mechanizm for the CDB API implementation.
00126     // Explicitly set up a user specified view if the one specified by a user differs
00127     // from the current default. This extra check is done to avoid noisy diagnostics
00128     // produced by CDB API when modifying default.
00129 
00130     std::string t = CdbEnvironment::defaultTechnology    ( );
00131     std::string i = CdbEnvironment::defaultImplementation( t.c_str( ));
00132     std::string d = CdbEnvironment::defaultDatabase      ( t.c_str( ), i.c_str( ));
00133     std::string v = CdbEnvironment::defaultView          ( t.c_str( ), i.c_str( ), d.c_str( ));
00134 
00135     if( v != std::string( _view.value( ))) {
00136       CdbEnvironment::setDefault( t.c_str( ),
00137                                   i.c_str( ),
00138                                   d.c_str( ),
00139                                   _view.value( ));
00140     }
00141     break;
00142   }
00143   ErrMsg(warning) << "CdbRooInit: Using CDB view \"" << _view.value( ) << "\"." << endmsg;
00144 
00145   // set truncate time if changed from the default (+Infinity)
00146   if( 0 != strcmp( "+Infinity", _truncateTime.value( ))) {
00147     BdbTime truncateTime( 0 );
00148     if( !BdbTime::parseTime( std::string( _truncateTime.value( )),
00149                              BdbTime::Local,
00150                              truncateTime )) {
00151       ErrMsg(error) << "CdbRooInit: Failed to trunslate specified value of the truncate time \"" << _truncateTime.value( ) << "\"." << endmsg;
00152       return AppResult::ERROR;
00153     }
00154     CdbEnvironment::setTruncateTime( truncateTime );
00155     ErrMsg(warning) << "CdbRooInit: Using truncate time \"" << _truncateTime.value( ) << "\" \"Local Timezone\"." << endmsg;
00156   }
00157 
00158   // Make sure we're running on the right type of CDB installation (so called "origin")
00159   // See the description of the syntax of the corresponding parameter at the header file
00160   // of the current class.
00161   //
00162   // NOTE: That the diagnostics message will only be reported if a non-default
00163   //       value of the parameter is set up.
00164 
00165   if( 0 != strcmp( "*", _allowedOrigins.value( ))) {
00166 
00167     CdbTransaction readOnlyTransaction;  // the transaction will end at the end of the current block
00168 
00169     CdbDatabasePtr databasePtr;
00170     if( CdbStatus::Success != CdbDatabase::instance( databasePtr )) {
00171       ErrMsg(fatal) << "CdbRooInit: Failed to obtain an API object for the default database." << endmsg;
00172       return AppResult::ERROR;
00173     }
00174     CdbOriginPtr originPtr = databasePtr->localOrigin( );
00175     if( originPtr.isNull( )) {
00176       ErrMsg(fatal) << "CdbRooInit: Failed to obtain an API object for the current database origin." << endmsg;
00177       return AppResult::ERROR;
00178     }
00179 
00180     ErrMsg(warning) << "CdbRooInit: Using user defined filter of CDB origins AllowedOrigins=\"" << _allowedOrigins.value( ) << "\"." << endmsg;
00181     ErrMsg(warning) << "CdbRooInit: The current origin is \"" << originPtr->name( ) << "\"." << endmsg;
00182 
00183     // Now translate the filter and cross-check it with the current origin
00184 
00185     bool isAllowedOrigin = false;
00186     {
00187       Tokenize theTokenizer( _allowedOrigins.value( ));
00188       while( true ) {
00189         std::string origin = theTokenizer( "," );
00190         if( origin.empty( )) break;
00191 
00192         if( 0 == strcmp( originPtr->name( ), origin.c_str( ))) {
00193           isAllowedOrigin = true;
00194           break;
00195         }
00196       }
00197     }
00198     if( !isAllowedOrigin ) {
00199       ErrMsg(fatal) << "CdbRooInit: The current CDB database you're attempting to use is not"
00200                     << " explicitly allowed by the \"AllowedOrigins\" parameter of CdbRooInit module."
00201                     << " This parameter is here to prevent your application from running against a wrong CDB." << endmsg;
00202       return AppResult::ERROR;
00203     }
00204   }
00205   return AppResult::OK;
00206 }
00207 
00208 AppResult
00209 CdbRooInit::event( AbsEvent* anEvent )
00210 {
00211     ++_numEvents;
00212 
00213 // Uncomment this debugging message if needed:
00214 //
00215 //  CDB_DEBUG_STREAM << "CdbRooInit::event() _numEvents = " << _numEvents << endl;
00216 
00217     switch( _mainTransactionPolicy.value( )) {
00218 
00219     case CdbRooInit::RESTARTED_LONG_MAIN_TRANSACTION:
00220         {
00221             BdbTime currentTimestamp( BdbTime::now( ));
00222             d_ULong nSecondsElapsed = currentTimestamp.getGmtSec( ) - _previousTimestamp.getGmtSec( );
00223 
00224             if( nSecondsElapsed >= _mainTransactionRestartInterval.value( )) {
00225                 CDB_DEBUG_STREAM << "Restarting main CDB trans in CdbRooInit::event after " << _mainTransactionRestartInterval.value( ) << " seconds." << endl;
00226                 commitTransaction( );
00227                 startTransaction( );
00228                _previousTimestamp = currentTimestamp;
00229             }
00230         }
00231         break;
00232 
00233     case CdbRooInit::SHORT_MAIN_TRANSACTION:
00234 
00235         if( 2 == _numEvents ) {
00236             CDB_DEBUG_STREAM << "Commiting main CDB trans in CdbRooInit::event at event " << _numEvents << endl;
00237             commitTransaction( );
00238         }
00239         break;
00240 
00241     default:
00242 
00243         break;
00244     }
00245     return AppResult::OK;
00246 }
00247 
00248 AppResult
00249 CdbRooInit::endJob( AbsEvent* anEvent )
00250 {
00251     if( CdbRooInit::NO_MAIN_TRANSACTION != _mainTransactionPolicy.value( )) {
00252         CDB_DEBUG_STREAM << "Commiting main CDB trans in CdbRooInit::endJob" << endl;
00253         commitTransaction( );
00254     }
00255     return AppResult::OK;
00256 }
00257 
00258 void
00259 CdbRooInit::startTransaction( )
00260 {
00261     if( 0 == _myTransactionPtr ) _myTransactionPtr = new CdbTransaction( );
00262 }
00263 
00264 void
00265 CdbRooInit::commitTransaction( )
00266 {
00267     if( 0 != _myTransactionPtr ) {
00268         delete _myTransactionPtr;
00269         _myTransactionPtr = 0;
00270     }
00271 }
00272 
00273 /////////////////
00274 // End Of File //
00275 /////////////////

Generated on Mon Dec 5 18:22:09 2005 for CDB by doxygen1.3-rc3