00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbRooReadonly/CdbRooRoMetaDataR.hh"
00012
00013 #include "CdbBase/CdbTimeUtils.hh"
00014
00015 #include "ErrLogger/ErrLog.hh"
00016
00017 #include <assert.h>
00018 #include <string.h>
00019
00020 #include <iostream>
00021 using std::cout;
00022 using std::endl;
00023
00024 namespace {
00025
00026
00027
00028
00029
00030 class CombinedIntervalIterator : public CdbItr<CdbRooRoCi>::InterfaceType {
00031
00032 private:
00033
00034
00035
00036 CombinedIntervalIterator( );
00037
00038
00039
00040 CombinedIntervalIterator& operator=( const CombinedIntervalIterator& theItr );
00041
00042 protected:
00043
00044
00045
00046
00047
00048
00049
00050
00051 CombinedIntervalIterator( const CombinedIntervalIterator& theItr ) :
00052 _viCollection (theItr._viCollection),
00053 _oiCollection (theItr._oiCollection),
00054 _beginTime (theItr._beginTime),
00055 _endTime (theItr._endTime),
00056 _isValid (theItr._isValid),
00057 _hasEverBeenAdvanced(theItr._hasEverBeenAdvanced),
00058 _value (theItr._value),
00059 _viItr (theItr._viItr)
00060 { }
00061
00062 public:
00063
00064
00065
00066 CombinedIntervalIterator( const CdbRooRoViCollectionR& theViCollection,
00067 const CdbRooRoOiCollectionR& theOiCollection,
00068 const BdbTime& theBeginTime,
00069 const BdbTime& theEndTime ) :
00070 _viCollection (theViCollection),
00071 _oiCollection (theOiCollection),
00072 _beginTime (theBeginTime),
00073 _endTime (theEndTime),
00074 _isValid (false),
00075 _hasEverBeenAdvanced(false)
00076 { }
00077
00078
00079
00080 virtual ~CombinedIntervalIterator( )
00081 { }
00082
00083
00084
00085
00086
00087
00088
00089
00090 virtual CdbStatus reset( )
00091 {
00092 _isValid = false;
00093 _hasEverBeenAdvanced = false;
00094
00095 return CdbStatus::Success;
00096 }
00097
00098
00099
00100 bool tryNext( )
00101 {
00102 bool result = false;
00103 do {
00104
00105
00106
00107 if( !_viItr.next( )) break;
00108
00109 CdbRooRoViR vi = _viItr.value( );
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 if( ! ((( _beginTime <= vi.begin( )) && ( vi.begin( ) < _endTime )) ||
00136 (( vi.begin( ) <= _beginTime ) && ( _beginTime < vi.end( ))))) break;
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 if( 0 == vi.value( )){
00147
00148
00149
00150
00151 _value = CdbRooRoCi( vi.begin( ),
00152 vi.end( ));
00153 } else {
00154
00155
00156
00157
00158 CdbRooRoOiR oi;
00159 BdbTime beginOfDuration;
00160 BdbTime endOfDuration;
00161
00162 if( CdbStatus::Success != _oiCollection.find( vi.value( ),
00163 oi,
00164 beginOfDuration,
00165 endOfDuration )) {
00166
00167 assert( 0 );
00168
00169 break;
00170 }
00171 _value = CdbRooRoCi( vi.begin( ),
00172 vi.end( ),
00173 oi );
00174 }
00175
00176
00177
00178 result = true;
00179
00180 } while( false );
00181
00182 return result;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 virtual bool next( )
00192 {
00193 if( !_isValid ) {
00194
00195 if( !_hasEverBeenAdvanced ) {
00196 _hasEverBeenAdvanced = true;
00197
00198 _viItr = _viCollection.iterator( _beginTime );
00199
00200 } else {
00201
00202
00203
00204
00205 return false;
00206 }
00207 }
00208 if( !_isValid ) {
00209
00210
00211
00212 _isValid = tryNext( );
00213
00214 } else if( _isValid && _viItr.isValid( )) {
00215
00216
00217
00218 _isValid = tryNext( );
00219
00220 } else {
00221
00222
00223
00224 _isValid = false;
00225 }
00226 return _isValid;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 virtual ValueType value( )
00239 {
00240 if( ! _isValid ) assert( 0 );
00241 return _value;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 virtual bool isValid( )
00251 {
00252 return _isValid;
00253 }
00254
00255
00256
00257
00258
00259 virtual InterfaceType* clone( ) const
00260 {
00261 return new CombinedIntervalIterator( *this );
00262 }
00263
00264 private:
00265
00266
00267
00268 CdbRooRoViCollectionR _viCollection;
00269 CdbRooRoOiCollectionR _oiCollection;
00270
00271 BdbTime _beginTime;
00272 BdbTime _endTime;
00273
00274
00275
00276 bool _isValid;
00277 bool _hasEverBeenAdvanced;
00278
00279 CdbRooRoCi _value;
00280
00281 CdbRooRoViCollectionR::IteratorOfIntervals _viItr;
00282 };
00283 };
00284
00285
00286
00287
00288
00289 CdbRooRoMetaDataR::CdbRooRoMetaDataR( const BdbTime& theMinValidityTime,
00290 const BdbTime& theMaxValidityTime,
00291 const BdbTime& theMinInsertionTime ) :
00292 CdbRooRoPersistentCollectionR( ),
00293 _minValidityTime (theMinValidityTime),
00294 _maxValidityTime (theMaxValidityTime),
00295 _minInsertionTime(theMinInsertionTime),
00296 _maxInsertionTime(BdbTime::plusInfinity),
00297 _modified (theMinInsertionTime)
00298 { }
00299
00300 CdbRooRoMetaDataR::CdbRooRoMetaDataR( const BdbTime& theMinValidityTime,
00301 const BdbTime& theMaxValidityTime,
00302 const BdbTime& theMinInsertionTime,
00303 const BdbTime& theMaxInsertionTime,
00304 const BdbTime& theModificationTime,
00305 const CdbRooRoOiCollectionR& theOriginalCollection,
00306 const CdbRooRoRevCollectionR& theRevisionCollection ) :
00307 CdbRooRoPersistentCollectionR( ),
00308 _minValidityTime (theMinValidityTime),
00309 _maxValidityTime (theMaxValidityTime),
00310 _minInsertionTime (theMinInsertionTime),
00311 _maxInsertionTime (theMaxInsertionTime),
00312 _modified (theModificationTime),
00313 _originalCollection(theOriginalCollection),
00314 _revCollection (theRevisionCollection)
00315 { }
00316
00317 CdbCPtr< CdbRooRoOiCollectionR>
00318 CdbRooRoMetaDataR::originalIntervalsCollection( ) const
00319 {
00320 return CdbCPtr< CdbRooRoOiCollectionR>( new CdbRooRoOiCollectionR( _originalCollection ));
00321 }
00322
00323 CdbStatus
00324 CdbRooRoMetaDataR::findByRevision( const BdbTime& theRevisionId,
00325 const BdbTime& theValidityTime,
00326 CdbRooRoOiR& theOriginalInterval,
00327 BdbTime& theBeginOfVisiblePeriod,
00328 BdbTime& theEndOfVisiblePeriod ) const
00329 {
00330 const char* errorStr = "CdbRooRoMetaDataR::findByRevision() -- ERROR";
00331 const char* fatalStr = "CdbRooRoMetaDataR::findByRevision() -- FATAL INTERNAL ERROR!!!";
00332
00333 CdbStatus result = CdbStatus::Error;
00334 do {
00335
00336
00337
00338 theBeginOfVisiblePeriod = minValidity( );
00339 theEndOfVisiblePeriod = maxValidity( );
00340
00341
00342
00343 if(( theValidityTime < minValidity( )) ||
00344 ( theValidityTime >= maxValidity( ))) {
00345 cout << errorStr << endl
00346 << " The passed validity time is out of the validity window" << endl
00347 << " of the current metadata object." << endl;
00348 break;
00349 }
00350
00351
00352
00353
00354 CdbRooRoRevisionR revision;
00355 if( CdbStatus::Success != _revCollection.find( theRevisionId,
00356 revision )) {
00357 cout << errorStr << endl
00358 << " Specified revision was not found in the collection." << endl
00359 << " PASSED REVISION ID: " << CdbTimeUtils::time2string( theRevisionId ) << endl;
00360 break;
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 CdbRooRoViR viInterval;
00378
00379 if( !revision.collection( ).find( viInterval, theValidityTime )) {
00380
00381 assert( 0 );
00382
00383 break;
00384 }
00385
00386 theBeginOfVisiblePeriod = viInterval.begin( );
00387 theEndOfVisiblePeriod = viInterval.end( );
00388
00389 UInt_t oiIdx = viInterval.value( );
00390 if( 0 == oiIdx ) {
00391 result = CdbStatus::NotFound;
00392 break;
00393 }
00394
00395
00396
00397
00398 BdbTime beginOfDurationPeriod( 0 );
00399 BdbTime endOfDurationPeriod ( 0 );
00400
00401 if( CdbStatus::Success != _originalCollection.find( oiIdx,
00402 theOriginalInterval,
00403 beginOfDurationPeriod,
00404 endOfDurationPeriod )) {
00405 cout << fatalStr << endl
00406 << " The collection of 'visible' intervals does not match" << endl
00407 << " the collection of 'original' intervals." << endl;
00408
00409 assert( 0 );
00410
00411 break;
00412 }
00413
00414
00415
00416 result = CdbStatus::Success;
00417
00418 } while( false );
00419
00420 return result;
00421 }
00422
00423 CdbStatus
00424 CdbRooRoMetaDataR::findByRevision( const char* theRevisionName,
00425 const BdbTime& theValidityTime,
00426 CdbRooRoOiR& theOriginalInterval,
00427 BdbTime& theBeginOfVisiblePeriod,
00428 BdbTime& theEndOfVisiblePeriod ) const
00429 {
00430 const char* errorStr = "CdbRooRoMetaDataR::findByRevision() -- ERROR";
00431
00432 CdbStatus result = CdbStatus::Error;
00433 do {
00434
00435
00436
00437 theBeginOfVisiblePeriod = minValidity( );
00438 theEndOfVisiblePeriod = maxValidity( );
00439
00440
00441
00442
00443 BdbTime revisionId( 0 );
00444
00445 if( 0 == theRevisionName ) {
00446 revisionId = BdbTime::plusInfinity;
00447 } else {
00448 CdbRooRoRevisionR revision;
00449 if( CdbStatus::Success != _revCollection.find( theRevisionName,
00450 revision )) {
00451 cout << errorStr << endl
00452 << " Specified revision was not found in the collection." << endl
00453 << " PASSED REVISION NAME: \"" << theRevisionName << "\"" << endl;
00454 break;
00455 }
00456 revisionId = revision.id( );
00457 }
00458
00459
00460
00461 result = findByRevision( revisionId,
00462 theValidityTime,
00463 theOriginalInterval,
00464 theBeginOfVisiblePeriod,
00465 theEndOfVisiblePeriod );
00466
00467 } while( false );
00468
00469 return result;
00470 }
00471
00472 CdbStatus
00473 CdbRooRoMetaDataR::findByInsertion( const BdbTime& theInsertionTime,
00474 const BdbTime& theValidityTime,
00475 CdbRooRoOiR& theOriginalInterval,
00476 BdbTime& theBeginOfDurationPeriod,
00477 BdbTime& theEndOfDurationPeriod ) const
00478 {
00479 const char* errorStr = "CdbRooRoMetaDataR::findByInsertion() -- ERROR";
00480
00481 CdbStatus result = CdbStatus::Error;
00482 do {
00483
00484
00485
00486 if(( theValidityTime < minValidity( )) ||
00487 ( theValidityTime >= maxValidity( ))) {
00488 cout << errorStr << endl
00489 << " The passed validity time is out of the validity window" << endl
00490 << " of the current metadata object." << endl;
00491 break;
00492 }
00493 if(( theInsertionTime < minInsertion( )) ||
00494 ( theInsertionTime >= maxInsertion( ))) {
00495 cout << errorStr << endl
00496 << " The passed insertion time is out of the insertion window" << endl
00497 << " of the current metadata object." << endl;
00498 break;
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508 CdbStatus status = _originalCollection.find( theInsertionTime,
00509 theOriginalInterval,
00510 theBeginOfDurationPeriod,
00511 theEndOfDurationPeriod );
00512 if( CdbStatus::Success != status ) {
00513 result = status;
00514 break;
00515 }
00516
00517
00518
00519
00520 if(( theValidityTime < theOriginalInterval.begin( )) ||
00521 ( theValidityTime >= theOriginalInterval.end( ) )) {
00522 cout << errorStr << endl
00523 << " This is inappropriate use of this finding method since" << endl
00524 << " the passed validity time does not fall into the 'validity' period" << endl
00525 << " of found 'original' interval." << endl;
00526 break;
00527 }
00528
00529
00530
00531 result = CdbStatus::Success;
00532
00533 } while( false );
00534
00535 return result;
00536 }
00537 CdbStatus
00538 CdbRooRoMetaDataR::findRevision( const BdbTime& theId,
00539 CdbRooRoRevisionR& theRef ) const
00540 {
00541 CdbStatus result = CdbStatus::NotFound;
00542 do {
00543
00544
00545
00546
00547
00548
00549 if( theId < minInsertion( )) break;
00550 if(( BdbTime::plusInfinity != maxInsertion( )) && ( theId >= maxInsertion( ))) break;
00551
00552
00553
00554 result = _revCollection.find( theId,
00555 theRef );
00556 } while( false );
00557
00558 return result;
00559 }
00560
00561 CdbStatus
00562 CdbRooRoMetaDataR::findRevision( const BdbTime& theId,
00563 CdbCPtr< CdbRooRoRevisionR >& thePtr ) const
00564 {
00565 CdbStatus result = CdbStatus::Error;
00566 {
00567 CdbRooRoRevisionR revision;
00568 if( CdbStatus::Success == ( result = findRevision( theId,
00569 revision ))) thePtr = new CdbRooRoRevisionR( revision );
00570 }
00571 return result;
00572 }
00573
00574 CdbStatus
00575 CdbRooRoMetaDataR::findRevision( const char* theName,
00576 CdbRooRoRevisionR& theRef ) const
00577 {
00578 const char* errorStr = "CdbRooRoMetaDataR::findRevision(name) -- ERROR";
00579
00580 CdbStatus result = CdbStatus::NotFound;
00581 do {
00582
00583
00584
00585 if( 0 == theName ) {
00586 cout << errorStr << endl
00587 << " Null pointer passed instead of a valid revision name." << endl;
00588 break;
00589 }
00590
00591
00592
00593 result = _revCollection.find( theName,
00594 theRef );
00595 } while( false );
00596
00597 return result;
00598 }
00599
00600 CdbStatus
00601 CdbRooRoMetaDataR::findRevision( const char* theName,
00602 CdbCPtr< CdbRooRoRevisionR >& thePtr ) const
00603 {
00604 CdbStatus result = CdbStatus::Error;
00605 {
00606 CdbRooRoRevisionR revision;
00607 if( CdbStatus::Success == ( result = findRevision( theName,
00608 revision ))) thePtr = new CdbRooRoRevisionR( revision );
00609 }
00610 return result;
00611 }
00612
00613 CdbStatus
00614 CdbRooRoMetaDataR::revisionIdIterator( CdbItr<BdbTime>& theItr ) const
00615 {
00616 theItr = _revCollection.iterator_identifiers( );
00617 return CdbStatus::Success;
00618 }
00619
00620 CdbStatus
00621 CdbRooRoMetaDataR::revisionNameIterator( CdbItr<const char*>& theItr ) const
00622 {
00623 theItr = _revCollection.iterator_names( );
00624 return CdbStatus::Success;
00625 }
00626
00627 bool
00628 CdbRooRoMetaDataR::verifyObjectIteratorParameters( const BdbTime& theBeginTime,
00629 const BdbTime& theEndTime ) const
00630 {
00631 const char* errorStr = "CdbRooRoMetaDataR::verifyObjectIteratorParameters() -- ERROR";
00632
00633 bool result = false;
00634 do {
00635
00636
00637
00638 if( ! (( minValidity( ) <= theBeginTime ) && ( theBeginTime < maxValidity( )))) {
00639 cout << errorStr << endl
00640 << " Incorrect begin time passed to the method. It's out of the validity" << endl
00641 << " time window of the current meta-data object." << endl
00642 << " PASSED BEGIN TIME : " << CdbTimeUtils::time2string( theBeginTime ) << endl
00643 << " WINDOW BEGIN TIME : " << CdbTimeUtils::time2string( minValidity( )) << endl
00644 << " WINDOW END TIME : " << CdbTimeUtils::time2string( maxValidity( )) << endl;
00645 break;
00646 }
00647 if( ! (( minValidity( ) < theEndTime ) && ( theEndTime <= maxValidity( )))) {
00648 cout << errorStr << endl
00649 << " Incorrect end time passed to the method. It's out of the validity" << endl
00650 << " time window of the current meta-data object." << endl
00651 << " PASSED END TIME : " << CdbTimeUtils::time2string( theEndTime ) << endl
00652 << " WINDOW BEGIN TIME : " << CdbTimeUtils::time2string( minValidity( )) << endl
00653 << " WINDOW END TIME : " << CdbTimeUtils::time2string( maxValidity( )) << endl;
00654 break;
00655 }
00656 if( ! ( theBeginTime < theEndTime )) {
00657 cout << errorStr << endl
00658 << " Incorrect begin and end times passed to the method." << endl
00659 << " The begin time must be strictly less (older) than the end one. " << endl
00660 << " PASSED BEGIN TIME : " << CdbTimeUtils::time2string( theBeginTime ) << endl
00661 << " PASSED END TIME : " << CdbTimeUtils::time2string( theEndTime ) << endl;
00662 break;
00663 }
00664
00665
00666
00667 result = true;
00668
00669 } while( false );
00670
00671 return result;
00672 }
00673
00674 CdbStatus
00675 CdbRooRoMetaDataR::objectIterator( CdbItr<CdbRooRoCi>& theItr,
00676 const BdbTime& theRevisionId,
00677 const BdbTime& theBeginTime,
00678 const BdbTime& theEndTime ) const
00679 {
00680 const char* errorStr = "CdbRooRoMetaDataR::objectIterator(id) -- ERROR";
00681
00682 CdbStatus result = CdbStatus::Error;
00683 do {
00684
00685 if( !verifyObjectIteratorParameters( theBeginTime,
00686 theEndTime )) break;
00687
00688
00689 CdbRooRoRevisionR revision;
00690 if( CdbStatus::Success != findRevision( theRevisionId,
00691 revision )) {
00692 cout << errorStr << endl
00693 << " Failed to find a revision for specified identifier." << endl
00694 << " REVISION IDENTIFIER : " << CdbTimeUtils::time2string( theRevisionId ) << endl;
00695 }
00696
00697
00698
00699
00700 theItr = CdbItr<CdbRooRoCi>( new CombinedIntervalIterator( revision.collection( ),
00701 _originalCollection,
00702 theBeginTime,
00703 theEndTime ));
00704
00705
00706
00707 result = CdbStatus::Success;
00708
00709 } while( false );
00710
00711 return result;
00712 }
00713
00714 CdbStatus
00715 CdbRooRoMetaDataR::objectIterator( CdbItr<CdbRooRoCi>& theItr,
00716 const char* theName,
00717 const BdbTime& theBeginTime,
00718 const BdbTime& theEndTime ) const
00719 {
00720 const char* errorStr = "CdbRooRoMetaDataR::objectIterator(name) -- ERROR";
00721
00722 CdbStatus result = CdbStatus::Error;
00723 do {
00724
00725 if( !verifyObjectIteratorParameters( theBeginTime,
00726 theEndTime )) break;
00727
00728
00729
00730 CdbRooRoRevisionR revision;
00731 if( CdbStatus::Success != findRevision( theName,
00732 revision )) {
00733 cout << errorStr << endl
00734 << " Failed to find a revision for specified name." << endl
00735 << " REVISION NAME : \"" << theName << "\"" << endl;
00736 break;
00737 }
00738
00739
00740
00741
00742 theItr = CdbItr<CdbRooRoCi>( new CombinedIntervalIterator( revision.collection( ),
00743 _originalCollection,
00744 theBeginTime,
00745 theEndTime ));
00746
00747
00748
00749 result = CdbStatus::Success;
00750
00751 } while( false );
00752
00753 return result;
00754 }
00755
00756 void
00757 CdbRooRoMetaDataR::dump( std::ostream& o ) const
00758 {
00759 o << "MIN.VALIDITY : " << CdbTimeUtils::time2string( minValidity ( )) << " : " << minValidity ( ) << endl
00760 << "MAX.VALIDITY : " << CdbTimeUtils::time2string( maxValidity ( )) << " : " << maxValidity ( ) << endl
00761 << "MIN.INSERTION : " << CdbTimeUtils::time2string( minInsertion( )) << " : " << minInsertion( ) << endl
00762 << "MAX.INSERTION : " << CdbTimeUtils::time2string( maxInsertion( )) << " : " << maxInsertion( ) << endl
00763 << "MODIFIED : " << CdbTimeUtils::time2string( modified ( )) << " : " << modified ( ) << endl
00764 << "NUM.ORIGINAL INTERVALS : " << _originalCollection.size( ) << endl
00765 << "NUM.REVISIONS : " << _revCollection.size( ) << endl;
00766 }
00767
00768 CdbStatus
00769 CdbRooRoMetaDataR::storeSubCollectionsAt( const CdbRooRoCollectionAddressR& theCollectionAddress,
00770 const CdbCPtr<TFile>& theFilePtr,
00771 Int_t& theNumBytesStored )
00772 {
00773 const char* errorStr = "CdbRooRoMetaDataR::storeSubCollectionsAt() -- ERROR";
00774
00775 Int_t numBytesStored = 0;
00776
00777
00778
00779 Int_t numBytes = 0;
00780
00781 CdbStatus status = CdbStatus::Error;
00782
00783 if( CdbStatus::Success != ( status = _originalCollection.storeAsEmbeddedAt( theCollectionAddress,
00784 theFilePtr,
00785 numBytes ))) {
00786 ErrMsg(error) << errorStr << endl
00787 << " Failed to store an embedded collection of original intervals." << endl
00788 << " File Name : " << theFilePtr->GetName( ) << endl
00789 << " Collection Address: " << theCollectionAddress << endl
00790 << " Error Status : " << status
00791 << endmsg;
00792 return status;
00793 }
00794 numBytesStored += numBytes;
00795
00796 if( CdbStatus::Success != ( status = _revCollection.storeAsEmbeddedAt( theCollectionAddress,
00797 theFilePtr,
00798 numBytes ))) {
00799 ErrMsg(error) << errorStr << endl
00800 << " Failed to store an embedded collection of revisions." << endl
00801 << " File Name : " << theFilePtr->GetName( ) << endl
00802 << " Collection Address: " << theCollectionAddress << endl
00803 << " Error Status : " << status
00804 << endmsg;
00805 return status;
00806 }
00807 numBytesStored += numBytes;
00808
00809
00810
00811 theNumBytesStored = numBytesStored;
00812
00813 return CdbStatus::Success;
00814 }
00815
00816
00817
00818