00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbBdbShared/CdbBdbSSimpleClusterP.hh"
00012 #include "CdbBdbShared/CdbBdbSUtils.hh"
00013 #include "CdbBdbShared/CdbBdbSMetaDataP.hh"
00014 #include "CdbBdbShared/CdbBdbSConditionP.hh"
00015
00016 #include "BdbClustering/BdbCondIndexClusteringHint.hh"
00017
00018 #include <stdio.h>
00019 #include <iostream>
00020
00021 #include <string>
00022 using std::cout;
00023 using std::endl;
00024
00025 CdbBdbSSimpleClusterP::CdbBdbSSimpleClusterP( const char* theName,
00026 d_UShort theId,
00027 const char* theDescription,
00028 const BdbTime& theCreationTime ) :
00029 CdbBdbSClusterP( theName,
00030 theId,
00031 theDescription,
00032 theCreationTime )
00033 { }
00034
00035 CdbBdbSSimpleClusterP::~CdbBdbSSimpleClusterP( )
00036 { }
00037
00038 d_UShort
00039 CdbBdbSSimpleClusterP::numIncrements( ) const
00040 {
00041 return _increments.numIncrements( );
00042 }
00043
00044 CdbStatus
00045 CdbBdbSSimpleClusterP::increment( d_UShort theNumber,
00046 CdbBdbSIncrement& theIncrement ) const
00047 {
00048 return _increments.increment( theNumber,
00049 theIncrement );
00050 }
00051
00052 CdbStatus
00053 CdbBdbSSimpleClusterP::createIncrement( const BdbTime& theSplitTime )
00054 {
00055 const char* errorStr = "CdbBdbSSimpleClusterP::createIncrement() -- ERROR";
00056
00057 ooUpdate( );
00058
00059 CdbStatus result = CdbStatus::Error;
00060 do {
00061
00062
00063
00064 d_UShort oldIncrementNumber = 0;
00065 {
00066 d_UShort num = _increments.numIncrements( );
00067
00068 assert( num >= 1 );
00069
00070 oldIncrementNumber = num - 1;
00071 }
00072
00073
00074
00075 if( CdbStatus::Success != _increments.createIncrement( theSplitTime )) {
00076 cout << errorStr << endl
00077 << " Failed to create the new cluster increment." << endl;
00078 break;
00079 }
00080
00081
00082
00083 d_UShort newIncrementNumber = 0;
00084 {
00085 d_UShort num = _increments.numIncrements( );
00086
00087 assert( num >= 2 );
00088
00089
00090 newIncrementNumber = num - 1;
00091 }
00092
00093
00094
00095 std::string localRangeName;
00096 {
00097 CdbStatus returnStatus = CdbBdbSUtils::getLocalDbIdRange( localRangeName );
00098 assert( CdbStatus::Success == returnStatus );
00099 }
00100 std::string newDatabaseSuffix;
00101 {
00102 char clusterIdStr[12];
00103 char clusterNewIncrementStr[12];
00104
00105 sprintf( clusterIdStr, "%u", id( ));
00106 sprintf( clusterNewIncrementStr, "%u", newIncrementNumber );
00107
00108 newDatabaseSuffix = std::string( "c" ) + std::string( clusterIdStr ) +
00109 std::string( "_i" ) + std::string( clusterNewIncrementStr );
00110
00111 newDatabaseSuffix = newDatabaseSuffix +
00112 std::string( "_" ) +
00113 localRangeName;
00114 }
00115
00116
00117
00118
00119 BdbCondIndexClusteringHint systemHint( "cdb",
00120 "System",
00121 newDatabaseSuffix.c_str( ));
00122
00123 BdbHandle(BdbContObj) systemContH;
00124 systemContH = systemHint.updatedHint( );
00125
00126 assert( ! BdbIsNull(systemContH));
00127 assert( systemHint.newContainerCreated( ));
00128
00129
00130
00131 CdbItr< BdbRef(CdbBdbSConditionP) > itr;
00132 if( CdbStatus::Success != iterator( itr )) {
00133 cout << errorStr << endl
00134 << " Failed to obtain the conditions - members of the cluster." << endl;
00135 break;
00136 }
00137
00138 bool failed = false;
00139
00140 while( itr.next( )) {
00141
00142
00143
00144 BdbRef(CdbBdbSConditionP) conditionRef = itr.value( );
00145 assert( !BdbIsNull(conditionRef));
00146
00147 d_UShort conditionId = conditionRef->id( );
00148
00149
00150
00151 BdbRef(CdbBdbSMetaDataP) oldMetaDataRef;
00152 if( CdbStatus::Success != CdbBdbSUtils::findMetaData( oldMetaDataRef,
00153 conditionId,
00154 false,
00155 0,
00156 id( ),
00157 oldIncrementNumber,
00158 localRangeName )) {
00159
00160 cout << errorStr << endl
00161 << " Failed to find MetaData object for increment #" << oldIncrementNumber << endl;
00162
00163 failed = true;
00164 break;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 BdbTime modificationTime = oldMetaDataRef->modified( );
00174 char finalRevisionNameStr[32];
00175
00176 sprintf( finalRevisionNameStr,
00177 "final_%u.%u",
00178 modificationTime.getGmtSec( ),
00179 modificationTime.getGmtNsec( ));
00180
00181 if( CdbStatus::Success != oldMetaDataRef->close( theSplitTime,
00182 finalRevisionNameStr,
00183 theSplitTime,
00184 "automatically created by the increment creation procedure" )) {
00185 cout << errorStr << endl
00186 << " Failed to close the old MetaData object." << endl;
00187
00188 failed = true;
00189 break;
00190 }
00191
00192
00193
00194
00195
00196
00197 char conditionIdStr[12];
00198 sprintf( conditionIdStr, "%u", conditionId );
00199
00200 std::string mdContainerName = std::string( "MetaData." ) + std::string( conditionIdStr );
00201 std::string obContainerName = std::string( "Objects." ) + std::string( conditionIdStr );
00202
00203
00204
00205
00206 BdbCondIndexClusteringHint mdHint( "cdb",
00207 mdContainerName.c_str( ),
00208 newDatabaseSuffix.c_str( ));
00209
00210 BdbHandle(BdbContObj) mdContH;
00211 mdContH = mdHint.updatedHint( );
00212
00213 assert( ! BdbIsNull(mdContH));
00214 assert( mdHint.newContainerCreated( ));
00215
00216 BdbCondIndexClusteringHint obHint( "cdb",
00217 obContainerName.c_str( ),
00218 newDatabaseSuffix.c_str( ));
00219
00220 BdbHandle(BdbContObj) obContH;
00221 obContH = obHint.updatedHint( );
00222
00223 assert( ! BdbIsNull(obContH));
00224 assert( obHint.newContainerCreated( ));
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 BdbHandle(CdbBdbSMetaDataP) newMetaDataH;
00236 newMetaDataH = new( mdContH ) CdbBdbSMetaDataP( oldMetaDataRef->minValidity( ),
00237 oldMetaDataRef->maxValidity( ),
00238 theSplitTime );
00239 assert( ! BdbIsNull(newMetaDataH));
00240
00241
00242
00243 BdbStatus status;
00244 status = newMetaDataH.nameObj( mdContH,
00245 "MetaData" );
00246 assert( BdbcSuccess == status );
00247 }
00248 if( failed ) break;
00249
00250
00251
00252 result = CdbStatus::Success;
00253
00254 } while( false );
00255
00256 return result;
00257 }
00258
00259
00260
00261