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

CdbBdbSMasterRegistryP.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbSMasterRegistryP.cc,v 1.7 2004/08/06 05:54:24 bartoldu Exp $
00003 
00004 /// Implementation file for the CdbBdbSMasterRegistryP class
00005 /**
00006   * @see CdbBdbSMasterRegistryP
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbBdbShared/CdbBdbSMasterRegistryP.hh"
00012 #include "CdbBdbShared/CdbBdbSUtils.hh"
00013 #include "CdbBdbShared/CdbBdbSOriginP.hh"
00014 
00015 #include "BdbClustering/BdbCondIndexClusteringHint.hh"
00016 
00017 #include <iostream>
00018 #include <assert.h>
00019 
00020 #include <string>
00021 using std::cout;
00022 using std::endl;
00023 
00024 CdbStatus
00025 CdbBdbSMasterRegistryP::create( BdbHandle(CdbBdbSRegistryP)& theRegistryH,
00026                                 const char*                  theDescription,
00027                                 const BdbTime&               theCreationTime )
00028 {
00029     const char* errorStr = "CdbBdbSMasterRegistryP::create() -- ERROR";
00030 
00031     theRegistryH = 0;
00032 
00033     CdbStatus result = CdbStatus::Error;
00034     do {
00035 
00036       // Verify parameters
00037 
00038         if( 0 == theDescription ) {
00039             cout << errorStr << endl
00040                  << "    Invalid description string passed as a parameter. Non-zero" << endl
00041                  << "    length null terminated string was expected." << endl;
00042             break;
00043         }
00044 
00045       // Check if the local registry already exists
00046 
00047         BdbHandle(CdbBdbSRegistryP) localRegistryH;
00048         if( CdbStatus::Success == findLocal( localRegistryH )) {
00049             cout << errorStr << endl
00050                  << "    There local registry already exists in the current Condition/DB." << endl;
00051             break;
00052         }
00053 
00054       // Check if the master already exists
00055 
00056         BdbHandle(CdbBdbSRegistryP) masterRegistryH;
00057         if( CdbStatus::Success == findMaster( masterRegistryH )) {
00058             cout << errorStr << endl
00059                  << "    There is already a master registry in the current Condition/DB." << endl;
00060             break;
00061         }
00062 
00063       // Find the name of the DBID range of the current federation.
00064       // Make sure this is the right federation.
00065 
00066         std::string localDbIdRangeName;
00067         if( CdbStatus::Success != CdbBdbSUtils::getLocalDbIdRange( localDbIdRangeName )) {
00068             cout << errorStr << endl
00069                  << "    Failed to find the local DBID range." << endl;
00070             break;
00071         }
00072         if( localDbIdRangeName == defaultDbIdRange( )) {
00073             cout << errorStr << endl
00074                  << "    The current federation has inappropriate local DBID range, which" << endl
00075                  << "    can't be used to create a MASTER kind of database." << endl
00076                  << "        LOCAL DBID TANGE: " << localDbIdRangeName.c_str( ) << endl;
00077             break;
00078         }
00079 
00080       // Generate the hint and create the container
00081 
00082         BdbCondIndexClusteringHint hint( "cdb",
00083                                          "System",
00084                                          "database" );
00085 
00086         BdbHandle(BdbContObj) contH = hint.updatedHint( );
00087         if( BdbIsNull(contH)) {
00088             cout << errorStr << endl
00089                  << "    Failed to create a container for persistent objects." << endl;
00090             break;
00091         }
00092 
00093       // The following check should always be positive, unless we have
00094       // an unexpected missmatch between ::findIntervalCont() and ::updatedHint()
00095       // behavior.
00096 
00097         if( ! hint.newContainerCreated( )) {
00098             cout << errorStr << endl
00099                  << "    The clustering hint has not created a new persistent container." << endl;
00100             break;
00101         }
00102 
00103       // Create the object.
00104 
00105         theRegistryH = new( contH ) CdbBdbSMasterRegistryP( theDescription,
00106                                                             theCreationTime );
00107         if( BdbIsNull(theRegistryH)) {
00108             cout << errorStr << endl
00109                  << "    Failed to create a persistent registry object." << endl;
00110             break;
00111         }
00112 
00113       // Done
00114 
00115         result = CdbStatus::Success;
00116 
00117     } while( false );
00118 
00119     return result;
00120 }
00121 
00122 CdbBdbSMasterRegistryP::CdbBdbSMasterRegistryP( const char*    theDescription,
00123                                                 const BdbTime& theCreationTime ) :
00124     CdbBdbSRegistryP( CdbBdbSOriginP::MASTER_ID,    // origin identifier
00125                       theDescription,               //
00126                       theCreationTime,              //
00127                       theCreationTime,              // unique database identifier
00128                       true ),                       // has local collections
00129     _originCollectionRef(0),
00130     _partitionsLayoutRef(0),
00131     _pClusterCollectionRef(0),
00132     _originRallocatorRef(0),
00133     _partitionRallocatorRef(0),
00134     _pClusterRallocatorRef(0)
00135 {
00136     assert( 0 != theDescription );
00137 
00138   // Make sure the code is run against the right federation (DBID range)
00139 
00140     std::string localRangeName;
00141     {
00142         CdbStatus result = CdbBdbSUtils::getLocalDbIdRange( localRangeName );
00143         assert( CdbStatus::Success == result );
00144 
00145       // The MASTER kind of databases can NOT be created in the default
00146       // space of DBID ranges since MASTER may export databases to SLAVE
00147       // and TEST kinds of databases.
00148 
00149         assert( localRangeName != defaultDbIdRange( ));
00150     }
00151 
00152   // Create master's collections and resources allocators.
00153   //
00154   // NOTE: To minimiza lock conflicts these collections can be created in
00155   //       other (than the current) containers.
00156 
00157     _originCollectionRef    = new( ooThis( )) CdbBdbSOriginCollectionP ( );
00158     _partitionsLayoutRef    = new( ooThis( )) CdbBdbSPartitionsLayoutP ( );
00159     _pClusterCollectionRef  = new( ooThis( )) CdbBdbSClusterCollectionP( );
00160 
00161     _originRallocatorRef    = new( ooThis( )) CdbBdbSRallocatorP< d_UShort >( );
00162     _partitionRallocatorRef = new( ooThis( )) CdbBdbSRallocatorP< d_UShort >( );
00163     _pClusterRallocatorRef  = new( ooThis( )) CdbBdbSRallocatorP< d_UShort >( );
00164 
00165   // Create and register the predefined origins using:
00166   //
00167   // - use predefined identifiers (their allocation is "force"-ed)
00168   // - appropriate (in each particular case) local range of DBID-s
00169   // - use the same creation time
00170   // - put origin objects into the same container
00171 
00172     CdbStatus              status;
00173     BdbRef(CdbBdbSOriginP) originRef;
00174 
00175   // MASTER:
00176 
00177     originRef = new( ooThis( )) CdbBdbSOriginP( CdbBdbSOriginP::type2name( CdbBdbSOriginP::MASTER ),
00178                                                 CdbBdbSOriginP::MASTER_ID,
00179                                                 CdbBdbSOriginP::MASTER,
00180                                                 theCreationTime,
00181                                                 "The origin for any kind of MASTER databases.",
00182                                                 localRangeName.c_str( ));
00183 
00184     status = _originCollectionRef->insert( originRef );
00185     assert( CdbStatus::Success == status );
00186 
00187   // TEST:
00188 
00189     originRef = new( ooThis( )) CdbBdbSOriginP( CdbBdbSOriginP::type2name( CdbBdbSOriginP::TEST ),
00190                                                 CdbBdbSOriginP::TEST_ID,
00191                                                 CdbBdbSOriginP::TEST,
00192                                                 theCreationTime,
00193                                                 "The origin for any kind of TEST databases.",
00194                                                 defaultDbIdRange( ));
00195 
00196     status = _originCollectionRef->insert( originRef );
00197     assert( CdbStatus::Success == status );
00198 
00199   // Force allocation for reserved origin identifiers, including those once
00200   // used above.
00201 
00202     d_UShort first = CdbBdbSOriginP::FIRST_ID;
00203     d_UShort last  = CdbBdbSOriginP::LAST_ID;
00204 
00205     for( d_UShort id = first; id <= last; ++id ) {
00206         status = _originRallocatorRef->force( id );
00207         assert( CdbStatus::Success == status );
00208     }
00209     
00210 }
00211 
00212 CdbBdbSMasterRegistryP::~CdbBdbSMasterRegistryP( ) 
00213 {
00214     if( ! BdbIsNull(_originCollectionRef))    BdbDelete(_originCollectionRef);
00215     if( ! BdbIsNull(_partitionsLayoutRef))    BdbDelete(_partitionsLayoutRef);
00216     if( ! BdbIsNull(_pClusterCollectionRef))  BdbDelete(_pClusterCollectionRef);
00217 
00218     if( ! BdbIsNull(_originRallocatorRef))    BdbDelete(_originRallocatorRef);
00219     if( ! BdbIsNull(_partitionRallocatorRef)) BdbDelete(_partitionRallocatorRef);
00220     if( ! BdbIsNull(_pClusterRallocatorRef))  BdbDelete(_pClusterRallocatorRef);
00221 }
00222 
00223 bool
00224 CdbBdbSMasterRegistryP::isMaster( ) const
00225 {
00226     return true;
00227 }
00228 
00229 BdbRef(CdbBdbSOriginCollectionP)
00230 CdbBdbSMasterRegistryP::originCollection( ) const
00231 {
00232     return _originCollectionRef;
00233 }
00234 
00235 BdbRef(CdbBdbSPartitionsLayoutP)
00236 CdbBdbSMasterRegistryP::partitionsLayout( ) const
00237 {
00238     return _partitionsLayoutRef;
00239 }
00240 
00241 BdbRef(CdbBdbSClusterCollectionP)
00242 CdbBdbSMasterRegistryP::pClusterCollection( ) const
00243 {
00244     return _pClusterCollectionRef;
00245 }
00246 
00247 BdbRef(CdbBdbSRallocatorP< d_UShort >)
00248 CdbBdbSMasterRegistryP::originRallocator( ) const
00249 {
00250     return _originRallocatorRef;
00251 }
00252 
00253 BdbRef(CdbBdbSRallocatorP< d_UShort >)
00254 CdbBdbSMasterRegistryP::partitionRallocator( ) const
00255 {
00256     return _partitionRallocatorRef;
00257 }
00258 
00259 BdbRef(CdbBdbSRallocatorP< d_UShort >)
00260 CdbBdbSMasterRegistryP::pClusterRallocator( ) const
00261 {
00262     return _pClusterRallocatorRef;
00263 }
00264 
00265 /////////////////
00266 // End Of File //
00267 /////////////////

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