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

CdbBdbSSlaveRegistryP.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbSSlaveRegistryP.cc,v 1.6 2004/08/06 05:54:25 bartoldu Exp $
00003 
00004 /// Implementation file for the CdbBdbSSlaveRegistryP class
00005 /**
00006   * @see CdbBdbSSlaveRegistryP
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbBdbShared/CdbBdbSSlaveRegistryP.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 CdbBdbSSlaveRegistryP::create( BdbHandle(CdbBdbSRegistryP)& theRegistryH )
00026 {
00027     const char* errorStr = "CdbBdbSSlaveRegistryP::create() -- ERROR";
00028 
00029     theRegistryH = 0;
00030 
00031     CdbStatus result = CdbStatus::Error;
00032     do {
00033 
00034       // Check if the local registry already exists
00035 
00036         BdbHandle(CdbBdbSRegistryP) localRegistryH;
00037         if( CdbStatus::Success == findLocal( localRegistryH )) {
00038             cout << errorStr << endl
00039                  << "    There local registry already exists in the current Condition/DB." << endl;
00040             break;
00041         }
00042 
00043       // Get to the MASTER registry to extract the information about
00044       // its unique identifier and the origin of the new SLAVE registry. This (origin) record
00045       // should already exist in the MASTER's collection.
00046 
00047         BdbHandle(CdbBdbSRegistryP) masterRegistryH;
00048         if( CdbStatus::Success != findMaster( masterRegistryH )) {
00049             cout << errorStr << endl
00050                  << "    Failed to locate MASTER registry in the current federation." << endl
00051                  << "    The federation has to be preloaded with at least a partial" << endl
00052                  << "    snapshot of the MASTER database in order to initialize a SLAVE one." << endl;
00053             break;
00054         }
00055 
00056       // Find the name of the DBID range of the current federation.
00057       // Make sure this is the right federation.
00058 
00059         std::string localDbIdRangeName;
00060         if( CdbStatus::Success != CdbBdbSUtils::getLocalDbIdRange( localDbIdRangeName )) {
00061             cout << errorStr << endl
00062                  << "    Failed to find the local DBID range." << endl;
00063             break;
00064         }
00065         if( localDbIdRangeName == defaultDbIdRange( )) {
00066             cout << errorStr << endl
00067                  << "    The current federation has inappropriate local DBID range, which" << endl
00068                  << "    can't be used to create a SLAVE kind of database." << endl
00069                  << "        LOCAL RANGE: " << localDbIdRangeName.c_str( ) << endl;
00070             break;
00071         }
00072 
00073       // Find the origin corresponding to the DBID range of the current federation.
00074 
00075         BdbHandle(CdbBdbSOriginCollectionP) originCollectionH;
00076 
00077         originCollectionH = masterRegistryH->originCollection( );
00078         assert( !BdbIsNull(originCollectionH));
00079 
00080         BdbHandle(CdbBdbSOriginP) originH;
00081         {
00082             bool failed = false;
00083 
00084             CdbBdbSOriginCollectionP::IteratorOfIdentifiers itr = originCollectionH->iterator_identifiers( );
00085             while( itr.next( )) {
00086 
00087                 d_UShort originId = itr.value( );
00088 
00089                 BdbRef(CdbBdbSOriginP) originRef;
00090                 if( CdbStatus::Success != originCollectionH->find( originId,
00091                                                                    originRef )) {
00092                     cout << errorStr << endl
00093                          << "    Failed to find an obejct for origin ID=" << originId << endl;
00094 
00095                     failed = true;
00096                     break;
00097 
00098                 } else {
00099 
00100                   // Check for the SLAVE kind of ORIGIN only
00101 
00102                     if( CdbBdbSOriginP::SLAVE == originRef->type( )) {
00103 
00104                         ooString(32) dbidrange = originRef->dbidrange( );
00105                         if( 0 == strcmp( localDbIdRangeName.c_str( ), dbidrange.head( ))) {
00106                             originH = originRef;
00107                             break;
00108                         }
00109                     }
00110                 }
00111             }
00112             if( failed ) break;
00113         }
00114         if( BdbIsNull(originH)) {
00115             cout << errorStr << endl
00116                  << "    Failed to find an origin object with the current DBID range name" << endl
00117                  << "    in the MASTER's collection." << endl;
00118             break;
00119         }
00120 
00121       // Generate the hint and create the container
00122 
00123         std::string databaseSuffix = std::string( "database_" ) + localDbIdRangeName;
00124 
00125         BdbCondIndexClusteringHint hint( "cdb",
00126                                          "System",
00127                                          databaseSuffix.c_str( ));
00128 
00129         BdbHandle(BdbContObj) contH = hint.updatedHint( );
00130         if( BdbIsNull(contH)) {
00131             cout << errorStr << endl
00132                  << "    Failed to create a container for persistent objects." << endl;
00133             break;
00134         }
00135 
00136       // The following check should always be positive, unless we have
00137       // an unexpected missmatch between ::findIntervalCont() and ::updatedHint()
00138       // behavior.
00139 
00140         if( ! hint.newContainerCreated( )) {
00141             cout << errorStr << endl
00142                  << "    The clustering hint has not created a new persistent container." << endl;
00143             break;
00144         }
00145 
00146       // Create the object.
00147 
00148         theRegistryH = new( contH ) CdbBdbSSlaveRegistryP( originH->id( ),
00149                                                            originH->description( ),
00150                                                            BdbTime::now( ),
00151                                                            masterRegistryH->id( ));
00152         if( BdbIsNull(theRegistryH)) {
00153             cout << errorStr << endl
00154                  << "    Failed to create a persistent registry object." << endl;
00155             break;
00156         }
00157 
00158       // Done
00159 
00160         result = CdbStatus::Success;
00161 
00162     } while( false );
00163 
00164     return result;
00165 }
00166 
00167 CdbBdbSSlaveRegistryP::CdbBdbSSlaveRegistryP( d_UShort       theOriginId,
00168                                               const char*    theDescription,
00169                                               const BdbTime& theCreationTime,
00170                                               const BdbTime& theId ) :
00171     CdbBdbSRegistryP( theOriginId,
00172                       theDescription,
00173                       theCreationTime,
00174                       theId,
00175                       true )
00176 {
00177   // Make sure the specified origin ID is the correct one. It should not be in a list
00178   // of reserved ones.
00179 
00180     assert( ! CdbBdbSOriginP::isReservedId( theOriginId ));
00181 
00182   // Make sure the code is run against the right federation (DBID range)
00183 
00184     std::string localRangeName;
00185     {
00186         CdbStatus result = CdbBdbSUtils::getLocalDbIdRange( localRangeName );
00187         assert( CdbStatus::Success == result );
00188 
00189       // The SLAVE kind of databases can NOT be created in the default
00190       // space of DBID ranges since SLAVE may export databases to MASTER
00191       // and TEST kinds of databases.
00192 
00193         assert( localRangeName != defaultDbIdRange( ));
00194     }
00195 
00196   // Create slave's collections and resources allocators.
00197   //
00198   // NOTE: To minimiza lock conflicts these collections can be created in
00199   //       other (than the current) containers.
00200 
00201     _partitionsLayoutRef    = new( ooThis( )) CdbBdbSPartitionsLayoutP ( );
00202 
00203   // WARNING: The cluster collection and its allocator are not used by the SLAVE.
00204   //          It was a design mistake to introduce them in here.
00205 
00206 //  _pClusterCollectionRef  = new( ooThis( )) CdbBdbSClusterCollectionP( );
00207 //  _pClusterRallocatorRef  = new( ooThis( )) CdbBdbSRallocatorP< d_UShort >( );    
00208 }
00209 
00210 CdbBdbSSlaveRegistryP::~CdbBdbSSlaveRegistryP( ) 
00211 {
00212     if( ! BdbIsNull(_partitionsLayoutRef))   BdbDelete(_partitionsLayoutRef);
00213 
00214 //  if( ! BdbIsNull(_pClusterCollectionRef)) BdbDelete(_pClusterCollectionRef);
00215 //  if( ! BdbIsNull(_pClusterRallocatorRef)) BdbDelete(_pClusterRallocatorRef);
00216 }
00217 
00218 bool
00219 CdbBdbSSlaveRegistryP::isMaster( ) const
00220 {
00221     return false;
00222 }
00223 
00224 BdbRef(CdbBdbSOriginCollectionP)
00225 CdbBdbSSlaveRegistryP::originCollection( ) const
00226 {
00227     return 0;
00228 }
00229 
00230 BdbRef(CdbBdbSPartitionsLayoutP)
00231 CdbBdbSSlaveRegistryP::partitionsLayout( ) const
00232 {
00233     return _partitionsLayoutRef;
00234 }
00235 
00236 BdbRef(CdbBdbSClusterCollectionP)
00237 CdbBdbSSlaveRegistryP::pClusterCollection( ) const
00238 {
00239     return 0;
00240 //  return _pClusterCollectionRef;
00241 }
00242 
00243 BdbRef(CdbBdbSRallocatorP< d_UShort >)
00244 CdbBdbSSlaveRegistryP::originRallocator( ) const
00245 {
00246     return 0;
00247 }
00248 
00249 BdbRef(CdbBdbSRallocatorP< d_UShort >)
00250 CdbBdbSSlaveRegistryP::partitionRallocator( ) const
00251 {
00252     return 0;
00253 }
00254 
00255 BdbRef(CdbBdbSRallocatorP< d_UShort >)
00256 CdbBdbSSlaveRegistryP::pClusterRallocator( ) const
00257 {
00258     return 0;
00259 //  return _pClusterRallocatorRef;
00260 }
00261 
00262 /////////////////
00263 // End Of File //
00264 /////////////////

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