Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

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

/CdbBdbShared/CdbBdbSUtils.cc

Go to the documentation of this file.
00001 /// Implementation file for the CdbBdbSUtils lass
00002 /**
00003   * @see CdbBdbSCell
00004   */
00005 
00006 #include "BaBar/BaBar.hh"
00007 
00008 #include "CdbBdbShared/CdbBdbSUtils.hh"
00009 
00010 #include "BdbApplication/BdbDomain.hh"
00011 #include "BdbDistribution/BdbStandardDbIdMgr.hh"
00012 #include "BdbDistribution/BdbDbIdAlloc.hh"
00013 #include "BdbDistribution/BdbFdName.hh"
00014 
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <stdio.h>
00018 #include <assert.h>
00019 
00020 std::string
00021 CdbBdbSUtils::_localDbIdRange = "";
00022 
00023 CdbStatus
00024 CdbBdbSUtils::getLocalDbIdRange( std::string& theName )
00025 {
00026   // WARNING: The method is using the cached value of the range, which might not
00027   //          work if there is more than one Objectivity context for the Condition/DB.
00028 
00029     CdbStatus result = CdbStatus::NotFound;
00030     do {
00031 
00032       // Still need to load the cache?
00033 
00034         if ( _localDbIdRange == "" ) {
00035 
00036           // The following operations are executed withing a separate context
00037           // in order not to leave locks 
00038 
00039             BdbStatus status;
00040 
00041             status = BdbApplicationOrDomain::activeInstance( )->startNestedRead( );
00042             if( status != BdbcSuccess ) {
00043                 assert( 0 );
00044                 result = CdbStatus::Error;
00045                 break;
00046             }
00047             {
00048                 BdbHandle(BdbFdName) theFdNameH;
00049                 theFdNameH = BdbFdName::localInstance( BdbApplicationOrDomain::activeInstance()->fd( ));
00050 
00051                 const char* rangeName = BdbStandardDbIdMgr::findKey( theFdNameH, "con" );
00052                 if ( 0 != rangeName ) _localDbIdRange = rangeName;
00053             }
00054             status = BdbApplicationOrDomain::activeInstance( )->commitNested( );
00055             if( status != BdbcSuccess ) {
00056                 assert( 0 );
00057                 result = CdbStatus::Error;
00058                 break;
00059             }
00060         }
00061         if ( _localDbIdRange != "" ) {
00062             theName = _localDbIdRange;
00063             result = CdbStatus::Success;
00064         }
00065 
00066     } while( false );
00067 
00068     return result;
00069 }
00070 
00071 CdbStatus
00072 CdbBdbSUtils::verifyDbIdRange( const char* theName )
00073 {
00074     CdbStatus result = CdbStatus::NotFound;
00075     do {
00076 
00077         if( 0 != theName ) {
00078 
00079           // The following operations are executed withing a separate context
00080           // in order not to leave locks 
00081 
00082             BdbStatus status;
00083 
00084             status = BdbApplicationOrDomain::activeInstance( )->startNestedRead( );
00085             if( status != BdbcSuccess ) {
00086                 assert( 0 );
00087                 result = CdbStatus::Error;
00088                 break;
00089             }
00090             {
00091                 BdbHandle(BdbDbIdAlloc) allocH;
00092                 allocH = BdbDbIdAlloc::localInstance( BdbApplicationOrDomain::activeInstance()->fd( ));
00093                 if( ! BdbIsNull(allocH)) {
00094 
00095                     char** aList = 0;
00096                     size_t aNum  = 0;
00097                     
00098                     aNum = allocH->keys( aList );
00099                     if(( 0 != aList ) && ( 0 != aNum )) {
00100                         for( size_t i = 0; i < aNum; ++i ) {
00101                             if( 0 == strcmp( theName, aList[i] )) result = CdbStatus::Success;
00102                             delete [] aList[i]; // Don't need this anymore
00103                         }
00104                         delete [] aList;    // Don't need the list anymore
00105                     }
00106                 }
00107             }
00108             status = BdbApplicationOrDomain::activeInstance( )->commitNested( );
00109             if( status != BdbcSuccess ) {
00110                 assert( 0 );
00111                 result = CdbStatus::Error;
00112                 break;
00113             }
00114         }
00115 
00116     } while( false );
00117 
00118     return result;
00119 }
00120 
00121 CdbStatus
00122 CdbBdbSUtils::findContainer( BdbHandle(BdbContObj)& theContH,
00123                              const char*            theDatabaseName,
00124                              const char*            theContainerName,
00125                              const BdbMode          theOpenMode )
00126 {
00127     const char* errorStr = "CdbBdbSUtils::findContainer() - ERROR.";
00128 
00129     CdbStatus result = CdbStatus::NotFound;
00130     do {
00131 
00132       // Find the database
00133 
00134         BdbHandle(BdbDBObj) dH;
00135 
00136         if( CdbStatus::Success != findDatabase( dH,
00137                                                 theDatabaseName,
00138                                                 BdbcRead )) {
00139             break;
00140         }
00141 
00142       // Find the container.
00143 
00144         BdbStatus status;
00145 
00146         BdbHandle(BdbContObj) cH;
00147 
00148         if( d_True != cH.exist( dH,
00149                                 theContainerName,
00150                                 BdbcNoOpen )) {
00151             break;
00152         }
00153         status = cH.open( dH,
00154                           theContainerName,
00155                           theOpenMode );
00156         if(( BdbcSuccess != status ) || BdbIsNull(cH)) {
00157             cout << errorStr << endl
00158                  << "    Failed to open the container: \"" << theContainerName << "\"" << endl
00159                  << "    at the database:              \"" << theDatabaseName << "\"" << endl;
00160             result = CdbStatus::Error;
00161             break;
00162         }
00163 
00164       // Done
00165 
00166         theContH = cH;
00167 
00168         result = CdbStatus::Success;
00169 
00170     } while( false );
00171 
00172     return result;
00173 }
00174 
00175 CdbStatus
00176 CdbBdbSUtils::findDatabase( BdbHandle(BdbDBObj)& theDatabaseH,
00177                             const char*          theDatabaseName,
00178                             const BdbMode        theOpenMode )
00179 {
00180     const char* errorStr = "CdbBdbSUtils::findDatabase() - ERROR.";
00181 
00182     CdbStatus result = CdbStatus::NotFound;
00183     do {
00184 
00185         BdbStatus status;
00186 
00187       // Find the database
00188 
00189         BdbHandle(BdbDBObj) dH;
00190 
00191         if( d_True != dH.exist( BdbApplicationOrDomain::activeInstance()->fd( ),
00192                                 theDatabaseName,
00193                                 BdbcNoOpen )) {
00194             break;
00195         }
00196         status = dH.open( BdbApplicationOrDomain::activeInstance()->fd( ),
00197                           theDatabaseName,
00198                           theOpenMode );
00199         if(( BdbcSuccess != status ) || BdbIsNull(dH)) {
00200             cout << errorStr << endl
00201                  << "    Failed to open the database: \"" << theDatabaseName << "\"" << endl;
00202             result = CdbStatus::Error;
00203             break;
00204         }
00205 
00206       // Done
00207 
00208         theDatabaseH = dH;
00209 
00210         result = CdbStatus::Success;
00211 
00212     } while( false );
00213 
00214     return result;
00215 }
00216 
00217 CdbStatus
00218 CdbBdbSUtils::findMetaData( BdbRef(CdbBdbSMetaDataP)& theRef,
00219                             const d_UShort            theConditionId,
00220                             bool                      isPartitionableFlag,
00221                             d_UShort                  thePartitionId,
00222                             d_UShort                  theClusterId,
00223                             d_UShort                  theIncrementNumber,
00224                             const std::string&        theDbIdRange )
00225 {
00226     const char* errorStr = "CdbBdbSUtils::findMetaData() -- ERROR";
00227 
00228     CdbStatus result = CdbStatus::Error;
00229     do {
00230 
00231       // Build the database name
00232 
00233         std::string databaseName( "con_cdb" );
00234 
00235         if( isPartitionableFlag ) {
00236 
00237             char numberStr[12];
00238             sprintf( numberStr, "%u", thePartitionId );
00239 
00240             databaseName = databaseName +
00241                            std::string( "_p" ) +
00242                            std::string( numberStr );
00243         }
00244         {
00245             char numberStr[12];
00246             sprintf( numberStr, "%u", theClusterId );
00247 
00248             databaseName = databaseName +
00249                            std::string( "_c" ) +
00250                            std::string( numberStr );
00251         }
00252         {
00253             char numberStr[12];
00254             sprintf( numberStr, "%u", theIncrementNumber );
00255 
00256             databaseName = databaseName +
00257                            std::string( "_i" ) +
00258                            std::string( numberStr );
00259         }
00260         databaseName = databaseName +
00261                        std::string( "_" ) +
00262                        theDbIdRange;
00263 
00264       // Build the container name
00265 
00266         char cIdStr[12];
00267         sprintf( cIdStr, "%u", theConditionId );
00268 
00269         std::string mdContainerName = std::string( "MetaData." ) +
00270                                       std::string( cIdStr );
00271 
00272       // Try to locate the container and the MetaData object in this
00273       // container.
00274 
00275         BdbHandle(BdbContObj) mdContH;
00276         if( CdbStatus::Success != CdbBdbSUtils::findContainer( mdContH,
00277                                                                databaseName.c_str( ),
00278                                                                mdContainerName.c_str( ))) {
00279             cout << errorStr << endl
00280                  << "    Failed to locate/open a persistent container with meta-data." << endl
00281                  << "    The database may not be properly initialized/loaded." << endl
00282                  << "        CONTAINER NAME:     \"" << mdContainerName.c_str( ) << "\"" << endl
00283                  << "        DATABASE FILE NAME: \"" << databaseName.c_str( ) << "\"" << endl;
00284             break;
00285         }
00286 
00287         BdbHandle(CdbBdbSMetaDataP) mdH;
00288         if( BdbcSuccess != mdH.lookupObj( mdContH,
00289                                           "MetaData" )) {
00290             cout << errorStr << endl
00291                  << "    Failed to locate the \"MetaData\" registry object in the container." << endl
00292                  << "    The database may not be properly initialized/loaded." << endl
00293                  << "        CONTAINER NAME:     \"" << mdContainerName.c_str( ) << "\"" << endl
00294                  << "        DATABASE FILE NAME: \"" << databaseName.c_str( ) << "\"" << endl;
00295             break;
00296         }
00297         assert( mdH->ooIsKindOf( ooTypeN( CdbBdbSMetaDataP )));
00298 
00299       // Done.
00300 
00301         theRef = mdH;
00302 
00303         result = CdbStatus::Success;
00304 
00305     } while( false );
00306 
00307     return result;
00308 }
00309 
00310 /////////////////
00311 // End Of File //
00312 /////////////////

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002