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

DbImplementation.cpp

Go to the documentation of this file.
00001 //====================================================================
00002 //      Basic implementation base classes
00003 //--------------------------------------------------------------------
00004 //
00005 //      Package    : DbCnv ( The LHCb Offline System)
00006 //
00007 //      Author     : M.Frank
00008 //====================================================================
00009 #define DBCNV_DBIMPLEMENTATION_CPP 1
00010 
00012 #include "GaudiDb/DbImplementation.h"
00013 #include "GaudiDb/DbSession.h"
00014 #include "GaudiDb/DbFederation.h"
00015 #include "GaudiDb/DbDataBase.h"
00016 
00017 #include "GaudiKernel/IFactory.h"
00018 #include "GaudiKernel/IMessageSvc.h"
00019 #include "GaudiKernel/ISvcLocator.h"
00020 #include "GaudiKernel/IObjManager.h"
00021 #include "GaudiKernel/IJobOptionsSvc.h"
00022 #include "GaudiKernel/GenericAddress.h"
00023 #include "GaudiKernel/PropertyMgr.h"
00024 
00025 // Standard Constructor
00026 DbImplementation::DbImplementation(IOODataBase* idb, 
00027                                    IMessageSvc* msg, 
00028                                    unsigned char typ, 
00029                                    const std::string& name)   {
00030   m_type = typ;
00031   m_ooDB = idb;
00032   m_name = name;
00033   m_messageSvc = msg;
00034   if ( m_messageSvc )    m_messageSvc->addRef();
00035   if ( m_ooDB       )    m_ooDB->addRef();
00036   if ( m_ooDB       )    m_driver = m_ooDB->driver();
00037   if ( m_ooDB       )    m_server = m_ooDB->server();
00038 }
00039 
00040 // Standard destructor
00041 DbImplementation::~DbImplementation()   {
00042   if ( m_ooDB       )    m_ooDB->release();
00043   if ( m_messageSvc )    m_messageSvc->release();
00044 }
00045 
00046 // Standard Constructor
00047 OODataBaseImplementation::OODataBaseImplementation(IInterface* loc, unsigned char typ)
00048   : m_refCount(0), 
00049     m_type(typ),
00050     m_name(""),
00051     m_driver("NoDriver"),
00052     m_server("(local)"),
00053     m_svcLocator(0),
00054     m_messageSvc(0),
00055     m_propertyMgr(0),
00056     m_objMgr(0)
00057 {
00058   if ( loc )    {
00059     loc->queryInterface(IID_ISvcLocator, (void**)&m_svcLocator);
00060     loc->queryInterface(IID_IObjManager, (void**)&m_objMgr);
00061   }
00062   m_propertyMgr = new PropertyMgr();
00063   declareProperty("Driver", m_driver);
00064   declareProperty("Server", m_server);
00065 }
00066 
00067 // Standard destructor
00068 OODataBaseImplementation::~OODataBaseImplementation()   {
00069   m_types.erase(m_types.begin(), m_types.end());
00070   if ( 0 != m_svcLocator ) m_svcLocator->release();
00071   if ( 0 != m_objMgr     ) m_objMgr->release();
00072   delete m_propertyMgr;
00073 }
00074 
00075 // Allow access to message service
00076 IMessageSvc* OODataBaseImplementation::msgSvc()   {
00077   return m_messageSvc;
00078 }
00079 
00080 // Allow access to message service (CONST)
00081 const IMessageSvc* OODataBaseImplementation::msgSvc()  const    {
00082   return m_messageSvc;
00083 }
00084 
00085 // IInterface implementation: Increase reference count
00086 unsigned long OODataBaseImplementation::addRef()    {
00087   return ++m_refCount;
00088 }
00089 
00090 // IInterface implementation: Decrease reference count
00091 unsigned long OODataBaseImplementation::release()   {
00092   long cnt = --m_refCount;
00093   if ( cnt <= 0 )   {
00094     delete this;
00095   }
00096   return cnt;
00097 }
00098 
00099 // IInterface implementation: Query interface
00100 StatusCode OODataBaseImplementation::queryInterface(const IID& riid, void** ppvInterface)  {
00101   if ( riid == IID_IInterface )   {
00102     *ppvInterface = (IInterface*)this;
00103   }
00104   else if ( riid == IID_IOODataBase )  {
00105     *ppvInterface = (IOODataBase*)this;
00106   }
00107   else if ( riid == IID_IProperty )  {
00108     *ppvInterface = (IProperty*)this;
00109   }
00110   else   {
00111     return NO_INTERFACE;
00112   }
00113   addRef();
00114   return SUCCESS;
00115 }
00116 
00117 // IOODataBase implementation: Access Db name
00118 const std::string& OODataBaseImplementation::name()   const   {
00119   return m_name;
00120 }
00121 
00122 // IOODataBase implementation: Access to OODataBase type
00123 unsigned char OODataBaseImplementation::type()  const   {
00124   return m_type;
00125 }
00126 
00127 // IOODataBase implementation: Access driver's name
00128 const std::string& OODataBaseImplementation::driver()  const    {
00129   return m_driver;
00130 }
00131 
00132 // IOODataBase implementation: Set driver's name
00133 void OODataBaseImplementation::setDriver(const std::string& drv)    {
00134   m_driver = drv;
00135 }
00136 
00137 // IOODataBase implementation: Access database server's name
00138 const std::string& OODataBaseImplementation::server()  const    {
00139   return m_server;
00140 }
00141 
00142 // IOODataBase implementation: Set database server's name
00143 void OODataBaseImplementation::setServer(const std::string& srv)    {
00144   m_server = srv;
00145 }
00146 
00147 // IOODataBase implementation: Initialize the database
00148 StatusCode OODataBaseImplementation::initialize       (const std::string& name)  {
00149   StatusCode status = StatusCode::FAILURE;
00150   m_name  =  name;
00151   m_name += ".DataBase";
00152   if ( 0 != m_svcLocator )    {
00153         status = m_svcLocator->getService( "MessageSvc", IID_IMessageSvc, (IInterface*&)m_messageSvc);
00154     if ( status.isSuccess() )   {
00155       setProperties();
00156     }
00157   }
00158   return status;
00159 }
00160 
00161 // IOODataBase implementation: Finalize the database
00162 StatusCode OODataBaseImplementation::finalize (void)   {
00163   return StatusCode::SUCCESS;
00164 }
00165 
00166 // Request type information to database (CONST)
00167 const DbTypeInfo* OODataBaseImplementation::typeInfo(const std::string& name) const  {
00168   DataTypes::const_iterator itm = m_types.find( name );
00169   if( itm != m_types.end() ) { // found name in the known list
00170     return (*itm).second;
00171   }
00172   return 0;
00173 }
00174 
00175 // Request type information to database
00176 DbTypeInfo* OODataBaseImplementation::typeInfo(const std::string& name)  {
00177   DataTypes::iterator itm = m_types.find( name );
00178   if( itm != m_types.end() ) { // found name in the known list
00179     return (*itm).second;
00180   }
00181   return 0;
00182 }
00183 
00184 // Add type information to database
00185 DbResult OODataBaseImplementation::addTypeInfo(const std::string& name, DbTypeInfo * info)   {
00186   std::pair<DataTypes::iterator, bool> p = m_types.insert(DataTypes::value_type( name, info) );
00187   if ( p.second )   {
00188     info->makeDescription();
00189     return DbOOMs::DbSuccess;
00190   }
00191   return DbOOMs::DbError;
00192 }
00193 
00194 // IOODataBase implementation: Create type information item for object binding
00195 DbTypeInfo* OODataBaseImplementation::addTypeInfo(const std::string& nam, const std::string& t_type, const std::string& p_type, const std::string& desc, std::vector<DbColumn*>& cols)    {
00196   DbTypeInfo* typ = 0;
00197   if ( 0 != m_objMgr )    {
00198     typ = typeInfo(nam);
00199     if ( 0 == typ )   {
00200       std::string ptyp(p_type), ttyp(t_type);
00201       if ( ptyp == "" ) ptyp = m_defPType;
00202       if ( ttyp == "" ) ttyp = m_defTType;
00203       std::string facT="DbFactory<"+ttyp+">";
00204       std::string facP="DbFactory<"+ptyp+">";
00205       const IFactory* fT = m_objMgr->objFactory(facT);
00206       const IFactory* fP = m_objMgr->objFactory(facP);
00207       if ( fT && fP )    {
00208         std::vector<DbColumn*> columns;
00209         DbTypeInfo::CreateT crT = (DbTypeInfo::CreateT)fT->instantiate((IInterface*)0);
00210         DbTypeInfo::CreateP crP = (DbTypeInfo::CreateP)fP->instantiate((IInterface*)1);
00211         DbTypeInfo::CreateC crC = (DbTypeInfo::CreateC)fP->instantiate((IInterface*)2);
00212         long t_size = long(fT->instantiate((IInterface*)11));
00213         long p_size = long(fP->instantiate((IInterface*)11));
00214         (*crC)(columns);
00215         for ( long i = 0, len = cols.size(); i < len; i++ )   {
00216           columns.push_back(cols[i]);
00217         }
00218         cols.erase(cols.begin(), cols.end());
00219         typ = new DbTypeInfo(crP, ptyp, p_size, crT, ttyp, t_size, nam, desc, m_type, columns);
00220         if ( addTypeInfo(nam, typ) )    {
00221           return typ;
00222         }
00223         delete typ;
00224         typ = 0;
00225       }
00226     }
00227   }
00228   return typ;
00229 }
00230 
00231 // IOODataBase implementation: Create type information item for object binding
00232 DbTypeInfo* OODataBaseImplementation::addTypeInfo(const IDbDataBase::Type* pType)    {
00233   DbTypeInfo* typ = DbTypeInfo::create(pType);
00234   if ( typ )    {
00235     DbTypeInfo* typ2 = typeInfo(typ->name());
00236     if ( typ2 )   {
00237       delete typ;
00238       return typ2;
00239     }
00240     if ( 0 != m_objMgr )    {
00241       std::string facT="DbFactory<"+typ->typeNameT()+">";
00242       std::string facP="DbFactory<"+typ->typeNameP()+">";
00243       const IFactory* fT = m_objMgr->objFactory(facT);
00244       const IFactory* fP = m_objMgr->objFactory(facP);
00245       if ( fT && fP )    {
00246         DbTypeInfo::CreateT crT = (DbTypeInfo::CreateT)fT->instantiate((IInterface*)0);
00247         DbTypeInfo::CreateP crP = (DbTypeInfo::CreateP)fP->instantiate((IInterface*)1);
00248         typ->setCreateT(crT);
00249         typ->setCreateP(crP);
00250         if ( addTypeInfo(typ->name(), typ) )      {
00251           return typ;
00252         }
00253       }
00254     }
00255     delete typ;
00256   }
00257   return 0;
00258 }
00259 
00260 // Prepare link between objects
00261 DbResult OODataBaseImplementation::prepareLink(const dbObjectHandle<DbObject>& objH, dbHandle<DbLink>& linkH, void* gen)   {
00262   unsigned char typ = objH.type();
00263   if ( typ == OBJY_StorageType ) {
00264     dbHandle<DbLink>::GENERIC* gene = (dbHandle<DbLink>::GENERIC*)gen;
00265     unsigned short* src = (unsigned short*)linkH.genericInfo()->m_info;
00266     unsigned short* tar = (unsigned short*)gene->m_info;
00267     for ( int i = 0; i < 4; i++ )   {
00268       tar[i] = src[i];
00269     }
00270     return DbOOMs::DbSuccess;
00271   }
00272   return DbOOMs::DbError;
00273 }
00274 
00275 // IProperty implementation
00276 // Delegate to the Property manager
00277 StatusCode OODataBaseImplementation::setProperty(const Property& p) {
00278         return m_propertyMgr->setProperty(p);
00279 }
00280 
00281 StatusCode OODataBaseImplementation::setProperty(std::istream& s) {
00282         return m_propertyMgr->setProperty(s);
00283 }
00284 
00285 StatusCode OODataBaseImplementation::setProperty(const std::string& n, const std::string& v) {
00286         return m_propertyMgr->setProperty(n,v);
00287 }
00288 
00289 StatusCode OODataBaseImplementation::getProperty(Property* p) const {
00290         return m_propertyMgr->getProperty(p);
00291 }
00292 
00293 const Property& OODataBaseImplementation::getProperty( const std::string& name) const  {
00294         return m_propertyMgr->getProperty(name);
00295 }
00296 
00297 StatusCode OODataBaseImplementation::getProperty(const std::string& n, std::string& v ) const {
00298         return m_propertyMgr->getProperty(n,v);
00299 }
00300 
00301 const std::vector<Property*>& OODataBaseImplementation::getProperties( ) const {
00302         return m_propertyMgr->getProperties();
00303 }
00304 
00305 // Use the job options service to set declared properties
00306 StatusCode OODataBaseImplementation::setProperties() {
00307         IJobOptionsSvc* jos = 0;
00308         StatusCode sc = m_svcLocator->getService( "JobOptionsSvc", IID_IJobOptionsSvc, 
00309                                             (IInterface*&) jos);
00310     if( !sc.isSuccess() )  return StatusCode::FAILURE;
00311 
00312     jos->setMyProperties( name(), this );
00313 
00314         return StatusCode::SUCCESS;
00315 }
00316 
00317 // Standard Constructor
00318 DbSessionImplementation::DbSessionImplementation(IOODataBase* idb, 
00319                                                  IMessageSvc* msg, 
00320                                                  unsigned char typ)
00321 : DbImplementation(idb, msg, typ)
00322 {
00323   m_state = DbOOMs::NOT_OPEN;
00324 }
00325 
00326 // Standard Destructor
00327 DbSessionImplementation::~DbSessionImplementation()   {
00328 }
00329 
00330 // Release instance (Abstract interfaces do not expose destructor!)
00331 void DbSessionImplementation::release()    {
00332   delete this;
00333 }
00334 
00335 // Get session implementation from handle
00336 IDbSession* DbSessionImplementation::getSession(const dbHandle<DbSession>& sessionH)    {
00337   IDbSession* ptr = 0;
00338   if ( sessionH.isValid() )     {
00339     ptr = sessionH.info();
00340   }
00341   return ptr;
00342 }
00343 
00344 // Standard Constructor
00345 DbFederationImplementation::DbFederationImplementation(IOODataBase* idb, 
00346                                                        IMessageSvc* msg, 
00347                                                        unsigned char typ)
00348 : DbImplementation(idb, msg, typ)
00349 {
00350 }
00351 
00352 // Standard Destructor
00353 DbFederationImplementation::~DbFederationImplementation()   {
00354 }
00355 
00356 // Release instance (Abstract interfaces do not expose destructor!)
00357 void DbFederationImplementation::release()    {
00358   delete this;
00359 }
00360 
00361 // Get Federation implementation from handle
00362 IDbFederation* DbFederationImplementation::getFederation(const dbHandle<DbFederation>& FederationH)    {
00363   IDbFederation* ptr = 0;
00364   if ( FederationH.isValid() )     {
00365     ptr = FederationH.info();
00366   }
00367   return ptr;
00368 }
00369 
00370 // Standard constructor
00371 DbDataBaseImplementation::DbDataBaseImplementation(IOODataBase* idb, 
00372                                                    IMessageSvc* msg, 
00373                                                    unsigned char typ)
00374 : DbImplementation(idb, msg, typ)
00375 {
00376 }
00377 
00378 // Standard Destructor
00379 DbDataBaseImplementation::~DbDataBaseImplementation()  {
00380 }
00381 
00382 // Release instance (Abstract interfaces do not expose destructor!)
00383 void DbDataBaseImplementation::release()    {
00384   delete this;
00385 }
00386 
00387 // Find container in database: to be overridden
00388 DbResult DbDataBaseImplementation::find(dbHandle<DbContainer>& /* handle */ )  {
00389   return DbOOMs::DbSuccess;
00390 }
00391 
00392 // Check two links for equality
00393 bool DbDataBaseImplementation::isEqual(const IDbDataBase::Link* link, const dbHandle<DbLink>* test)    {
00394   if ( link->clID() == test->clID() )   {
00395     const unsigned char  type      = link->svcType();
00396     const unsigned long* info      = test->genericInfo()->m_info;
00397     const unsigned long* link_info = link->genericInfo()->m_info;
00398     if ( type != OBJY_StorageType && type != TEST_StorageType )   {
00399       return info[0] == link_info[1];
00400     }
00401     else  {
00402       return info[0] == link_info[0] && info[1] == link_info[1];
00403     }
00404   }
00405   return false;
00406 }
00407 
00409 DbResult DbDataBaseImplementation::assocEntry(const std::vector<IDbDataBase::Link*>& links, const dbHandle<DbLink>& test, std::string& db, std::string& cnt, std::string& obj)   {
00410   const unsigned long* info = test.genericInfo()->m_info;
00411   const unsigned long  lnk  = info[0];
00412   if ( /* lnk >= 0 && */ lnk < links.size() )   {
00413     Link* link = links[lnk];
00414     bool result = DbDataBaseImplementation::isEqual(links[lnk], &test);
00415     if ( result )   {
00416       db  = link->dbase;
00417       cnt = link->container;
00418       obj = link->object;
00419       return DbOOMs::DbSuccess;
00420     }
00421     // If this does not help, check all other existing links...
00422     for( std::vector<IDbDataBase::Link*>::const_iterator i=links.begin(); i != links.end(); i++ )    {
00423       link = (*i);
00424       if ( isEqual(link, &test) )   {
00425         db  = link->dbase;
00426         cnt = link->container;
00427         obj = link->object;
00428         return DbOOMs::DbSuccess;
00429       }
00430     }
00431   }
00432   return DbOOMs::DbError;
00433 }
00434 
00435 // Get container implementation from handle
00436 IDbDataBase* DbDataBaseImplementation::getDataBase(const dbHandle<DbDataBase>& dbH)  {
00437   IDbDataBase* ptr = 0;
00438   if ( dbH.isValid() )     {
00439     ptr = dbH.info();
00440   }
00441   return ptr;
00442 }
00443 
00444 // Standard constructor
00445 DbContainerImplementation::DbContainerImplementation(IOODataBase* idb, 
00446                                                      IMessageSvc* msg, 
00447                                                      unsigned char typ)
00448 : DbImplementation(idb, msg, typ)
00449 {
00450 }
00451 
00452 // Standard destructor
00453 DbContainerImplementation::~DbContainerImplementation()    {
00454 }
00455 
00456 // Release instance (Abstract interfaces do not expose destructor!)
00457 void DbContainerImplementation::release()    {
00458   delete this;
00459 }
00460 
00461 // Remove object from container
00462 DbResult DbContainerImplementation::remove(dbHandle<DbContainer>& cntH, dbHandle<DbObject>& enclosed)    {
00463   return cntH.remove(enclosed);
00464 }
00465 
00466 // Get container implementation from handle
00467 IDbContainer* DbContainerImplementation::getContainer(const dbHandle<DbContainer>& cntH)  {
00468   IDbContainer* ptr = 0;
00469   if ( cntH.isValid() )     {
00470     ptr = cntH.info();
00471   }
00472   return ptr;
00473 }

Generated at Wed Nov 21 12:22:19 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000