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

CdbBdbWCdb.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbWCdb.cc,v 1.18 2005/02/02 13:58:08 gapon Exp $
00003 
00004 /// The implementation of the CdbBdbWCdb singleton.
00005 /**
00006   * @see CdbBdbWCdb
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbBdbWrapper/CdbBdbWCdb.hh"
00012 
00013 #include "CdbBase/CdbEnvironment.hh"
00014 #include "CdbBase/CdbDatabase.hh"
00015 #include "CdbBase/CdbDebugStream.hh"
00016 
00017 #include "CdbBdb/CdbBdbTransaction.hh"
00018 
00019 #include "CdbBdbWrapper/CdbBdbWrapper.hh"
00020 #include "CdbBdbWrapper/CdbBdbWDatabase.hh"
00021 #include "CdbBdbWrapper/CdbBdbWDatabaseItr.hh"
00022 
00023 #include "BdbCond/BdbConditions.hh"
00024 
00025 #include "ErrLogger/ErrLog.hh"
00026 
00027 #include <string.h>
00028 #include <assert.h>
00029 using std::endl;
00030 
00031 CdbBdbWCdb::CdbBdbWCdb( ) :
00032     Cdb( ),
00033     _isInitialized(false)   // For the sake of deffered initialization
00034 {
00035     CDB_DEBUG_STREAM << "Condition/DB API instantiated for"
00036                      << " TECHNOLOGY=\"" << CdbBdbWrapper::technology( ) << "\""
00037                      << " IMPLEMENTATION=\"" << CdbBdbWrapper::implementation( ) << "\""
00038                      << endl;
00039 
00040   // Try to register itself in the API registry.
00041   //
00042   // NOTE: If the registration will fail then we may probably encounter a fatal
00043   //       unrecoverable problem with implementations of CDB API. One of the possible
00044   //       reasons of the falure would be if another implementation of the API has already
00045   //       registered itself in the dictionary under the same names.
00046 
00047     CdbPtr myPtr = this;
00048 
00049     if( CdbStatus::Success != set( myPtr ))
00050         ErrMsg(fatal) << "failed to register the top level API component in the dictionary." << endmsg;
00051 }
00052 
00053 CdbBdbWCdb::~CdbBdbWCdb( )
00054 { }
00055 
00056 const char*
00057 CdbBdbWCdb::technologyName( ) const
00058 {
00059     return CdbBdbWrapper::technology( );
00060 }
00061 
00062 const char*
00063 CdbBdbWCdb::implementationName( ) const
00064 {
00065     return CdbBdbWrapper::implementation( );
00066 }
00067 
00068 void
00069 CdbBdbWCdb::initialize( )
00070 {
00071     if( !_isInitialized ) {
00072 
00073       // There is nothing else we should do to be initializaed
00074 
00075         _isInitialized = true;
00076     }
00077 }
00078 
00079 bool
00080 CdbBdbWCdb::isInitialized( )
00081 {
00082     return _isInitialized;
00083 }
00084 
00085 CdbStatus
00086 CdbBdbWCdb::getDefaultDatabase( std::string& theDatabase )
00087 {
00088     assert( isInitialized( ));
00089 
00090     theDatabase = BdbConditions::instance( )->bootName( );
00091 
00092     return CdbStatus::Success;
00093 }
00094 
00095 CdbStatus
00096 CdbBdbWCdb::getDefaultView( std::string& theView,
00097                             const char*  theDatabaseName )
00098 {
00099     assert( isInitialized( ));
00100 
00101     CdbStatus result = CdbStatus::NotFound;
00102     do {
00103 
00104       // Verify the database name
00105       //
00106       // We only allow the following:
00107       //
00108       //   - "<default>"
00109       //   - the actual BOOT file name of the CDB federation.
00110       //
00111       // All these options are treated equally since we only have just one database
00112       // in the current implementation.
00113 
00114         if( 0 == theDatabaseName ) break;
00115 
00116         if( ! (( 0 == strcmp( theDatabaseName, "<default>" )) ||
00117                ( 0 == strcmp( theDatabaseName, BdbConditions::instance( )->bootName( ))))) break;
00118 
00119       // The only name we know about is the pseudo name of the most recent view
00120       // in the local database.
00121 
00122         theView = "<local>::<recent>";
00123 
00124       // Done
00125 
00126         result = CdbStatus::Success;
00127 
00128     } while( false );
00129 
00130     return result; 
00131 }
00132 
00133 CdbStatus
00134 CdbBdbWCdb::setDefaultDatabase( const char* theDatabaseName )
00135 {
00136     assert( isInitialized( ));
00137 
00138   // This is basically "do nothing; method. It just checks for the correctness
00139   // of the database name.
00140 
00141     CdbStatus result = CdbStatus::NotFound;
00142     do {
00143 
00144       // Verify the database name
00145       //
00146       // We only allow the following:
00147       //
00148       //   - "<default>"
00149       //   - the actual BOOT file name of the CDB federation.
00150       //
00151       // All these options are treated equally since we only have just one database
00152       // in the current implementation.
00153 
00154         if( 0 == theDatabaseName ) break;
00155 
00156         if( ! (( 0 == strcmp( theDatabaseName, "<default>" )) ||
00157                ( 0 == strcmp( theDatabaseName, BdbConditions::instance( )->bootName( ))))) break;
00158 
00159       // Done
00160 
00161         result = CdbStatus::Success;
00162 
00163     } while( false );
00164 
00165     return result; 
00166 }
00167 
00168 CdbStatus
00169 CdbBdbWCdb::setDefaultView( const char* theViewName,
00170                             const char* theDatabaseName )
00171 {
00172     assert( isInitialized( ));
00173 
00174   // This is basically "do nothing; method. It just checks for the correctness
00175   // of the database name.
00176 
00177     CdbStatus result = CdbStatus::NotFound;
00178     do {
00179 
00180       // Verify the database name
00181       //
00182       // We only allow the following:
00183       //
00184       //   - "<default>"
00185       //   - the actual BOOT file name of the CDB federation.
00186       //
00187       // All these options are treated equally since we only have just one database
00188       // in the current implementation.
00189 
00190         if( 0 == theDatabaseName ) break;
00191 
00192         if( ! (( 0 == strcmp( theDatabaseName, "<default>" )) ||
00193                ( 0 == strcmp( theDatabaseName, BdbConditions::instance( )->bootName( ))))) break;
00194 
00195 
00196       // Verify the database name
00197       //
00198       // The only name we know about is the pseudo name of the most recent view
00199       // in the local database.
00200 
00201         if( 0 == theViewName ) break;
00202 
00203         if( 0 != strcmp( theViewName, "<local>::<recent>" )) break;
00204 
00205       // Done
00206 
00207         result = CdbStatus::Success;
00208 
00209     } while( false );
00210 
00211     return result; 
00212 }
00213 
00214 CdbStatus
00215 CdbBdbWCdb::findDatabase( CdbDatabasePtr& thePtr,
00216                           const char*     theName )
00217 {
00218     assert( isInitialized( ));
00219 
00220     CdbStatus result = CdbStatus::Error;
00221 
00222     thePtr = 0;
00223 
00224     do {
00225 
00226       // Verify and correct parameters if 0 pointer is passed to indicate
00227       // that default database is needed. The "<default>" string is also allowed
00228       // since the current implementation maps it onto the boot file name.
00229 
00230         std::string defaultDatabaseName;
00231         if( CdbStatus::Success != getDefaultDatabase( defaultDatabaseName )) break;
00232 
00233         std::string databaseName = defaultDatabaseName;
00234         if( 0 != theName ) {
00235             if( 0 != strcmp( "<default>", theName )) {
00236 
00237               // This is a special keyword to be mapped onto whatever default
00238               // bootfile name we have.
00239 
00240                 ;
00241 
00242             } else {
00243                 databaseName = theName;
00244             }
00245         }
00246         if( databaseName != defaultDatabaseName ) {
00247 
00248             ErrMsg(error) << "no such database in this implementation: \"" << databaseName << "\"" << endl
00249                           << "The only name allowed is the default one: \"" << defaultDatabaseName << "\"" << endmsg;
00250 
00251             break;
00252         }
00253 
00254       // NOTE: - the above corrected parameters object is used.
00255       //       - a smart pointer onto itself is found through the registry.
00256 
00257         CdbPtr selfPtr;
00258         if( CdbStatus::Success != get( selfPtr,
00259                                        technologyName( ),
00260                                        implementationName( ))) {
00261 
00262             ErrMsg(fatal) << "failed to recover its own instance of the CDB API." << endl
00263                           << "    TECHNOLOGY     : \"" << technologyName( ) << "\"" << endl
00264                           << "    IMPLEMENTATION : \"" << implementationName( ) << "\"" << endmsg;
00265         }
00266         thePtr = new CdbBdbWDatabase( selfPtr,
00267                                       databaseName.c_str( ));
00268         result = CdbStatus::Success;
00269 
00270     } while( false );
00271 
00272     return result;
00273 }
00274 
00275 CdbStatus
00276 CdbBdbWCdb::databaseIterator( CdbDatabaseItr& theItr )
00277 {
00278     assert( isInitialized( ));
00279 
00280   // Note, that we find its own smart pointer through the Registry
00281 
00282   // ATTENTION: This is not a memory leak - the created object
00283   //            will be destroyed by the iterator.
00284 
00285     CdbPtr selfPtr;
00286     if( CdbStatus::Success != get( selfPtr,
00287                                    technologyName( ),
00288                                    implementationName( ))) {
00289 
00290         ErrMsg(fatal) << "failed to recover its own instance of the CDB API." << endl
00291                       << "    TECHNOLOGY     : \"" << technologyName( ) << "\"" << endl
00292                       << "    IMPLEMENTATION : \"" << implementationName( ) << "\"" << endmsg;
00293     }
00294     theItr = CdbDatabaseItr( new CdbBdbWDatabaseItr( selfPtr ));
00295 
00296     return CdbStatus::Success;
00297 }
00298 
00299 CdbTransactionBase*
00300 CdbBdbWCdb::transaction( CdbTransaction::Mode theMode ) const
00301 {
00302   // Translate the transaction mode from "Bdb"-specific enum into
00303   // the corresponding technology-neutral one.
00304 
00305     switch( theMode ) {
00306     case CdbTransaction::Read:   return new CdbBdbTransaction( BdbcRead );
00307     case CdbTransaction::Update: return new CdbBdbTransaction( BdbcUpdate );
00308     };
00309 
00310   // For unknown mode complain report the problem, try to crash the application,
00311   // and if this doesn't help then return 0.
00312 
00313     ErrMsg(fatal) << "CdbBdbWCdb::transaction( ) -- FATAL ERROR" << endl
00314                   << "    Unssuported transaction mode: " << theMode << endl
00315                   << "    The implementation of the current method doesn't seem to follow changes" << endl
00316                   << "    in the technology-neutral CDB API." << endmsg;
00317     return 0;
00318 }
00319 
00320 /////////////////
00321 // End Of File //
00322 /////////////////

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