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

CdbBdbMemoryUsageTest.cc

Go to the documentation of this file.
00001 /// A test of using transient memory in the ROOT I/O based implementation of the CDB API.
00002 /**
00003   * The application would browse the contents of CDB, find out all PID efficiency
00004   * conditions and load one object from them using "now" as the validity key.
00005   * The objects will be converted into a transient form and cached (in a transient form),
00006   * including metadata object CdbObject (it caches the persistent object).
00007   *
00008   * A goal of the test is to see how much memory this would require, and how
00009   * to improve the memory usage.
00010   */
00011 
00012 #include "BaBar/BaBar.hh"
00013 
00014 #include "CdbBase/CdbView.hh"
00015 #include "CdbBase/CdbFolder.hh"
00016 #include "CdbBase/CdbCondition.hh"
00017 #include "CdbBase/CdbObject.hh"
00018 #include "CdbBase/CdbTimeUtils.hh"
00019 #include "CdbBase/CdbEnvironment.hh"
00020 #include "CdbBase/CdbTransaction.hh"
00021 
00022 #include "CdbTable/CdbNTupleBase.hh"
00023 
00024 #include "CdbBdb/CdbBdbObjectConvertor.hh"
00025 
00026 #include "BdbCond/BdbObject.hh"
00027 
00028 #include "CdbBdbShared/CdbBdbShared.hh"
00029 
00030 #include "CdbBdbTable/CdbBdbNTupleSimpleImplP_ooFloat64.hh"
00031 
00032 #include <iostream>
00033 #include <string>
00034 using std::cout;
00035 using std::cerr;
00036 using std::endl;
00037 #include <vector>
00038 #include <map>
00039 
00040 #include <stdio.h>
00041 
00042 namespace {
00043 
00044   /// Loader for Objectivity/DB dictionary of persistent classes
00045   /**
00046     * Default constructors of the 'payload' classes to be loaded from CDB
00047     * needs to be executed at least one to bring in the corresponding dictionary.
00048     * Otherwise the loaded objects will have incomplete shape (no virtual tables
00049     * and subsequent crashes).
00050     */
00051     //void loadPayloadClassesDictionary( )
00052     //{
00053     //    const std::vector<std::string> columns;
00054     //    CdbBdbNTupleSimpleImplP<ooFloat64> tuple( 0, "", "", columns);
00055     //}
00056 
00057   // Find all know conditions and fill the vector with their full names.
00058 
00059     bool find_conditions( std::vector<std::string>&           theListOfConditions,
00060                           std::map<std::string,CdbFolderPtr>& theMapOfFolders,
00061                           const std::string&                  theFolderName );
00062 
00063     bool
00064     translate_int( int&        theValue,
00065                    const char* theBuf )
00066     {
00067         assert( 0 != theBuf );
00068 
00069         int v;
00070 
00071         if( 1 == sscanf( theBuf, "%d", &v )) {
00072             theValue = v;
00073             return true;
00074         }
00075         return false;
00076     }
00077 };
00078 
00079 int
00080 main( int argc, char* argv[] )
00081 {
00082     CdbBdbShared::forceLoad( );
00083 
00084   // Translate command-line parameters and set up default environment
00085 
00086     int numArgs = argc - 1;
00087     int nextArg = 1;
00088 
00089   // Start read-mode transaction and execute the found command
00090 
00091     CdbTransaction transaction;
00092 
00093   // Check if debug mode is present
00094 
00095     int debugMode = 0;
00096 
00097     if( numArgs >= 2 ) {
00098         if( 0 == strcmp( argv[nextArg], "-debug" )) {
00099             if( translate_int( debugMode, argv[nextArg+1] )) {
00100                 nextArg = nextArg + 2;
00101                 numArgs = numArgs - 2;
00102             } else {
00103                 cout << "Error:\n"
00104                      << "  Failed to translate '-debug <mode>' in a list of\n"
00105                      << "  command-line arguments." << endl;
00106                 return 1;
00107             }
00108         }
00109     }
00110     CdbEnvironment::setDebugMode( debugMode );
00111     CdbStatus result = CdbStatus::Error;
00112 
00113   // Browse the contents of this folder and all its subfolders to find out
00114   // all relevant conditions.
00115 
00116     std::vector<std::string>           listOfConditions;
00117     std::map<std::string,CdbFolderPtr> mapOfFolders;
00118 
00119     const std::string baseFolderName = "/physicstools/pid/";
00120 
00121     if( !find_conditions( listOfConditions,
00122                           mapOfFolders,
00123                           baseFolderName )) return 1;
00124 
00125     cout << "Found the following conditions:\n"
00126          << "\n";
00127 
00128     const unsigned int nConditions = listOfConditions.size( );
00129     for( unsigned int iCondition = 0; iCondition < nConditions; ++iCondition ) {
00130 //      cout << "  " << listOfConditions[iCondition] << "\n";
00131     }
00132     cout << "\n"
00133          << "Total of " << nConditions << " found.\n"
00134          << endl;
00135 
00136     CdbViewPtr viewPtr;
00137     if( CdbStatus::Success != ( result = CdbView::instance( viewPtr ))) {
00138         cerr << "Failed to find the default view because of: " << result << "." << endl;
00139         return 1;
00140     }
00141 
00142 /*
00143   // Cached objects
00144 
00145    std::map< std::string, CdbObjectPtr >             metaObjects;       // also caches a persistent object behind the scene
00146    std::map< std::string, CdbCPtr< CdbNTupleBase > > transientObjects;  // converted N-Tuple object
00147 */
00148 
00149   // Now browse through the list of found conditions and load objects at "now"
00150 
00151  //   const BdbTime now = BdbTime::now( );
00152     const BdbTime now = BdbTime( 2450894400, 0000000000 );
00153 
00154     unsigned int totalBytes = 0;
00155 
00156     for( unsigned int iCondition = 0; iCondition < nConditions; ++iCondition ) {
00157 
00158         CdbPathName fullPath( listOfConditions[iCondition] );
00159         CdbPathName folderPath( fullPath.beforeLast( ));
00160         CdbPathName conditionPath( fullPath.last( ));
00161 
00162         const std::string folderName    = folderPath.toString( ) + CdbPathName::separator( );
00163         const std::string conditionName = conditionPath.toString( ) ;
00164 
00165         CdbFolderPtr folderPtr = mapOfFolders[folderName];
00166         if( folderPtr.isNull( )) {
00167             cout << "no folder found in the map for: '" << folderName << "'" << endl;
00168             continue;
00169         }
00170 
00171         CdbConditionPtr conditionPtr;
00172         if( CdbStatus::Success != ( result = folderPtr->findCondition( conditionPtr,
00173                                                                        conditionName.c_str( )))) {
00174             cerr << "Failed to find the condition '" << folderName + conditionName << "' because of: " << result << "." << endl;
00175             return 1;
00176         }
00177         //if( CdbStatus::Success != ( result = CdbCondition::instance( conditionPtr,
00178         //                                                             conditionName.c_str( )))) {
00179         //    cerr << "Failed to find the condition \"" << folderName + conditionName << "\" because of: " << result << "." << endl;
00180         //    return 1;
00181         //}
00182         cout << "'" << conditionPtr->name( ) << "':" << endl;
00183 
00184         CdbObjectPtr objectPtr;
00185         if( CdbStatus::Success != ( result = conditionPtr->findObject( objectPtr,
00186                                                                        now ))) {
00187             cerr << "Failed to find an object from the condition '" << conditionName << "'\n"
00188                 << "at the validity point: " << CdbTimeUtils::time2string2( now ) << "\n"
00189                 << "because of: " << result << "." << endl;
00190             return 1;
00191         }
00192 
00193         BdbHandle(BdbObject) persistentObjectH;
00194         if( CdbStatus::Success != ( result = CdbBdbObjectConvertor::narrow( persistentObjectH,
00195                                                                             objectPtr ))) {
00196             cerr << "Failed to narrow the found API object into the corresponding persistent one" << endl
00197                  << "because of the following error: " << result << endl;
00198             return 1;
00199         }
00200 
00201       //// Try to get to the actual class specific contents of the found payload object
00202 
00203       //  const std::string className = persistentObjectH.typeName( );
00204       //  cout << iCondition << " : '" << className << "'";
00205 
00206       //  if( className == CdbRooNTupleSimpleImplR<Double_t>::Class( )->GetName( )) {
00207       //      const CdbCPtr< CdbRooNTupleSimpleImplR<Double_t> >& testObjectPtr
00208       //          = (const CdbCPtr< CdbRooNTupleSimpleImplR<Double_t> >&)persistentObjectPtr;
00209       //      unsigned int bytes = sizeof( Double_t ) * testObjectPtr->columns( ) * testObjectPtr->rows( );
00210       //      totalBytes += bytes;
00211       //      cout << " : " << bytes << " : " << totalBytes;
00212       //  } else {
00213       //      cerr << " : * : *";
00214       //  }
00215       //  cout << endl;
00216     }
00217     return 0;
00218 }
00219 
00220 namespace {
00221 
00222     bool
00223     find_conditions( std::vector<std::string>&           theListOfConditions,
00224                      std::map<std::string,CdbFolderPtr>& theMapOfFolders,
00225                      const std::string&                  theFolderName )
00226     {
00227         CdbStatus result = CdbStatus::Error;
00228 
00229       // Find the base folder itself
00230 
00231         CdbFolderPtr folderPtr;
00232         if( CdbStatus::Success != ( result = CdbFolder::instance( folderPtr,
00233                                                                   theFolderName.c_str( )))) {
00234             cerr << "Failed to find the folder '" << theFolderName << "'\n"
00235                  << "because of: " << result << "." << endl;
00236             return false;
00237         }
00238         theMapOfFolders[theFolderName] = folderPtr;
00239         //cout << "added folder to the map: '" << theFolderName << "'" << endl;
00240 
00241       // Find all conditions in that folder and register them in the vector
00242 
00243         CdbConditionItr conditionItr;
00244         if( CdbStatus::Success != folderPtr->conditionIterator( conditionItr )) {
00245             cerr << "Failed to set up an iterator of conditions within the folder '" << theFolderName << "'\n"
00246                  << "because of: " << result << "." << endl;
00247             return false;
00248         }
00249         while( conditionItr.next( )) {
00250             const std::string conditionName = conditionItr.value( );
00251             theListOfConditions.push_back( theFolderName + conditionName );
00252         }
00253 
00254       // Find all sub-folders in that folder and propagate the operation down to them
00255 
00256         CdbFolderItr folderItr;
00257         if( CdbStatus::Success != folderPtr->folderIterator( folderItr )) {
00258             cerr << "Failed to set up an iterator of sub-folders within the folder '" << theFolderName << "'\n"
00259                  << "because of: " << result << "." << endl;
00260             return false;
00261         }
00262         while( folderItr.next( ))
00263             if( !find_conditions( theListOfConditions,
00264                                   theMapOfFolders,
00265                                   theFolderName + folderItr.value( ) + std::string( "/" )));
00266 
00267         return true;
00268     }
00269 }
00270 
00271 /////////////////
00272 // End Of File //
00273 /////////////////

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