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

CdbBdbSimpleCache.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbSimpleCache.cc,v 1.6 2005/07/26 00:09:24 gapon Exp $
00003 #include "BaBar/BaBar.hh"
00004 
00005 /// The implementation of the CdbBdbSimpleCache.
00006 /**
00007   * @see CdbBdbSimpleCache
00008   */
00009 
00010 #include "BaBar/BaBar.hh"
00011 
00012 #include "CdbBdb/CdbBdbSimpleCache.hh"
00013 #include "CdbBase/CdbItr.hh"
00014 
00015 #include <assert.h>
00016 #include <stdio.h>
00017 using std::cerr;
00018 using std::endl;
00019 
00020 namespace {
00021 
00022   /// An iterator class for keys stored in the current cache
00023   /**
00024     * @see class CdbItr
00025     */
00026     class SimpleCacheItr : public CdbIItr< ooRef(ooObj) > {
00027 
00028     private:
00029 
00030       // The following c-tors and operators are useless
00031 
00032         SimpleCacheItr( );
00033         SimpleCacheItr( const SimpleCacheItr& theItr );
00034         SimpleCacheItr& operator=( const SimpleCacheItr& theItr );
00035 
00036     public:
00037 
00038         SimpleCacheItr( const std::map<std::string, ooRef(ooObj)>* theInputItr ) :
00039             _isValid            (false),
00040             _hasEverBeenAdvanced(false),
00041             _myInputItr         (theInputItr),
00042             _current            (theInputItr->end( ))
00043         { }
00044 
00045         virtual ~SimpleCacheItr( ) { }
00046 
00047         virtual CdbStatus reset( )
00048         {
00049             _isValid             = false;
00050             _hasEverBeenAdvanced = false;
00051 
00052             return CdbStatus::Success;
00053         }
00054 
00055         virtual bool next( )
00056         {
00057             if( _hasEverBeenAdvanced ) {
00058 
00059                 if( _isValid ) {
00060 
00061                     ++_current;
00062                     _isValid = ( _myInputItr->end( ) != _current );
00063 
00064                 } else {
00065 
00066                   // There is nothing we can do at this point
00067 
00068                     ;
00069                 }
00070 
00071             } else {
00072 
00073                 _current = _myInputItr->begin( );
00074                 _isValid = ( _myInputItr->end( ) != _current );
00075 
00076                 _hasEverBeenAdvanced = true;
00077             }
00078             return _isValid;
00079         }
00080 
00081         virtual ooRef(ooObj) value( )
00082         {
00083             if( !isValid( )) {
00084                cerr << "CdbBdbSimpleCache.cc::<anonymous>::SimpleCacheItr::value() -- inproper use of the iterator's method." << endl
00085                     << "The problem can be caused by a bug in the caller's code." << endl;
00086                ::abort( );
00087             }
00088             return CdbBdbSimpleCache::stringToRef( (*_current).first );
00089         }
00090 
00091         virtual bool isValid( )
00092         {
00093             return _isValid;
00094         }
00095 
00096         virtual CdbIItr< ooRef(ooObj) >* clone( ) const
00097         {
00098             return new SimpleCacheItr( _myInputItr );
00099         }
00100 
00101     private:
00102 
00103     private:
00104 
00105         bool _isValid;
00106         bool _hasEverBeenAdvanced;
00107 
00108         const std::map<std::string, ooRef(ooObj)>* _myInputItr;
00109 
00110         std::map<std::string, ooRef(ooObj)>::const_iterator _current;
00111     };
00112 };
00113 
00114 CdbBdbSimpleCache::CdbBdbSimpleCache( ) :
00115     CdbBdbCache<ooRef(ooObj)>( ),
00116         _dict( new std::map<std::string, ooRef(ooObj)>)
00117 { }
00118 
00119 CdbBdbSimpleCache::~CdbBdbSimpleCache( )
00120 {
00121     if( 0 != _dict ) delete _dict;
00122     _dict = 0;
00123 }
00124 
00125 void
00126 CdbBdbSimpleCache::addObject( const ooRef(ooObj)& theKeyRef,
00127                               const ooRef(ooObj)& theValueRef )
00128 {
00129     if( 0 == theKeyRef ) {
00130         cerr << "CdbBdbSimpleCache::addObject() - a null pointer passed instead of a reference to a key object." << endl;
00131         ::abort( );
00132     }
00133     if( 0 == theValueRef ) {
00134         cerr << "CdbBdbSimpleCache::addObject() - a null pointer passed instead of a reference to a value object." << endl;
00135         ::abort( );
00136     }
00137     (*_dict)[ refToString( theKeyRef ) ] = theValueRef;
00138 }
00139 
00140 ooRef(ooObj)
00141 CdbBdbSimpleCache::findObject( const ooRef(ooObj)& theKeyRef ) const
00142 {
00143     if( 0 == theKeyRef ) {
00144        cerr << "CdbBdbSimpleCache::findObject() - a null pointer passed instead of a reference to a key object." << endl;
00145        ::abort();
00146     }
00147 
00148     std::string key = refToString( theKeyRef );
00149 
00150     if( _dict->find( key ) != _dict->end( )) return (*_dict)[key];
00151     return 0;
00152 }
00153 
00154 CdbItr< ooRef(ooObj) >
00155 CdbBdbSimpleCache::iterator( ) const
00156 {
00157     return CdbItr< ooRef(ooObj) >( new SimpleCacheItr( _dict ));
00158 }
00159 
00160 std::string
00161 CdbBdbSimpleCache::refToString( const ooRef(ooObj)& theRef )
00162 {
00163     ooId oid = theRef;
00164 
00165     char buf[255];
00166     sprintf( buf,
00167              "%d-%d-%d-%d",
00168              oid.get_DB( ),
00169              oid.get_OC( ),
00170              oid.get_page( ),
00171              oid.get_slot( ));
00172 
00173     return std::string( buf );
00174 }
00175 
00176 ooRef(ooObj)
00177 CdbBdbSimpleCache::stringToRef( const std::string& theString )
00178 {
00179     unsigned int database;
00180     unsigned int container;
00181     unsigned int page;
00182     unsigned int slot;
00183 
00184     if( 4 != sscanf( theString.c_str( ),
00185                      "%u-%u-%u-%u",
00186                      &database,
00187                      &container,
00188                      &page,
00189                      &slot )) {
00190 
00191         cerr << "CdbBdbSimpleCache::stringToRef( ) -- the string can't be translated \"" << theString << "\"." << endl
00192              << "The cache may be corrupted." << endl;
00193 
00194         ::abort( );
00195     }
00196     if( database  > 0xFFFF || container <= 0xFFFF || page <= 0xFFFF || slot <= 0xFFFF ) {
00197 
00198         cerr << "CdbBdbSimpleCache::stringToRef( ) -- incorrect range of the OID's fields. They shouldn't exceed" << endl
00199              << "a maximum value of the unsigned 16-bit integer. The input string was \"" << theString << "\"." << endl
00200              << "The cache may be corrupted as well." << endl;
00201         ::abort( );
00202     }
00203     ooId oid;
00204 
00205     oid.set_DB( database );
00206     oid.set_OC( container);
00207     oid.set_page( page );
00208     oid.set_slot( slot );
00209 
00210     ooRef(ooObj) resultRef = oid;
00211 
00212     return resultRef;
00213 }
00214 /////////////////
00215 // End Of File //
00216 /////////////////

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