00001
00002
00003
00004
00005
00006
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
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
00044
00045
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
00057
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
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
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
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
00137
00138
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
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
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
00178
00179
00180 assert( ! CdbBdbSOriginP::isReservedId( theOriginId ));
00181
00182
00183
00184 std::string localRangeName;
00185 {
00186 CdbStatus result = CdbBdbSUtils::getLocalDbIdRange( localRangeName );
00187 assert( CdbStatus::Success == result );
00188
00189
00190
00191
00192
00193 assert( localRangeName != defaultDbIdRange( ));
00194 }
00195
00196
00197
00198
00199
00200
00201 _partitionsLayoutRef = new( ooThis( )) CdbBdbSPartitionsLayoutP ( );
00202
00203
00204
00205
00206
00207
00208 }
00209
00210 CdbBdbSSlaveRegistryP::~CdbBdbSSlaveRegistryP( )
00211 {
00212 if( ! BdbIsNull(_partitionsLayoutRef)) BdbDelete(_partitionsLayoutRef);
00213
00214
00215
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
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
00260 }
00261
00262
00263
00264