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

CdbBdbSDatabase.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbSDatabase.cc,v 1.15 2005/08/13 01:25:44 gapon Exp $
00003 
00004 /// The implementation of the CdbBdbSDatabase class.
00005 /**
00006   * @see CdbBdbSDatabase
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbBdbShared/CdbBdbSDatabase.hh"
00012 
00013 #include "CdbBase/Cdb.hh"
00014 #include "CdbBase/CdbView.hh"
00015 #include "CdbBase/CdbId.hh"
00016 #include "CdbBase/CdbCompositeName.hh"
00017 #include "CdbBase/CdbEnvironment.hh"
00018 #include "CdbBase/CdbPartition.hh"
00019 #include "CdbBase/CdbProperty.hh"
00020 #include "CdbBase/CdbOrigin.hh"
00021 #include "CdbBase/CdbFolder.hh"
00022 #include "CdbBase/CdbCondition.hh"
00023 #include "CdbBase/CdbSimpleVectorAdapterItr.hh"
00024 
00025 #include "CdbBdbShared/CdbBdbSOriginP.hh"
00026 #include "CdbBdbShared/CdbBdbSView.hh"
00027 #include "CdbBdbShared/CdbBdbSViewItr.hh"
00028 #include "CdbBdbShared/CdbBdbSPartitionsLayoutP.hh"
00029 #include "CdbBdbShared/CdbBdbSPartitionP.hh"
00030 #include "CdbBdbShared/CdbBdbSCondition.hh"
00031 
00032 #include <iostream>
00033 using std::cout;
00034 using std::cerr;
00035 using std::endl;
00036 
00037 namespace {
00038 
00039 ///////////////////////////////
00040 // Class CdbBdbSPartitionItr //
00041 ///////////////////////////////
00042 
00043 // This iterator is only meant to retranslate type of the result
00044 // produced by a persistent iterator.
00045 
00046 class CdbBdbSPartitionItr : public CdbItr<unsigned short>::InterfaceType {
00047 
00048 private:
00049 
00050   /// The default constructor (NOT IMPLEMENTED)
00051   /**
00052     */
00053     CdbBdbSPartitionItr( );
00054 
00055 public:
00056 
00057   /// The normal constructor
00058   /**
00059     */
00060     CdbBdbSPartitionItr( const CdbItr<d_UShort>& theInputItr );
00061 
00062   /// The copy constructor
00063   /**
00064     * More details to come...
00065     */
00066     CdbBdbSPartitionItr( const CdbBdbSPartitionItr& theItr );
00067 
00068   /// The assignment operator
00069   /**
00070     * More details to come...
00071     */
00072     CdbBdbSPartitionItr& operator=( const CdbBdbSPartitionItr& theItr );
00073 
00074   /// The destructor
00075   /**
00076     * Details...
00077     */
00078     virtual ~CdbBdbSPartitionItr( );
00079 
00080   /// Reset an iterator to its initial state.
00081   /**
00082     * This implements the corresponding method of the base class.
00083     *
00084     * @see CdbIItr::reset
00085     * @see CdbStatus
00086     */
00087     virtual CdbStatus reset( );
00088 
00089   /// Advance an iterator to the next position.
00090   /**
00091     * This implements the corresponding method of the base class.
00092     *
00093     * @see CdbIItr::next()
00094     */
00095     virtual bool next( );
00096 
00097   /// Obtain the currently reffered value.
00098   /**
00099     * This implements the corresponding method of the base class.
00100     *
00101     * @see CdbIItr::value()
00102     * @see CdbIItr::ValueType
00103     *
00104     * @return the current value the iterator is set on
00105     */
00106     virtual ValueType value( );
00107 
00108   /// Check if an iterator is valid.
00109   /**
00110     * This implements the corresponding method of the base class.
00111     *
00112     * @see CdbIItr::isValid()
00113     */
00114     virtual bool isValid( );
00115 
00116   /// Make a clone of itself
00117   /**
00118     * @see CdbIItr::clone()
00119     */
00120     virtual InterfaceType* clone( ) const;
00121 
00122 private:
00123 
00124     CdbItr<d_UShort> _itr;
00125 };
00126 
00127 CdbBdbSPartitionItr::CdbBdbSPartitionItr( const CdbItr<d_UShort>& theInputItr ) :
00128     _itr(theInputItr)
00129 { }
00130 
00131 CdbBdbSPartitionItr::CdbBdbSPartitionItr( const CdbBdbSPartitionItr& theItr ) :
00132     _itr(theItr._itr)
00133 { }
00134 
00135 CdbBdbSPartitionItr::~CdbBdbSPartitionItr( )
00136 { }
00137 
00138 CdbBdbSPartitionItr&
00139 CdbBdbSPartitionItr::operator=( const CdbBdbSPartitionItr& theItr )
00140 {
00141     if( this != &theItr ) {
00142         _itr = theItr._itr;
00143     }
00144     return *this;
00145 }
00146 
00147 CdbStatus
00148 CdbBdbSPartitionItr::reset( )
00149 {
00150     return _itr.reset( );
00151 }
00152 
00153 bool
00154 CdbBdbSPartitionItr::next( )
00155 {
00156     return _itr.next( );
00157 }
00158 
00159 CdbBdbSPartitionItr::ValueType
00160 CdbBdbSPartitionItr::value( )
00161 {
00162     return _itr.value( );
00163 }
00164 
00165 bool
00166 CdbBdbSPartitionItr::isValid( )
00167 {
00168     return _itr.isValid( );
00169 }
00170 
00171 CdbBdbSPartitionItr::InterfaceType*
00172 CdbBdbSPartitionItr::clone( ) const
00173 {
00174     return new CdbBdbSPartitionItr( *this );
00175 }
00176 
00177 ////////////////////////////
00178 // Class CdbBdbSPartition //
00179 ////////////////////////////
00180 
00181 class CdbBdbSPartition : public CdbPartition {
00182 
00183 private:
00184 
00185   /// The default constructor (NOT IMPLEMENTED)
00186   /**
00187     */
00188     CdbBdbSPartition( );
00189 
00190   /// The copy constructor (NOT IMPLEMENTED)
00191   /**
00192     * More details to come...
00193     */
00194     CdbBdbSPartition( const CdbBdbSPartition& thePartition );
00195 
00196   /// The assignment operator (NOT IMPLEMENTED)
00197   /**
00198     * More details to come...
00199     */
00200     CdbBdbSPartition& operator=( const CdbBdbSPartition& thePartition );
00201 
00202 public:
00203 
00204   /// The normal constructor
00205   /**
00206     */
00207     CdbBdbSPartition( const CdbDatabasePtr&               theDatabasePtr,                      
00208                       const BdbHandle(CdbBdbSPartitionP)& thePartitionH,
00209                       unsigned short                      theOwnerId,
00210                       const char*                         theOwnerName );
00211 
00212   /// The destructor
00213   /**
00214     * Details...
00215     */
00216     virtual ~CdbBdbSPartition( );
00217 
00218   /// The begin time in the "insertion" dimension
00219   /**
00220     * Implements the corresponding method of its base class or an interface.
00221     *
00222     * @see CdbPartition::endInsertion()
00223     */
00224     virtual BdbTime endInsertion( ) const;
00225 
00226   /// The recent modification time of the partition
00227   /**
00228     * Implements the corresponding method of its base class or an interface.
00229     *
00230     * @see CdbPartition::modified()
00231     */
00232     virtual BdbTime modified( ) const;
00233 
00234   /// The status of the partition
00235   /**
00236     * Implements the corresponding method of its base class or an interface.
00237     *
00238     * @see CdbPartition::isClosed()
00239     */
00240     virtual bool isClosed( ) const;
00241 
00242   /// The status of the partition
00243   /**
00244     * Implements the corresponding method of its base class or an interface.
00245     *
00246     * @see CdbPartition::isInstantiated()
00247     */
00248     virtual bool isInstantiated( ) const;
00249 
00250 private:
00251 
00252     BdbHandle(CdbBdbSPartitionP) _partitionH;
00253 };
00254 
00255 CdbBdbSPartition::CdbBdbSPartition( const CdbDatabasePtr&               theDatabasePtr,
00256                                     const BdbHandle(CdbBdbSPartitionP)& thePartitionH,
00257                                     unsigned short                      theOwnerId,
00258                                     const char*                         theOwnerName ) :
00259     CdbPartition( theDatabasePtr,
00260                   thePartitionH->id( ),
00261                   theOwnerId,
00262                   theOwnerName,
00263                   thePartitionH->description( ).head( ),
00264                   thePartitionH->created( ),
00265                   thePartitionH->cell( ).beginValidity,
00266                   thePartitionH->cell( ).endValidity,
00267                   thePartitionH->cell( ).beginInsertion ),
00268    _partitionH(thePartitionH)
00269 { }
00270 
00271 CdbBdbSPartition::~CdbBdbSPartition( )
00272 { }
00273 
00274 BdbTime 
00275 CdbBdbSPartition::endInsertion( ) const
00276 {
00277     return _partitionH->cell( ).endInsertion;
00278 }
00279 
00280 BdbTime
00281 CdbBdbSPartition::modified( ) const
00282 {
00283     return _partitionH->modified( );
00284 }
00285 
00286 bool
00287 CdbBdbSPartition::isClosed( ) const
00288 {
00289     return _partitionH->isClosed( );
00290 }
00291 
00292 bool
00293 CdbBdbSPartition::isInstantiated( ) const
00294 {
00295     return _partitionH->isInstantiated( );
00296 }
00297 
00298 ////////////////////////////
00299 // Class CdbBdbSOriginItr //
00300 ////////////////////////////
00301 
00302 // This iterator is only meant to retranslate type of the result
00303 // produced by a persistent iterator.
00304 
00305 class CdbBdbSOriginItr : public CdbItr<unsigned short>::InterfaceType {
00306 
00307 private:
00308 
00309   /// The default constructor (NOT IMPLEMENTED)
00310   /**
00311     */
00312     CdbBdbSOriginItr( );
00313 
00314 public:
00315 
00316   /// The normal constructor
00317   /**
00318     */
00319     CdbBdbSOriginItr( const CdbItr<d_UShort>& theInputItr );
00320 
00321   /// The copy constructor
00322   /**
00323     * More details to come...
00324     */
00325     CdbBdbSOriginItr( const CdbBdbSOriginItr& theItr );
00326 
00327   /// The assignment operator
00328   /**
00329     * More details to come...
00330     */
00331     CdbBdbSOriginItr& operator=( const CdbBdbSOriginItr& theItr );
00332 
00333   /// The destructor
00334   /**
00335     * Details...
00336     */
00337     virtual ~CdbBdbSOriginItr( );
00338 
00339   /// Reset an iterator to its initial state.
00340   /**
00341     * This implements the corresponding method of the base class.
00342     *
00343     * @see CdbIItr::reset
00344     * @see CdbStatus
00345     */
00346     virtual CdbStatus reset( );
00347 
00348   /// Advance an iterator to the next position.
00349   /**
00350     * This implements the corresponding method of the base class.
00351     *
00352     * @see CdbIItr::next()
00353     */
00354     virtual bool next( );
00355 
00356   /// Obtain the currently reffered value.
00357   /**
00358     * This implements the corresponding method of the base class.
00359     *
00360     * @see CdbIItr::value()
00361     * @see CdbIItr::ValueType
00362     *
00363     * @return the current value the iterator is set on
00364     */
00365     virtual ValueType value( );
00366 
00367   /// Check if an iterator is valid.
00368   /**
00369     * This implements the corresponding method of the base class.
00370     *
00371     * @see CdbIItr::isValid()
00372     */
00373     virtual bool isValid( );
00374 
00375   /// Make a clone of itself
00376   /**
00377     * @see CdbIItr::clone()
00378     */
00379     virtual InterfaceType* clone( ) const;
00380 
00381 private:
00382 
00383     CdbItr<d_UShort> _itr;
00384 };
00385 
00386 CdbBdbSOriginItr::CdbBdbSOriginItr( const CdbItr<d_UShort>& theInputItr ) :
00387     _itr(theInputItr)
00388 { }
00389 
00390 CdbBdbSOriginItr::CdbBdbSOriginItr( const CdbBdbSOriginItr& theItr ) :
00391     _itr(theItr._itr)
00392 { }
00393 
00394 CdbBdbSOriginItr::~CdbBdbSOriginItr( )
00395 { }
00396 
00397 CdbBdbSOriginItr&
00398 CdbBdbSOriginItr::operator=( const CdbBdbSOriginItr& theItr )
00399 {
00400     if( this != &theItr ) {
00401         _itr = theItr._itr;
00402     }
00403     return *this;
00404 }
00405 
00406 CdbStatus
00407 CdbBdbSOriginItr::reset( )
00408 {
00409     return _itr.reset( );
00410 }
00411 
00412 bool
00413 CdbBdbSOriginItr::next( )
00414 {
00415     return _itr.next( );
00416 }
00417 
00418 CdbBdbSOriginItr::ValueType
00419 CdbBdbSOriginItr::value( )
00420 {
00421     return _itr.value( );
00422 }
00423 
00424 bool
00425 CdbBdbSOriginItr::isValid( )
00426 {
00427     return _itr.isValid( );
00428 }
00429 
00430 CdbBdbSOriginItr::InterfaceType*
00431 CdbBdbSOriginItr::clone( ) const
00432 {
00433     return new CdbBdbSOriginItr( *this );
00434 }
00435 ////////////////////////////////////
00436 // Class CdbBdbSOriginPropertyItr //
00437 ////////////////////////////////////
00438 
00439 /// The wrapper implementation of a transient origin's property iterator
00440 /**
00441   * This is the "one-element" iterator since there is just one
00442   * property specific to the "Shared" implementation - the DBID range name
00443   * of an origin.
00444   */
00445 class CdbBdbSOriginPropertyItr : public CdbItr<CdbProperty>::InterfaceType {
00446 
00447 private:
00448 
00449   /// The default constructor (NOT IMPLEMENTED)
00450   /**
00451     */
00452     CdbBdbSOriginPropertyItr( );
00453 
00454 public:
00455 
00456   /// The normal constructor
00457   /**
00458     * NOTE: The pointer passed as the parameter must not be 0 one.
00459     */
00460     CdbBdbSOriginPropertyItr( const char* theDBIDRangeName );
00461 
00462   /// The copy constructor
00463   /**
00464     * More details to come...
00465     */
00466     CdbBdbSOriginPropertyItr( const CdbBdbSOriginPropertyItr& theItr );
00467 
00468   /// The assignment operator
00469   /**
00470     * More details to come...
00471     */
00472     CdbBdbSOriginPropertyItr& operator=( const CdbBdbSOriginPropertyItr& theItr );
00473 
00474   /// The destructor
00475   /**
00476     * Details...
00477     */
00478     virtual ~CdbBdbSOriginPropertyItr( );
00479 
00480   /// Reset an iterator to its initial state.
00481   /**
00482     * This implements the corresponding method of the base class.
00483     *
00484     * @see CdbIItr::reset
00485     * @see CdbStatus
00486     */
00487     virtual CdbStatus reset( );
00488 
00489   /// Advance an iterator to the next position.
00490   /**
00491     * This implements the corresponding method of the base class.
00492     *
00493     * @see CdbIItr::next()
00494     */
00495     virtual bool next( );
00496 
00497   /// Obtain the currently reffered value.
00498   /**
00499     * This implements the corresponding method of the base class.
00500     *
00501     * @see CdbIItr::value()
00502     * @see CdbIItr::ValueType
00503     *
00504     * @return the current value the iterator is set on
00505     */
00506     virtual ValueType value( );
00507 
00508   /// Check if an iterator is valid.
00509   /**
00510     * This implements the corresponding method of the base class.
00511     *
00512     * @see CdbIItr::isValid()
00513     */
00514     virtual bool isValid( );
00515 
00516   /// Make a clone of itself
00517   /**
00518     * @see CdbIItr::clone()
00519     */
00520     virtual InterfaceType* clone( ) const;
00521 
00522 private:
00523 
00524     bool _isValid;
00525     bool _hasEverBeenAdvanced;
00526 
00527     CdbProperty _value;
00528 };
00529 
00530 CdbBdbSOriginPropertyItr::CdbBdbSOriginPropertyItr( const char* theDBIDRangeName ) :
00531     _isValid(false),
00532     _hasEverBeenAdvanced(false),
00533     _value( "DBID Range Name", theDBIDRangeName )
00534 { }
00535 
00536 CdbBdbSOriginPropertyItr::CdbBdbSOriginPropertyItr( const CdbBdbSOriginPropertyItr& theItr ) :
00537     _isValid(theItr._isValid),
00538     _hasEverBeenAdvanced(theItr._hasEverBeenAdvanced),
00539     _value(theItr._value)
00540 { }
00541 
00542 CdbBdbSOriginPropertyItr::~CdbBdbSOriginPropertyItr( )
00543 { }
00544 
00545 CdbBdbSOriginPropertyItr&
00546 CdbBdbSOriginPropertyItr::operator=( const CdbBdbSOriginPropertyItr& theItr )
00547 {
00548     if( &theItr != this ) {
00549         _isValid             = theItr._isValid;
00550         _hasEverBeenAdvanced = theItr._hasEverBeenAdvanced;
00551         _value               = theItr._value;
00552     }
00553     return *this;
00554 }
00555 
00556 CdbStatus
00557 CdbBdbSOriginPropertyItr::reset( )
00558 {
00559     _isValid             = false;
00560     _hasEverBeenAdvanced = false;
00561 
00562     return CdbStatus::Success;
00563 }
00564 
00565 bool
00566 CdbBdbSOriginPropertyItr::next( )
00567 {
00568     if( _isValid ) {
00569         _isValid             = false;
00570     } else {
00571         _isValid             = true;
00572         _hasEverBeenAdvanced = true;
00573     }
00574     return _isValid;
00575 }
00576 
00577 CdbBdbSOriginPropertyItr::ValueType
00578 CdbBdbSOriginPropertyItr::value( )
00579 {
00580     assert( isValid( ));
00581     return _value;
00582 }
00583 
00584 bool
00585 CdbBdbSOriginPropertyItr::isValid( )
00586 {
00587     return _isValid;
00588 }
00589 
00590 CdbBdbSOriginPropertyItr::InterfaceType*
00591 CdbBdbSOriginPropertyItr::clone( ) const
00592 {
00593     return new CdbBdbSOriginPropertyItr( *this );
00594 }
00595 
00596 /////////////////////////
00597 // Class CdbBdbSOrigin //
00598 /////////////////////////
00599 
00600 /// The wrapper implementation of a transient origin
00601 /**
00602   * This is the only "origin" of the 'Wraper' implementation.
00603   */
00604 class CdbBdbSOrigin : public CdbOrigin {
00605 
00606 private:
00607 
00608   /// The constructor (NOT IMPLEMENTED)
00609   /**
00610     * Is disabled...
00611     */
00612     CdbBdbSOrigin( );
00613 
00614   /// The copy constructor (NOT IMPLEMENTED)
00615   /**
00616     * Is disabled...
00617     */
00618     CdbBdbSOrigin( const CdbBdbSOrigin& theOrigin );
00619 
00620   /// The assignment operator (NOT IMPLEMENTED)
00621   /**
00622     * Is disabled...
00623     */
00624     CdbBdbSOrigin& operator=( const CdbBdbSOrigin& theOrigin );
00625 
00626 public:
00627 
00628   /// The normal constructor.
00629   /**
00630     * Initialize context with specified parameters.
00631     *
00632     * @see CdbDatabase
00633     * @see CdbDatabasePtr
00634     */
00635     CdbBdbSOrigin( const CdbDatabasePtr&         theDatabasePtr,    /**< the smart pointer providing a back back link to the parent database object */
00636                    const BdbRef(CdbBdbSOriginP)& theOriginRef       /**< the persistent origin */
00637                  );
00638 
00639   /// The destructor.
00640   /**
00641     * More details...
00642     */
00643     virtual ~CdbBdbSOrigin( );
00644 
00645   /// Obtain the creation time of the origin
00646   /**
00647     * This implements the corresponding method of the base class.
00648     *
00649     * @see CdbOrigin::created()
00650     */
00651     virtual BdbTime created( );
00652 
00653   /// Obtain the origin description
00654   /**
00655     * This implements the corresponding method of the base class.
00656     *
00657     * @see CdbOrigin::description()
00658     */
00659     virtual std::string description( );
00660 
00661   /// Check if this origin is the local one
00662   /**
00663     * This implements the corresponding method of the base class.
00664     *
00665     * @see CdbOrigin::isLocal()
00666     */
00667     virtual bool isLocal( ) const;
00668 
00669   /// Check if data corresponding to this origin are available in the local database
00670   /**
00671     * This implements the corresponding method of the base class.
00672     *
00673     * @see CdbOrigin::isInstantiated()
00674     */
00675     virtual bool isInstantiated( ) const;
00676 
00677   /// Check if this origin is the "master" one
00678   /**
00679     * This implements the corresponding method of the base class.
00680     *
00681     * @see CdbOrigin::isMaster()
00682     */
00683     virtual bool isMaster( ) const;
00684 
00685   /// Check if this origin is a "slave" one
00686   /**
00687     * This implements the corresponding method of the base class.
00688     *
00689     * @see CdbOrigin::isSlave()
00690     */
00691     virtual bool isSlave( ) const;
00692 
00693   /// Check if this origin is a "test" one
00694   /**
00695     * This implements the corresponding method of the base class.
00696     *
00697     * @see CdbOrigin::isTest()
00698     */
00699     virtual bool isTest( ) const;
00700 
00701   /// Set up an iterator of properties
00702   /**
00703     * This implements the corresponding method of the base class.
00704     *
00705     * @see CdbOrigin::propertyIterator()
00706     */
00707     virtual CdbStatus propertyIterator( CdbItr<CdbProperty>& theItr );
00708 
00709 private:
00710 
00711     BdbHandle(CdbBdbSOriginP) _originH;
00712 };
00713 
00714 CdbBdbSOrigin::CdbBdbSOrigin( const CdbDatabasePtr&         theDatabasePtr,
00715                               const BdbRef(CdbBdbSOriginP)& theOriginRef ) :
00716     CdbOrigin( theDatabasePtr,
00717                theOriginRef->name( ).head( ),
00718                theOriginRef->id( )),
00719     _originH(theOriginRef)
00720 { }
00721 
00722 CdbBdbSOrigin::~CdbBdbSOrigin( )
00723 { }
00724 
00725 BdbTime
00726 CdbBdbSOrigin::created( )
00727 {
00728     return  _originH->created( );
00729 }
00730 
00731 std::string
00732 CdbBdbSOrigin::description( )
00733 {
00734     return std::string(  _originH->description( ).head( ));
00735 }
00736 
00737 bool
00738 CdbBdbSOrigin::isLocal( ) const
00739 {
00740   // Use parent object to get the local origin's identifier.
00741 
00742     return parent( )->localOrigin( )->id( ) == _originH->id( );
00743 }
00744 
00745 bool
00746 CdbBdbSOrigin::isInstantiated( ) const
00747 {
00748   // Try to find the persistent registry corresponding to the current origin's identifier.
00749   // The presense of this reqistry would mean that the data from this database are either
00750   // native to the current database or they have already been brough from the corresponding
00751   // remote database into the current one.
00752 
00753     BdbHandle(CdbBdbSRegistryP) h;
00754     return CdbStatus::Success == CdbBdbSRegistryP::findByOrigin( h, _originH->id( ));
00755 }
00756 
00757 bool
00758 CdbBdbSOrigin::isMaster( ) const
00759 {
00760     return _originH->type( ) == CdbBdbSOriginP::MASTER;
00761 }
00762 
00763 bool
00764 CdbBdbSOrigin::isSlave( ) const
00765 {
00766     return _originH->type( ) == CdbBdbSOriginP::SLAVE;
00767 }
00768 
00769 bool
00770 CdbBdbSOrigin::isTest( ) const
00771 {
00772     return _originH->type( ) == CdbBdbSOriginP::TEST;
00773 }
00774 
00775 CdbStatus
00776 CdbBdbSOrigin::propertyIterator( CdbItr<CdbProperty>& theItr )
00777 {
00778   // ATTENTION: This is not a memory leak - the created object
00779   //            will be destroyed by the iterator.
00780 
00781     theItr = CdbItr<CdbProperty>( new CdbBdbSOriginPropertyItr( _originH->dbidrange( ).head( )));
00782 
00783     return CdbStatus::Success;
00784 }
00785 
00786 };
00787 
00788 ///////////////////////////
00789 // Class CdbBdbSDatabase //
00790 ///////////////////////////
00791 
00792 CdbDatabase*
00793 CdbBdbSDatabase::clone( ) const
00794 {
00795   // Get rid of the "const" to access non-const methods
00796 
00797     CdbBdbSDatabase* mySelf = const_cast<CdbBdbSDatabase*>( this );
00798     return new CdbBdbSDatabase( mySelf->parent( ),
00799                                 mySelf->name( ));
00800 }
00801 
00802 CdbBdbSDatabase::CdbBdbSDatabase( const CdbPtr& theCdbPtr,
00803                                   const char*   theName ) :
00804     CdbDatabase( theCdbPtr,
00805                  theName ),
00806     _isValid(true),
00807     _isOpen(false)
00808 { }
00809 
00810 CdbBdbSDatabase::~CdbBdbSDatabase( )
00811 { }
00812 
00813 bool
00814 CdbBdbSDatabase::isValid( )
00815 {
00816     return _isValid;
00817 }
00818 
00819 bool
00820 CdbBdbSDatabase::isOpen( )
00821 {
00822     return _isOpen;
00823 }
00824 
00825 CdbStatus
00826 CdbBdbSDatabase::open( )
00827 {
00828     const char* errorStr = "CdbBdbSDatabase::open() - ERROR.";
00829 
00830     CdbStatus result = CdbStatus::Error;
00831     do {
00832 
00833         if( ! _isOpen ) {
00834 
00835           // Find local registry first because it can also be a master one
00836 
00837             if( CdbStatus::Success != CdbBdbSRegistryP::findLocal( _localRegistryH )) {
00838                 cout << errorStr << endl
00839                      << "    Failed to find the local registry. The database was not properly" << endl
00840                      << "    initialized/loaded." << endl;
00841                 break;
00842             }
00843 
00844           // Find the master if required
00845 
00846             if( _localRegistryH->isMaster( )) {
00847                 _masterRegistryH = _localRegistryH;
00848             } else {
00849                 if( CdbStatus::Success != CdbBdbSRegistryP::findMaster( _masterRegistryH )) {
00850                     cout << errorStr << endl
00851                          << "    Failed to find the master registry. The database was not properly" << endl
00852                          << "    initialized/loaded." << endl;
00853                     break;
00854                 }
00855             }
00856             _isOpen = true;
00857         }
00858 
00859       // Done
00860 
00861         result = CdbStatus::Success;
00862 
00863     } while( false );
00864 
00865     return result;
00866 }
00867 
00868 CdbStatus
00869 CdbBdbSDatabase::close( )
00870 {
00871     if( _isOpen ) {
00872         _isOpen = false;
00873 
00874         _masterRegistryH = 0;
00875         _localRegistryH  = 0;
00876     }
00877     return CdbStatus::Success;
00878 }
00879 
00880 BdbTime
00881 CdbBdbSDatabase::id( )
00882 {
00883   // Make sure the database is open
00884 
00885     if( CdbStatus::Success != open( )) {
00886         assert( 0 );
00887         return BdbTime::plusInfinity;
00888     }
00889     return _localRegistryH->id( );
00890 }
00891 
00892 BdbTime
00893 CdbBdbSDatabase::created( )
00894 {
00895   // Make sure the database is open
00896 
00897     if( CdbStatus::Success != open( )) {
00898         assert( 0 );
00899         return BdbTime::plusInfinity;
00900     }
00901     return _localRegistryH->created( );
00902 }
00903 
00904 std::string
00905 CdbBdbSDatabase::description( )
00906 {
00907   // Make sure the database is open
00908 
00909     if( CdbStatus::Success != open( )) {
00910         assert( 0 );
00911         return std::string( );
00912     }
00913     return std::string( _localRegistryH->description( ).head( ));
00914 }
00915 
00916 std::string
00917 CdbBdbSDatabase::defaultView( ) const
00918 {
00919     return CdbEnvironment::defaultView( parent( )->technologyName( ),
00920                                         parent( )->implementationName( ),
00921                                         name( ));
00922 }
00923 
00924 CdbStatus
00925 CdbBdbSDatabase::findView( CdbViewPtr& thePtr,
00926                            const char* theName )
00927 {
00928     const char* errorStr = "CdbBdbSDatabase::findView(name) -- ERROR" ;
00929 
00930     CdbStatus result = CdbStatus::Error;
00931     do {
00932 
00933       // Make sure the database is open
00934 
00935         if( CdbStatus::Success != open( )) break;
00936 
00937       // Verify and correct parameters if 0 pointer is passed to indicate
00938       // that default database is needed.
00939 
00940         std::string viewName = defaultView( );
00941         if( 0 != theName ) viewName = theName;
00942 
00943         CdbCompositeName vName( viewName.c_str( ));
00944         if( !vName.isValid( )) {
00945             cout << errorStr << endl
00946                  << "    The view name passed to the procedure has invalid format: \"" << viewName.c_str( ) << "\"" << endl;
00947             break;
00948         }
00949 
00950       // Extract origin name and local view names
00951 
00952         std::string originName    = vName.first( );
00953         std::string localViewName = vName.last( );
00954 
00955       // Find the registry where the specified view is supposed to belong to
00956       //
00957       // NOTE: Watch for the simple optimizations in case of local or master
00958       //       origins.
00959 
00960         BdbHandle(CdbBdbSRegistryP) rH;
00961         if(      "<master>" == originName ) rH = _masterRegistryH;
00962         else if( "<local>"  == originName ) rH = _localRegistryH;
00963         else {
00964             if( CdbStatus::Success != CdbBdbSRegistryP::findByOrigin( rH,
00965                                                                       originName.c_str( ))) {
00966                 cout << errorStr << endl
00967                      << "    Registry for specified origin was not found." << endl;
00968                 break;
00969             }
00970         }
00971 
00972       // Get to the collection of views managed by found registry.
00973 
00974         BdbRef(CdbBdbSViewCollectionP) vCollRef = rH->viewCollection( );
00975         assert( ! BdbIsNull(vCollRef));
00976 
00977       // Find the view by its name at the collection.
00978       //
00979       // NOTE: Watch for side track in case if view nicknames are used.
00980 
00981         BdbRef(CdbBdbSViewP) vRef;
00982         if( 0 == strcmp( "<recent>", localViewName.c_str( ))) {
00983             if( CdbStatus::Success != vCollRef->newest( vRef )) {
00984                 result = CdbStatus::NotFound;
00985                 break;
00986             }
00987         } else {
00988             if( CdbStatus::Success != vCollRef->find( localViewName.c_str( ),
00989                                                       vRef )) {
00990                 result = CdbStatus::NotFound;
00991                 break;
00992             }
00993         }
00994 
00995       // Construct the extended identifier of the view (nneded to construct
00996       // the transient API object).
00997 
00998         CdbId viewId( rH->originId( ),
00999                       vRef->id( ));
01000 
01001       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01002       //              we don't know a smart pointer pointing to the current
01003       //              instance, therefore we can't guarantee the lifetime
01004       //              of the current instance.
01005       //
01006       // NOTE: The above corrected parameters object is used.
01007 
01008         CdbDatabasePtr myPtr( this->clone( ));
01009 
01010         thePtr = new CdbBdbSView( myPtr,
01011                                   viewName.c_str( ),
01012                                   viewId,
01013                                   vRef,
01014                                   _masterRegistryH,
01015                                   _localRegistryH );
01016 
01017         result = CdbStatus::Success;
01018 
01019     } while( false );
01020 
01021     return result;
01022 }
01023 
01024 CdbStatus
01025 CdbBdbSDatabase::findView( CdbViewPtr&  thePtr,
01026                            const CdbId& theId )
01027 {
01028     const char* errorStr = "CdbBdbSDatabase::findView(id) -- ERROR" ;
01029 
01030     CdbStatus result = CdbStatus::Error;
01031     do {
01032 
01033       // Make sure the database is open
01034 
01035         if( CdbStatus::Success != open( )) break;
01036 
01037       // Find the registry where the specified view is supposed to belong to
01038       //
01039       // NOTE: Watch for the simple optimizations in case of local or master
01040       //       origins.
01041 
01042         BdbHandle(CdbBdbSRegistryP) rH;
01043         if(      theId.origin == _masterRegistryH->originId( )) rH = _masterRegistryH;
01044         else if( theId.origin == _localRegistryH->originId( ))  rH = _localRegistryH;
01045         else {
01046             if( CdbStatus::Success != CdbBdbSRegistryP::findByOrigin( rH,
01047                                                                       theId.origin )) {
01048                 cout << errorStr << endl
01049                      << "    Registry for specified origin was not found." << endl;
01050                 break;
01051             }
01052         }
01053 
01054       // Get to the collection of views managed by found registry.
01055 
01056         BdbRef(CdbBdbSViewCollectionP) vCollRef = rH->viewCollection( );
01057         assert( ! BdbIsNull(vCollRef));
01058 
01059       // Find the view by its name at the collection.
01060 
01061         BdbRef(CdbBdbSViewP) vRef;
01062         if( CdbStatus::Success != vCollRef->find( theId.local,
01063                                                   vRef )) {
01064             result = CdbStatus::NotFound;
01065             break;
01066         }
01067 
01068       // Build the full name of the view
01069 
01070         std::string viewName( "" );
01071         {
01072             BdbRef(CdbBdbSOriginCollectionP) oCollRef = _masterRegistryH->originCollection( );
01073             assert( ! BdbIsNull(oCollRef));
01074 
01075             BdbRef(CdbBdbSOriginP) oRef;
01076             if( CdbStatus::Success != oCollRef->find( theId.origin,
01077                                                       oRef )) {
01078                 cout << errorStr << endl
01079                      << "    The origin with ID: " << theId.origin << " not found in the master." << endl;
01080                 break;
01081             }
01082             CdbCompositeName vName( oRef->name( ), vRef->name( ));
01083             assert( vName.isValid( ));
01084 
01085             viewName = vName.getName( );
01086         }
01087 
01088       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01089       //              we don't know a smart pointer pointing to the current
01090       //              instance, therefore we can't guarantee the lifetime
01091       //              of the current instance.
01092       //
01093       // NOTE: The above corrected parameters object is used.
01094 
01095         CdbDatabasePtr myPtr( this->clone( ));
01096 
01097         thePtr = new CdbBdbSView( myPtr,
01098                                   viewName.c_str( ),
01099                                   theId,
01100                                   vRef,
01101                                   _masterRegistryH,
01102                                   _localRegistryH );
01103 
01104         result = CdbStatus::Success;
01105 
01106     } while( false );
01107 
01108     return result;
01109 }
01110 
01111 CdbStatus
01112 CdbBdbSDatabase::viewIterator( CdbViewItr& theItr )
01113 {
01114     CdbStatus result = CdbStatus::Error;
01115     do {
01116 
01117       // Make sure the database is open
01118 
01119         if( CdbStatus::Success != open( )) break;
01120 
01121       // ATTENTION: This is not a memory leak - the created object
01122       //            will be destroyed by the iterator.
01123 
01124         theItr = CdbViewItr( new CdbBdbSViewItr( _masterRegistryH ));
01125 
01126       // Done
01127 
01128         result = CdbStatus::Success;
01129 
01130     } while( false );
01131 
01132     return result;
01133 }
01134 
01135 CdbStatus
01136 CdbBdbSDatabase::physicalConditionIterator( CdbItr<CdbId>& theItr )
01137 {
01138     const char* errorStr = "CdbBdbSDatabase::physicalConditionIterator() -- ERROR" ;
01139 
01140     CdbStatus result = CdbStatus::Error;
01141 
01142   // Make sure the database is open
01143 
01144     if( CdbStatus::Success != open( )) return CdbStatus::Error;
01145 
01146   // Find all physical conditions and put their 'global physical identifiers'
01147   // into thsi vector.
01148 
01149     std::vector<CdbId> vectorOfIds;
01150 
01151     CdbBdbSOriginCollectionP::IteratorOfIdentifiers originItr = _masterRegistryH->originCollection( )->iterator_identifiers( );
01152     while( originItr.next( )) {
01153 
01154         d_UShort originId = originItr.value( );
01155 
01156       // Try to get the corresponding registry. If it doesn't exist (not instantiated) then we
01157       // won't look for conditions of that registry.
01158 
01159         BdbHandle(CdbBdbSRegistryP) registryH;
01160         {
01161             if( CdbStatus::Success != ( result = CdbBdbSRegistryP::findByOrigin( registryH,
01162                                                                                  originId ))) {
01163                 if( CdbStatus::NotFound == result ) {
01164 
01165                   // The origin is not currently instantiated in the input database. This may happen
01166                   // when the origin's data are yet to be imported.
01167 
01168                     continue;
01169 
01170                 } else {
01171                     cerr << errorStr << endl
01172                          << "    Failed to find a registry for origin ID = " << originId << "." << endl;
01173                     return result;
01174                 }
01175             }
01176         }
01177 
01178       // Get a collection of physical conditions from the registry
01179       //
01180       // NOTE; Some registries may not have local collections.. So we just skip them.
01181 
01182         BdbHandle(CdbBdbSConditionCollectionP) conditionCollectionH = registryH->conditionCollection( );
01183         if( !BdbIsNull(conditionCollectionH)) {
01184 
01185             CdbBdbSConditionCollectionP::IteratorOfIdentifiers pConditionItr = conditionCollectionH->iterator_identifiers( );
01186             while( pConditionItr.next( )) {
01187                 vectorOfIds.push_back( CdbId( originId,
01188                                               pConditionItr.value( )));
01189             }
01190         }
01191     }
01192 
01193   // Build an iterator out of the vector
01194 
01195     theItr = CdbItr<CdbId>( new CdbSimpleVectorAdapterItr<CdbId>( vectorOfIds ));
01196 
01197     return CdbStatus::Success;
01198 }
01199 
01200 CdbStatus
01201 CdbBdbSDatabase::findCondition( CdbConditionPtr& thePtr,
01202                                 const CdbId&     theId )
01203 {
01204     const char* errorStr = "CdbBdbSDatabase::findCondition('physical address') -- ERROR" ;
01205 
01206     CdbStatus result = CdbStatus::Error;
01207     do {
01208 
01209       // Make sure the database is open
01210 
01211         if( CdbStatus::Success != open( )) break;
01212 
01213       // Find the registry where the specified physical condition is supposed to belong to
01214       //
01215       // NOTE: Watch for the simple optimizations in case of local or master
01216       //       origins.
01217 
01218         BdbHandle(CdbBdbSRegistryP) rH;
01219         if(      theId.origin == _masterRegistryH->originId( )) rH = _masterRegistryH;
01220         else if( theId.origin == _localRegistryH->originId( ))  rH = _localRegistryH;
01221         else {
01222             if( CdbStatus::Success != CdbBdbSRegistryP::findByOrigin( rH,
01223                                                                       theId.origin )) {
01224                 cout << errorStr << endl
01225                      << "    Registry for specified origin " << theId.origin << " was not found." << endl;
01226                 break;
01227             }
01228         }
01229 
01230       // Get to the collection of physical conditions managed by found registry.
01231 
01232         BdbRef(CdbBdbSConditionCollectionP) cCollRef = rH->conditionCollection( );
01233         assert( ! BdbIsNull(cCollRef));
01234 
01235       // Find the condition by its identifier at the collection.
01236 
01237         BdbRef(CdbBdbSConditionP) cRef;
01238         if( CdbStatus::Success != cCollRef->find( theId.local,
01239                                                   cRef )) {
01240             result = CdbStatus::NotFound;
01241             break;
01242         }
01243 
01244       // Build the full name of the condition
01245 
01246         std::string conditionName( "" );
01247         {
01248             BdbRef(CdbBdbSOriginCollectionP) oCollRef = _masterRegistryH->originCollection( );
01249             assert( ! BdbIsNull(oCollRef));
01250 
01251             BdbRef(CdbBdbSOriginP) oRef;
01252             if( CdbStatus::Success != oCollRef->find( theId.origin,
01253                                                       oRef )) {
01254                 cout << errorStr << endl
01255                      << "    The origin with ID: " << theId.origin << " not found in the master." << endl;
01256                 break;
01257             }
01258             conditionName = std::string( oRef->name( )) + std::string( "::" ) + std::string( cRef->name( ).head( ));
01259         }
01260 
01261       // DESIGN NOTE: The condition API component is going to be disjoined from
01262       //              any view or a folder. The direct database pointer will be added instead.
01263 
01264         CdbFolderPtr fPtr = 0;
01265         BdbHandle(CdbBdbSConditionAtFolderP) conditionAtFolderH = 0;
01266 
01267       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01268       //              we don't know a smart pointer pointing to the current
01269       //              instance, therefore we can't guarantee the lifetime
01270       //              of the current instance.
01271 
01272         CdbDatabasePtr myPtr( this->clone( ));
01273 
01274         thePtr = new CdbBdbSCondition( fPtr,
01275                                        myPtr,
01276                                        conditionName.c_str( ),
01277                                        CdbBdbSId( theId.origin,
01278                                                   theId.local ),
01279                                        conditionAtFolderH,
01280                                        cRef,
01281                                        _masterRegistryH,
01282                                        _localRegistryH );
01283 
01284         result = CdbStatus::Success;
01285 
01286     } while( false );
01287 
01288     return result;
01289 }
01290 
01291 CdbStatus
01292 CdbBdbSDatabase::findCondition( CdbConditionPtr&        thePtr,
01293                                 const CdbCompositeName& thePhysicalName )
01294 {
01295     const char* errorStr = "CdbBdbSDatabase::findCondition('physical name') -- ERROR" ;
01296 
01297     CdbStatus result = CdbStatus::Error;
01298     do {
01299 
01300       // Make sure the database is open
01301 
01302         if( CdbStatus::Success != open( )) break;
01303 
01304       // Verify parameters
01305 
01306         if( !thePhysicalName.isValid( )) {
01307             cout << errorStr << endl
01308                  << "    The 'physical name' name passed to the procedure has invalid format: \"" << thePhysicalName.getName( ).c_str( ) << "\"" << endl;
01309             break;
01310         }
01311 
01312       // Extract origin name and local condition names
01313 
01314         std::string originName         = thePhysicalName.first( );
01315         std::string localConditionName = thePhysicalName.last( );
01316 
01317       // Find the registry where the specified physical condition is supposed to belong to
01318       //
01319       // NOTE: Watch for the simple optimizations in case of local or master
01320       //       origins.
01321 
01322         BdbHandle(CdbBdbSRegistryP) rH;
01323         if     ( "<master>" == originName ) rH = _masterRegistryH;
01324         else if(  "<local>" == originName ) rH =  _localRegistryH;
01325         else {
01326             if( CdbStatus::Success != CdbBdbSRegistryP::findByOrigin( rH,
01327                                                                       originName.c_str( ))) {
01328                 cout << errorStr << endl
01329                      << "    Registry for specified origin \"" << originName.c_str( ) << "\" was not found." << endl;
01330                 break;
01331             }
01332         }
01333 
01334       // Get to the collection of physical conditions managed by found registry.
01335 
01336         BdbRef(CdbBdbSConditionCollectionP) cCollRef = rH->conditionCollection( );
01337         assert( ! BdbIsNull(cCollRef));
01338 
01339       // Find the condition by its 'physical' name at the collection.
01340 
01341         BdbRef(CdbBdbSConditionP) cRef;
01342         if( CdbStatus::Success != cCollRef->find( localConditionName.c_str( ),
01343                                                   cRef )) {
01344             result = CdbStatus::NotFound;
01345             break;
01346         }
01347 
01348       // DESIGN NOTE: The condition API component is going to be disjoined from
01349       //              any view or a folder. The direct database pointer will be added instead.
01350 
01351         CdbFolderPtr fPtr = 0;
01352         BdbHandle(CdbBdbSConditionAtFolderP) conditionAtFolderH = 0;
01353 
01354       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01355       //              we don't know a smart pointer pointing to the current
01356       //              instance, therefore we can't guarantee the lifetime
01357       //              of the current instance.
01358 
01359         CdbDatabasePtr myPtr( this->clone( ));
01360 
01361         thePtr = new CdbBdbSCondition( fPtr,
01362                                        myPtr,
01363                                        thePhysicalName.getName( ).c_str( ),
01364                                        CdbBdbSId( rH->originId( ),
01365                                                   cRef->id( )),
01366                                        conditionAtFolderH,
01367                                        cRef,
01368                                        _masterRegistryH,
01369                                        _localRegistryH );
01370 
01371         result = CdbStatus::Success;
01372 
01373     } while( false );
01374 
01375     return result;
01376 }
01377 
01378 CdbStatus
01379 CdbBdbSDatabase::historyEventIterator( CdbHistoryEventItr& theItr,
01380                                        const BdbTime&      theBeginTime,
01381                                        const BdbTime&      theEndTime,
01382                                        const char**        theEventsToSelect )
01383 {
01384     return CdbStatus::NotImplemented;
01385 }
01386 
01387 CdbStatus
01388 CdbBdbSDatabase::historyEventTypeIterator( CdbHistoryEventTypeItr& theItr )
01389 {
01390     return CdbStatus::NotImplemented;
01391 }
01392 
01393 CdbStatus
01394 CdbBdbSDatabase::partitionIterator( CdbItr<unsigned short>& theItr )
01395 {
01396     CdbStatus result = CdbStatus::Error;
01397     do {
01398 
01399       // Make sure the database is open
01400 
01401         if( CdbStatus::Success != open( )) break;
01402 
01403       // ATTENTION: This is not a memory leak - the created object
01404       //            will be destroyed by the iterator.
01405 
01406         theItr = CdbItr<unsigned short>( new CdbBdbSPartitionItr( _masterRegistryH->partitionsLayout( )->iterator( )));
01407 
01408       // Done
01409 
01410         result = CdbStatus::Success;
01411 
01412     } while( false );
01413 
01414     return result;
01415 }
01416 
01417 CdbStatus
01418 CdbBdbSDatabase::partitionIterator( CdbItr<unsigned short>& theItr,
01419                                     unsigned int            theOriginId )
01420 {
01421     const char* errorStr = "CdbBdbSDatabase::partitionIterator(by origin id) - ERROR.";
01422 
01423     CdbStatus result = CdbStatus::Error;
01424     do {
01425 
01426       // Make sure the database is open
01427 
01428         if( CdbStatus::Success != open( )) break;
01429 
01430       // Find the corresponding registry
01431 
01432         BdbHandle(CdbBdbSRegistryP) registryH;
01433         CdbStatus                   status = CdbBdbSRegistryP::findByOrigin( registryH,
01434                                                                              theOriginId );
01435         if( CdbStatus::Success != status ) {
01436             if( CdbStatus::NotFound == status ) {
01437                 cout << errorStr << endl
01438                      << "    The specified origin identifier " << theOriginId << " does not correspond to any" << endl
01439                      << "    known origin in the current database. Please check the logic of your application." << endl;
01440             } else {
01441                 cout << errorStr << endl
01442                      << "    Failed to find the registry for specified origin identifier " << theOriginId << "." << endl
01443                      << "    Perhaps the database was not properly initialized/loaded." << endl;
01444             }
01445             break;
01446         }
01447 
01448       // Try to get the P-Layout. Some origins may not have it. Silently return
01449       // the CdbStatus::NotFound if this is going to be the case.
01450 
01451         BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = registryH->partitionsLayout( );
01452         if( BdbIsNull(pLayoutRef)) {
01453             result = CdbStatus::NotFound;
01454             break;
01455         }
01456 
01457       // ATTENTION: This is not a memory leak - the created object
01458       //            will be destroyed by the iterator.
01459 
01460         theItr = CdbItr<unsigned short>( new CdbBdbSPartitionItr( pLayoutRef->iterator( )));
01461 
01462       // Done
01463 
01464         result = CdbStatus::Success;
01465 
01466     } while( false );
01467 
01468     return result;
01469 }
01470 
01471 CdbStatus
01472 CdbBdbSDatabase::partitionIterator( CdbItr<unsigned short>& theItr,
01473                                     const char*             theOriginName )
01474 {
01475     const char* errorStr = "CdbBdbSDatabase::partitionIterator(by origin name) - ERROR.";
01476 
01477     CdbStatus result = CdbStatus::Error;
01478     do {
01479 
01480       // Make sure the database is open
01481 
01482         if( CdbStatus::Success != open( )) break;
01483 
01484       // Find the corresponding registry
01485 
01486         BdbHandle(CdbBdbSRegistryP) registryH;
01487         CdbStatus                   status = CdbBdbSRegistryP::findByOrigin( registryH,
01488                                                                              theOriginName );
01489         if( CdbStatus::Success != status ) {
01490             if( CdbStatus::NotFound == status ) {
01491                 cout << errorStr << endl
01492                      << "    The specified origin name \"" << theOriginName << "\" does not correspond to any" << endl
01493                      << "    known origin in the current database. Please check the logic of your application." << endl;
01494             } else {
01495                 cout << errorStr << endl
01496                      << "    Failed to find the registry for specified origin name \"" << theOriginName << "\"." << endl
01497                      << "    Perhaps the database was not properly initialized/loaded." << endl;
01498             }
01499             break;
01500         }
01501 
01502       // Try to get the P-Layout. Some origins may not have it. Silently return
01503       // the CdbStatus::NotFound if this is going to be the case.
01504 
01505         BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = registryH->partitionsLayout( );
01506         if( BdbIsNull(pLayoutRef)) {
01507             result = CdbStatus::NotFound;
01508             break;
01509         }
01510 
01511       // ATTENTION: This is not a memory leak - the created object
01512       //            will be destroyed by the iterator.
01513 
01514         theItr = CdbItr<unsigned short>( new CdbBdbSPartitionItr( pLayoutRef->iterator( )));
01515 
01516       // Done
01517 
01518         result = CdbStatus::Success;
01519 
01520     } while( false );
01521 
01522     return result;
01523 }
01524 
01525 CdbStatus
01526 CdbBdbSDatabase::findPartition( CdbPartitionPtr& thePtr,
01527                                 unsigned short   thePartition )
01528 {
01529     const char* errorStr = "CdbBdbSDatabase::findPartition() -- ERROR" ;
01530 
01531     CdbStatus result = CdbStatus::Error;
01532     do {
01533 
01534       // Make sure the database is open
01535 
01536         if( CdbStatus::Success != open( )) break;
01537 
01538       // Find specified partition
01539       //
01540       // NOTE: This algorithm will use partition object from the local
01541       //       P-Layout if it will discover that the partition belongs to the current
01542       //       database. This is a typical scenario for the SLAVE type databases.
01543 
01544         BdbHandle( CdbBdbSPartitionP ) pH;
01545         {
01546             BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = _masterRegistryH->partitionsLayout( );
01547             if( BdbIsNull(pLayoutRef)) {
01548                 assert( 0 );
01549                 break;
01550             }
01551 
01552             BdbRef( CdbBdbSPartitionP ) pRef;
01553             if( CdbStatus::Success != pLayoutRef->find( thePartition,
01554                                                         pRef )) {
01555                 result = CdbStatus::NotFound;
01556                 break;
01557             }
01558             pH = pRef;
01559 
01560             if( pH->originId( ) != _masterRegistryH->originId( )) {
01561                 if( pH->originId( ) == _localRegistryH->originId( )) {
01562 
01563                     pLayoutRef = _localRegistryH->partitionsLayout( );
01564                     if( BdbIsNull(pLayoutRef)) {
01565                         assert( 0 );
01566                         break;
01567                     }
01568                     if( CdbStatus::Success != pLayoutRef->find( thePartition,
01569                                                                 pRef )) {
01570                         cout << errorStr << endl
01571                              << "    Failed to find partition with ID=" << thePartition << " at the current" << endl
01572                              << "    database to whom the partition is supposed to belong to. The database" << endl
01573                              << "    may not be properly initialized/loaded." << endl;
01574 
01575                         result = CdbStatus::NotFound;
01576                         break;
01577                     }
01578                     pH = pRef;
01579                 }
01580             }
01581         }
01582         if( BdbIsNull(pH)) {
01583             assert( 0 );
01584             break;
01585         }
01586 
01587       // Get the owner's origin name of this partition.
01588 
01589         std::string ownerOriginName;
01590         {
01591             BdbHandle(CdbBdbSOriginCollectionP) oCollectionH;
01592 
01593             oCollectionH = _masterRegistryH->originCollection( );
01594             assert( !BdbIsNull(oCollectionH));
01595 
01596             BdbRef(CdbBdbSOriginP) oRef;
01597             if( CdbStatus::Success != oCollectionH->find( pH->originId( ),
01598                                                           oRef )) {
01599                 cout << errorStr << endl
01600                      << "    There is no information on the origin ID=" << pH->originId( ) << endl
01601                      << "    in the corresponding collection of the MASTER's registry." << endl
01602                      << "    The database may be corrupted or in an inconsistent state." << endl;
01603                 break;
01604             }
01605 
01606             ownerOriginName = oRef->name( );
01607         }
01608 
01609       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01610       //              we don't know a smart pointer pointing to the current
01611       //              instance, therefore we can't guarantee the lifetime
01612       //              of the current instance.
01613       //
01614       // NOTE: The above corrected parameters object is used.
01615 
01616         CdbDatabasePtr myPtr = this->clone( );
01617 
01618         thePtr = new CdbBdbSPartition( myPtr,
01619                                        pH,
01620                                        pH->originId( ),
01621                                        ownerOriginName.c_str( ));
01622 
01623       // Done
01624 
01625         result = CdbStatus::Success;
01626 
01627     } while( false );
01628 
01629     return result;
01630 }
01631 
01632 CdbStatus
01633 CdbBdbSDatabase::findPartition( CdbPartitionPtr& thePtr,
01634                                 unsigned short   thePartition,
01635                                 unsigned int     theOriginId )
01636 {
01637     const char* errorStr = "CdbBdbSDatabase::findPartition(by origin id) -- ERROR" ;
01638 
01639     CdbStatus result = CdbStatus::Error;
01640     do {
01641 
01642       // Make sure the database is open
01643 
01644         if( CdbStatus::Success != open( )) break;
01645 
01646       // Find the corresponding registry
01647 
01648         BdbHandle(CdbBdbSRegistryP) registryH;
01649         CdbStatus                   status = CdbBdbSRegistryP::findByOrigin( registryH,
01650                                                                              theOriginId );
01651         if( CdbStatus::Success != status ) {
01652             if( CdbStatus::NotFound == status ) {
01653                 cout << errorStr << endl
01654                      << "    The specified origin identifier " << theOriginId << " does not correspond to any" << endl
01655                      << "    known origin in the current database. Please check the logic of your application." << endl;
01656             } else {
01657                 cout << errorStr << endl
01658                      << "    Failed to find the registry for specified origin identifier " << theOriginId << "." << endl
01659                      << "    Perhaps the database was not properly initialized/loaded." << endl;
01660             }
01661             break;
01662         }
01663 
01664       // Try to get the P-Layout. Some origins may not have it.
01665 
01666         BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = registryH->partitionsLayout( );
01667         if( BdbIsNull(pLayoutRef)) {
01668             cout << errorStr << endl
01669                  << "    The origin for specified origin identifier " << theOriginId << " is not allowed" << endl
01670                  << "    to have partitions. Please check the logic of your application." << endl;
01671             break;
01672         }
01673 
01674       // Find the specified partition
01675 
01676         BdbHandle( CdbBdbSPartitionP ) pH;
01677         {
01678             BdbRef( CdbBdbSPartitionP ) pRef;
01679             if( CdbStatus::Success != pLayoutRef->find( thePartition,
01680                                                         pRef )) {
01681                 result = CdbStatus::NotFound;
01682                 break;
01683             }
01684             pH = pRef;
01685         }
01686 
01687       // Get the owner's origin name of this partition.
01688 
01689         std::string ownerOriginName;
01690         {
01691             BdbHandle(CdbBdbSOriginCollectionP) oCollectionH;
01692 
01693             oCollectionH = _masterRegistryH->originCollection( );
01694             assert( !BdbIsNull(oCollectionH));
01695 
01696             BdbRef(CdbBdbSOriginP) oRef;
01697             if( CdbStatus::Success != oCollectionH->find( pH->originId( ),
01698                                                           oRef )) {
01699                 cout << errorStr << endl
01700                      << "    There is no information on the origin ID=" << pH->originId( ) << endl
01701                      << "    in the corresponding collection of the MASTER's registry." << endl
01702                      << "    The database may be corrupted or in an inconsistent state." << endl;
01703                 break;
01704             }
01705 
01706             ownerOriginName = oRef->name( );
01707         }
01708 
01709       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01710       //              we don't know a smart pointer pointing to the current
01711       //              instance, therefore we can't guarantee the lifetime
01712       //              of the current instance.
01713       //
01714       // NOTE: The above corrected parameters object is used.
01715 
01716         CdbDatabasePtr myPtr = this->clone( );
01717 
01718         thePtr = new CdbBdbSPartition( myPtr,
01719                                        pH,
01720                                        pH->originId( ),
01721                                        ownerOriginName.c_str( ));
01722 
01723       // Done
01724 
01725         result = CdbStatus::Success;
01726 
01727     } while( false );
01728 
01729     return result;
01730 }
01731 
01732 CdbStatus
01733 CdbBdbSDatabase::findPartition( CdbPartitionPtr& thePtr,
01734                                 unsigned short   thePartition,
01735                                 const char*      theOriginName )
01736 {
01737     const char* errorStr = "CdbBdbSDatabase::findPartition(by origin name) -- ERROR" ;
01738 
01739     CdbStatus result = CdbStatus::Error;
01740     do {
01741 
01742       // Make sure the database is open
01743 
01744         if( CdbStatus::Success != open( )) break;
01745 
01746       // Find the corresponding registry
01747 
01748         BdbHandle(CdbBdbSRegistryP) registryH;
01749         CdbStatus                   status = CdbBdbSRegistryP::findByOrigin( registryH,
01750                                                                              theOriginName );
01751         if( CdbStatus::Success != status ) {
01752             if( CdbStatus::NotFound == status ) {
01753                 cout << errorStr << endl
01754                      << "    The specified origin name \"" << theOriginName << "\" does not correspond to any" << endl
01755                      << "    known origin in the current database. Please check the logic of your application." << endl;
01756             } else {
01757                 cout << errorStr << endl
01758                      << "    Failed to find the registry for specified origin name \"" << theOriginName << "\"." << endl
01759                      << "    Perhaps the database was not properly initialized/loaded." << endl;
01760             }
01761             break;
01762         }
01763 
01764       // Try to get the P-Layout. Some origins may not have it.
01765 
01766         BdbRef(CdbBdbSPartitionsLayoutP) pLayoutRef = registryH->partitionsLayout( );
01767         if( BdbIsNull(pLayoutRef)) {
01768             cout << errorStr << endl
01769                  << "    The origin for specified origin name \"" << theOriginName << "\" is not allowed" << endl
01770                  << "    to have partitions. Please check the logic of your application." << endl;
01771             break;
01772         }
01773 
01774       // Find the specified partition
01775 
01776         BdbHandle( CdbBdbSPartitionP ) pH;
01777         {
01778             BdbRef( CdbBdbSPartitionP ) pRef;
01779             if( CdbStatus::Success != pLayoutRef->find( thePartition,
01780                                                         pRef )) {
01781                 result = CdbStatus::NotFound;
01782                 break;
01783             }
01784             pH = pRef;
01785         }
01786 
01787       // Get the owner's origin name of this partition.
01788 
01789         std::string ownerOriginName;
01790         {
01791             BdbHandle(CdbBdbSOriginCollectionP) oCollectionH;
01792 
01793             oCollectionH = _masterRegistryH->originCollection( );
01794             assert( !BdbIsNull(oCollectionH));
01795 
01796             BdbRef(CdbBdbSOriginP) oRef;
01797             if( CdbStatus::Success != oCollectionH->find( pH->originId( ),
01798                                                           oRef )) {
01799                 cout << errorStr << endl
01800                      << "    There is no information on the origin ID=" << pH->originId( ) << endl
01801                      << "    in the corresponding collection of the MASTER's registry." << endl
01802                      << "    The database may be corrupted or in an inconsistent state." << endl;
01803                 break;
01804             }
01805 
01806             ownerOriginName = oRef->name( );
01807         }
01808 
01809       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01810       //              we don't know a smart pointer pointing to the current
01811       //              instance, therefore we can't guarantee the lifetime
01812       //              of the current instance.
01813       //
01814       // NOTE: The above corrected parameters object is used.
01815 
01816         CdbDatabasePtr myPtr = this->clone( );
01817 
01818         thePtr = new CdbBdbSPartition( myPtr,
01819                                        pH,
01820                                        pH->originId( ),
01821                                        ownerOriginName.c_str( ));
01822 
01823       // Done
01824 
01825         result = CdbStatus::Success;
01826 
01827     } while( false );
01828 
01829     return result;
01830 }
01831 
01832 CdbOriginPtr
01833 CdbBdbSDatabase::localOrigin( )
01834 {
01835   // Make sure the database is open
01836 
01837     if( CdbStatus::Success != open( )) {
01838         assert( 0 );
01839         return 0;
01840     }
01841 
01842   // Use cached object pointer if possible
01843 
01844     if( _localOriginPtr.isNull( )) {
01845         if( CdbStatus::Success != findOrigin( _localOriginPtr,
01846                                               _localRegistryH->originId( ))) {
01847             assert( 0 );
01848             return 0;
01849         }
01850     }
01851     return _localOriginPtr;
01852 }
01853 
01854 CdbStatus
01855 CdbBdbSDatabase::originIterator( CdbItr<unsigned short>& theItr )
01856 {
01857     CdbStatus result = CdbStatus::Error;
01858     do {
01859 
01860       // Make sure the database is open
01861 
01862         if( CdbStatus::Success != open( )) break;
01863 
01864       // Set up an iterator of persistent origin identifiers. We're going
01865       // to retranslate this iterator to the corresponding transient one.
01866 
01867         BdbRef(CdbBdbSOriginCollectionP) oCollRef = _masterRegistryH->originCollection( );
01868         assert( ! BdbIsNull(oCollRef));
01869 
01870       // ATTENTION: This is not a memory leak - the created object
01871       //            will be destroyed by the iterator.
01872 
01873         theItr = CdbItr<unsigned short>( new CdbBdbSOriginItr( oCollRef->iterator_identifiers( )));
01874 
01875       // Done
01876 
01877         result = CdbStatus::Success;
01878 
01879     } while( false );
01880 
01881     return result;
01882 }
01883 
01884 CdbStatus
01885 CdbBdbSDatabase::findOrigin( CdbOriginPtr& thePtr,
01886                              const char*   theName )
01887 {
01888     assert( 0 != theName );
01889 
01890     CdbStatus result = CdbStatus::NotFound;
01891     do {
01892 
01893       // Make sure the database is open
01894 
01895         if( CdbStatus::Success != open( )) {
01896             result = CdbStatus::Error;
01897             break;
01898         }
01899 
01900       // Try to find a persistent object for specified origin. If not found then
01901       // return the whatever status is obtained.
01902 
01903         BdbRef(CdbBdbSOriginCollectionP) oCollRef = _masterRegistryH->originCollection( );
01904         assert( ! BdbIsNull(oCollRef));
01905 
01906         BdbRef(CdbBdbSOriginP) oRef;
01907         {
01908             result = oCollRef->find( theName, oRef );
01909             if( CdbStatus::Success != result ) break;
01910         }
01911         assert( ! BdbIsNull(oRef));
01912 
01913       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01914       //              we don't know a smart pointer pointing to the current
01915       //              instance, therefore we can't guarantee the lifetime
01916       //              of the current instance.
01917       //
01918       // NOTE: The above corrected parameters object is used.
01919 
01920         CdbDatabasePtr myPtr( this->clone( ));
01921 
01922         thePtr = new CdbBdbSOrigin( myPtr,
01923                                     oRef );
01924 
01925       // Done
01926 
01927         result = CdbStatus::Success;
01928 
01929     } while( false );
01930 
01931     return result;
01932 }
01933 
01934 CdbStatus
01935 CdbBdbSDatabase::findOrigin( CdbOriginPtr&  thePtr,
01936                              unsigned short theId )
01937 {
01938     CdbStatus result = CdbStatus::NotFound;
01939     do {
01940 
01941       // Make sure the database is open
01942 
01943         if( CdbStatus::Success != open( )) {
01944             result = CdbStatus::Error;
01945             break;
01946         }
01947 
01948       // Try to find a persistent object for specified origin. If not found then
01949       // return the whatever status is obtained.
01950 
01951         BdbRef(CdbBdbSOriginCollectionP) oCollRef = _masterRegistryH->originCollection( );
01952         assert( ! BdbIsNull(oCollRef));
01953 
01954         BdbRef(CdbBdbSOriginP) oRef;
01955         {
01956             result = oCollRef->find( theId, oRef );
01957             if( CdbStatus::Success != result ) break;
01958         }
01959         assert( ! BdbIsNull(oRef));
01960 
01961       // DESIGN NOTE: We need to construct a smart pointer against a clone since
01962       //              we don't know a smart pointer pointing to the current
01963       //              instance, therefore we can't guarantee the lifetime
01964       //              of the current instance.
01965       //
01966       // NOTE: The above corrected parameters object is used.
01967 
01968         CdbDatabasePtr myPtr( this->clone( ));
01969 
01970         thePtr = new CdbBdbSOrigin( myPtr,
01971                                     oRef );
01972 
01973       // Done
01974 
01975         result = CdbStatus::Success;
01976 
01977     } while( false );
01978 
01979     return result;
01980 }
01981 
01982 /////////////////
01983 // End Of File //
01984 /////////////////

Generated on Mon Dec 5 18:22:01 2005 for CDB by doxygen1.3-rc3