00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbBdbShared/CdbBdbSViewP.hh"
00012
00013 #include "CdbBdbShared/CdbBdbSFolderP.hh"
00014 #include "CdbBdbShared/CdbBdbSFolderPItr.hh"
00015 #include "CdbBdbShared/CdbBdbSRegistryP.hh"
00016 #include "CdbBdbShared/CdbBdbSViewCollectionP.hh"
00017 #include "CdbBdbShared/CdbBdbSRallocatorP.hh"
00018
00019 #include "CdbBase/CdbPathName.hh"
00020
00021 #include <assert.h>
00022 using std::cout;
00023 using std::endl;
00024 using std::ostream;
00025
00026 CdbBdbSViewP::CdbBdbSViewP( const char* theName,
00027 d_UShort theId,
00028 const char* theDescription,
00029 const BdbTime& theCreationTime,
00030 const BdbTime& theMinValidityTime,
00031 const BdbTime& theMaxValidityTime,
00032 const BdbRef(CdbBdbSConfigCollectionP)& theDefaultConfigRef ) :
00033 CdbBdbSCollectionElementP( theName,
00034 theId ),
00035 _description(theDescription),
00036 _created(theCreationTime),
00037 _minValidityTime(theMinValidityTime),
00038 _maxValidityTime(theMaxValidityTime),
00039 _defaultConfigRef(theDefaultConfigRef),
00040 _rootFolderRef(0),
00041 _isFrozen(d_False)
00042 {
00043 assert( 0 != theDescription );
00044 assert(( BdbTime::minusInfinity != theCreationTime ) && ( BdbTime::plusInfinity != theCreationTime ));
00045
00046 _rootFolderRef = new( ooThis( )) CdbBdbSFolderP( CdbPathName::separator( ),
00047 "The ROOT folder of the current view",
00048 theCreationTime,
00049 ooThis( ),
00050 0 );
00051 }
00052
00053 CdbBdbSViewP::~CdbBdbSViewP( )
00054 { }
00055
00056 CdbStatus
00057 CdbBdbSViewP::clone( BdbRef(CdbBdbSViewP)& theRef,
00058 const char* theName,
00059 const char* theDescription ) const
00060 {
00061 const char* errorStr = "CdbBdbSViewP::clone() -- ERROR";
00062
00063 CdbStatus result = CdbStatus::Error;
00064 do {
00065
00066 BdbTime operationTime( BdbTime::now( ));
00067
00068
00069
00070 BdbHandle(CdbBdbSRegistryP) rH;
00071 if( CdbStatus::Success != CdbBdbSRegistryP::findLocal( rH )) {
00072 cout << errorStr << endl
00073 << " Failed to locate the local registry." << endl;
00074 break;
00075 }
00076
00077
00078
00079 BdbRef(CdbBdbSViewCollectionP) vCollRef = rH->viewCollection( );
00080 assert( ! BdbIsNull(vCollRef));
00081
00082 BdbRef(CdbBdbSRallocatorP< d_UShort >) vRaRef = rH->viewRallocator( );
00083 assert( ! BdbIsNull(vRaRef));
00084
00085
00086
00087 if( CdbStatus::Success == vCollRef->find( theName,
00088 theRef )) {
00089 cout << errorStr << endl
00090 << " A view with specified name already exists." << endl;
00091 break;
00092 }
00093
00094
00095
00096 d_UShort vId;
00097 if( CdbStatus::Success != vRaRef->allocate( vId )) {
00098 cout << errorStr << endl
00099 << " Failed to allocate a view identifier." << endl;
00100 break;
00101 }
00102
00103
00104
00105 BdbRef(CdbBdbSConfigCollectionP) configRef;
00106 if( !BdbIsNull(_defaultConfigRef)) {
00107 if( CdbStatus::Success != _defaultConfigRef->clone( configRef,
00108 vCollRef )) {
00109 cout << errorStr << endl
00110 << " Failed to clone the default configuration object." << endl;
00111
00112 vRaRef->release( vId );
00113
00114 break;
00115 }
00116 }
00117
00118
00119
00120
00121
00122 theRef = new( vCollRef ) CdbBdbSViewP( theName,
00123 vId,
00124 theDescription,
00125 operationTime,
00126 _minValidityTime,
00127 _maxValidityTime,
00128 configRef );
00129 assert( ! BdbIsNull(theRef));
00130
00131
00132
00133
00134 if( CdbStatus::Success != cloneComponents( _rootFolderRef,
00135 theRef->_rootFolderRef )) {
00136 cout << errorStr << endl
00137 << " Failed to clone the components of the view object." << endl;
00138
00139 vRaRef->release( vId );
00140 BdbDelete(theRef);
00141
00142 break;
00143 }
00144
00145
00146
00147 if( CdbStatus::Success != vCollRef->insert( theRef )) {
00148 cout << errorStr << endl
00149 << " Failed to insert new view into the collection." << endl;
00150
00151 vRaRef->release( vId );
00152 BdbDelete(theRef);
00153
00154 break;
00155 }
00156
00157
00158
00159 result = CdbStatus::Success;
00160
00161 } while( false );
00162
00163 return result;
00164 }
00165
00166 ooString(32)
00167 CdbBdbSViewP::description( ) const
00168 {
00169 return _description;
00170 }
00171
00172 BdbTime
00173 CdbBdbSViewP::created( ) const
00174 {
00175 return _created;
00176 }
00177
00178 BdbTime
00179 CdbBdbSViewP::minValidity( ) const
00180 {
00181 return _minValidityTime;
00182 }
00183
00184 BdbTime
00185 CdbBdbSViewP::maxValidity( ) const
00186 {
00187 return _maxValidityTime;
00188 }
00189
00190 BdbRef(CdbBdbSConfigCollectionP)
00191 CdbBdbSViewP::defaultConfig( ) const
00192 {
00193 return _defaultConfigRef;
00194 }
00195
00196 BdbRef(CdbBdbSFolderP)
00197 CdbBdbSViewP::rootFolder( ) const
00198 {
00199 return _rootFolderRef;
00200 }
00201
00202 bool
00203 CdbBdbSViewP::isFrozen( ) const
00204 {
00205 return _isFrozen;
00206 }
00207
00208 void
00209 CdbBdbSViewP::freeze( )
00210 {
00211 ooUpdate( );
00212 _isFrozen = d_True;
00213 }
00214
00215 void
00216 CdbBdbSViewP::warm( )
00217 {
00218 ooUpdate( );
00219 _isFrozen = d_False;
00220 }
00221
00222 CdbStatus
00223 CdbBdbSViewP::setDefaultConfig( const BdbRef(CdbBdbSConfigCollectionP)& theConfigRef )
00224 {
00225 const char* errorStr = "CdbBdbSViewP::setDefaultConfig() - ERROR.";
00226
00227 ooUpdate( );
00228
00229 CdbStatus result = CdbStatus::Error;
00230 do {
00231
00232
00233
00234 if( isFrozen( )) {
00235 cout << errorStr << endl
00236 << " This view is already frozen, which means that it can't be" << endl
00237 << " reconfigured anymore." << endl;
00238 break;
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 if( ! BdbIsNull(_defaultConfigRef)) {
00250 if( theConfigRef != _defaultConfigRef ) BdbDelete(_defaultConfigRef);
00251 }
00252 _defaultConfigRef = theConfigRef;
00253
00254
00255
00256 result = CdbStatus::Success;
00257
00258 } while( false );
00259
00260 return result;
00261 }
00262
00263 CdbStatus
00264 CdbBdbSViewP::setConfig( const char* thePathName,
00265 const BdbRef(CdbBdbSConfigCollectionP)& theConfigRef,
00266 bool dontDestroyConfigObject )
00267 {
00268 const char* errorStr = "CdbBdbSViewP::setConfig() - ERROR.";
00269
00270 ooUpdate( );
00271
00272 CdbStatus result = CdbStatus::Error;
00273 do {
00274
00275
00276
00277 if( isFrozen( )) {
00278 cout << errorStr << endl
00279 << " This view is already frozen, which means that it can't be" << endl
00280 << " reconfigured anymore." << endl;
00281 break;
00282 }
00283
00284
00285
00286 BdbRef(CdbBdbSConditionAtFolderP) cRef;
00287 if( CdbStatus::Success != findCondition( thePathName,
00288 cRef )) {
00289 result = CdbStatus::NotFound;
00290 break;
00291 }
00292
00293
00294
00295 result = cRef->setConfig( theConfigRef,
00296 dontDestroyConfigObject );
00297
00298 } while( false );
00299
00300 return result;
00301 }
00302
00303 CdbStatus
00304 CdbBdbSViewP::createFolder( const char* thePathName,
00305 const char* theNewFolderName,
00306 const char* theDescription,
00307 const BdbTime& theCreationTime )
00308 {
00309 const char* errorStr = "CdbBdbSViewP::createFolder() - ERROR.";
00310
00311 ooUpdate( );
00312
00313 CdbStatus result = CdbStatus::Error;
00314 do {
00315
00316
00317
00318 CdbPathName fPath( theNewFolderName );
00319
00320 if(( ! fPath.isValid( )) || fPath.isRoot( ) || fPath.isComposite( )) {
00321 cout << errorStr << endl
00322 << " Illegal new folder name passed to the procedure." << endl
00323 << " A non-composite and non-root folder name was expected." << endl
00324 << " PASSED NAME: \"" << theNewFolderName << "\"" << endl;
00325 break;
00326 }
00327
00328
00329
00330 if( isFrozen( )) {
00331 cout << errorStr << endl
00332 << " This view is already frozen, which means that it can't be" << endl
00333 << " reconfigured anymore." << endl;
00334 break;
00335 }
00336
00337
00338
00339 BdbRef(CdbBdbSFolderP) hRef;
00340
00341 result = findFolder( thePathName, hRef );
00342 if( CdbStatus::Success != result ) break;
00343
00344
00345
00346 BdbRef(CdbBdbSFolderP) fRef = new( ooThis( )) CdbBdbSFolderP( theNewFolderName,
00347 theDescription,
00348 theCreationTime,
00349 ooThis( ),
00350 hRef );
00351
00352
00353
00354
00355 result = hRef->insert( fRef );
00356 if( CdbStatus::Success != result ) BdbDelete( fRef );
00357
00358 } while( false );
00359
00360 return result;
00361 }
00362
00363 CdbStatus
00364 CdbBdbSViewP::createFolder( const char* theFullPathName,
00365 const char* theDescription,
00366 const BdbTime& theCreationTime )
00367 {
00368 const char* errorStr = "CdbBdbSViewP::createFolder(recursive) - ERROR.";
00369
00370 ooUpdate( );
00371
00372 CdbStatus result = CdbStatus::Error;
00373 do {
00374
00375
00376
00377 CdbPathName fPath( theFullPathName );
00378
00379 if(( ! fPath.isValid( )) || ( ! fPath.isAbsolute( ))) {
00380 cout << errorStr << endl
00381 << " Illegal folder name passed to the procedure." << endl
00382 << " A valid, absolute folder name was expected." << endl
00383 << " PASSED NAME: \"" << theFullPathName << "\"" << endl;
00384 break;
00385 }
00386
00387
00388
00389 if( isFrozen( )) {
00390 cout << errorStr << endl
00391 << " This view is already frozen, which means that it can't be" << endl
00392 << " reconfigured anymore." << endl;
00393 break;
00394 }
00395
00396
00397
00398
00399 if( fPath.isRoot( )) {
00400 result = CdbStatus::Success;
00401 } else {
00402
00403
00404
00405
00406
00407
00408 result = _rootFolderRef->createFolder( fPath.afterFirst( ).toString( ).c_str( ),
00409 theDescription,
00410 theCreationTime );
00411 }
00412
00413 } while( false );
00414
00415 return result;
00416 }
00417
00418 CdbStatus
00419 CdbBdbSViewP::createCondition( const char* thePathName,
00420 const char* theNewConditionName,
00421 const char* theDescription,
00422 const BdbTime& theCreationTime,
00423 const CdbBdbSId& theId,
00424 const BdbRef(CdbBdbSConfigCollectionP)& theConfigRef )
00425 {
00426 ooUpdate( );
00427
00428 CdbStatus result = CdbStatus::Error;
00429 do {
00430
00431
00432
00433 CdbPathName cPath( theNewConditionName );
00434
00435 if(( ! cPath.isValid( )) || cPath.isRoot( ) || cPath.isComposite( )) {
00436 cout << "CdbBdbSViewP::createCondition() - ERROR." << endl
00437 << " Illegal new condition name passed to the procedure." << endl
00438 << " A non-composite and non-root condition name was expected." << endl
00439 << " PASSED NAME: \"" << theNewConditionName << "\"" << endl;
00440 break;
00441 }
00442
00443
00444
00445 if( isFrozen( )) {
00446 cout << "CdbBdbSViewP::createCondition() - ERROR." << endl
00447 << " This view is already frozen, which means that it can't be" << endl
00448 << " reconfigured anymore." << endl;
00449 break;
00450 }
00451
00452
00453
00454 BdbRef(CdbBdbSFolderP) hRef;
00455
00456 result = findFolder( thePathName, hRef );
00457 if( CdbStatus::Success != result ) break;
00458
00459
00460
00461 BdbRef(CdbBdbSConditionAtFolderP) cRef
00462 = new( ooThis( )) CdbBdbSConditionAtFolderP( theNewConditionName,
00463 theDescription,
00464 theCreationTime,
00465 theId,
00466 hRef,
00467 theConfigRef );
00468
00469
00470
00471
00472 result = hRef->insert( cRef );
00473 if( CdbStatus::Success != result ) BdbDelete( cRef );
00474
00475 } while( false );
00476
00477 return result;
00478 }
00479
00480 CdbStatus
00481 CdbBdbSViewP::removeFolder( const char* thePathName,
00482 bool forceFlag )
00483 {
00484 ooUpdate( );
00485
00486 const char* errorStr = "CdbBdbSViewP::removeFolder( ) - ERROR";
00487
00488 CdbStatus result = CdbStatus::Error;
00489 do {
00490
00491
00492
00493 CdbPathName fName( thePathName );
00494
00495 if( fName.isValid( ) && fName.isRoot( )) {
00496 cout << errorStr << endl
00497 << " The ROOT folder can't be deleted in this way. It can only be" << endl
00498 << " deleted with its host view." << endl;
00499 break;
00500 }
00501
00502
00503
00504 if( isFrozen( )) {
00505 cout << "CdbBdbSViewP::removeFolder() - ERROR." << endl
00506 << " This view is already frozen, which means that it can't be" << endl
00507 << " reconfigured anymore." << endl;
00508 break;
00509 }
00510
00511
00512
00513 BdbRef(CdbBdbSFolderP) fRef;
00514
00515 result = findFolder( fName.beforeLast( ).toString( ).c_str( ), fRef );
00516 if( CdbStatus::Success != result ) break;
00517
00518
00519
00520 result = fRef->removeFolder( fName.last( ),
00521 forceFlag );
00522
00523 } while( false );
00524
00525 return result;
00526 }
00527
00528 CdbStatus
00529 CdbBdbSViewP::removeCondition( const char* thePathName )
00530 {
00531 ooUpdate( );
00532
00533 const char* errorStr = "CdbBdbSViewP::removeCondition( ) - ERROR";
00534
00535 CdbStatus result = CdbStatus::Error;
00536 do {
00537
00538
00539
00540 CdbPathName fName( thePathName );
00541
00542 if( fName.isValid( ) && fName.isRoot( )) {
00543 cout << errorStr << endl
00544 << " The ROOT name can't be passed to this procedure." << endl;
00545 break;
00546 }
00547
00548
00549
00550 if( isFrozen( )) {
00551 cout << "CdbBdbSViewP::removeCondition() - ERROR." << endl
00552 << " This view is already frozen, which means that it can't be" << endl
00553 << " reconfigured anymore." << endl;
00554 break;
00555 }
00556
00557
00558
00559 BdbRef(CdbBdbSFolderP) fRef;
00560
00561 result = findFolder( fName.beforeLast( ).toString( ).c_str( ), fRef );
00562 if( CdbStatus::Success != result ) break;
00563
00564
00565
00566 result = fRef->removeCondition( fName.last( ));
00567
00568 } while( false );
00569
00570 return result;
00571 }
00572
00573 CdbStatus
00574 CdbBdbSViewP::findFolder( const char* thePathName,
00575 BdbRef(CdbBdbSFolderP)& theRef ) const
00576 {
00577 const char* errorStr = "CdbBdbSViewP::findFolder( ) - ERROR";
00578
00579 CdbStatus result = CdbStatus::Error;
00580 do {
00581
00582
00583
00584 CdbPathName fPath( thePathName );
00585
00586 if( ! ( fPath.isValid( ) && fPath.isAbsolute( ))) {
00587 cout << errorStr << endl
00588 << " Illegal path name passed to the procedure: " << thePathName << endl;
00589 break;
00590 }
00591
00592
00593
00594 if( fPath.isRoot( )) theRef = _rootFolderRef;
00595 else
00596 return _rootFolderRef->findFolder( fPath.afterFirst( ),
00597 theRef );
00598
00599
00600 result = CdbStatus::Success;
00601
00602 } while( false );
00603
00604 return result;
00605 }
00606
00607 CdbStatus
00608 CdbBdbSViewP::findCondition( const char* thePathName,
00609 BdbRef(CdbBdbSConditionAtFolderP)& theRef ) const
00610 {
00611 const char* errorStr = "CdbBdbSViewP::findCondition( ) - ERROR";
00612
00613 CdbStatus result = CdbStatus::Error;
00614 do {
00615
00616
00617
00618 CdbPathName cPath( thePathName );
00619
00620 if( ! ( cPath.isValid( ) && cPath.isAbsolute( ) && ( ! cPath.isRoot( )))) {
00621 cout << errorStr << endl
00622 << " Illegal path name passed to the procedure: " << thePathName << endl;
00623 break;
00624 }
00625
00626
00627
00628 BdbRef(CdbBdbSFolderP) fRef;
00629 if( CdbStatus::Success != findFolder( cPath.beforeLast( ).toString( ).c_str( ), fRef )) {
00630 result = CdbStatus::NotFound;
00631 break;
00632 }
00633
00634
00635
00636 result = fRef->findCondition( cPath.last( ), theRef );
00637
00638 } while( false );
00639
00640 return result;
00641 }
00642
00643 CdbStatus
00644 CdbBdbSViewP::folderIterator( CdbItr< const char* >& theItr ) const
00645 {
00646
00647
00648
00649
00650
00651
00652
00653 theItr = CdbItr< const char* >( new CdbBdbSFolderPItr( ));
00654 return CdbStatus::Success;
00655 }
00656
00657 void
00658 CdbBdbSViewP::dump( ostream& o ) const
00659 {
00660 o << " NAME: " << name( ).head( ) << endl
00661 << " ID: " << id( ) << endl
00662 << " DESCRIPTION: " << _description.head( ) << endl
00663 << " CREATED: " << _created << endl
00664 << " MIN.VALIDITY: " << _minValidityTime << endl
00665 << " MAX.VALIDITY: " << _maxValidityTime << endl
00666 << " IS FROZEN: " << ( _isFrozen ? "Yes" : "No" ) << endl
00667 << "DEFAULT CONFIG: " << _defaultConfigRef.sprint( ) << endl;
00668
00669 if( !BdbIsNull(_defaultConfigRef)) {
00670 o << "......................................................." << endl
00671 << endl;
00672 _defaultConfigRef->dump( o );
00673 o << "......................................................." << endl;
00674 }
00675 }
00676
00677 CdbStatus
00678 CdbBdbSViewP::cloneComponents( const BdbRef(CdbBdbSFolderP)& theInFolderRef,
00679 const BdbRef(CdbBdbSFolderP)& theOutFolderRef )
00680 {
00681
00682 const char* errorStr = "CdbBdbSViewP::cloneComponents( ) - ERROR";
00683
00684 CdbStatus result = CdbStatus::Error;
00685 do {
00686
00687
00688
00689 {
00690 CdbItr< const char* > cInItr;
00691 if( CdbStatus::Success != theInFolderRef->conditionIterator( cInItr )) {
00692 cout << errorStr << endl
00693 << " Failed to set up an iterator of conditions." << endl;
00694 break;
00695 }
00696
00697 bool failed = false;
00698
00699 while( cInItr.next( )) {
00700
00701 BdbRef(CdbBdbSConditionAtFolderP) cInRef;
00702 if( CdbStatus::Success != theInFolderRef->findCondition( CdbPathName( cInItr.value( )),
00703 cInRef )) {
00704 cout << errorStr << endl
00705 << " Failed to find the condition. The input view may be" << endl
00706 << " internally inconsistent." << endl;
00707
00708 failed = true;
00709 break;
00710 }
00711 BdbRef(CdbBdbSConfigCollectionP) configOutRef = 0;
00712 {
00713 BdbRef(CdbBdbSConfigCollectionP) configInRef = cInRef->config( );
00714 if( !BdbIsNull(configInRef)) {
00715 if( CdbStatus::Success != configInRef->clone( configOutRef,
00716 theOutFolderRef )) {
00717 cout << errorStr << endl
00718 << " Failed to clone the condition's configuration object." << endl;
00719
00720 failed = true;
00721 break;
00722 }
00723 }
00724 }
00725 BdbRef(CdbBdbSConditionAtFolderP) cOutRef;
00726 cOutRef = new( theOutFolderRef ) CdbBdbSConditionAtFolderP( cInRef->name( ),
00727 cInRef->description( ),
00728 theOutFolderRef->created( ),
00729 cInRef->id( ),
00730 theOutFolderRef,
00731 configOutRef );
00732 assert( !BdbIsNull(cOutRef));
00733
00734 if( CdbStatus::Success != theOutFolderRef->insert( cOutRef )) {
00735
00736 cout << errorStr << endl
00737 << " Failed to insert cloned condition into its parrent folder." << endl;
00738
00739 BdbDelete(cOutRef);
00740
00741 failed = true;
00742 break;
00743 }
00744 }
00745 if( failed ) break;
00746 }
00747
00748
00749
00750 {
00751 CdbItr< const char* > fInItr;
00752 if( CdbStatus::Success != theInFolderRef->folderIterator( fInItr )) {
00753 cout << errorStr << endl
00754 << " Failed to set up an iterator of folders." << endl;
00755 break;
00756 }
00757
00758 bool failed = false;
00759
00760 while( fInItr.next( )) {
00761
00762 BdbRef(CdbBdbSFolderP) fInRef;
00763 if( CdbStatus::Success != theInFolderRef->findFolder( CdbPathName( fInItr.value( )),
00764 fInRef )) {
00765 cout << errorStr << endl
00766 << " Failed to find the folder. The input view may be" << endl
00767 << " internally inconsistent." << endl;
00768
00769 failed = true;
00770 break;
00771 }
00772 BdbRef(CdbBdbSFolderP) fOutRef;
00773 fOutRef = new( theOutFolderRef ) CdbBdbSFolderP( fInRef->name( ),
00774 fInRef->description( ),
00775 theOutFolderRef->created( ),
00776 theOutFolderRef->parentView( ),
00777 theOutFolderRef );
00778 assert( !BdbIsNull(fOutRef));
00779
00780 if( CdbStatus::Success != theOutFolderRef->insert( fOutRef )) {
00781
00782 cout << errorStr << endl
00783 << " Failed to insert cloned folder into its parrent folder." << endl;
00784
00785 BdbDelete(fOutRef);
00786
00787 failed = true;
00788 break;
00789 }
00790
00791
00792
00793 if( CdbStatus::Success != cloneComponents( fInRef,
00794 fOutRef )) {
00795 failed = true;
00796 break;
00797 }
00798
00799 }
00800 if( failed ) break;
00801 }
00802
00803
00804
00805 result = CdbStatus::Success;
00806
00807 } while( false );
00808
00809 return result;
00810 }
00811
00812
00813
00814