00001
00002
00003
00004
00005
00006
00007
00008 #include "BaBar/BaBar.hh"
00009
00010 #include "CdbBdb/CdbBdbProxyCache.hh"
00011 #include "CdbBdb/CdbBdbProxyElement.hh"
00012 #include "CdbBdb/CdbBdbObjectConvertor.hh"
00013
00014 #include "CdbBase/CdbStateId.hh"
00015 #include "CdbBase/CdbObject.hh"
00016 #include "CdbBase/CdbCondition.hh"
00017 #include "CdbBase/CdbRevision.hh"
00018 #include "CdbBase/CdbRevisionPolicy.hh"
00019
00020 #include "BdbTime/BdbTime.hh"
00021
00022 #include "ErrLogger/ErrLog.hh"
00023
00024 #include <stdio.h>
00025 using std::endl;
00026
00027 CdbBdbProxyCache::CdbBdbProxyCache( const char* theFullConditionPath )
00028 {
00029 if( 0 != theFullConditionPath ) {
00030 _elements.push_back( CdbBdbProxyElement( std::string( theFullConditionPath ),
00031 0,
00032 BdbIntervalBase( BdbTime::minusInfinity,
00033 BdbTime::plusInfinity ),
00034 false ));
00035 }
00036 }
00037
00038 CdbBdbProxyCache::~CdbBdbProxyCache( )
00039 { }
00040
00041 void
00042 CdbBdbProxyCache::subscribeCondition( const char* theFullPath )
00043 {
00044 if( 0 == theFullPath ) ErrMsg(fatal) << "a null string passed instead of a condition name." << endmsg;
00045
00046 _elements.push_back( CdbBdbProxyElement( std::string( theFullPath ),
00047 0,
00048 BdbIntervalBase( BdbTime::minusInfinity,
00049 BdbTime::plusInfinity ),
00050 false ));
00051 }
00052
00053 void
00054 CdbBdbProxyCache::subscribeCondition( const std::string& theFullPath )
00055 {
00056 subscribeCondition( theFullPath.c_str( ));
00057 }
00058
00059
00060 void
00061 CdbBdbProxyCache::unSubscribeCondition( const char* theFullPath )
00062 {
00063 unSubscribeCondition( std::string( theFullPath ));
00064 }
00065
00066 void
00067 CdbBdbProxyCache::unSubscribeCondition( const std::string& theFullPath )
00068 {
00069 std::vector<CdbBdbProxyElement>::iterator itr;
00070 for( itr = _elements.begin( ); itr < _elements.end( ); ++itr ) {
00071 if((*itr).name( ) == theFullPath ) {
00072 _elements.erase( itr );
00073 }
00074 }
00075 }
00076
00077 void
00078 CdbBdbProxyCache::unSubscribeAllConditions( )
00079 {
00080 _elements.clear( );
00081 }
00082
00083 bool
00084 CdbBdbProxyCache::isCacheValid( const BdbTime& theValidityTime ) const
00085 {
00086 bool result = true;
00087 {
00088 std::vector<CdbBdbProxyElement>::const_iterator itr;
00089 for( itr = _elements.begin( ); itr < _elements.end( ); ++itr ) {
00090
00091
00092
00093
00094
00095 if( BdbIsNull((*itr).objectRef( )) ||
00096 !(*itr).validity( ).inInterval( theValidityTime )) {
00097
00098 result = false;
00099 break;
00100 }
00101 }
00102 }
00103 return result;
00104 }
00105
00106 void
00107 CdbBdbProxyCache::invalidatePersistentCacheAll( )
00108 {
00109 unsigned int num = _elements.size( );
00110 for( unsigned int idx = 0; idx < num; ++idx ) {
00111 _elements[idx] = CdbBdbProxyElement( _elements[idx].name( ),
00112 0,
00113 BdbIntervalBase( BdbTime::minusInfinity,
00114 BdbTime::plusInfinity ),
00115 false );
00116 }
00117 }
00118
00119 CdbStatus
00120 CdbBdbProxyCache::invalidatePersistentCache( const char* theFullPath )
00121 {
00122 return invalidatePersistentCache( std::string( theFullPath ));
00123 }
00124
00125 CdbStatus
00126 CdbBdbProxyCache::invalidatePersistentCache( const std::string& theFullPath )
00127 {
00128 unsigned int num = _elements.size( );
00129 for( unsigned int idx = 0; idx < num; ++idx ) {
00130 if( _elements[idx].name( ) == theFullPath ) {
00131 _elements[idx] = CdbBdbProxyElement( _elements[idx].name( ),
00132 0,
00133 BdbIntervalBase( BdbTime::minusInfinity,
00134 BdbTime::plusInfinity ),
00135 false );
00136 return CdbStatus::Success;
00137 }
00138 }
00139 return CdbStatus::NotFound;
00140 }
00141
00142 CdbStatus
00143 CdbBdbProxyCache::updateCache( const BdbTime& theValidityTime,
00144 const CdbStateId& theStateId,
00145 std::vector<CdbBdbProxyElement>& theListOfElements )
00146 {
00147 CdbStatus result = CdbStatus::Error;
00148
00149
00150
00151 theListOfElements.clear( );
00152
00153 do {
00154
00155
00156
00157 bool failed = false;
00158 {
00159 unsigned int num = _elements.size( );
00160 for( unsigned int idx = 0; idx < num; ++idx ) {
00161
00162
00163
00164
00165
00166 if( BdbIsNull(_elements[idx].objectRef( )) ||
00167 !_elements[idx].validity( ).inInterval( theValidityTime )) {
00168
00169
00170
00171 CdbConditionPtr cPtr;
00172 if( CdbStatus::Success != CdbCondition::instance( cPtr,
00173 _elements[idx].name( ).c_str( ))) {
00174
00175 ErrMsg(error) << "failed to find the condition \"" << _elements[idx].name( ) << "\"." << endmsg;
00176
00177 failed = true;
00178 break;
00179 }
00180 CdbObjectPtr oPtr;
00181 if( CdbStatus::Success != cPtr->findObject( oPtr,
00182 theValidityTime )) {
00183
00184 ErrMsg(error) << "failed to find a condition object from the condition \""<< _elements[idx].name( ) << "\"" << endl
00185 << "at the validity time point " << time2string( theValidityTime ) << " : " << theValidityTime << endmsg;
00186
00187 failed = true;
00188 break;
00189 }
00190
00191 BdbHandle(BdbObject) objectH;
00192 if( CdbStatus::Success != CdbBdbObjectConvertor::narrow( objectH, oPtr )) {
00193
00194 ErrMsg(error) << "failed to narrow a handle of the found condition object " << objectH.sprint( ) << endl
00195 << "from the condition \""<< _elements[idx].name( ) << "\"" << endl
00196 << "at the validity time point " << time2string( theValidityTime ) << " : " << theValidityTime << endl
00197 << "into a handle of the BdbObject type. This can be a sign of a database corruption" << endl
00198 << "or any other problem related to the currently used database installation." << endmsg;
00199
00200 failed = true;
00201 break;
00202 }
00203
00204
00205
00206 _elements[idx] = CdbBdbProxyElement( _elements[idx].name( ),
00207 objectH,
00208 BdbIntervalBase( oPtr->begin( ),
00209 oPtr->end( )),
00210 true );
00211 } else {
00212
00213
00214
00215
00216
00217 if( _elements[idx].updated( )) {
00218 _elements[idx] = CdbBdbProxyElement( _elements[idx].name( ),
00219 _elements[idx].objectRef( ),
00220 _elements[idx].validity( ),
00221 false );
00222 }
00223 }
00224
00225
00226
00227 theListOfElements.push_back( _elements[idx] );
00228 }
00229 }
00230 if( failed ) break;
00231
00232
00233
00234 result = CdbStatus::Success;
00235
00236 } while( false );
00237
00238 return result;
00239 }
00240
00241 CdbStatus
00242 CdbBdbProxyCache::updateCache( const BdbTime& theValidityTime,
00243 const char* theRevisionName,
00244 unsigned int thePartitionId,
00245 std::vector<CdbBdbProxyElement>& theListOfElements )
00246 {
00247 CdbStatus result = CdbStatus::Error;
00248
00249
00250
00251 theListOfElements.clear( );
00252
00253 do {
00254
00255
00256
00257 if( 0 == theRevisionName ) {
00258 ErrMsg(error) << "a null string passed to the method where a revision name was expected." << endmsg;
00259 result = CdbStatus::IllegalParameters;
00260 break;
00261 }
00262
00263
00264
00265 bool failed = false;
00266 {
00267 unsigned int num = _elements.size( );
00268 for( unsigned int idx = 0; idx < num; ++idx ) {
00269
00270
00271
00272
00273
00274 if( BdbIsNull(_elements[idx].objectRef( )) ||
00275 !_elements[idx].validity( ).inInterval( theValidityTime )) {
00276
00277
00278
00279 CdbConditionPtr cPtr;
00280 if( CdbStatus::Success != CdbCondition::instance( cPtr,
00281 _elements[idx].name( ).c_str( ))) {
00282
00283 ErrMsg(error) << "failed to find the condition \"" << _elements[idx].name( ) << "\"." << endmsg;
00284
00285 failed = true;
00286 break;
00287 }
00288
00289 CdbRevisionPtr rPtr;
00290 if( CdbStatus::Success != cPtr->findRevision( rPtr,
00291 theRevisionName,
00292 thePartitionId )) {
00293
00294 ErrMsg(error) << "failed to find a revision object from the condition \""<< _elements[idx].name( ) << "\"" << endl
00295 << "for revision name \"" << theRevisionName << "\" in partition " << thePartitionId << "." << endmsg;
00296
00297 failed = true;
00298 break;
00299 }
00300
00301 CdbObjectPtr oPtr;
00302 if( CdbStatus::Success != cPtr->findObject( oPtr,
00303 CdbRevisionPolicy( rPtr->id( ),
00304 thePartitionId ),
00305 theValidityTime )) {
00306
00307 ErrMsg(error) << "failed to find a condition object from the condition \""<< _elements[idx].name( ) << "\"" << endl
00308 << "at the validity time point " << time2string( theValidityTime ) << " : " << theValidityTime << endl
00309 << "using revision name \"" << theRevisionName << "\" in partition " << thePartitionId << "." << endmsg;
00310
00311 failed = true;
00312 break;
00313 }
00314
00315 BdbHandle(BdbObject) objectH;
00316 if( CdbStatus::Success != CdbBdbObjectConvertor::narrow( objectH, oPtr )) {
00317
00318 ErrMsg(error) << "failed to narrow a handle of the found condition object " << objectH.sprint( ) << endl
00319 << "from the condition \""<< _elements[idx].name( ) << "\"" << endl
00320 << "at the validity time point " << time2string( theValidityTime ) << " : " << theValidityTime << endl
00321 << "into a handle of the BdbObject type. This can be a sign of a database corruption" << endl
00322 << "or any other problem related to the currently used database installation." << endmsg;
00323
00324 failed = true;
00325 break;
00326 }
00327
00328
00329
00330 _elements[idx] = CdbBdbProxyElement( _elements[idx].name( ),
00331 objectH,
00332 BdbIntervalBase( oPtr->begin( ),
00333 oPtr->end( )),
00334 true );
00335 } else {
00336
00337
00338
00339
00340
00341 if( _elements[idx].updated( )) {
00342 _elements[idx] = CdbBdbProxyElement( _elements[idx].name( ),
00343 _elements[idx].objectRef( ),
00344 _elements[idx].validity( ),
00345 false );
00346 }
00347 }
00348
00349
00350
00351 theListOfElements.push_back( _elements[idx] );
00352 }
00353 }
00354 if( failed ) break;
00355
00356
00357
00358 result = CdbStatus::Success;
00359
00360 } while( false );
00361
00362 return result;
00363 }
00364
00365 std::string
00366 CdbBdbProxyCache::time2string( const BdbTime& theTime,
00367 bool forceInternalFormat,
00368 bool packedFormat )
00369 {
00370 unsigned int sec = theTime.getGmtSec( );
00371 unsigned int nsec = theTime.getGmtNsec( );
00372
00373 char buf[80];
00374
00375 if( forceInternalFormat ) {
00376 if( packedFormat ) sprintf( buf, "%u.%u", sec, nsec );
00377 else sprintf( buf, "%10.10u.%10.10u", sec, nsec );
00378 } else {
00379 if( packedFormat ) {
00380 if( BdbTime::minusInfinity == theTime ) sprintf( buf, "-Infinity" );
00381 else if( BdbTime::plusInfinity == theTime ) sprintf( buf, "+Infinity" );
00382 else sprintf( buf, "%u.%u", sec, nsec );
00383 } else {
00384 if( BdbTime::minusInfinity == theTime ) sprintf( buf, " -Infinity " );
00385 else if( BdbTime::plusInfinity == theTime ) sprintf( buf, " +Infinity " );
00386 else sprintf( buf, "%10.10u.%10.10u", sec, nsec );
00387 }
00388 }
00389 return std::string( buf );
00390 }
00391
00392
00393
00394