Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/CdbBdbShared/CdbBdbSView.cc

Go to the documentation of this file.
00001 /// The implementation of the CdbBdbSView class.
00002 /**
00003   * @see CdbBdbSView
00004   */
00005 #include "BaBar/BaBar.hh"
00006 
00007 #include "CdbBdbShared/CdbBdbSView.hh"
00008 
00009 #include "CdbBase/Cdb.hh"
00010 #include "CdbBase/CdbId.hh"
00011 #include "CdbBase/CdbStateId.hh"
00012 #include "CdbBase/CdbFolder.hh"
00013 #include "CdbBase/CdbPathName.hh"
00014 #include "CdbBase/CdbRevisionPolicy.hh"
00015 
00016 #include "CdbBdbShared/CdbBdbSFolder.hh"
00017 #include "CdbBdbShared/CdbBdbSPartitionP.hh"
00018 #include "CdbBdbShared/CdbBdbSViewP.hh"
00019 #include "CdbBdbShared/CdbBdbSConditionAtFolderP.hh"
00020 #include "CdbBdbShared/CdbBdbSConfigCollectionP.hh"
00021 
00022 #include "BdbTime/BdbTime.hh"
00023 
00024 #include <iostream.h>
00025 
00026 namespace {
00027 
00028     /////////////////////////////
00029     // Class: CdbBdbSConfigItr //
00030     /////////////////////////////
00031 
00032     class CdbBdbSConfigItr : public CdbItr<CdbConfigElement>::InterfaceType {
00033 
00034     private:
00035 
00036         CdbBdbSConfigItr( );
00037 
00038         CdbBdbSConfigItr& operator=( const CdbBdbSConfigItr& theItr );
00039 
00040     protected:
00041 
00042         CdbBdbSConfigItr( const CdbBdbSConfigItr& theItr ) :
00043             _itr  (theItr._itr),
00044             _value(theItr._value)
00045         { }
00046 
00047     public:
00048 
00049         CdbBdbSConfigItr( const CdbBdbSConfigCollectionP::IteratorOfIntervals& theInputItr ) :
00050             _itr(theInputItr)
00051         { }
00052 
00053         virtual ~CdbBdbSConfigItr( )
00054         { }
00055 
00056         virtual CdbStatus reset( )
00057         {
00058             return _itr.reset( );
00059         }
00060 
00061         virtual bool next( )
00062         {
00063             bool result = _itr.next( );
00064             if( result ) {
00065 
00066                 CdbBdbSConfigInterval in = _itr.value( );
00067                 if( in.value.accessIsAllowed( )) {
00068 
00069                   // This interval is allowed for accessing the database.
00070 
00071                     CdbRevisionPolicy policy;
00072                     if( in.value.useRevision ) {
00073                         policy = CdbRevisionPolicy( in.value.revision,
00074                                                     in.value.partition );
00075                     }
00076                     _value = CdbConfigElement( policy,
00077                                                in.begin,
00078                                                in.end );
00079                 } else {
00080 
00081                   // This interval is NOT allowed.
00082 
00083                     _value = CdbConfigElement( in.begin,
00084                                                in.end );
00085                 }
00086 
00087             } else {
00088 
00089               // The default constructor will construct an element disallowing any
00090               // access to the database for the whole range of the validity time.
00091 
00092                 _value = CdbConfigElement( );
00093             }
00094             return result;
00095         }
00096 
00097         virtual ValueType value( )
00098         {
00099             if( ! isValid( )) {
00100                 assert( 0 );
00101             }
00102             return _value;
00103         }
00104 
00105         virtual bool isValid( )
00106         {
00107             return _itr.isValid( );
00108         }
00109 
00110         virtual InterfaceType* clone( ) const
00111         {
00112             return new CdbBdbSConfigItr( *this );
00113         }
00114 
00115     private:
00116 
00117         CdbBdbSConfigCollectionP::IteratorOfIntervals _itr;
00118 
00119         CdbConfigElement _value;
00120     };
00121 };
00122 
00123 ////////////////////////
00124 // Class: CdbBdbSView //
00125 ////////////////////////
00126 
00127 CdbView*
00128 CdbBdbSView::clone( ) const
00129 {
00130   // Get rid of the "const" to access non-const methods
00131 
00132     CdbBdbSView* mySelf = const_cast<CdbBdbSView*>( this );
00133     return new CdbBdbSView( mySelf->parent( ),
00134                             mySelf->name( ),
00135                             mySelf->id( ),
00136                             _viewH,
00137                             _masterRegistryH,
00138                             _localRegistryH );
00139 }
00140 
00141 CdbBdbSView::CdbBdbSView( const CdbDatabasePtr&              thePtr,
00142                           const char*                        theName,
00143                           const CdbId&                       theId,
00144                           const BdbHandle(CdbBdbSViewP)&     theViewH,
00145                           const BdbHandle(CdbBdbSRegistryP)& theMasterRegistryH ,
00146                           const BdbHandle(CdbBdbSRegistryP)& theLocalRegistryH ) :
00147     CdbView( thePtr,
00148              theName,
00149              theId ),
00150     _viewH(theViewH),
00151     _masterRegistryH(theMasterRegistryH),
00152     _localRegistryH(theLocalRegistryH)
00153 { }
00154 
00155 CdbBdbSView::~CdbBdbSView( )
00156 { }
00157 
00158 bool
00159 CdbBdbSView::isValid( )
00160 {
00161     return true;
00162 }
00163 
00164 bool
00165 CdbBdbSView::isOpen( )
00166 {
00167     return true;
00168 }
00169 
00170 CdbStatus
00171 CdbBdbSView::open( )
00172 {
00173     return CdbStatus::Success;
00174 }
00175 
00176 CdbStatus
00177 CdbBdbSView::close( )
00178 {
00179     return CdbStatus::Success;
00180 }
00181 
00182 bool 
00183 CdbBdbSView::isFrozen( ) const
00184 {
00185     return _viewH->isFrozen( );
00186 }
00187 
00188 CdbStatus
00189 CdbBdbSView::findFolder( CdbFolderPtr& thePtr,
00190                          const char*   theName
00191                        )
00192 {
00193     CdbStatus result = CdbStatus::Error;
00194 
00195     thePtr = 0;
00196 
00197     do {
00198 
00199         if( 0 != strcmp( theName, CdbPathName::separator( ))) {
00200 
00201             cout << "CdbBdbSView::findFolder() -- ERROR" << endl
00202                  << "    No such folder in this implementation: \"" << theName                   << "\"" << endl
00203                  << "    The only name allowed is the rood one: \"" << CdbPathName::separator( ) << "\"" << endl;
00204 
00205             break;
00206         }
00207 
00208       // Find out persistent folder for sp[ecified name
00209 
00210         BdbRef(CdbBdbSFolderP) fRef;
00211         if( CdbStatus::Success != _viewH->findFolder( theName,
00212                                                       fRef )) {
00213             result = CdbStatus::NotFound;
00214             break;
00215         }
00216 
00217       // DESIGN NOTE: We need to construct a smart pointer against a clone since
00218       //              we don't know a smart pointer pointing to the current
00219       //              instance, therefore we can't guarantee the lifetime
00220       //              of the current instance.
00221 
00222         CdbViewPtr   myPtr( this->clone( ));
00223         CdbFolderPtr fPtr( 0 );
00224 
00225         thePtr = new CdbBdbSFolder( myPtr,
00226                                     fPtr,
00227                                     theName,
00228                                     fRef,
00229                                     _masterRegistryH,
00230                                     _localRegistryH );
00231 
00232       // Done.
00233 
00234         result = CdbStatus::Success;
00235 
00236     } while( false );
00237 
00238     return result;
00239 }
00240 
00241 CdbStatus
00242 CdbBdbSView::folderIterator( CdbFolderItr& theItr )
00243 {
00244     return _viewH->folderIterator( theItr );
00245 }
00246 
00247 CdbStatus
00248 CdbBdbSView::get( const char*       theName,
00249                   const BdbTime&    theValidityTime,
00250                   CdbConfigElement& theConfigElement ) const
00251 {
00252     const char* errorStr = "CdbBdbSView::get() -- ERROR.";
00253 
00254     CdbStatus result = CdbStatus::Error;
00255     do {
00256 
00257       // Verify the condition path name.
00258 
00259         if( 0 == theName ) {
00260             cout << errorStr << endl
00261                  << "    Non 0 pointer onto a fully qualified condition path name was expected." << endl;
00262             break;
00263         }
00264 
00265         CdbPathName fullPath( theName );
00266 
00267         if( ! ( fullPath.isValid( ) &&
00268                 fullPath.isAbsolute( ) &&
00269                 fullPath.isComposite( ))) {
00270 
00271             cout << errorStr << endl
00272                  << "    Unsupported format of the condition name. Fully qualified" << endl
00273                  << "    condition name including its folder path was expected." << endl
00274                  << "        Passed condition name: \"" << theName << "\"" << endl;
00275             break;
00276         }
00277 
00278       // Check if specified time fits into the validity interval covered by
00279       // the current view.
00280 
00281         if(( theValidityTime <  _viewH->minValidity( )) ||
00282            ( theValidityTime >= _viewH->maxValidity( ))) {
00283 
00284             cout << errorStr << endl
00285                  << "    The pased validity time is out of the validity interval covered" << endl
00286                  << "    by the current view." << endl
00287                  << "        Passed  validity time: " << theValidityTime << endl
00288                  << "        Minimum validity time: " << _viewH->minValidity( ) << endl
00289                  << "        Maximum validity time: " << _viewH->maxValidity( ) << endl;
00290             break;
00291         }
00292 
00293       // First we're going to see if specified condition has  its own revision
00294       // configuration. This always superseeds the default configuration at
00295       // the level of the view.
00296 
00297         BdbRef(CdbBdbSConditionAtFolderP) cRef;
00298         if( CdbStatus::Success != _viewH->findCondition( theName,
00299                                                          cRef )) {
00300             result = CdbStatus::NotFound;
00301             break;
00302         }
00303 
00304         BdbRef(CdbBdbSConfigCollectionP) configCollRef = cRef->config( );
00305         if( BdbIsNull(configCollRef)) {
00306 
00307           // No configuration found at the conditin level. Get the default
00308           // configuration at the view level then.
00309 
00310             configCollRef = _viewH->defaultConfig( );
00311             if( BdbIsNull(configCollRef)) {
00312                 cout << errorStr << endl
00313                      << "    The current view is inproperly configured for specified condition." << endl
00314                      << "    No configuration found both at specified condition scope and" << endl
00315                      << "    at the view level." << endl
00316                      << "        Current view name:     \"" << name( ) << "\"" << endl
00317                      << "        Passed condition name: \"" << theName << "\"" << endl;
00318                 break;
00319             }
00320         }
00321 
00322       // Get the configuration interval.
00323 
00324         CdbBdbSConfigInterval configInterval;
00325         if( CdbStatus::Success != configCollRef->find( theValidityTime,
00326                                                        configInterval )) {
00327             cout << errorStr << endl
00328                  << "    The current view is inproperly configured for specified condition." << endl
00329                  << "    No configuration record found for specified validity time." << endl
00330                  << "    at the view level." << endl
00331                  << "        Current view name:     \"" << name( ) << "\"" << endl
00332                  << "        Passed condition name: \"" << theName << "\"" << endl
00333                  << "        Passed validity time:  "   << theValidityTime << endl;
00334             break;
00335         }
00336 
00337       // Build the configuration element
00338 
00339         if( configInterval.value.accessIsAllowed( )) {
00340 
00341             CdbRevisionPolicy policy;
00342             if( configInterval.value.useRevision ) {
00343                 policy = CdbRevisionPolicy( configInterval.value.revision,
00344                                             configInterval.value.partition );
00345             } else {
00346                 policy = CdbRevisionPolicy( );
00347             }
00348             theConfigElement = CdbConfigElement( policy,
00349                                                  configInterval.begin,
00350                                                  configInterval.end );
00351 
00352         } else {
00353 
00354           // This element will prohibit accessing the database
00355           // for specified validity interval
00356 
00357             theConfigElement = CdbConfigElement( configInterval.begin,
00358                                                  configInterval.end );
00359         }
00360 
00361       // Done
00362 
00363         result = CdbStatus::Success;
00364 
00365     } while( false );
00366 
00367     return result;
00368 }
00369 
00370 CdbStatus
00371 CdbBdbSView::configIterator( const char*               theName,
00372                              CdbItr<CdbConfigElement>& theItr ) const
00373 {
00374     const char* errorStr = "CdbBdbSView::configIterator() -- ERROR.";
00375 
00376     CdbStatus result = CdbStatus::Error;
00377     do {
00378 
00379       // Verify the condition path name.
00380 
00381         if( 0 == theName ) {
00382             cout << errorStr << endl
00383                  << "    Non 0 pointer onto a fully qualified condition path name was expected." << endl;
00384             break;
00385         }
00386 
00387         CdbPathName fullPath( theName );
00388 
00389         if( ! ( fullPath.isValid( ) &&
00390                 fullPath.isAbsolute( ) &&
00391                 fullPath.isComposite( ))) {
00392 
00393             cout << errorStr << endl
00394                  << "    Unsupported format of the condition name. Fully qualified" << endl
00395                  << "    condition name including its folder path was expected." << endl
00396                  << "        Passed condition name: \"" << theName << "\"" << endl;
00397             break;
00398         }
00399 
00400       // First we're going to see if specified condition has  its own revision
00401       // configuration. This always superseeds the default configuration at
00402       // the level of the view.
00403 
00404         BdbRef(CdbBdbSConditionAtFolderP) cRef;
00405         if( CdbStatus::Success != _viewH->findCondition( theName,
00406                                                          cRef )) {
00407             result = CdbStatus::NotFound;
00408             break;
00409         }
00410 
00411         BdbRef(CdbBdbSConfigCollectionP) configCollRef = cRef->config( );
00412         if( BdbIsNull(configCollRef)) {
00413 
00414           // No configuration found at the conditin level. Get the default
00415           // configuration at the view level then.
00416 
00417             configCollRef = _viewH->defaultConfig( );
00418             if( BdbIsNull(configCollRef)) {
00419                 cout << errorStr << endl
00420                      << "    The current view is inproperly configured for specified condition." << endl
00421                      << "    No configuration found both at specified condition scope and" << endl
00422                      << "    at the view level." << endl
00423                      << "        Current view name:     \"" << name( ) << "\"" << endl
00424                      << "        Passed condition name: \"" << theName << "\"" << endl;
00425                 break;
00426             }
00427         }
00428 
00429       // ATTENTION: This is not a memory leak - the created object
00430       //            will be destroyed by the iterator.
00431 
00432         theItr = CdbItr<CdbConfigElement>( new CdbBdbSConfigItr( configCollRef->iterator( )));
00433 
00434       // Done
00435 
00436         result = CdbStatus::Success;
00437 
00438     } while( false );
00439 
00440     return result;
00441 }
00442 
00443 CdbStateId
00444 CdbBdbSView::currentStateId( const BdbTime& theValidityTime ) const
00445 {
00446     CdbStateId result;
00447     {
00448       // Find out an open partition corresponding to specified validity time
00449       // and obtain its modification time. If there is no open partition then
00450       // we should go along with the _current_ time.
00451       //
00452       // IMPLEMENTATION NOTE:
00453       //
00454       //       For the simplicity of this implementation, we're only looking for
00455       //       a partiton in the master registry. This may not give us the correct result
00456       //       if the found partition belongs to the current database and the current database
00457       //       is not the master one. So the right answer would be to check if the found
00458       //       (through the master) partition belongs to the current database, and then
00459       //       look for the correct value of the modification time in the local
00460       //       replica of the partition.
00461       //
00462       //       A similar problem is when the found partition belongs to some other database
00463       //       (not the local one). In this case the only correct state for the partition is
00464       //       the closed one. In addition we may have a problem of incompatible type, which
00465       //       we're not checking below, since "foreign" partitions are represented through
00466       //       simplified type in the master database, until they (partitions) got synchronized
00467       //       from the database they belong to.
00468       //
00469       //       This correct logic will be implemented later with the distributed version of
00470       //       the Condition/DB implementation.
00471 
00472         BdbTime partitionModificationTime( BdbTime::now( ));    // _current_ time by default
00473         {
00474             BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = _masterRegistryH->partitionsLayout( );
00475             assert( !BdbIsNull(pLayoutRef));
00476 
00477             BdbRef(CdbBdbSPartitionP) pRef;
00478             if( CdbStatus::Success == pLayoutRef->find( theValidityTime,
00479                                                         pRef )) {
00480 
00481                 if( pRef->isInstantiated( ) && !pRef->isClosed( )) {
00482                     partitionModificationTime = pRef->modified( );
00483                 }
00484             }
00485         }
00486 
00487       // Build the state identifier
00488 
00489         result = CdbStateId( id( ), partitionModificationTime );
00490     }
00491     return result;
00492 }
00493 
00494 CdbStatus
00495 CdbBdbSView::historyEventIterator( CdbHistoryEventItr& theItr,
00496                                    const BdbTime&      theBeginTime,
00497                                    const BdbTime&      theEndTime,
00498                                    const char**        theEventsToSelect )
00499 {
00500     return CdbStatus::Error;
00501 }
00502 
00503 CdbStatus
00504 CdbBdbSView::historyEventTypeIterator( CdbHistoryEventTypeItr& theItr )
00505 {
00506     return CdbStatus::Error;
00507 }
00508 
00509 /////////////////
00510 // End Of File //
00511 /////////////////

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002