00001
00002
00003
00004
00005
00006
00007
00008 #include "BaBar/BaBar.hh"
00009
00010 #include "CdbBdbShared/CdbBdbSView.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
00019 #include "CdbBdbShared/CdbBdbSFolder.hh"
00020 #include "CdbBdbShared/CdbBdbSPartitionP.hh"
00021 #include "CdbBdbShared/CdbBdbSViewP.hh"
00022 #include "CdbBdbShared/CdbBdbSConditionAtFolderP.hh"
00023 #include "CdbBdbShared/CdbBdbSConfigCollectionP.hh"
00024
00025 #include "BdbTime/BdbTime.hh"
00026
00027 #include <iostream>
00028 using std::cout;
00029 using std::endl;
00030
00031 namespace {
00032
00033
00034
00035
00036
00037 class CdbBdbSConfigItr : public CdbItr<CdbConfigElement>::InterfaceType {
00038
00039 private:
00040
00041 CdbBdbSConfigItr( );
00042
00043 CdbBdbSConfigItr& operator=( const CdbBdbSConfigItr& theItr );
00044
00045 protected:
00046
00047 CdbBdbSConfigItr( const CdbBdbSConfigItr& theItr ) :
00048 _itr (theItr._itr),
00049 _value(theItr._value)
00050 { }
00051
00052 public:
00053
00054 CdbBdbSConfigItr( const CdbBdbSConfigCollectionP::IteratorOfIntervals& theInputItr ) :
00055 _itr(theInputItr)
00056 { }
00057
00058 virtual ~CdbBdbSConfigItr( )
00059 { }
00060
00061 virtual CdbStatus reset( )
00062 {
00063 return _itr.reset( );
00064 }
00065
00066 virtual bool next( )
00067 {
00068 bool result = _itr.next( );
00069 if( result ) {
00070
00071 CdbBdbSConfigInterval in = _itr.value( );
00072 if( in.value.accessIsAllowed( )) {
00073
00074
00075
00076 CdbRevisionPolicy policy;
00077 if( in.value.useRevision ) {
00078 policy = CdbRevisionPolicy( in.value.revision,
00079 in.value.partition );
00080 }
00081 _value = CdbConfigElement( policy,
00082 in.begin,
00083 in.end );
00084 } else {
00085
00086
00087
00088 _value = CdbConfigElement( in.begin,
00089 in.end );
00090 }
00091
00092 } else {
00093
00094
00095
00096
00097 _value = CdbConfigElement( );
00098 }
00099 return result;
00100 }
00101
00102 virtual ValueType value( )
00103 {
00104 if( ! isValid( )) {
00105 assert( 0 );
00106 }
00107 return _value;
00108 }
00109
00110 virtual bool isValid( )
00111 {
00112 return _itr.isValid( );
00113 }
00114
00115 virtual InterfaceType* clone( ) const
00116 {
00117 return new CdbBdbSConfigItr( *this );
00118 }
00119
00120 private:
00121
00122 CdbBdbSConfigCollectionP::IteratorOfIntervals _itr;
00123
00124 CdbConfigElement _value;
00125 };
00126 };
00127
00128
00129
00130
00131
00132 CdbView*
00133 CdbBdbSView::clone( ) const
00134 {
00135
00136
00137 CdbBdbSView* mySelf = const_cast<CdbBdbSView*>( this );
00138 return new CdbBdbSView( mySelf->parent( ),
00139 mySelf->name( ),
00140 mySelf->id( ),
00141 _viewH,
00142 _masterRegistryH,
00143 _localRegistryH );
00144 }
00145
00146 CdbBdbSView::CdbBdbSView( const CdbDatabasePtr& thePtr,
00147 const char* theName,
00148 const CdbId& theId,
00149 const BdbHandle(CdbBdbSViewP)& theViewH,
00150 const BdbHandle(CdbBdbSRegistryP)& theMasterRegistryH ,
00151 const BdbHandle(CdbBdbSRegistryP)& theLocalRegistryH ) :
00152 CdbView( thePtr,
00153 theName,
00154 theId ),
00155 _viewH(theViewH),
00156 _masterRegistryH(theMasterRegistryH),
00157 _localRegistryH(theLocalRegistryH)
00158 { }
00159
00160 CdbBdbSView::~CdbBdbSView( )
00161 { }
00162
00163 bool
00164 CdbBdbSView::isValid( )
00165 {
00166 return true;
00167 }
00168
00169 bool
00170 CdbBdbSView::isOpen( )
00171 {
00172 return true;
00173 }
00174
00175 CdbStatus
00176 CdbBdbSView::open( )
00177 {
00178 return CdbStatus::Success;
00179 }
00180
00181 CdbStatus
00182 CdbBdbSView::close( )
00183 {
00184 return CdbStatus::Success;
00185 }
00186
00187 BdbTime
00188 CdbBdbSView::minValidity( ) const
00189 {
00190 return _viewH->minValidity( );
00191 }
00192
00193 BdbTime
00194 CdbBdbSView::maxValidity( ) const
00195 {
00196 return _viewH->maxValidity( );
00197 }
00198
00199 BdbTime
00200 CdbBdbSView::created( ) const
00201 {
00202 return _viewH->created( );
00203 }
00204
00205 std::string
00206 CdbBdbSView::description( ) const
00207 {
00208 return std::string( _viewH->description( ).head( ));
00209 }
00210
00211 bool
00212 CdbBdbSView::hasDefaultConfig( ) const
00213 {
00214 return ! BdbIsNull( _viewH->defaultConfig( ));
00215 }
00216
00217 bool
00218 CdbBdbSView::isLocal( ) const
00219 {
00220 return id( ).origin == _localRegistryH->originId( );
00221 }
00222
00223 bool
00224 CdbBdbSView::isFrozen( ) const
00225 {
00226 return _viewH->isFrozen( );
00227 }
00228
00229 CdbStatus
00230 CdbBdbSView::findFolder( CdbFolderPtr& thePtr,
00231 const char* theName
00232 )
00233 {
00234 CdbStatus result = CdbStatus::Error;
00235
00236 thePtr = 0;
00237
00238 do {
00239
00240 if( 0 != strcmp( theName, CdbPathName::separator( ))) {
00241
00242 cout << "CdbBdbSView::findFolder() -- ERROR" << endl
00243 << " No such folder in this implementation: \"" << theName << "\"" << endl
00244 << " The only name allowed is the rood one: \"" << CdbPathName::separator( ) << "\"" << endl;
00245
00246 break;
00247 }
00248
00249
00250
00251 BdbRef(CdbBdbSFolderP) fRef;
00252 if( CdbStatus::Success != _viewH->findFolder( theName,
00253 fRef )) {
00254 result = CdbStatus::NotFound;
00255 break;
00256 }
00257
00258
00259
00260
00261
00262
00263 CdbViewPtr myPtr( this->clone( ));
00264 CdbFolderPtr fPtr( 0 );
00265
00266 thePtr = new CdbBdbSFolder( myPtr,
00267 fPtr,
00268 theName,
00269 fRef,
00270 _masterRegistryH,
00271 _localRegistryH );
00272
00273
00274
00275 result = CdbStatus::Success;
00276
00277 } while( false );
00278
00279 return result;
00280 }
00281
00282 CdbStatus
00283 CdbBdbSView::folderIterator( CdbFolderItr& theItr )
00284 {
00285 return _viewH->folderIterator( theItr );
00286 }
00287
00288 CdbStatus
00289 CdbBdbSView::getDefault( const BdbTime& theValidityTime,
00290 CdbConfigElement& theConfigElement ) const
00291 {
00292 const char* errorStr = "CdbBdbSView::getDefault() -- ERROR.";
00293
00294 CdbStatus result = CdbStatus::Error;
00295 do {
00296
00297
00298
00299
00300 if(( theValidityTime < _viewH->minValidity( )) ||
00301 ( theValidityTime >= _viewH->maxValidity( ))) {
00302
00303 cout << errorStr << endl
00304 << " The pased validity time is out of the validity interval covered" << endl
00305 << " by the current view." << endl
00306 << " Passed validity time: " << theValidityTime << endl
00307 << " Minimum validity time: " << _viewH->minValidity( ) << endl
00308 << " Maximum validity time: " << _viewH->maxValidity( ) << endl;
00309 break;
00310 }
00311
00312
00313
00314 BdbRef(CdbBdbSConfigCollectionP) configCollRef = _viewH->defaultConfig( );
00315 if( BdbIsNull(configCollRef)) {
00316 result = CdbStatus::NotFound;
00317 break;
00318 }
00319
00320
00321
00322 CdbBdbSConfigInterval configInterval;
00323 if( CdbStatus::Success != configCollRef->find( theValidityTime,
00324 configInterval )) {
00325 cout << errorStr << endl
00326 << " The default configuration of the current view is inproperly configured." << endl
00327 << " No configuration record found for specified validity time." << endl
00328 << " at the view level." << endl
00329 << " Current view name: \"" << name( ) << "\"" << endl
00330 << " Passed validity time: " << theValidityTime << endl;
00331 break;
00332 }
00333
00334
00335
00336 if( configInterval.value.accessIsAllowed( )) {
00337
00338 CdbRevisionPolicy policy;
00339 if( configInterval.value.useRevision ) {
00340 policy = CdbRevisionPolicy( configInterval.value.revision,
00341 configInterval.value.partition );
00342 } else {
00343 policy = CdbRevisionPolicy( );
00344 }
00345 theConfigElement = CdbConfigElement( policy,
00346 configInterval.begin,
00347 configInterval.end );
00348
00349 } else {
00350
00351
00352
00353
00354 theConfigElement = CdbConfigElement( configInterval.begin,
00355 configInterval.end );
00356 }
00357
00358
00359
00360 result = CdbStatus::Success;
00361
00362 } while( false );
00363
00364 return result;
00365 }
00366
00367 CdbStatus
00368 CdbBdbSView::defaultConfigIterator( CdbItr<CdbConfigElement>& theItr ) const
00369 {
00370 CdbStatus result = CdbStatus::NotFound;
00371 do {
00372
00373 BdbRef(CdbBdbSConfigCollectionP) configCollRef = _viewH->defaultConfig( );
00374 if( BdbIsNull(configCollRef)) break;
00375
00376
00377
00378
00379 theItr = CdbItr<CdbConfigElement>( new CdbBdbSConfigItr( configCollRef->iterator( )));
00380
00381
00382
00383 result = CdbStatus::Success;
00384
00385 } while( false );
00386
00387 return result;
00388 }
00389
00390 CdbStatus
00391 CdbBdbSView::get( const char* theName,
00392 const BdbTime& theValidityTime,
00393 CdbConfigElement& theConfigElement ) const
00394 {
00395 const char* errorStr = "CdbBdbSView::get() -- ERROR.";
00396
00397 CdbStatus result = CdbStatus::Error;
00398 do {
00399
00400
00401
00402 if( 0 == theName ) {
00403 cout << errorStr << endl
00404 << " Non 0 pointer onto a fully qualified condition path name was expected." << endl;
00405 break;
00406 }
00407
00408 CdbPathName fullPath( theName );
00409
00410 if( ! ( fullPath.isValid( ) &&
00411 fullPath.isAbsolute( ) &&
00412 fullPath.isComposite( ))) {
00413
00414 cout << errorStr << endl
00415 << " Unsupported format of the condition name. Fully qualified" << endl
00416 << " condition name including its folder path was expected." << endl
00417 << " Passed condition name: \"" << theName << "\"" << endl;
00418 break;
00419 }
00420
00421
00422
00423
00424 if(( theValidityTime < _viewH->minValidity( )) ||
00425 ( theValidityTime >= _viewH->maxValidity( ))) {
00426
00427 cout << errorStr << endl
00428 << " The pased validity time is out of the validity interval covered" << endl
00429 << " by the current view." << endl
00430 << " Passed validity time: " << theValidityTime << endl
00431 << " Minimum validity time: " << _viewH->minValidity( ) << endl
00432 << " Maximum validity time: " << _viewH->maxValidity( ) << endl;
00433 break;
00434 }
00435
00436
00437
00438
00439
00440 BdbRef(CdbBdbSConditionAtFolderP) cRef;
00441 if( CdbStatus::Success != _viewH->findCondition( theName,
00442 cRef )) {
00443 result = CdbStatus::NotFound;
00444 break;
00445 }
00446
00447 BdbRef(CdbBdbSConfigCollectionP) configCollRef = cRef->config( );
00448 if( BdbIsNull(configCollRef)) {
00449
00450
00451
00452
00453 configCollRef = _viewH->defaultConfig( );
00454 if( BdbIsNull(configCollRef)) {
00455 cout << errorStr << endl
00456 << " The current view is inproperly configured for specified condition." << endl
00457 << " No configuration found both at specified condition scope and" << endl
00458 << " at the view level." << endl
00459 << " Current view name: \"" << name( ) << "\"" << endl
00460 << " Passed condition name: \"" << theName << "\"" << endl;
00461 break;
00462 }
00463 }
00464
00465
00466
00467 CdbBdbSConfigInterval configInterval;
00468 if( CdbStatus::Success != configCollRef->find( theValidityTime,
00469 configInterval )) {
00470 cout << errorStr << endl
00471 << " The current view is inproperly configured for specified condition." << endl
00472 << " No configuration record found for specified validity time." << endl
00473 << " at the view level." << endl
00474 << " Current view name: \"" << name( ) << "\"" << endl
00475 << " Passed condition name: \"" << theName << "\"" << endl
00476 << " Passed validity time: " << theValidityTime << endl;
00477 break;
00478 }
00479
00480
00481
00482 if( configInterval.value.accessIsAllowed( )) {
00483
00484 CdbRevisionPolicy policy;
00485 if( configInterval.value.useRevision ) {
00486 policy = CdbRevisionPolicy( configInterval.value.revision,
00487 configInterval.value.partition );
00488 } else {
00489 policy = CdbRevisionPolicy( );
00490 }
00491 theConfigElement = CdbConfigElement( policy,
00492 configInterval.begin,
00493 configInterval.end );
00494
00495 } else {
00496
00497
00498
00499
00500 theConfigElement = CdbConfigElement( configInterval.begin,
00501 configInterval.end );
00502 }
00503
00504
00505
00506 result = CdbStatus::Success;
00507
00508 } while( false );
00509
00510 return result;
00511 }
00512
00513 CdbStatus
00514 CdbBdbSView::configIterator( const char* theName,
00515 CdbItr<CdbConfigElement>& theItr ) const
00516 {
00517 const char* errorStr = "CdbBdbSView::configIterator() -- ERROR.";
00518
00519 CdbStatus result = CdbStatus::Error;
00520 do {
00521
00522
00523
00524 if( 0 == theName ) {
00525 cout << errorStr << endl
00526 << " Non 0 pointer onto a fully qualified condition path name was expected." << endl;
00527 break;
00528 }
00529
00530 CdbPathName fullPath( theName );
00531
00532 if( ! ( fullPath.isValid( ) &&
00533 fullPath.isAbsolute( ) &&
00534 fullPath.isComposite( ))) {
00535
00536 cout << errorStr << endl
00537 << " Unsupported format of the condition name. Fully qualified" << endl
00538 << " condition name including its folder path was expected." << endl
00539 << " Passed condition name: \"" << theName << "\"" << endl;
00540 break;
00541 }
00542
00543
00544
00545
00546
00547 BdbRef(CdbBdbSConditionAtFolderP) cRef;
00548 if( CdbStatus::Success != _viewH->findCondition( theName,
00549 cRef )) {
00550 result = CdbStatus::NotFound;
00551 break;
00552 }
00553
00554 BdbRef(CdbBdbSConfigCollectionP) configCollRef = cRef->config( );
00555 if( BdbIsNull(configCollRef)) {
00556
00557
00558
00559
00560 configCollRef = _viewH->defaultConfig( );
00561 if( BdbIsNull(configCollRef)) {
00562 cout << errorStr << endl
00563 << " The current view is inproperly configured for specified condition." << endl
00564 << " No configuration found both at specified condition scope and" << endl
00565 << " at the view level." << endl
00566 << " Current view name: \"" << name( ) << "\"" << endl
00567 << " Passed condition name: \"" << theName << "\"" << endl;
00568 break;
00569 }
00570 }
00571
00572
00573
00574
00575 theItr = CdbItr<CdbConfigElement>( new CdbBdbSConfigItr( configCollRef->iterator( )));
00576
00577
00578
00579 result = CdbStatus::Success;
00580
00581 } while( false );
00582
00583 return result;
00584 }
00585
00586 CdbStateId
00587 CdbBdbSView::currentStateId( const BdbTime& theValidityTime ) const
00588 {
00589 const char* errorStr = "CdbBdbSView::currentStateId() -- ERROR.";
00590
00591 CdbStateId result;
00592 do {
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639 BdbTime partitionModificationTime( BdbTime::now( ));
00640 {
00641 BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = _masterRegistryH->partitionsLayout( );
00642 assert( !BdbIsNull(pLayoutRef));
00643
00644
00645
00646
00647 BdbRef(CdbBdbSPartitionP) pRef;
00648 if( CdbStatus::Success == pLayoutRef->topmost( theValidityTime,
00649 pRef )) {
00650
00651
00652
00653 if( !pRef->isClosed( )) {
00654
00655
00656
00657
00658
00659 if( _masterRegistryH->originId( ) != _localRegistryH->originId( )) {
00660 if( _localRegistryH->originId( ) == pRef->originId( )) {
00661
00662
00663
00664
00665 pLayoutRef = _localRegistryH->partitionsLayout( );
00666 assert( !BdbIsNull(pLayoutRef));
00667
00668
00669
00670
00671
00672
00673 BdbRef(CdbBdbSPartitionP) pFinalRef;
00674 if( CdbStatus::Success != pLayoutRef->find( pRef->id( ),
00675 pFinalRef )) {
00676 cout << errorStr << endl
00677 << " Failed to locate a final partition with ID=" << pRef->id( ) << endl
00678 << " at the local database where it's supposed to belong to." << endl
00679 << " The local database may not be properly initialized/loaded." << endl;
00680 break;
00681 }
00682
00683
00684
00685 pRef = pFinalRef;
00686 }
00687 }
00688 }
00689
00690
00691
00692
00693
00694
00695 if( pRef->isClosed( )) {
00696
00697
00698
00699
00700 partitionModificationTime = pRef->modified( );
00701
00702 } else {
00703
00704
00705
00706
00707
00708
00709 if( pRef->isInstantiated( )) {
00710 partitionModificationTime = pRef->modified( );
00711 } else {
00712 if( _localRegistryH->originId( ) == pRef->originId( )) {
00713 cout << errorStr << endl
00714 << " The partition with ID=" << pRef->id( ) << " corresponding to the following" << endl
00715 << " validity time: " << theValidityTime << endl
00716 << " belonging to the local database is not properly instantiated." << endl
00717 << " The local database may not be properly initialized/loaded." << endl;
00718 break;
00719
00720 } else {
00721
00722
00723
00724
00725
00726
00727
00728
00729 partitionModificationTime = pRef->created( );
00730 }
00731 }
00732 }
00733 }
00734 }
00735
00736
00737
00738 result = CdbStateId( id( ), partitionModificationTime );
00739
00740 } while( false );
00741
00742 return result;
00743 }
00744
00745 CdbStatus
00746 CdbBdbSView::historyEventIterator( CdbHistoryEventItr& theItr,
00747 const BdbTime& theBeginTime,
00748 const BdbTime& theEndTime,
00749 const char** theEventsToSelect )
00750 {
00751 return CdbStatus::Error;
00752 }
00753
00754 CdbStatus
00755 CdbBdbSView::historyEventTypeIterator( CdbHistoryEventTypeItr& theItr )
00756 {
00757 return CdbStatus::Error;
00758 }
00759
00760
00761
00762