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