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

CdbSimplestWriteTest.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbSimplestWriteTest.cc,v 1.14 2005/02/02 15:25:40 gapon Exp $
00003 
00004 /// A simplest possible test exploring the API of the Condition/DB
00005 /**
00006   * This application will write a simple condition object into a database.
00007   * Note the following simplifications:
00008   *
00009   * - default instance (technology/implementation) of Cdb API is used.
00010   * - default database is assumed
00011   * - default view is assumed
00012   */
00013 #include "BaBar/BaBar.hh"
00014 
00015 #include "BdbTime/BdbTime.hh"
00016 
00017 #include "CdbBase/Cdb.hh"
00018 #include "CdbBase/CdbEnvironment.hh"
00019 #include "CdbBase/CdbCondition.hh"
00020 #include "CdbBase/CdbObject.hh"
00021 #include "CdbBase/CdbTimeUtils.hh"
00022 #include "CdbBase/CdbTransaction.hh"
00023 
00024 #include "CdbBdb/CdbBdbTObjectFactory.hh"
00025 #include "CdbBdb/CdbBdbObjectConvertor.hh"
00026 
00027 #include "CdbBdbWrapper/CdbBdbWrapper.hh"
00028 #include "CdbBdbShared/CdbBdbShared.hh"
00029 
00030 #include "BdbCond/BdbDataListsP.hh"
00031 #include "BdbCond/BdbConditions.hh"
00032 
00033 #include "BdbApplication/BdbCondFSNamesStr.hh"
00034 using std::cin;
00035 using std::cout;
00036 using std::endl;
00037 
00038 //////////////
00039 // Commands //
00040 //////////////
00041 
00042 static void cmd_usage( );
00043 
00044 static void cmd_write( const char*    theCondition,
00045                        const BdbTime& theBeginTime,
00046                        const BdbTime& theEndTime );
00047 
00048 /////////////
00049 // Helpers //
00050 /////////////
00051 
00052 static void translate_and_set_defaults( char* argv[],
00053                                         int&  numArgs,
00054                                         int&  nextArg );
00055 
00056 int
00057 main( int argc, char** argv )
00058 {
00059     if( argc <= 1 ) {
00060         cmd_usage( );
00061         return 0;
00062     } else if( argc >= 2 ) {
00063         if(( 0 == strcmp( argv[1], "-h" )) || ( 0 == strcmp( argv[1], "help" ))) {
00064             cmd_usage( );
00065             return 0;
00066         }
00067     }
00068 
00069   // Make sure the proper libraries are linked and factories
00070   // are instantiated.
00071 
00072     CdbBdbWrapper::forceLoad( );
00073     CdbBdbShared::forceLoad( );
00074 
00075   // Translate command-line parameters and set up default environment
00076 
00077     int numArgs = argc - 1;
00078     int nextArg = 1;
00079 
00080   // Make sure the transaction is in the right mode
00081 
00082     CdbTransaction transaction( CdbTransaction::Update );
00083 
00084   // Check for the environment parameters first. Apply them immediatelly
00085   // if found.
00086   //
00087   // IMPORTANT: Dealing with defaults requires an open transaction since
00088   //            certain operations may require information loading from
00089   //            a persistent store.
00090 
00091     translate_and_set_defaults( argv,
00092                                 numArgs,
00093                                 nextArg );
00094 
00095   // Trunslate commands
00096 
00097     const char* condition = 0;
00098 
00099     BdbTime beginTime( BdbTime::now( ));
00100     BdbTime endTime  ( BdbTime::plusInfinity );
00101 
00102     enum { COMMAND_WRITE } command;
00103 
00104     if( numArgs >= 1 ) {
00105 
00106         if( 0 == strcmp( "write", argv[nextArg] )) {
00107 
00108             command = COMMAND_WRITE;
00109 
00110             numArgs = numArgs - 1;
00111             nextArg = nextArg + 1;
00112 
00113             if( numArgs >= 1 ) {
00114 
00115                 condition = argv[nextArg];
00116 
00117                 numArgs = numArgs - 1;
00118                 nextArg = nextArg + 1;
00119 
00120             } else {
00121 
00122                 cout << "Illegal syntax of command parameters. Condition name (path)" << endl
00123                      << "was expected." << endl;
00124 
00125                 cmd_usage( );
00126 
00127                 return 1;
00128             }
00129 
00130         } else {
00131             cout << "Illegal command. A command name was expected." << endl;
00132             cmd_usage( );
00133             return 1;
00134         }
00135     }
00136     if( numArgs >= 2 ) {
00137         if( 0 == strcmp( argv[nextArg], "-begin" )) {
00138 
00139             numArgs = numArgs - 1;
00140             nextArg = nextArg + 1;
00141 
00142             if( numArgs < 1 ) {
00143                 cout << "Illegal syntax of command parameters. Begin time value" << endl
00144                      << "was expected." << endl;
00145                 cmd_usage( );
00146                 return 1;
00147             }
00148             if( !CdbTimeUtils::string2time( beginTime, argv[nextArg] )) {
00149                 cout << "Failed to translate the begin time." << endl;
00150                 cmd_usage( );
00151                 return 1;
00152             }
00153 
00154             numArgs = numArgs - 1;
00155             nextArg = nextArg + 1;
00156         }
00157     }
00158     if( numArgs >= 2 ) {
00159         if( 0 == strcmp( argv[nextArg], "-end" )) {
00160 
00161             numArgs = numArgs - 1;
00162             nextArg = nextArg + 1;
00163 
00164             if( numArgs < 1 ) {
00165                 cout << "Illegal syntax of command parameters. End time value" << endl
00166                      << "was expected." << endl;
00167                 cmd_usage( );
00168                 return 1;
00169             }
00170             if( !CdbTimeUtils::string2time( endTime, argv[nextArg] )) {
00171                 cout << "Failed to translate the end time." << endl;
00172                 cmd_usage( );
00173                 return 1;
00174             }
00175 
00176             numArgs = numArgs - 1;
00177             nextArg = nextArg + 1;
00178         }
00179     }
00180     if( numArgs >= 2 ) {
00181         if( 0 == strcmp( argv[nextArg], "-truncate_time" )) {
00182 
00183             numArgs = numArgs - 1;
00184             nextArg = nextArg + 1;
00185 
00186             if( numArgs < 1 ) {
00187                 cout << "Illegal syntax of command parameters. Truncate time value" << endl
00188                      << "was expected." << endl;
00189                 cmd_usage( );
00190                 return 1;
00191             }
00192             BdbTime truncateTime( 0 );
00193             if( ! CdbTimeUtils::string2time( truncateTime, argv[nextArg] )) {
00194                 cout << "Failed to translate the truncate time." << endl;
00195                 cmd_usage( );
00196                 return 1;
00197             }
00198 
00199             numArgs = numArgs - 1;
00200             nextArg = nextArg + 1;
00201 
00202           // Apply truncate time immediatelly. This operation does not imply
00203           // any manipulations with a persistent store.
00204 
00205             CdbEnvironment::setTruncateTime( truncateTime );
00206         }
00207     }
00208     if( numArgs > 0 ) {
00209         cout << "Illegal syntax or number of command parameters." << endl;
00210         cmd_usage( );
00211         return 1;
00212     }
00213 
00214   // Proceed to the required test
00215 
00216     switch( command ) {
00217 
00218     case COMMAND_WRITE:
00219 
00220         cmd_write( condition,
00221                    beginTime,
00222                    endTime );
00223         break;
00224     }
00225 
00226   // Just to check if it works
00227 
00228     transaction.commitAndHold( );
00229 
00230     return 0;
00231 }
00232 
00233 static
00234 void
00235 cmd_usage( )
00236 {
00237     cout << "Usage:" << endl
00238          << "    -h" << endl
00239          << "    help" << endl
00240          << "    [-t <tech>] [-i <impl>] [-d <db>] [-v <view>] write <condition>        [-begin <time>] [-end <time>] [-truncate_time]" << endl
00241          << "    [-t <tech>] [-i <impl>] [-d <db>] [-v <view>] write <physical_address> [-begin <time>] [-end <time>] [-truncate_time]" << endl
00242          << "    [-t <tech>] [-i <impl>] [-d <db>] [-v <view>] write <physical_name>    [-begin <time>] [-end <time>] [-truncate_time]" << endl;
00243 }
00244 
00245 static
00246 void
00247 cmd_write( const char*    theCondition,
00248            const BdbTime& theBeginTime,
00249            const BdbTime& theEndTime )
00250 {
00251     assert( 0 != theCondition );
00252 
00253   // Step A : Locate a condition
00254   //
00255   // Defaults:
00256   //
00257   //   Condition/DB API: technology, implementation
00258   //   Scope:            database, view
00259  
00260     CdbConditionPtr cPtr;
00261     if( CdbStatus::Success != CdbCondition::instanceFromAny( cPtr,
00262                                                              theCondition )) {
00263 
00264         cout << "Specified condition was not found in the database." << endl
00265              << "    CONDITION : \"" << theCondition << "\"" << endl;
00266         return;
00267     }
00268 
00269   // Step B : Establish specialized object factory
00270   //
00271   // Notes:
00272   //
00273   //    This factory object when used by the Condition/DB API will
00274   //    create a persistent data list with specified (to be passed to the constructor) name
00275   //    in a proper persistent location.
00276 
00277     CdbBdbTObjectFactory< const char* const, BdbDataListsP > oFactory( "Hello Persistent World!" );
00278 
00279     CdbObjectPtr         oPtr;
00280     BdbHandle(BdbObject) oH;
00281 
00282   // Step C.1 : Store an object.
00283   //
00284   // Notes:
00285   //
00286   //   This operation will invoke object creation and its registration in the Conditions
00287   //   database's meta-data.
00288 
00289     int yes2so;
00290     cout << "STORE_OBJECT [0=no] : ";
00291     cin >> yes2so;
00292 
00293     if( yes2so ) {
00294 
00295         if( CdbStatus::Success != cPtr->storeObject( oFactory,
00296                                                      theBeginTime,
00297                                                      theEndTime,
00298                                                      oPtr )) {
00299 
00300             cout << "Failed to store an object for specified period of the validity time" << endl
00301                  << "using 'storeObject' operation." << endl
00302                  << "    BEGIN VALIDITY : " << theBeginTime << endl
00303                  << "    END   VALIDTY  : " << theEndTime << endl;
00304 
00305             return;
00306         }
00307         if( CdbStatus::Success != CdbBdbObjectConvertor::narrow( oH,
00308                                                                  oPtr )) {
00309 
00310             cout << "Failed to narrow a generic object down to the handle." << endl;
00311             return;
00312         }
00313         cout << endl
00314              << "Stored new object using : 'storeObject'"         << endl
00315              << "             valid from : " << oPtr->begin   ( ) << endl
00316              << "                   till : " << oPtr->end     ( ) << endl
00317              << "             created at : " << oPtr->inserted( ) << endl
00318              << "        and with handle : " << oH.sprint( )      << endl
00319              << endl;
00320     }
00321 
00322   // Step C.2 : Store an object using "storeAndTruncate" method
00323   //
00324   // Notes:
00325   //
00326   //   This operation will invoke object creation and its registration in the Conditions
00327   //   database's meta-data.
00328 
00329     int yes2snto;
00330     cout << "STORE_AND_TRUNCATE_OBJECT [0=no] : ";
00331     cin >> yes2snto;
00332 
00333     if( yes2snto ) {
00334 
00335         BdbTime storeTime   ( theBeginTime );
00336         BdbTime truncateTime( theBeginTime + 10 );
00337 
00338         if( CdbStatus::Success != cPtr->storeAndTruncateObject( oFactory,
00339                                                                 storeTime,
00340                                                                 truncateTime,
00341                                                                 oPtr )) {
00342 
00343             cout << "Failed to store an object for specified period of the validity time" << endl
00344                  << "using 'storeAndTruncateObject' operation." << endl
00345                  << "    STORE    TIME : " << storeTime << endl
00346                  << "    TRUNCATE TIME : " << truncateTime << endl;
00347 
00348             return;
00349         }
00350         if( CdbStatus::Success != CdbBdbObjectConvertor::narrow( oH,
00351                                                                  oPtr )) {
00352 
00353             cout << "Failed to narrow a generic object down to the handle." << endl;
00354             return;
00355         } 
00356         cout << endl
00357              << "Stored new object using : 'storeAndTruncateObject'" << endl
00358              << "             valid from : " << oPtr->begin  ( )     << endl
00359              << "                   till : " << oPtr->end    ( )     << endl
00360              << "             created at : " << oPtr->inserted( )    << endl
00361              << "        and with handle : " << oH.sprint( )         << endl
00362              << endl;
00363     }
00364 
00365   // Do the split operation if required
00366 
00367     int yes2split;
00368     cout << "SPLIT [0=no] : ";
00369     cin >> yes2split;
00370 
00371     if( yes2split ) {
00372 
00373         d_ULong seconds;
00374         cout << "Enter the split point [seconds] : ";
00375         cin >> seconds;
00376 
00377         BdbTime splitTime( seconds );
00378         if( CdbStatus::Success != cPtr->split( splitTime )) {
00379 
00380             cout << "Failed to accomplish 'split' operation at specified point" << endl
00381                  << "of the validity time." << endl
00382                  << "    SPLIT TIME : " << splitTime << endl;
00383 
00384             return;
00385         }
00386         cout << endl
00387              << "Split done at : " << splitTime << endl
00388              << endl;
00389     }
00390 }
00391 
00392 static
00393 void
00394 setDefaultEnvironment( const char* theTechnology,
00395                        const char* theImplementation,
00396                        const char* theDatabase,
00397                        const char* theView )
00398 {
00399     if(( 0 == theTechnology     ) &&
00400        ( 0 == theImplementation ) &&
00401        ( 0 == theDatabase       ) &&
00402        ( 0 == theView           )) return;
00403 
00404   // Get the current environment.
00405 
00406     std::string t = CdbEnvironment::defaultTechnology    ( );
00407     std::string i = CdbEnvironment::defaultImplementation( t.c_str( ));
00408     std::string d = CdbEnvironment::defaultDatabase      ( t.c_str( ), i.c_str( ));
00409     std::string v = CdbEnvironment::defaultView          ( t.c_str( ), i.c_str( ), d.c_str( ));
00410 
00411   // Replace selected components
00412 
00413     if( 0 != theTechnology     ) t = theTechnology;
00414     if( 0 != theImplementation ) i = theImplementation;
00415     if( 0 != theDatabase       ) d = theDatabase;
00416     if( 0 != theView           ) v = theView;
00417 
00418   // And finally put its modified version back
00419 
00420     CdbEnvironment::setDefault( t.c_str( ),
00421                                 i.c_str( ),
00422                                 d.c_str( ),
00423                                 v.c_str( ));
00424 }
00425 
00426 static
00427 void
00428 translate_and_set_defaults( char* argv[],
00429                             int&  numArgs,
00430                             int&  nextArg )
00431 {
00432   // Check for the environment parameters first. Apply them immediatelly
00433   // if found.
00434 
00435     const char* technology     = 0;
00436     const char* implementation = 0;
00437     const char* database       = 0;
00438     const char* view           = 0;
00439 
00440     if( numArgs >= 2 ) {
00441         if( 0 == strcmp( argv[nextArg], "-t" )) {
00442             technology = argv[nextArg+1];
00443             nextArg = nextArg + 2;
00444             numArgs = numArgs - 2;
00445         }
00446     }
00447     if( numArgs >= 2 ) {
00448         if( 0 == strcmp( argv[nextArg], "-i" )) {
00449             implementation = argv[nextArg+1];
00450             nextArg = nextArg + 2;
00451             numArgs = numArgs - 2;
00452         }
00453     }
00454     if( numArgs >= 2 ) {
00455         if( 0 == strcmp( argv[nextArg], "-d" )) {
00456             database = argv[nextArg+1];
00457             nextArg = nextArg + 2;
00458             numArgs = numArgs - 2;
00459         }
00460     }
00461     if( numArgs >= 2 ) {
00462         if( 0 == strcmp( argv[nextArg], "-v" )) {
00463             view = argv[nextArg+1];
00464             nextArg = nextArg + 2;
00465             numArgs = numArgs - 2;
00466         }
00467     }
00468     setDefaultEnvironment( technology,
00469                            implementation,
00470                            database,
00471                            view );
00472 }
00473 
00474 /////////////////
00475 // End Of File //
00476 /////////////////

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