00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbRooReadonly/CdbRooRoCdb.hh"
00012
00013 #include "CdbBase/CdbView.hh"
00014 #include "CdbBase/CdbDatabase.hh"
00015 #include "CdbBase/CdbEnvironment.hh"
00016 #include "CdbBase/CdbDebugStream.hh"
00017 #include "CdbBase/CdbAnyTypeDict.hh"
00018
00019 #include "CdbRooReadonly/CdbRooReadonly.hh"
00020 #include "CdbRooReadonly/CdbRooRoFileUtils.hh"
00021 #include "CdbRooReadonly/CdbRooRoDatabase.hh"
00022 #include "CdbRooReadonly/CdbRooRoDatabaseItr.hh"
00023 #include "CdbRooReadonly/CdbRooRoBootRecordR.hh"
00024
00025 #include "ErrLogger/ErrLog.hh"
00026
00027 #include "BbrStdUtils/Tokenize.hh"
00028 #include "BbrStdUtils/String.hh"
00029 using namespace babar::String;
00030
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033
00034 #include <string.h>
00035 #include <assert.h>
00036
00037 #include <iostream>
00038 #include <fstream>
00039 #include <map>
00040
00041 using std::ifstream;
00042 using std::cout;
00043 using std::cerr;
00044 using std::endl;
00045
00046 namespace {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 class CdbRooRoDummyTransaction : public CdbTransactionBase {
00057
00058 public:
00059
00060 CdbRooRoDummyTransaction( ) : CdbTransactionBase( ) { }
00061
00062 virtual ~CdbRooRoDummyTransaction( ) { }
00063
00064 virtual void commitAndHold( ) { }
00065 };
00066 }
00067
00068
00069
00070
00071
00072 CdbRooRoCdb::CdbRooRoCdb( ) :
00073 Cdb( ),
00074 _isInitialized (false),
00075 _defaultDatabaseName("<default>"),
00076 _defaultViewName ("<local>::<recent>")
00077 {
00078 CDB_DEBUG_STREAM
00079 << "Condition/DB API instantiated for"
00080 << " TECHNOLOGY=\"" << CdbRooReadonly::technology( ) << "\""
00081 << " IMPLEMENTATION=\"" << CdbRooReadonly::implementation( ) << "\""
00082 << endl;
00083
00084
00085
00086
00087
00088
00089
00090
00091 CdbPtr myPtr = this;
00092
00093 CdbStatus status = set( myPtr );
00094 assert( CdbStatus::Success == status );
00095 }
00096
00097 CdbRooRoCdb::~CdbRooRoCdb( )
00098 { }
00099
00100 const char*
00101 CdbRooRoCdb::technologyName( ) const
00102 {
00103 return CdbRooReadonly::technology( );
00104 }
00105
00106 const char*
00107 CdbRooRoCdb::implementationName( ) const
00108 {
00109 return CdbRooReadonly::implementation( );
00110 }
00111
00112 void
00113 CdbRooRoCdb::initialize( )
00114 {
00115 if( !_isInitialized ) {
00116
00117
00118
00119
00120
00121
00122 _isInitialized = true;
00123
00124
00125
00126 CdbStatus result = readBootFile( );
00127 if( CdbStatus::Success == result ) {
00128
00129
00130
00131 ;
00132
00133 } else if( CdbStatus::NotFound == result ) {
00134
00135
00136
00137 ;
00138
00139 } else {
00140
00141
00142
00143 cerr << "CdbRooRoCdb::initialize() -- WARNING." << endl
00144 << " Failed to read and/or translate the contents of the boot file. See details above." << endl;
00145 }
00146
00147
00148
00149
00150 {
00151 const char* var = "CDB_ROO_MAX_TTREES";
00152 const char* ptr = getenv( var );
00153 if( ptr ) {
00154 unsigned num = 0;
00155 if( 1 == sscanf( ptr, "%u", &num )) {
00156 const char* key = "Roo/Readonly/max_num_ttrees";
00157 if( CdbStatus::Success != CdbEnvironment::extra( ).replace( key, num )) {
00158 ErrMsg(fatal) << "CdbRooRoCdb::initialize( ) -- FATAL ERROR\n"
00159 << " Failed to set the parameter '" << key << "' = " << num << " in CdbEnvironment.\n"
00160 << " This is an unrecoverable error caused by an inconsistency in the CDB API Logic.\n"
00161 << " Please, report the problem to CDB API developers." << endmsg;
00162 }
00163 }
00164 CDB_DEBUG_STREAM
00165 << "Condition/DB API for"
00166 << " TECHNOLOGY=\"" << CdbRooReadonly::technology( ) << "\""
00167 << " IMPLEMENTATION=\"" << CdbRooReadonly::implementation( ) << "\""
00168 << " translated environment variable: " << var << " = " << num
00169 << endl;
00170 }
00171 }
00172 }
00173 }
00174
00175 bool
00176 CdbRooRoCdb::isInitialized( )
00177 {
00178 return _isInitialized;
00179 }
00180
00181 CdbStatus
00182 CdbRooRoCdb::getDefaultDatabase( std::string& theDatabaseName )
00183 {
00184 assert( isInitialized( ));
00185
00186 theDatabaseName = _defaultDatabaseName;
00187
00188 return CdbStatus::Success;
00189 }
00190
00191 CdbStatus
00192 CdbRooRoCdb::getDefaultView( std::string& theViewName,
00193 const char* theDatabaseName )
00194 {
00195 assert( isInitialized( ));
00196
00197 if( 0 == theDatabaseName ) return CdbStatus::IllegalParameters;
00198
00199
00200
00201
00202
00203
00204 if( 0 != strcmp( theDatabaseName, "<default>" )) {
00205 if( theDatabaseName != _defaultDatabaseName )
00206 return CdbStatus::NotFound;
00207 }
00208
00209
00210
00211 theViewName = _defaultViewName;
00212
00213 return CdbStatus::Success;
00214 }
00215
00216 CdbStatus
00217 CdbRooRoCdb::setDefaultDatabase( const char* theDatabaseName )
00218 {
00219 assert( isInitialized( ));
00220
00221 if( 0 == theDatabaseName ) return CdbStatus::IllegalParameters;
00222
00223
00224
00225
00226
00227
00228
00229 if( 0 == strcmp( theDatabaseName, "<default>" )) return CdbStatus::Success;
00230 if( theDatabaseName == _defaultDatabaseName ) return CdbStatus::Success;
00231
00232
00233
00234 return readBootFile( theDatabaseName );
00235 }
00236
00237 CdbStatus
00238 CdbRooRoCdb::setDefaultView( const char* theViewName,
00239 const char* theDatabaseName )
00240 {
00241 assert( isInitialized( ));
00242
00243 if( 0 == theViewName ) return CdbStatus::IllegalParameters;
00244 if( 0 == theDatabaseName ) return CdbStatus::IllegalParameters;
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 if( 0 != strcmp( theDatabaseName, "<default>" )) {
00258 if( theDatabaseName != _defaultDatabaseName )
00259 return CdbStatus::NotFound;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 _defaultViewName = theViewName;
00280
00281 return CdbStatus::Success;
00282 }
00283
00284 CdbStatus
00285 CdbRooRoCdb::findDatabase( CdbDatabasePtr& thePtr,
00286 const char* theName )
00287 {
00288 assert( isInitialized( ));
00289
00290 CdbStatus result = CdbStatus::Error;
00291
00292 thePtr = 0;
00293
00294 do {
00295
00296
00297
00298
00299
00300 std::string defaultDatabaseName;
00301 if( CdbStatus::Success != getDefaultDatabase( defaultDatabaseName )) break;
00302
00303 std::string databaseName = defaultDatabaseName;
00304 if( 0 != theName ) {
00305 if( 0 == strcmp( "<default>", theName )) {
00306
00307
00308
00309
00310 ;
00311
00312 } else {
00313 databaseName = theName;
00314 }
00315 }
00316
00317 if( databaseName != defaultDatabaseName ) {
00318
00319 cout << "CdbRooRoCdb::findDatabase( ) -- ERROR" << endl
00320 << " No such database in this implementation: \"" << databaseName << "\"" << endl
00321 << " The only name allowed is the default one: \"" << defaultDatabaseName << "\"" << endl;
00322
00323 break;
00324 }
00325
00326
00327
00328
00329 CdbPtr selfPtr;
00330 if( CdbStatus::Success != get( selfPtr,
00331 technologyName( ),
00332 implementationName( ))) {
00333
00334 cout << "CdbRooRoCdb::findDatabase( ) -- FATAL ERROR" << endl
00335 << " Failed to recover its own instance of the CDB API." << endl
00336 << " TECHNOLOGY : \"" << technologyName( ) << "\"" << endl
00337 << " IMPLEMENTATION : \"" << implementationName( ) << "\"" << endl;
00338
00339 assert( 0 );
00340
00341 break;
00342 }
00343 thePtr = new CdbRooRoDatabase( selfPtr,
00344 databaseName.c_str( ));
00345 result = CdbStatus::Success;
00346
00347 } while( false );
00348
00349 return result;
00350 }
00351
00352 CdbStatus
00353 CdbRooRoCdb::databaseIterator( CdbDatabaseItr& theItr )
00354 {
00355 assert( isInitialized( ));
00356
00357
00358
00359
00360
00361
00362 CdbPtr selfPtr;
00363 if( CdbStatus::Success != get( selfPtr,
00364 technologyName( ),
00365 implementationName( ))) {
00366
00367 cout << "CdbRooRoCdb::databaseIterator( ) -- FATAL ERROR" << endl
00368 << " Failed to recover its own instance of the CDB API." << endl
00369 << " TECHNOLOGY : \"" << technologyName( ) << "\"" << endl
00370 << " IMPLEMENTATION : \"" << implementationName( ) << "\"" << endl;
00371
00372 assert( 0 );
00373
00374 return CdbStatus::Error;
00375 }
00376 theItr = CdbDatabaseItr( new CdbRooRoDatabaseItr( selfPtr ));
00377
00378 return CdbStatus::Success;
00379 }
00380
00381 CdbTransactionBase*
00382 CdbRooRoCdb::transaction( CdbTransaction::Mode theMode ) const
00383 {
00384 return new CdbRooRoDummyTransaction( );
00385 }
00386
00387 CdbStatus
00388 CdbRooRoCdb::readBootFile( const char* theBootFileName )
00389 {
00390 const char* errorStr = "CdbRooRoCdb::readBootFile() -- ERROR.";
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 const char* bootFileName = theBootFileName;
00402 std::string location;
00403
00404 if( 0 != bootFileName ) {
00405 location = "the bootfile explicitly passed to the method";
00406 } else {
00407
00408 const char* variable = "CDB_ROO_BOOT";
00409 bootFileName = getenv( variable );
00410
00411 if( 0 != bootFileName ) {
00412 location = "the bootfile specified in the environment variable ";
00413 location += variable;
00414 } else {
00415 bootFileName = "con_cdb_boot.root";
00416 location = "default bootfile";
00417 }
00418 }
00419 assert( bootFileName );
00420 assert( !location.empty( ));
00421
00422 TFile bootFile( bootFileName, "read" );
00423 if( bootFile.IsZombie( )) {
00424 cerr << errorStr << endl
00425 << " Failed to open " << location << ".\n"
00426 << " File name: \"" << bootFileName << "\"" << endl;
00427 return CdbStatus::Error;
00428 }
00429
00430
00431
00432 CdbCPtr<CdbRooRoBootRecordR> bootRecordPtr;
00433 {
00434 CdbRooRoBootRecordR* ptr;
00435 bootFile.GetObject( CdbRooRoBootRecordR::PersistentObjectName( ), ptr );
00436 if( !ptr ) {
00437 cerr << errorStr << endl
00438 << " Failed to find/read the '" << CdbRooRoBootRecordR::PersistentObjectName( ) << "' object in the boot file\n"
00439 << " '" << bootFile.GetName( ) << "'" << endl;
00440 return CdbStatus::Error;
00441 }
00442 bootRecordPtr = ptr;
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456 CdbRooRoFileUtils::instance( ).reset( );
00457 CdbRooRoFileUtils::instance( ).setLocalOrigin( bootRecordPtr->localOriginName( ),
00458 bootRecordPtr->localOriginId( ));
00459 CdbRooRoFileUtils::instance( ).setBootFileLocation( bootFileName );
00460 CdbRooRoFileUtils::instance( ).setDatabase2LocationMap( bootRecordPtr->database2LocationMap( ));
00461 CdbRooRoFileUtils::instance( ).setObject2DatabaseMap( bootRecordPtr->object2DatabaseMap( ));
00462
00463
00464
00465 _defaultDatabaseName = bootFileName;
00466 _defaultViewName = bootRecordPtr->defaultViewName( );
00467
00468 return CdbStatus::Success;
00469 }
00470
00471
00472
00473