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

CdbRooRoNameIdCollectionR.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbRooRoNameIdCollectionR.cc,v 1.10 2005/11/11 01:41:27 gapon Exp $
00003 
00004 /// The implementation file for the CdbRooRoNameIdCollectionR class
00005 /**
00006   * @see CdbRooRoNameIdCollectionR
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbRooReadonly/CdbRooRoNameIdCollectionR.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 CdbRooRoNameIdCollectionR<T>::CdbRooRoNameIdCollectionR( ) :
00026     CdbRooRoPersistentCollectionR( )
00027 { }
00028 
00029 template< class T >
00030 CdbRooRoNameIdCollectionR<T>::CdbRooRoNameIdCollectionR( const CdbRooRoNameIdCollectionR<T>& theOther ) :
00031     CdbRooRoPersistentCollectionR( theOther ),
00032     _objects    (theOther._objects),
00033     _indexById  (theOther._indexById),
00034     _indexByName(theOther._indexByName)
00035 { }
00036 
00037 template< class T >
00038 CdbRooRoNameIdCollectionR<T>::~CdbRooRoNameIdCollectionR( )
00039 { }
00040 
00041 template< class T >
00042 CdbStatus
00043 CdbRooRoNameIdCollectionR<T>::add( const T& theObject )
00044 {
00045     const char* errorStr = "CdbRooRoIdCollectionR<T>::add() -- ERROR";
00046 
00047   // Adding elements to a collection which has already been stored (or fetched
00048   // from a database) is not allowed.
00049 
00050     if( isStored( )) return CdbStatus::Error;
00051 
00052   // Add the element into a transient cache. Also register the element
00053   // in the persistent index.
00054 
00055     if( _indexById.count  ( theObject.id  ( ))) return CdbStatus::Error;
00056     if( _indexByName.count( theObject.name( ))) return CdbStatus::Error;
00057 
00058     if( CdbStatus::Success != _objects.addElement( theObject )) {
00059 
00060         ErrMsg(error) << errorStr << endl
00061                       << "   Failed to add an object into a collection." << endmsg;
00062         return CdbStatus::Error;
00063     }
00064     _indexById  [theObject.id  ( )] = _objects.size( ) - 1;
00065     _indexByName[theObject.name( )] = _objects.size( ) - 1;
00066 
00067     return CdbStatus::Success;
00068 }
00069 
00070 template< class T >
00071 CdbStatus
00072 CdbRooRoNameIdCollectionR<T>::find( UShort_t theId,
00073                                     T&       theObject ) const
00074 {
00075     const char* fatalStr = "CdbRooRoNameIdCollectionR<T>::find(id,value) -- FATAL";
00076 
00077   // Check if an element with the specified identifier is known to
00078   // the collection and if so then get its index.
00079 
00080     typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.find( theId );
00081     if( itr == _indexById.end( )) return CdbStatus::NotFound;
00082 
00083     UInt_t index = itr->second;
00084 
00085   // Get the actual object
00086 
00087     if( CdbStatus::Success != _objects.elementAt( index,
00088                                                   theObject )) {
00089         ErrMsg(fatal) << fatalStr << endl
00090                       << "   Failed to find an object with the index " << index << " at the collection." << endl
00091                       << "   The internal structure of the class is inconsistent." << endmsg;
00092         return CdbStatus::Error;
00093     }
00094     return CdbStatus::Success;
00095 }
00096 
00097 template< class T >
00098 CdbStatus
00099 CdbRooRoNameIdCollectionR<T>::find( UShort_t      theId,
00100                                     CdbCPtr< T >& theObjectPtr ) const
00101 {
00102     const char* fatalStr = "CdbRooRoNameIdCollectionR<T>::find(id,pointer) -- FATAL";
00103 
00104   // Check if an element with the specified identifier is known to
00105   // the collection and if so then get its index.
00106 
00107     typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.find( theId );
00108     if( itr == _indexById.end( )) return CdbStatus::NotFound;
00109 
00110     UInt_t index = itr->second;
00111 
00112   // Get the actual object
00113 
00114     if( CdbStatus::Success != _objects.elementAt( index,
00115                                                   theObjectPtr )) {
00116         ErrMsg(fatal) << fatalStr << endl
00117                       << "   Failed to find an object with the index " << index << " at the collection." << endl
00118                       << "   The internal structure of the class is inconsistent." << endmsg;
00119         return CdbStatus::Error;
00120     }
00121     return CdbStatus::Success;
00122 }
00123 
00124 template< class T >
00125 CdbStatus
00126 CdbRooRoNameIdCollectionR<T>::find( const std::string& theName,
00127                                     T&                 theObject ) const
00128 {
00129     const char* fatalStr = "CdbRooRoNameIdCollectionR<T>::find(name,value) -- FATAL";
00130 
00131   // Check if an element with the specified identifier is known to
00132   // the collection and if so then get its index.
00133 
00134     typename std::map<std::string,UInt_t>::const_iterator itr = _indexByName.find( theName );
00135     if( itr == _indexByName.end( )) return CdbStatus::NotFound;
00136 
00137     UInt_t index = itr->second;
00138 
00139   // Get the actual object
00140 
00141     if( CdbStatus::Success != _objects.elementAt( index,
00142                                                   theObject )) {
00143         ErrMsg(fatal) << fatalStr << endl
00144                       << "   Failed to find an object with the index " << index << " at the collection." << endl
00145                       << "   The internal structure of the class is inconsistent." << endmsg;
00146         return CdbStatus::Error;
00147     }
00148     return CdbStatus::Success;
00149 }
00150 
00151 template< class T >
00152 CdbStatus
00153 CdbRooRoNameIdCollectionR<T>::find( const std::string& theName,
00154                                     CdbCPtr< T >&      theObjectPtr  ) const
00155 {
00156     const char* fatalStr = "CdbRooRoNameIdCollectionR<T>::find(name,pointer) -- FATAL";
00157 
00158   // Check if an element with the specified identifier is known to
00159   // the collection and if so then get its index.
00160 
00161     typename std::map<std::string,UInt_t>::const_iterator itr = _indexByName.find( theName );
00162     if( itr == _indexByName.end( )) return CdbStatus::NotFound;
00163 
00164     UInt_t index = itr->second;
00165 
00166   // Get the actual object
00167 
00168     if( CdbStatus::Success != _objects.elementAt( index,
00169                                                   theObjectPtr )) {
00170         ErrMsg(fatal) << fatalStr << endl
00171                       << "   Failed to find an object with the index " << index << " at the collection." << endl
00172                       << "   The internal structure of the class is inconsistent." << endmsg;
00173         return CdbStatus::Error;
00174     }
00175     return CdbStatus::Success;
00176 }
00177 
00178 template< class T >
00179 void
00180 CdbRooRoNameIdCollectionR<T>::identifiers( std::vector<UShort_t>& theCollection ) const
00181 {
00182     theCollection.resize( 0 );
00183 
00184     for( typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.begin( );
00185                                                             itr != _indexById.end( );
00186                                                           ++itr ) {
00187         theCollection.push_back( itr->first );
00188     }
00189     std::sort( theCollection.begin( ),
00190                theCollection.end( ));
00191 }
00192 
00193 template< class T >
00194 void
00195 CdbRooRoNameIdCollectionR<T>::names( std::vector<std::string>& theCollection ) const
00196 {
00197     theCollection.resize( 0 );
00198 
00199     for( typename std::map<std::string,UInt_t>::const_iterator itr = _indexByName.begin( );
00200                                                                itr != _indexByName.end( );
00201                                                              ++itr ) {
00202         theCollection.push_back( itr->first );
00203     }
00204     std::sort( theCollection.begin( ),
00205                theCollection.end( ));
00206 }
00207 
00208 template< class T >
00209 CdbItr< UShort_t >
00210 CdbRooRoNameIdCollectionR<T>::iterator_identifiers( ) const
00211 {
00212     std::vector<UShort_t> vectorOfIdentifiers;
00213     identifiers( vectorOfIdentifiers );
00214     std::sort( vectorOfIdentifiers.begin( ),
00215                vectorOfIdentifiers.end( ));
00216 
00217   // ATTENTION: This is not a memory leak - the created object
00218   //            will be destroyed by the iterator.
00219 
00220     CdbItr< UShort_t > itr( new CdbSimpleVectorAdapterItr< UShort_t >( vectorOfIdentifiers ));
00221     return itr;
00222 }
00223 
00224 template< class T >
00225 CdbItr< std::string >
00226 CdbRooRoNameIdCollectionR<T>::iterator_names( ) const
00227 {
00228     std::vector<std::string> vectorOfNames;
00229     names( vectorOfNames );
00230     std::sort( vectorOfNames.begin( ),
00231                vectorOfNames.end( ));
00232 
00233   // ATTENTION: This is not a memory leak - the created object
00234   //            will be destroyed by the iterator.
00235 
00236     CdbItr< std::string > itr( new CdbSimpleVectorAdapterItr< std::string >( vectorOfNames ));
00237     return itr;
00238 }
00239 
00240 template< class T >
00241 void
00242 CdbRooRoNameIdCollectionR<T>::dump( std::ostream&      o,
00243                                     const std::string& indent ) const
00244 {
00245     o << indent << "CdbRooRoIdCollectionR<" << T::Class( )->GetName( ) << "> {" << endl
00246       << indent << endl
00247       << indent << "  isStored( )                : " << isStored( ) << endl
00248       << indent << "  storedCollectionAddress( ) : " << storedCollectionAddress( ) << endl
00249       << indent << endl
00250       << indent << "  _objects.size()            : " << _objects.size( ) << endl
00251       << indent << "  _indexById.size()          : " << _indexById.size( ) << endl
00252       << indent << "  _indexByName.size()        : " << _indexByName.size( ) << endl
00253       << indent << "}" << endl;
00254 }
00255 
00256 template< class T >
00257 CdbStatus
00258 CdbRooRoNameIdCollectionR<T>::storeSubCollectionsAt( const CdbRooRoCollectionAddressR& theCollectionAddress,
00259                                                      const CdbCPtr<TFile>&             theFilePtr,
00260                                                      Int_t&                            theNumBytesStored )
00261 {
00262     const char* errorStr = "CdbRooRoNameIdCollectionR<T>::storeSubCollectionsAt() -- ERROR";
00263     const char* fatalStr = "CdbRooRoNameIdCollectionR<T>::storeSubCollectionsAt() -- FATAL";
00264 
00265     Int_t numBytesStored = 0;
00266 
00267   // Verify the internal consistency of the collection's data structures. In particular
00268   // we want to make sure that the transient cache is consistent with the index(es).
00269 
00270     {
00271         const unsigned int nObjects               = _objects.size( );
00272         const unsigned int nPersistentIndexById   = _indexById.size( );
00273         const unsigned int nPersistentIndexByName = _indexByName.size( );
00274 
00275         if(( nObjects != nPersistentIndexById ) || ( nObjects != nPersistentIndexByName )) {
00276             ErrMsg(fatal) << fatalStr << endl
00277                           << "   A fatal internal error #1 has been found. Report the problem" << endl
00278                           << "   to the code developer." << endmsg;
00279             return CdbStatus::Error;
00280         }
00281         for( typename std::map<UShort_t,UInt_t>::const_iterator itr = _indexById.begin( );
00282                                                                 itr != _indexById.begin( );
00283                                                               ++itr ) {
00284             UShort_t id    = itr->first;
00285             UInt_t   index = itr->second;
00286 
00287             if( index >= nObjects ) {
00288                 ErrMsg(fatal) << fatalStr << endl
00289                               << "   A fatal internal error #2.1 has been found. Report the problem" << endl
00290                               << "   to the code developer." << endmsg;
00291                 return CdbStatus::Error;
00292             }
00293             CdbCPtr<T> objectPtr;
00294             if( CdbStatus::Success != _objects.elementAt( index,
00295                                                           objectPtr )) {
00296                 ErrMsg(fatal) << fatalStr << endl
00297                               << "   A fatal internal error #2.2 has been found. Report the problem" << endl
00298                               << "   to the code developer." << endmsg;
00299                 return CdbStatus::Error;
00300             }
00301             if( id != objectPtr->id( )) {
00302                 ErrMsg(fatal) << fatalStr << endl
00303                               << "   A fatal internal error #2.3 has been found. Report the problem" << endl
00304                               << "   to the code developer." << endmsg;
00305                 return CdbStatus::Error;
00306             }
00307         }
00308         for( typename std::map<std::string,UInt_t>::const_iterator itr = _indexByName.begin( );
00309                                                                    itr != _indexByName.begin( );
00310                                                                  ++itr ) {
00311             std::string name  = itr->first;
00312             UInt_t      index = itr->second;
00313 
00314             if( index >= nObjects ) {
00315                 ErrMsg(fatal) << fatalStr << endl
00316                               << "   A fatal internal error #3.1 has been found. Report the problem" << endl
00317                               << "   to the code developer." << endmsg;
00318                 return CdbStatus::Error;
00319             }
00320             CdbCPtr<T> objectPtr;
00321             if( CdbStatus::Success != _objects.elementAt( index,
00322                                                           objectPtr )) {
00323                 ErrMsg(fatal) << fatalStr << endl
00324                               << "   A fatal internal error #3.2 has been found. Report the problem" << endl
00325                               << "   to the code developer." << endmsg;
00326                 return CdbStatus::Error;
00327             }
00328             if( name != objectPtr->name( )) {
00329                 ErrMsg(fatal) << fatalStr << endl
00330                               << "   A fatal internal error #3.3 has been found. Report the problem" << endl
00331                               << "   to the code developer." << endmsg;
00332                 return CdbStatus::Error;
00333             }
00334         }
00335     }
00336 
00337   // Store the embedded colletions.
00338 
00339     Int_t numBytes = 0;
00340 
00341     CdbStatus status = _objects.storeAsEmbeddedAt( theCollectionAddress,
00342                                                    theFilePtr,
00343                                                    numBytes );
00344     if( CdbStatus::Success != status ) {
00345 
00346         ErrMsg(error) << errorStr << endl
00347                       << "   Failed to store an embedded collection of objects." << endl
00348                       << "       File Name         : " << theFilePtr->GetName( ) << endl
00349                       << "       Collection Address: " << theCollectionAddress   << endl
00350                       << "       Error Status      : " << status
00351                       << endmsg;
00352         return status;
00353     }
00354     numBytesStored += numBytes;
00355 
00356   // Done
00357 
00358     theNumBytesStored = numBytesStored;
00359 
00360     return CdbStatus::Success;
00361 }
00362 
00363 /////////////////
00364 // End Of File //
00365 /////////////////

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