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

CdbRooRoIdCollectionR.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbRooRoIdCollectionR.cc,v 1.5 2005/11/11 01:41:26 gapon Exp $
00003 
00004 /// The implementation file for the CdbRooRoIdCollectionR class
00005 /**
00006   * @see CdbRooRoIdCollectionR
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbRooReadonly/CdbRooRoIdCollectionR.hh"
00012 #include "CdbRooReadonly/CdbRooRoFileUtils.hh"
00013 
00014 #include "CdbBase/CdbSimpleVectorAdapterItr.hh"
00015 
00016 #include "ErrLogger/ErrLog.hh"
00017 
00018 #include <algorithm>
00019 
00020 #include <iostream>
00021 using std::cout;
00022 using std::endl;
00023 
00024 template< class T >
00025 CdbRooRoIdCollectionR<T>::CdbRooRoIdCollectionR( ) :
00026     CdbRooRoPersistentCollectionR( )
00027 { }
00028 
00029 template< class T >
00030 CdbRooRoIdCollectionR<T>::CdbRooRoIdCollectionR( const CdbRooRoIdCollectionR<T>& theOther ) :
00031     CdbRooRoPersistentCollectionR( theOther ),
00032     _objects  (theOther._objects),
00033     _indexById(theOther._indexById)
00034 { }
00035 
00036 template< class T >
00037 CdbRooRoIdCollectionR<T>::~CdbRooRoIdCollectionR( )
00038 { }
00039 
00040 template< class T >
00041 CdbStatus
00042 CdbRooRoIdCollectionR<T>::add( const T& theObject )
00043 {
00044     const char* errorStr = "CdbRooRoIdCollectionR<T>::add() -- ERROR";
00045 
00046   // Adding elements to a collection which has already been stored (or fetched
00047   // from a database) is not allowed.
00048 
00049     if( isStored( )) return CdbStatus::Error;
00050 
00051   // Add the element into a transient cache. Also register the element
00052   // in the persistent index.
00053 
00054     if( _indexById.count( theObject.id( ))) return CdbStatus::Error;
00055 
00056     if( CdbStatus::Success != _objects.addElement( theObject )) {
00057 
00058         ErrMsg(error) << errorStr << endl
00059                       << "   Failed to add an object into a collection." << endmsg;
00060         return CdbStatus::Error;
00061     }
00062     _indexById[theObject.id( )] = _objects.size( ) - 1;
00063 
00064     return CdbStatus::Success;
00065 }
00066 
00067 template< class T >
00068 CdbStatus
00069 CdbRooRoIdCollectionR<T>::find( UShort_t theId,
00070                                 T&       theObject ) const
00071 {
00072     const char* fatalStr = "CdbRooRoIdCollectionR<T>::find(value) -- FATAL";
00073 
00074   // Check if an element with the specified identifier is known to
00075   // the collection and if so then get its index.
00076 
00077     typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.find( theId );
00078     if( itr == _indexById.end( )) return CdbStatus::NotFound;
00079 
00080     UInt_t index = itr->second;
00081 
00082   // Get the actual object
00083 
00084     if( CdbStatus::Success != _objects.elementAt( index,
00085                                                   theObject )) {
00086         ErrMsg(fatal) << fatalStr << endl
00087                       << "   Failed to find an object with the index " << index << " at the collection." << endl
00088                       << "   The internal structure of the class is inconsistent." << endmsg;
00089         return CdbStatus::Error;
00090     }
00091     return CdbStatus::Success;
00092 }
00093 
00094 template< class T >
00095 CdbStatus
00096 CdbRooRoIdCollectionR<T>::find( UShort_t      theId,
00097                                 CdbCPtr< T >& theObjectPtr ) const
00098 {
00099     const char* fatalStr = "CdbRooRoIdCollectionR<T>::find(pointer) -- FATAL";
00100 
00101   // Check if an element with the specified identifier is known to
00102   // the collection and if so then get its index.
00103 
00104     typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.find( theId );
00105     if( itr == _indexById.end( )) return CdbStatus::NotFound;
00106 
00107     UInt_t index = itr->second;
00108 
00109   // Get the actual object
00110 
00111     if( CdbStatus::Success != _objects.elementAt( index,
00112                                                   theObjectPtr )) {
00113         ErrMsg(fatal) << fatalStr << endl
00114                       << "   Failed to find an object with the index " << index << " at the collection." << endl
00115                       << "   The internal structure of the class is inconsistent." << endmsg;
00116         return CdbStatus::Error;
00117     }
00118     return CdbStatus::Success;
00119 
00120 }
00121 
00122 template< class T >
00123 void
00124 CdbRooRoIdCollectionR<T>::identifiers( std::vector<UShort_t>& theCollection ) const
00125 {
00126     theCollection.resize( 0 );
00127 
00128     for( typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.begin( );
00129                                                             itr != _indexById.end( );
00130                                                           ++itr ) {
00131         theCollection.push_back( itr->first );
00132     }
00133     std::sort( theCollection.begin( ),
00134                theCollection.end( ));
00135 }
00136 
00137 template< class T >
00138 CdbItr< UShort_t >
00139 CdbRooRoIdCollectionR<T>::iterator_identifiers( ) const
00140 {
00141     std::vector<UShort_t> vectorOfIdentifiers;
00142     identifiers( vectorOfIdentifiers );
00143     std::sort( vectorOfIdentifiers.begin( ),
00144                vectorOfIdentifiers.end( ));
00145 
00146   // ATTENTION: This is not a memory leak - the created object
00147   //            will be destroyed by the iterator.
00148 
00149     return CdbItr< UShort_t >( new CdbSimpleVectorAdapterItr< UShort_t >( vectorOfIdentifiers ));
00150 }
00151 
00152 template< class T >
00153 void
00154 CdbRooRoIdCollectionR<T>::dump( std::ostream&      o,
00155                                 const std::string& indent ) const
00156 {
00157     o << indent << "CdbRooRoIdCollectionR<" << T::Class( )->GetName( ) << "> {" << endl
00158       << indent << endl
00159       << indent << "  isStored( )                : " << isStored( ) << endl
00160       << indent << "  storedCollectionAddress( ) : " << storedCollectionAddress( ) << endl
00161       << indent << endl
00162       << indent << "  _objects.size()            : " << _objects.size( ) << endl
00163       << indent << "  _indexById.size()          : " << _indexById.size( ) << endl
00164       << indent << "}" << endl;
00165 }
00166 
00167 template< class T >
00168 CdbStatus
00169 CdbRooRoIdCollectionR<T>::storeSubCollectionsAt( const CdbRooRoCollectionAddressR& theCollectionAddress,
00170                                                  const CdbCPtr<TFile>&             theFilePtr,
00171                                                  Int_t&                            theNumBytesStored )
00172 {
00173     const char* errorStr = "CdbRooRoIdCollectionR<T>::storeSubCollectionsAt() -- ERROR";
00174     const char* fatalStr = "CdbRooRoIdCollectionR<T>::storeSubCollectionsAt() -- FATAL";
00175 
00176     Int_t numBytesStored = 0;
00177 
00178   // Verify the internal consistency of the collection's data structures. In particular
00179   // we want to make sure that the transient cache is consistent with the index(es).
00180 
00181     {
00182         const unsigned int nObjects         = _objects.size( );
00183         const unsigned int nPersistentIndex = _indexById.size( );
00184 
00185         if( nObjects != nPersistentIndex ) {
00186             ErrMsg(fatal) << fatalStr << endl
00187                           << "   A fatal internal error #1 has been found. Report the problem" << endl
00188                           << "   to the code developer." << endmsg;
00189             return CdbStatus::Error;
00190         }
00191         for( typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.begin( );
00192                                                                 itr != _indexById.begin( );
00193                                                               ++itr ) {
00194             UShort_t id    = itr->first;
00195             UInt_t   index = itr->second;
00196 
00197             if( index >= nObjects ) {
00198                 ErrMsg(fatal) << fatalStr << endl
00199                               << "   A fatal internal error #2 has been found. Report the problem" << endl
00200                               << "   to the code developer." << endmsg;
00201                 return CdbStatus::Error;
00202             }
00203             CdbCPtr<T> objectPtr;
00204             if( CdbStatus::Success != _objects.elementAt( index,
00205                                                           objectPtr )) {
00206                 ErrMsg(fatal) << fatalStr << endl
00207                               << "   A fatal internal error #3 has been found. Report the problem" << endl
00208                               << "   to the code developer." << endmsg;
00209                 return CdbStatus::Error;
00210             }
00211             if( id != objectPtr->id( )) {
00212                 ErrMsg(fatal) << fatalStr << endl
00213                               << "   A fatal internal error #4 has been found. Report the problem" << endl
00214                               << "   to the code developer." << endmsg;
00215                 return CdbStatus::Error;
00216             }
00217         }
00218     }
00219 
00220   // Store the embedded colletions.
00221 
00222     Int_t numBytes = 0;
00223 
00224     CdbStatus status = _objects.storeAsEmbeddedAt( theCollectionAddress,
00225                                                    theFilePtr,
00226                                                    numBytes );
00227     if( CdbStatus::Success != status ) {
00228 
00229         ErrMsg(error) << errorStr << endl
00230                       << "   Failed to store an embedded collection of objects." << endl
00231                       << "       File Name         : " << theFilePtr->GetName( ) << endl
00232                       << "       Collection Address: " << theCollectionAddress   << endl
00233                       << "       Error Status      : " << status
00234                       << endmsg;
00235         return status;
00236     }
00237     numBytesStored += numBytes;
00238 
00239   // Done
00240 
00241     theNumBytesStored = numBytesStored;
00242 
00243     return CdbStatus::Success;
00244 }
00245 
00246 /////////////////
00247 // End Of File //
00248 /////////////////

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