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  

/CdbBdbShared/CdbBdbSCdb.cc

Go to the documentation of this file.
00001 /// The implementation of the CdbBdbSCdb singleton.
00002 /**
00003   * @see CdbBdbSCdb
00004   */
00005 
00006 #include "BaBar/BaBar.hh"
00007 
00008 #include "CdbBdbShared/CdbBdbSCdb.hh"
00009 
00010 #include "CdbBase/CdbDatabase.hh"
00011 #include "CdbBase/CdbEnvironment.hh"
00012 
00013 #include "CdbBdb/CdbBdbTransaction.hh"
00014 
00015 #include "CdbBdbShared/CdbBdbShared.hh"
00016 #include "CdbBdbShared/CdbBdbSDatabase.hh"
00017 #include "CdbBdbShared/CdbBdbSDatabaseItr.hh"
00018 #include "CdbBdbShared/CdbBdbSMasterRegistryP.hh"
00019 #include "CdbBdbShared/CdbBdbSSlaveRegistryP.hh"
00020 #include "CdbBdbShared/CdbBdbSReplicaRegistryP.hh"
00021 #include "CdbBdbShared/CdbBdbSTestRegistryP.hh"
00022 
00023 #include "BdbTime/BdbTime.hh"
00024 
00025 #include "BdbCond/BdbConditions.hh"
00026 
00027 #include "BdbApplication/BdbDomain.hh"
00028 #include "BdbApplication/BdbDebug.hh"
00029 
00030 #include <assert.h>
00031 #include <iostream.h>
00032 
00033 namespace {
00034 
00035   ////////////////////
00036   // V-table loader //
00037   ////////////////////
00038 
00039     void
00040     loadVTables( const CdbPtr& thePtr )
00041     {
00042         const char* fatalStr = "CdbBdbSCdb::<anonymous>::loadVTables() -- FATAL ERROR.";
00043 
00044       // The smart pointer passed down to this method should always
00045       // point to something to avoid crash below.
00046 
00047         if( thePtr.isNull( )) {
00048 
00049           // Except their creation time, the following classes are never used on its
00050           // own - only through their base classes.
00051           //
00052           // NOTE: It's important to call a virtual method to ensure that the corresponding
00053           //       v-tables are loaded. Just constructing a transient object of a final
00054           //       persistent class is not going to help for some compilers or optimized
00055           //       compilation modes.
00056 
00057             CdbBdbSMasterRegistryP master( "This constructor never gets executed here",
00058                                             BdbTime::plusInfinity );
00059 
00060             BdbRef(CdbBdbSOriginCollectionP) masterOriginCollRef = master.originCollection( );
00061             if( ! BdbIsNull(masterOriginCollRef)) {
00062 
00063                 cout << fatalStr << endl
00064                      << "    You should never see this message. Still if you do then a nasty" << endl
00065                      << "    memory corruption is taking place in the current application." << endl
00066                      << "    Here is a number: " << masterOriginCollRef->elements( ) << endl;
00067                 assert( 0 );
00068 
00069                 return;
00070             }
00071 
00072             CdbBdbSSlaveRegistryP slave( 0,
00073                                          "This constructor never gets executed here",
00074                                           BdbTime::plusInfinity,
00075                                           BdbTime::plusInfinity );
00076 
00077             BdbRef(CdbBdbSOriginCollectionP) slaveOriginCollRef = slave.originCollection( );
00078             if( ! BdbIsNull(slaveOriginCollRef)) {
00079 
00080                 cout << fatalStr << endl
00081                      << "    You should never see this message. Still if you do then a nasty" << endl
00082                      << "    memory corruption is taking place in the current application." << endl
00083                      << "    Here is a number: " << slaveOriginCollRef->elements( ) << endl;
00084                 assert( 0 );
00085 
00086                 return;
00087             }
00088 
00089             CdbBdbSReplicaRegistryP replica( "This constructor never gets executed here",
00090                                               BdbTime::plusInfinity,
00091                                               BdbTime::plusInfinity );
00092 
00093             BdbRef(CdbBdbSOriginCollectionP) replicaOriginCollRef = replica.originCollection( );
00094             if( ! BdbIsNull(replicaOriginCollRef)) {
00095 
00096                 cout << fatalStr << endl
00097                      << "    You should never see this message. Still if you do then a nasty" << endl
00098                      << "    memory corruption is taking place in the current application." << endl
00099                      << "    Here is a number: " << replicaOriginCollRef->elements( ) << endl;
00100                 assert( 0 );
00101 
00102                 return;
00103             }
00104 
00105             CdbBdbSTestRegistryP test( "This constructor never gets executed here",
00106                                        BdbTime::plusInfinity,
00107                                        BdbTime::plusInfinity );
00108 
00109             BdbRef(CdbBdbSOriginCollectionP) testOriginCollRef = test.originCollection( );
00110             if( ! BdbIsNull(testOriginCollRef)) {
00111 
00112                 cout << fatalStr << endl
00113                      << "    You should never see this message. Still if you do then a nasty" << endl
00114                      << "    memory corruption is taking place in the current application." << endl
00115                      << "    Here is a number: " << testOriginCollRef->elements( ) << endl;
00116                 assert( 0 );
00117 
00118                 return;
00119             }
00120         }
00121     }
00122 
00123   ////////////////////////////////////////////////
00124   // Check the status of teh current federation //
00125   ////////////////////////////////////////////////
00126 
00127     bool currentFederationIsInitialized( )
00128     {
00129         CdbBdbTransaction transaction;
00130 
00131         BdbHandle(BdbDBObj) dH;
00132         return d_True == dH.exist( BdbApplicationOrDomain::activeInstance( )->fd( ),
00133                                    "con_cdb_database",
00134                                    BdbcNoOpen );
00135     }
00136 };
00137 
00138 ///////////////////////////////
00139 // CdbBdbSCdb implementation //
00140 ///////////////////////////////
00141 
00142 CdbBdbSCdb::CdbBdbSCdb( ) :
00143     Cdb( )
00144 {
00145     COUT1 << "Condition/DB API instantiated for"
00146           << " TECHNOLOGY=\""     << CdbBdbShared::technology( )     << "\""
00147           << " IMPLEMENTATION=\"" << CdbBdbShared::implementation( ) << "\""
00148           << endl;
00149 
00150   // Register itself in the API registry.
00151   //
00152   // NOTE: That this operation will also replace the previous instance (if any)
00153   //       for the same combination of "technology & implementation" keys.
00154 
00155     CdbPtr myPtr = this;
00156     Cdb::registry( ).set( myPtr );
00157 
00158   // Make sure that v-tables for persistent classes are loaded
00159 
00160     loadVTables( myPtr );
00161 
00162   // Check for the presense of the "Shared" databases in the federation and
00163   // if they are found setup the proper default.
00164 
00165     if( currentFederationIsInitialized( )) {
00166         CdbEnvironment::setDefault( CdbBdbShared::technology( ),
00167                                     CdbBdbShared::implementation( ),
00168                                     "<default>",
00169                                     "<local>::<recent>" );
00170     }
00171 }
00172 
00173 CdbBdbSCdb::~CdbBdbSCdb( )
00174 { }
00175 
00176 const char*
00177 CdbBdbSCdb::technologyName( ) const
00178 {
00179     return CdbBdbShared::technology( );
00180 }
00181 
00182 const char*
00183 CdbBdbSCdb::implementationName( ) const
00184 {
00185     return CdbBdbShared::implementation( );
00186 }
00187 
00188 std::string
00189 CdbBdbSCdb::defaultDatabase( ) const
00190 {
00191     std::string dbName = CdbEnvironment::defaultDatabase( technologyName( ),
00192                                                           implementationName( ));
00193 //    if(( "<default>" == dbName ) || ( "" == dbName )) {
00194     if( "<default>" == dbName ) {
00195         dbName = BdbConditions::instance( )->bootName( );
00196     }
00197     return dbName;
00198 }
00199 
00200 CdbStatus
00201 CdbBdbSCdb::findDatabase( CdbDatabasePtr& thePtr,
00202                           const char*     theName )
00203 {
00204     CdbStatus result = CdbStatus::Error;
00205 
00206     thePtr = 0;
00207 
00208     do {
00209 
00210       // Verify and correct parameters if 0 pointer is passed to indicate
00211       // that default database is needed.
00212 
00213         std::string databaseName = defaultDatabase( );
00214         if( 0 != theName ) databaseName = theName;
00215 
00216         if( databaseName != defaultDatabase( )) {
00217 
00218             cout << "CdbBdbSCdb::findDatabase( ) -- ERROR" << endl
00219                  << "    No such database in this implementation:  \"" << databaseName.c_str( )       << "\"" << endl
00220                  << "    The only name allowed is the default one: \"" << defaultDatabase( ).c_str( ) << "\"" << endl;
00221 
00222             break;
00223         }
00224 
00225       // NOTE: - the above corrected parameters object is used.
00226       //       - a smart pointer onto itself is found through the registry.
00227 
00228         CdbPtr selfPtr = registry( ).get( technologyName( ),
00229                                           implementationName( ));
00230         thePtr = new CdbBdbSDatabase( selfPtr,
00231                                       databaseName.c_str( ));
00232         result = CdbStatus::Success;
00233 
00234     } while( false );
00235 
00236     return result;
00237 }
00238 
00239 CdbStatus
00240 CdbBdbSCdb::databaseIterator( CdbDatabaseItr& theItr )
00241 {
00242   // Note, that we find its own smart pointer through the Registry
00243 
00244   // ATTENTION: This is not a memory leak - the created object
00245   //            will be destroyed by the iterator.
00246 
00247     theItr = CdbDatabaseItr( new CdbBdbSDatabaseItr( registry( ).get( technologyName( ),
00248                                                                       implementationName( ))));
00249 
00250     return CdbStatus::Success;
00251 }
00252 
00253 /////////////////
00254 // End Of File //
00255 /////////////////

 


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

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