00001
00002
00003
00004
00005
00006
00007
00008 #include "BaBar/BaBar.hh"
00009
00010 #include "CdbSQL/CdbSQLApiView.hh"
00011
00012 #include "CdbBase/Cdb.hh"
00013 #include "CdbBase/CdbId.hh"
00014 #include "CdbBase/CdbStateId.hh"
00015 #include "CdbBase/CdbFolder.hh"
00016 #include "CdbBase/CdbPathName.hh"
00017 #include "CdbBase/CdbRevisionPolicy.hh"
00018 #include "CdbBase/CdbVectorAdapterItr.hh"
00019 #include "CdbBase/CdbSimpleVectorAdapterItr.hh"
00020
00021 #include "CdbSQL/CdbSQLOrigin.hh"
00022 #include "CdbSQL/CdbSQLPartition.hh"
00023 #include "CdbSQL/CdbSQLView.hh"
00024 #include "CdbSQL/CdbSQLFolder.hh"
00025 #include "CdbSQL/CdbSQLConditionAtFolder.hh"
00026 #include "CdbSQL/CdbSQLApiFolder.hh"
00027
00028 #include <assert.h>
00029
00030 #include <iostream>
00031 using std::cout;
00032 using std::endl;
00033
00034 namespace {
00035
00036
00037
00038
00039
00040 class IteratorOfFolders : public CdbVectorAdapterItr< const char*, CdbCPtr<CdbSQLFolder> > {
00041
00042 private:
00043
00044
00045
00046 IteratorOfFolders( );
00047
00048
00049
00050 IteratorOfFolders& operator=( const IteratorOfFolders& );
00051
00052 protected:
00053
00054
00055
00056 IteratorOfFolders( const IteratorOfFolders& theItr ) :
00057 CdbVectorAdapterItr<const char*, CdbCPtr<CdbSQLFolder> >( theItr )
00058 { }
00059
00060 public:
00061
00062
00063
00064
00065
00066 IteratorOfFolders( const std::vector< CdbCPtr<CdbSQLFolder> >& theList ) :
00067 CdbVectorAdapterItr< const char*, CdbCPtr<CdbSQLFolder> >( theList )
00068 { }
00069
00070
00071
00072
00073
00074 virtual CdbIItr<const char*>* clone( ) const
00075 {
00076 return new IteratorOfFolders( *this );
00077 }
00078
00079 protected:
00080
00081
00082
00083
00084
00085 virtual const char* toValue( const CdbCPtr<CdbSQLFolder>& thePtr ) const
00086 {
00087 return thePtr->name( ).c_str( );
00088 }
00089 };
00090 }
00091
00092
00093
00094
00095
00096 CdbView*
00097 CdbSQLApiView::clone( ) const
00098 {
00099
00100
00101 CdbSQLApiView* mySelf = const_cast<CdbSQLApiView*>( this );
00102 return new CdbSQLApiView( mySelf->parent( ),
00103 mySelf->name( ),
00104 mySelf->id( ),
00105 _mySQLViewPtr,
00106 _myMasterSQLOriginPtr,
00107 _myLocalSQLOriginPtr );
00108 }
00109
00110 CdbSQLApiView::CdbSQLApiView( const CdbDatabasePtr& thePtr,
00111 const char* theName,
00112 const CdbId& theId,
00113 const CdbCPtr<CdbSQLView>& theViewPtr,
00114 const CdbCPtr<CdbSQLOrigin>& theMasterOriginPtr,
00115 const CdbCPtr<CdbSQLOrigin>& theLocalOriginPtr ) :
00116 CdbView( thePtr,
00117 theName,
00118 theId ),
00119 _mySQLViewPtr (theViewPtr),
00120 _myMasterSQLOriginPtr(theMasterOriginPtr),
00121 _myLocalSQLOriginPtr (theLocalOriginPtr)
00122 {
00123 assert( !_mySQLViewPtr.isNull( ));
00124 assert( !_myMasterSQLOriginPtr.isNull( ));
00125 assert( !_myLocalSQLOriginPtr.isNull( ));
00126 }
00127
00128 CdbSQLApiView::~CdbSQLApiView( )
00129 { }
00130
00131 bool
00132 CdbSQLApiView::isValid( )
00133 {
00134 return true;
00135 }
00136
00137 bool
00138 CdbSQLApiView::isOpen( )
00139 {
00140 return true;
00141 }
00142
00143 CdbStatus
00144 CdbSQLApiView::open( )
00145 {
00146 return CdbStatus::Success;
00147 }
00148
00149 CdbStatus
00150 CdbSQLApiView::close( )
00151 {
00152 return CdbStatus::Success;
00153 }
00154
00155 BdbTime
00156 CdbSQLApiView::minValidity( ) const
00157 {
00158 return _mySQLViewPtr->begin( );
00159 }
00160
00161 BdbTime
00162 CdbSQLApiView::maxValidity( ) const
00163 {
00164 return _mySQLViewPtr->end( );
00165 }
00166
00167 BdbTime
00168 CdbSQLApiView::created( ) const
00169 {
00170 return _mySQLViewPtr->created( );
00171 }
00172
00173 std::string
00174 CdbSQLApiView::description( ) const
00175 {
00176 return _mySQLViewPtr->description( );
00177 }
00178
00179 bool
00180 CdbSQLApiView::hasDefaultConfig( ) const
00181 {
00182 return _mySQLViewPtr->hasDefaultConfig( );
00183 }
00184
00185 bool
00186 CdbSQLApiView::isLocal( ) const
00187 {
00188 return id( ).origin == _myLocalSQLOriginPtr->id( );
00189 }
00190
00191 bool
00192 CdbSQLApiView::isFrozen( ) const
00193 {
00194 return _mySQLViewPtr->isFrozen( );
00195 }
00196
00197 CdbStatus
00198 CdbSQLApiView::findFolder( CdbFolderPtr& thePtr,
00199 const char* theName )
00200 {
00201 CdbStatus status = CdbStatus::Error;
00202
00203
00204
00205 if( std::string( theName ) != CdbPathName::separator( )) {
00206
00207 cout << "CdbSQLApiView::findFolder() -- ERROR" << endl
00208 << " No such folder in this implementation: \"" << theName << "\"" << endl
00209 << " The only name allowed is the rood one: \"" << CdbPathName::separator( ) << "\"" << endl;
00210
00211 return CdbStatus::Error;
00212 }
00213
00214
00215
00216 CdbCPtr<CdbSQLFolder> folderPtr;
00217 if( CdbStatus::Success != ( status = _mySQLViewPtr->find_folder( folderPtr,
00218 std::string( theName )))) {
00219 return status;
00220 }
00221
00222
00223
00224 CdbFolderPtr parentFolderPtr = 0;
00225
00226 thePtr = new CdbSQLApiFolder( CdbViewPtr( this->clone( )),
00227 parentFolderPtr,
00228 theName,
00229 folderPtr,
00230 _myMasterSQLOriginPtr,
00231 _myLocalSQLOriginPtr );
00232 return CdbStatus::Success;
00233 }
00234
00235 CdbStatus
00236 CdbSQLApiView::folderIterator( CdbFolderItr& theItr )
00237 {
00238 const char* errorStr = "CdbSQLApiView::folderIterator() -- ERROR.";
00239
00240 CdbStatus status = CdbStatus::Error;
00241
00242
00243
00244 std::vector< CdbCPtr<CdbSQLFolder> > folders;
00245 if( CdbStatus::Success != ( status = _mySQLViewPtr->folders( folders ))) {
00246 cout << errorStr << endl
00247 << " Failed to obtain a list of folders at the view." << endl
00248 << " VIEW NAME : \"" << name( ) << "\"." << endl;
00249 return status;
00250 }
00251 theItr = CdbFolderItr( new IteratorOfFolders( folders ));
00252
00253 return CdbStatus::Success;
00254 }
00255
00256 CdbStatus
00257 CdbSQLApiView::getDefault( const BdbTime& theValidityTime,
00258 CdbConfigElement& theConfigElement ) const
00259 {
00260 return CdbStatus::NotImplemented;
00261 }
00262
00263 CdbStatus
00264 CdbSQLApiView::defaultConfigIterator( CdbItr<CdbConfigElement>& theItr ) const
00265 {
00266 return CdbStatus::NotImplemented;
00267 }
00268
00269 CdbStatus
00270 CdbSQLApiView::get( const char* theName,
00271 const BdbTime& theValidityTime,
00272 CdbConfigElement& theConfigElement ) const
00273 {
00274 const char* errorStr = "CdbSQLApiView::get() -- ERROR.";
00275
00276 CdbStatus status = CdbStatus::Error;
00277
00278
00279
00280 if( 0 == theName ) {
00281 cout << errorStr << endl
00282 << " Non 0 pointer onto a fully qualified condition path name was expected." << endl;
00283 return CdbStatus::IllegalParameters;
00284 }
00285
00286 CdbPathName fullPath( theName );
00287
00288 if( ! ( fullPath.isValid( ) &&
00289 fullPath.isAbsolute( ) &&
00290 fullPath.isComposite( ))) {
00291
00292 cout << errorStr << endl
00293 << " Unsupported format of the condition name. Fully qualified" << endl
00294 << " condition name including its folder path was expected." << endl
00295 << " Passed condition name: \"" << theName << "\"" << endl;
00296 return CdbStatus::IllegalParameters;
00297 }
00298
00299
00300
00301
00302 if(( theValidityTime < _mySQLViewPtr->begin( )) ||
00303 ( theValidityTime >= _mySQLViewPtr->end( ))) {
00304
00305 cout << errorStr << endl
00306 << " The pased validity time is out of the validity interval covered" << endl
00307 << " by the current view." << endl
00308 << " Passed validity time: " << theValidityTime << endl
00309 << " Minimum validity time: " << _mySQLViewPtr->begin( ) << endl
00310 << " Maximum validity time: " << _mySQLViewPtr->end( ) << endl;
00311 return CdbStatus::IllegalParameters;
00312 }
00313
00314
00315
00316
00317
00318 CdbConfigElement configElement;
00319 {
00320 std::string folderName = fullPath.beforeLast( ).toString( );
00321 std::string conditionName = fullPath.last( ).toString( );
00322
00323
00324
00325 CdbCPtr<CdbSQLFolder> folderPtr;
00326 if( CdbStatus::Success != ( status = _mySQLViewPtr->find_folder( folderPtr,
00327 folderName ))) {
00328 return status;
00329 }
00330
00331
00332
00333 CdbCPtr<CdbSQLConditionAtFolder> conditionPtr;
00334 if( CdbStatus::Success != ( status = folderPtr->find_condition( conditionPtr,
00335 conditionName ))) {
00336 return status;
00337 }
00338
00339
00340
00341 status = conditionPtr->find_config_element( configElement,
00342 theValidityTime );
00343 if( CdbStatus::Success != status ) {
00344 if( CdbStatus::NotFound == status ) {
00345
00346
00347
00348
00349 if( CdbStatus::Success != _mySQLViewPtr->find_config_element( configElement,
00350 theValidityTime )) {
00351 cout << errorStr << endl
00352 << " The current view is inproperly configured for specified condition." << endl
00353 << " No configuration found both at specified condition scope and" << endl
00354 << " at the view level." << endl
00355 << " Current view name: \"" << name( ) << "\"" << endl
00356 << " Passed condition name: \"" << theName << "\"" << endl;
00357 return status;
00358 }
00359
00360 } else {
00361 cout << errorStr << endl
00362 << " A problem occured while attempting to get a configuration." << endl
00363 << " No configuration found both at specified condition scope and" << endl
00364 << " at the view level." << endl
00365 << " Current view name: \"" << name( ) << "\"" << endl
00366 << " Passed condition name: \"" << theName << "\"" << endl;
00367 return status;
00368 }
00369 }
00370 }
00371 theConfigElement = configElement;
00372
00373 return CdbStatus::Success;
00374 }
00375
00376 CdbStatus
00377 CdbSQLApiView::configIterator( const char* theName,
00378 CdbItr<CdbConfigElement>& theItr ) const
00379 {
00380 const char* errorStr = "CdbSQLApiView::configIterator() -- ERROR.";
00381
00382 CdbStatus status = CdbStatus::Error;
00383
00384
00385
00386 if( 0 == theName ) {
00387 cout << errorStr << endl
00388 << " Non 0 pointer onto a fully qualified condition path name was expected." << endl;
00389 return CdbStatus::IllegalParameters;
00390 }
00391
00392 CdbPathName fullPath( theName );
00393
00394 if( ! ( fullPath.isValid( ) &&
00395 fullPath.isAbsolute( ) &&
00396 fullPath.isComposite( ))) {
00397
00398 cout << errorStr << endl
00399 << " Unsupported format of the condition name. Fully qualified" << endl
00400 << " condition name including its folder path was expected." << endl
00401 << " Passed condition name: \"" << theName << "\"" << endl;
00402 return CdbStatus::IllegalParameters;
00403 }
00404
00405
00406
00407
00408
00409 std::vector<CdbConfigElement> config_elements;
00410 {
00411 std::string folderName = fullPath.beforeLast( ).toString( );
00412 std::string conditionName = fullPath.last( ).toString( );
00413
00414
00415
00416 CdbCPtr<CdbSQLFolder> folderPtr;
00417 if( CdbStatus::Success != ( status = _mySQLViewPtr->find_folder( folderPtr,
00418 folderName ))) {
00419 return status;
00420 }
00421
00422
00423
00424 CdbCPtr<CdbSQLConditionAtFolder> conditionPtr;
00425 if( CdbStatus::Success != ( status = folderPtr->find_condition( conditionPtr,
00426 conditionName ))) {
00427 return status;
00428 }
00429
00430
00431
00432 status = conditionPtr->config_elements( config_elements );
00433
00434 if( CdbStatus::Success != status ) {
00435 if( CdbStatus::NotFound == status ) {
00436
00437
00438
00439
00440 if( CdbStatus::Success != ( status = _mySQLViewPtr->config_elements( config_elements ))) {
00441 cout << errorStr << endl
00442 << " The current view is inproperly configured for specified condition." << endl
00443 << " No configuration found both at specified condition scope and" << endl
00444 << " at the view level." << endl
00445 << " Current view name: \"" << name( ) << "\"" << endl
00446 << " Passed condition name: \"" << theName << "\"" << endl;
00447 return status;
00448 }
00449
00450 } else {
00451 cout << errorStr << endl
00452 << " A problem occured while attempting to get a configuration." << endl
00453 << " No configuration found both at specified condition scope and" << endl
00454 << " at the view level." << endl
00455 << " Current view name: \"" << name( ) << "\"" << endl
00456 << " Passed condition name: \"" << theName << "\"" << endl;
00457 return status;
00458 }
00459 }
00460 }
00461
00462
00463
00464
00465 std::vector<CdbConfigElement> adjusted_config_elements;
00466 {
00467 BdbTime prevEnd = BdbTime::minusInfinity;
00468
00469 for( std::vector<CdbConfigElement>::const_iterator itr = config_elements.begin();
00470 itr != config_elements.end();
00471 ++itr ) {
00472 if( prevEnd < itr->begin )
00473 adjusted_config_elements.push_back( CdbConfigElement( prevEnd,
00474 itr->begin ));
00475 adjusted_config_elements.push_back( *itr );
00476 prevEnd = itr->end;
00477 }
00478 if( prevEnd < BdbTime::plusInfinity )
00479 adjusted_config_elements.push_back( CdbConfigElement( prevEnd,
00480 BdbTime::plusInfinity ));
00481 }
00482 theItr = CdbItr<CdbConfigElement>( new CdbSimpleVectorAdapterItr<CdbConfigElement>( adjusted_config_elements ));
00483
00484 return CdbStatus::Success;
00485 }
00486
00487 CdbStateId
00488 CdbSQLApiView::currentStateId( const BdbTime& theValidityTime ) const
00489 {
00490 const char* errorStr = "CdbSQLApiView::currentStateId() -- ERROR.";
00491
00492 CdbStateId result;
00493 do {
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 BdbTime partitionModificationTime( BdbTime::now( ));
00549 {
00550
00551
00552
00553 CdbCPtr<CdbSQLPartition> partitionPtr;
00554 if( CdbStatus::Success == _myMasterSQLOriginPtr->find_topmost_partition( partitionPtr,
00555 theValidityTime )) {
00556
00557
00558
00559 if( !partitionPtr->isClosed( )) {
00560
00561
00562
00563
00564
00565 if( _myMasterSQLOriginPtr->id( ) != _myLocalSQLOriginPtr->id( )) {
00566 if( _myLocalSQLOriginPtr->id( ) == partitionPtr->originId( )) {
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 if( CdbStatus::Success != _myLocalSQLOriginPtr->find_partition( partitionPtr,
00577 partitionPtr->id( ))) {
00578 cout << errorStr << endl
00579 << " Failed to locate a final partition with ID=" << partitionPtr->id( ) << endl
00580 << " at the local database where it's supposed to belong to." << endl
00581 << " The local database may not be properly initialized/loaded." << endl;
00582 break;
00583 }
00584 }
00585 }
00586 }
00587
00588
00589
00590
00591
00592
00593 if( partitionPtr->isClosed( )) {
00594
00595
00596
00597
00598 partitionModificationTime = partitionPtr->modified( );
00599
00600 } else {
00601
00602
00603
00604
00605
00606
00607 if( partitionPtr->isInstantiated( )) {
00608 partitionModificationTime = partitionPtr->modified( );
00609 } else {
00610 if( _myLocalSQLOriginPtr->id( ) == partitionPtr->originId( )) {
00611 cout << errorStr << endl
00612 << " The partition with ID=" << partitionPtr->id( ) << " corresponding to the following" << endl
00613 << " validity time: " << theValidityTime << endl
00614 << " belonging to the local database is not properly instantiated." << endl
00615 << " The local database may not be properly initialized/loaded." << endl;
00616 break;
00617
00618 } else {
00619
00620
00621
00622
00623
00624
00625
00626
00627 partitionModificationTime = partitionPtr->created( );
00628 }
00629 }
00630 }
00631 }
00632 }
00633
00634
00635
00636 result = CdbStateId( id( ), partitionModificationTime );
00637
00638 } while( false );
00639
00640 return result;
00641 }
00642
00643 CdbStatus
00644 CdbSQLApiView::historyEventIterator( CdbHistoryEventItr& theItr,
00645 const BdbTime& theBeginTime,
00646 const BdbTime& theEndTime,
00647 const char** theEventsToSelect )
00648 {
00649 return CdbStatus::NotImplemented;
00650 }
00651
00652 CdbStatus
00653 CdbSQLApiView::historyEventTypeIterator( CdbHistoryEventTypeItr& theItr )
00654 {
00655 return CdbStatus::NotImplemented;
00656 }
00657
00658
00659
00660