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  

/BdbCondTests/PerfTest3.cc

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // File and Version Information:
00004 //      $Id: PerfTest3.cc,v 1.8 2002/08/07 21:51:59 gapon Exp $
00005 //
00006 // Description:
00007 //      A program developed to measure time performance of BdbCond operations.
00008 //
00009 // Environment:
00010 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00011 //
00012 // Author List:
00013 //      D.Bukin       Budker Institute of Nuclear Physics, Nsk, Russia.
00014 //
00015 // Copyright Information:
00016 //      Copyright (C) 2000    Lawrence Berkeley Laboratory
00017 //
00018 //-----------------------------------------------------------------------------
00019 
00020 
00021 // -------------------------
00022 // -- Objectivity Headers --
00023 // -------------------------
00024 #include <ooIndex.h>
00025 
00026 //-------------
00027 // C Headers --
00028 //-------------
00029 extern "C" {
00030 #include <limits.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/types.h>
00034 #include <time.h>
00035 #if (defined OO_SOLARIS)
00036 # include <sys/time.h>
00037 #endif
00038 }
00039 
00040 //--------------------
00041 // C++ Headers -------
00042 //--------------------
00043 
00044 #include <iostream.h>
00045 #include <iomanip.h>
00046 
00047 //-------------------------------
00048 // Collaborating Class Headers --
00049 //-------------------------------
00050 
00051 #include "BaBar/BaBar.hh"
00052 #include "BdbUtil/Bdb.hh"
00053 #include "BdbCond/BdbConditions.hh"
00054 #include "BdbTime/BdbTime.hh"
00055 #include "BdbCondTests/EmcFooClassP.hh"
00056 #include "BdbCondTests/EmcFooClassP_001.hh"
00057 #include "BdbCondTests/EmcFooClassP_002.hh"
00058 #include "BdbCond/BdbObsoleteDatabase.hh"
00059 #include "BdbCond/BdbCondDatabaseMgr.hh"
00060 #include "BdbCond/BdbIntervalItr.hh"
00061 #include "BdbCond/BdbIntervalR.hh"
00062 #include "BdbCond/BdbCondStoreTime.hh"
00063 
00064 #include "BdbApplication/BdbDebug.hh"
00065 
00066 #include "rw/cstring.h"
00067 #include "rw/tvordvec.h"
00068 #include "rw/tvsrtvec.h"
00069 
00070 RWBoolean debugMode = FALSE;
00071 RWBoolean outputMode = TRUE;
00072 RWBoolean declareTransactionAtStart = FALSE;
00073 
00074 static long  timeToFind = 0;
00075 
00076 #define DBG( x ) if (debugMode) cerr << x
00077 #define DBGL( x ) if (debugMode) cerr << x << endl
00078 #define DBGRES( x ) if ( debugMode ) cerr << (CMD_SUCCESS == x ? " ok" : (CMD_WARNING == x ? " warn" : " fail" )) << endl
00079 #define OUTL( x, y ) if (outputMode) cout << x << ":== " << y << endl;
00080 #define CLKOUT cout
00081 
00082 //--------------------------------------------
00083 // Exported and Local Variable Declarations --
00084 //--------------------------------------------
00085 
00086 static const char rcsid[] = "$Id: PerfTest3.cc,v 1.8 2002/08/07 21:51:59 gapon Exp $";
00087 
00088 // command index type, CMD_WRONG must be the first
00089 enum cmd_index {
00090     CMD_WRONG = 0,
00091     CMD_UNKNOWN,
00092     CMD_HELP,
00093     CMD_TRANSACTION,
00094     CMD_CONTAINERS,
00095     CMD_DELETE,
00096     CMD_CLEANUP,
00097     CMD_STORE,
00098     CMD_ANALYZE,
00099     CMD_TOGGLEANALYZE,
00100     CMD_TOGGLEDEBUG,
00101     CMD_TOGGLEOUTPUT,
00102     CMD_EXIT,
00103     CMD_SETCONTAINER,
00104     CMD_STOREVECTOR,
00105     CMD_STOREVECTORN,
00106     CMD_REPEAT,
00107     CMD_END,
00108     CMD_OFFSET,
00109     CMD_RANDOM,
00110     CMD_COPY,
00111     CMD_OFFSETLAST,
00112     CMD_TAG,
00113     CMD_STORE2,
00114     CMD_STORETRUNC,
00115     CMD_REMOVEHISTORY,
00116     CMD_SETINDEXMODE,
00117     CMD_SCAN,
00118     CMD_FINDINTERVAL,
00119     CMD_PURGE
00120 };
00121 
00122 
00123 // arg index type, ARG_UNKNOWN must be the first
00124 enum arg_index {
00125     ARG_UNKNOWN = 0,
00126     ARG_BOOL,
00127     ARG_INT,
00128     ARG_STRING,
00129     ARG_TIME,
00130     ARG_DELTA,
00131     ARG_OBJECT1,
00132     ARG_OBJECT2,
00133     ARG_VECTOR,
00134     ARG_NOMORE
00135 };
00136 
00137 
00138 enum result {
00139     CMD_ERROR = 0,
00140     CMD_WARNING,
00141     CMD_SUCCESS
00142 };
00143 
00144 
00145 typedef struct {
00146   cmd_index     code;
00147   const char*   name;
00148   const char*   arguments;
00149   const char*   description;
00150   int           ismodifier;
00151 } cmd;
00152 
00153 
00154 typedef struct {
00155   arg_index     code;
00156   const char    name;
00157   const char*   description;
00158 } arg;
00159 
00160 
00161 // arg description list,  ARG_UNKNOWN MUST be the last
00162 const arg argument_list[] = {
00163     { ARG_BOOL,    'b', "bool" },
00164     { ARG_INT,     'i', "int" },
00165     { ARG_STRING,  's', "string" },
00166     { ARG_TIME,    'T', "time" },
00167     { ARG_DELTA,   't', "delta" },
00168     { ARG_OBJECT1, 'o', "object1" },
00169     { ARG_OBJECT2, 'O', "object2" },
00170     { ARG_VECTOR,  'v', "objvector" },
00171     { ARG_NOMORE,  '\0', "nomore" },
00172     { ARG_UNKNOWN, ',', "unknown" }
00173 };
00174 
00175 
00176 //------------------------------------
00177 //       function declaration
00178 //------------------------------------
00179 
00180 // print help
00181 result cmd_help(const cmd* commands);
00182 
00183 // start transaction. ensure can be:
00184 //    0 - just ensure that in transaction;
00185 //    1 - finish previous and start next;
00186 //    2 - finish previous.
00187 result cmd_transaction( RWBoolean updateMode = TRUE, int ensure = 0 );
00188 
00189 // list containers
00190 result cmd_containers(const RWCString& detector, std::vector<RWCString>* containerlist = 0);
00191 
00192 // delete container
00193 result cmd_delete( const RWCString& detector, const RWCString& container );
00194 
00195 // store object by start time
00196 result cmd_store(const RWCString& detector, const RWCString& container,
00197                const RWCString& object, const BdbTime& starttime );
00198 
00199 // store object by two times
00200 result cmd_store2(const RWCString& detector, const RWCString& container,
00201                   const RWCString& object, const BdbTime& starttime, const BdbTime& endtime );
00202 
00203 // truncate and store object by two times
00204 result cmd_storetrunc(const RWCString& detector, const RWCString& container,
00205                       const RWCString& object, const BdbTime& starttime, const BdbTime& endtime );
00206 
00207 // set container
00208 result cmd_setcontainer(const RWCString& newdetector, const RWCString& newcontainer,
00209                         RWCString& detector, RWCString& container );
00210 
00211 // analyze container structure
00212 result cmd_analyze(const RWCString& detector, const RWCString& container);
00213 
00214 // store a vector of objects
00215 result cmd_storevector(const RWCString& detector, const RWCString& container,
00216                        int elts,
00217                        RWTValOrderedVector<RWCString>& strings,
00218                        RWTValOrderedVector<BdbTime>& times );
00219 
00220 // store a vector of uniformly located objects
00221 result cmd_storevectorn(const RWCString& detector, const RWCString& container,
00222                         int elts, const RWCString& object,
00223                         const BdbTime& starttime, const BdbTime& offsettime );
00224                        
00225 // clean up all containers
00226 result cmd_cleanup( const RWCString& detector );
00227 
00228 // copy container
00229 result cmd_copy(const RWCString& detector, const RWCString& container,
00230                 const RWCString& newdetector, const RWCString& newcontainer );
00231 
00232 
00233 // remove history
00234 result cmd_removehistory(const RWCString& detector, const RWCString& container );
00235 
00236 // set offset to the begin time of the last interval
00237 result cmd_offsetlast(const RWCString& detector, const RWCString& container,
00238                       long& offset );
00239 
00240 // scan a container
00241 result cmd_scan(const RWCString& detector, const RWCString& container,
00242                 const BdbTime& intervaltime );
00243 
00244 // find interval in a container
00245 result cmd_findinterval(const RWCString& detector, const RWCString& container,
00246                         const BdbTime& intervaltime );
00247 
00248 // purge a container
00249 result cmd_purge(const RWCString& detector, const RWCString& container,
00250                  const BdbTime& starttime, const BdbTime& endtime,
00251                  long maxNumOfTop );
00252 
00253 // toggle indexmode
00254 result cmd_setindexmode(long mode);
00255 
00256 
00257 // read a command from stream
00258 int readCommand(const cmd*                      commands,
00259                 RWTValOrderedVector<RWCString>& strings,
00260                 RWTValOrderedVector<int>&       ints,
00261                 RWTValOrderedVector<BdbTime>&   times,
00262                 istream&                        in );
00263 int popCommand(const cmd*                      commands,
00264                RWTValOrderedVector<RWCString>& strings,
00265                RWTValOrderedVector<int>&       ints,
00266                RWTValOrderedVector<BdbTime>&   times,
00267                RWTValOrderedVector<int>&       cmdstack,
00268                RWTValOrderedVector<RWCString>& strstack,
00269                RWTValOrderedVector<int>&       intstack,
00270                RWTValOrderedVector<BdbTime>&   timstack,
00271                int& icmd, int& istr, int& iint, int& itim);
00272 
00273 // routime get/set time offset for new times
00274 long timeOffset( long* newoffset = 0);
00275 
00276 
00277 
00278 
00279 
00280 int main( int argc, char** argv ) {
00281   BdbConditions* theApp = BdbConditions::instance();
00282 
00283   RWCString detector("-");
00284   RWCString container("-");
00285   cmd_setcontainer( detector, container, detector, container );
00286 
00287   // command bank, CMD_EXIT MUST be the first,
00288   // CMD_UNKNOWN must separate true commands from pseudo-commands 
00289   static const cmd commands[] = {
00290       { CMD_EXIT, "exit", "",
00291           "\t\texit program", 0},
00292       { CMD_HELP, "help", "",
00293           "\t\tgetting help", 0 },
00294       { CMD_SETCONTAINER, "setcontainer", "sdatabase,scontainer",
00295           "\t\tset current detector and container,\n\t\tthat will be used by other commands;", 0},
00296       { CMD_TRANSACTION, "transaction", "",
00297           "\t\tfinish active transaction\n\t\tthe next transaction will be started automatically;", 0 },
00298       { CMD_CONTAINERS, "containers", "",
00299           "\t\tlist containers in the current detector;", 0 },
00300       { CMD_DELETE, "delete", "",
00301           "\t\tdelete current container.  More than two delete \n\t\ton the same container per session does not work;", 1},
00302       { CMD_CLEANUP, "cleanup", "",
00303           "\t\tdelete all the containers from detector;", 0 },
00304       { CMD_STORE, "store", "oobject",
00305           "\t\tstore an object by start time;", 1},
00306       { CMD_STORE2, "store2", "Oobject",
00307           "\t\tstore an object by start and end times;", 1},
00308       { CMD_STORETRUNC, "storetrunc", "Oobject",
00309           "\t\ttrunc and store an object by start and end times;", 1},
00310       { CMD_STOREVECTOR, "storevector", "vvector",
00311           "\t\tstore a vector of objects;", 1},
00312       { CMD_STOREVECTORN, "storevectorn", "ielts,Oobject",
00313           "\t\tstore a vector of objects;", 1},
00314       { CMD_ANALYZE, "analyze", "",
00315           "\t\tanalyze the structure of the container;", 0},
00316       { CMD_TOGGLEANALYZE, "toggleanalyze", "",
00317           "\t\ttoggle autoanalyze mode;", 0},
00318       { CMD_TOGGLEDEBUG, "toggledebug", "",
00319           "\t\ttoggle debug mode;", 0},
00320       { CMD_TOGGLEOUTPUT, "toggleoutput", "",
00321           "\t\ttoggle output mode;", 0},
00322       { CMD_REPEAT, "repeat", "ihowmany",
00323           "\t\trepeat commands several times;", 0},
00324       { CMD_END, "end", "",
00325           "\t\tends the repetetion.  Number of end commands\n\t\tshould match that of repeat ones.", 0},
00326       { CMD_RANDOM, "random", "isecmin,isecmax",
00327           "\t\tset additional random offset for all times", 0},
00328       { CMD_OFFSET, "offset", "iseconds",
00329           "\t\tchange offset to the delta seconds, if delta=0, then reset offset to 0", 0},
00330       { CMD_COPY, "copy", "solddetector,soldcontainer",
00331           "\t\tcopy from container", 1},
00332       { CMD_OFFSETLAST, "offsetlast", "",
00333           "\t\tset offset to the begin time of the last interval", 0},
00334       { CMD_TAG, "tag", "stag",
00335           "\t\tplace the string tag to the output", 0},
00336       { CMD_REMOVEHISTORY, "removehistory", "",
00337           "\t\tremove the history", 0},
00338       { CMD_SCAN, "scan", "Ttime",
00339           "\t\tscan container", 0},
00340       { CMD_FINDINTERVAL, "findinterval", "Ttime",
00341           "\t\tfind interval in container", 0},
00342       { CMD_SETINDEXMODE, "setindexmode", "imode",
00343           "\t\tset index mode: 0-explicit, 1-insense, 2-sense", 0},
00344       { CMD_PURGE, "purge", "Tstart,tend,itoplayers",
00345           "\t\tpurge container keeping number of top layers", 1},
00346       { CMD_UNKNOWN, 0, 0, 0, 0 },
00347       { CMD_WRONG, 0, 0, 0, 0 }
00348   };
00349 
00350   RWBoolean autoAnalyze = FALSE;
00351 
00352 
00353   // main loop over commands
00354   // readmode:
00355   //  0 -- read-and-fire mode
00356   //  1 -- accumulation mode (commands are read and stored into the storages below)
00357   //  2 -- discharge mode (commands are taken from storages and executed)
00358   int readmode = 0;
00359 
00360   // --- only for accumulation/discharge mode: the storages
00361   RWTValOrderedVector<int>       repstack;   // keeps pointers to repeat command position in cmdstack
00362   RWTValOrderedVector<int>       endstack;   // keeps number of passes executed for each repeat command
00363   RWTValOrderedVector<int>       cmdstack;   // keeps commands for repetition mode
00364   RWTValOrderedVector<RWCString> strstack;   // >
00365   RWTValOrderedVector<int>       intstack;   //   > -- arguments
00366   RWTValOrderedVector<BdbTime>   timstack;   // >
00367   int icmd =0, istr =0, iint =0, itim =0;    // current pointers
00368   int nestlevel = 0;                         // how deep nesting
00369 
00370   // place to move argument of commands before execution
00371   RWTValOrderedVector<RWCString> strings;
00372   RWTValOrderedVector<int> ints;
00373   RWTValOrderedVector<BdbTime> times;
00374 
00375   while ( true ) {
00376 
00377     int idx = CMD_EXIT;
00378 
00379     // reading or popping a command
00380     if ( readmode == 2 ) { // discharge mode
00381       
00382       idx = popCommand(commands, strings, ints, times,
00383                        cmdstack, strstack, intstack, timstack,
00384                        icmd, istr, iint, itim);
00385       
00386     } else {
00387 
00388       if ( 1 == readmode ) {
00389         istr = strstack.length();
00390         itim = timstack.length();
00391         iint = intstack.length();
00392       }
00393 
00394       idx = readCommand(commands, strings, ints, times, cin);
00395       // when the command is read,
00396       // its arguments are placed into strings, times and ints arrays.
00397       // if it is the repetition command then repetition mode is
00398       // switched on.
00399       // in repetition mode, the command is placed into the cmdstack array.
00400       // repetition command also place the pointer to cmdstack
00401       // into repstack and then saves pointers to ints, strings and times
00402       // array onto cmdstack. the level of repetition is incremented.
00403       // the value of 0 is placed into endstack.
00404       // if the command is end command (that finish the repeat command),
00405       // then it decrement the repetition level.
00406       // When the repetition level is goes to 0, then begins the execution
00407       // of the loop.
00408 
00409       if ( CMD_REPEAT == commands[idx].code ) {
00410         if ( 1 != readmode ) {
00411           cmdstack.clear();
00412           strstack.clear();
00413           intstack.clear();
00414           timstack.clear();
00415           icmd = 0;
00416           istr = 0;
00417           iint = 0;
00418           itim = 0;
00419         }
00420         readmode = 1;
00421       }
00422 
00423     } // end of reading
00424 
00425 
00426 
00427     // postprocessing command
00428     if ( 1 == readmode ) { // in accumulation mode
00429       
00430       // put a command and its arguments into the store
00431       cmdstack.insert( idx );
00432       int i;
00433       for ( i = 0; i < ints.length(); i++ ) {
00434         intstack.insert(ints[i]);
00435       }
00436       for ( i = 0; i < strings.length(); i++ ) {
00437         strstack.insert(strings[i]);
00438       }
00439       for ( i = 0; i < times.length(); i++ ) {
00440         timstack.insert(times[i]);
00441       }
00442 
00443       if ( CMD_REPEAT == commands[idx].code ) {
00444 
00445         cmdstack.insert( iint );
00446         cmdstack.insert( istr );
00447         cmdstack.insert( itim );
00448         ++nestlevel;
00449 
00450       } else if ( CMD_END == commands[idx].code ) {
00451 
00452         --nestlevel;
00453         if ( 0 == nestlevel ) {
00454           // switch to discharge
00455           readmode = 2;
00456           icmd = 0;
00457           iint = 0;
00458           istr = 0;
00459           itim = 0;
00460         }
00461 
00462       }
00463       
00464 
00465 
00466     } else { // not in accumulation mode
00467 
00468 
00469       // applying additional offset to times
00470       int i;
00471       for ( i = 0; i < times.length(); i++ ) {
00472         if ( times[i] != BdbTime::minusInfinity &&
00473             times[i] != BdbTime::plusInfinity ) {
00474           times[i] = BdbTime(times[i].getGmtSec() + timeOffset());
00475           if ( times[i] == BdbTime::minusInfinity ) times[i] = BdbTime(times[i].getGmtSec()+1);
00476           if ( times[i] == BdbTime::plusInfinity ) times[i] = BdbTime(times[i].getGmtSec()-1);
00477         }
00478       }
00479 
00480       // here is the execution code for commands
00481       result status = CMD_SUCCESS;
00482       switch ( commands[idx].code ) {
00483 
00484        case CMD_REPEAT: {
00485          while ( repstack.length() <= nestlevel ) {
00486            repstack.insert(0);
00487          }
00488          while ( endstack.length() <= nestlevel ) {
00489            endstack.insert(0);
00490          }
00491          repstack[nestlevel] = icmd - 1;
00492          iint = cmdstack[icmd++];
00493          istr = cmdstack[icmd++];
00494          itim = cmdstack[icmd++];
00495          if ( 0 == endstack[nestlevel] ) {
00496            // initialize the loop
00497            DBGL( "entering loop at lev#" << nestlevel << ", N = " << intstack[iint] );
00498            endstack[nestlevel] = intstack[iint];
00499          }
00500          OUTL("rep" << nestlevel, intstack[iint]-endstack[nestlevel] );
00501          ++iint;
00502          ++nestlevel;
00503          break;
00504        }
00505        case CMD_END: {
00506          --nestlevel;
00507          --endstack[nestlevel];
00508          if ( 0 == endstack[nestlevel] ) {
00509            // the loop has been finished
00510            DBGL( "finishing loop at lev#" << nestlevel );
00511            if ( 0 == nestlevel ) {
00512              readmode = 0;
00513            }
00514          } else { // go on to the start
00515            icmd = repstack[nestlevel];
00516            iint = cmdstack[icmd+1];
00517          }
00518          break;
00519        }
00520 
00521        case CMD_UNKNOWN: {
00522          DBGL( "unknown" );
00523          status = CMD_WARNING;
00524          break;
00525        }
00526 
00527        case CMD_WRONG: {
00528          cerr << "wrong arguments" << endl;
00529          status = CMD_WARNING;
00530          break;
00531        }
00532        case CMD_HELP: {
00533          cmd_help( commands );
00534          break;
00535        }
00536   
00537        case CMD_TRANSACTION: {
00538          status = cmd_transaction( TRUE, 2 );
00539          break;
00540        }
00541   
00542        case CMD_CONTAINERS: {
00543          status = cmd_containers( detector );
00544          break;
00545        }
00546   
00547        case CMD_DELETE: {
00548          status = cmd_delete( detector, container );
00549          break;
00550        }
00551   
00552        case CMD_CLEANUP: {
00553          status = cmd_cleanup( detector );
00554          break;
00555        }
00556   
00557        case CMD_STORE: {
00558          status = cmd_store( detector, container, strings(0), times(0) );
00559          break;
00560        }
00561   
00562        case CMD_STORE2: {
00563          status = cmd_store2( detector, container, strings(0), times(0), times(1) );
00564          break;
00565        }
00566   
00567        case CMD_STORETRUNC: {
00568          status = cmd_storetrunc( detector, container, strings(0), times(0), times(1) );
00569          break;
00570        }
00571   
00572        case CMD_ANALYZE: {
00573          status = cmd_analyze( detector, container );
00574          break;
00575        }
00576   
00577        case CMD_TOGGLEANALYZE: {
00578          autoAnalyze = TRUE - autoAnalyze;
00579          cout << "autoanalyze mode is " << ( autoAnalyze ? "ON" : "OFF" ) << endl;
00580          break;
00581        }
00582   
00583        case CMD_TOGGLEDEBUG: {
00584          debugMode = TRUE - debugMode;
00585          // DBGL( "debug mode is " << ( debugMode ? "ON" : "OFF" ) );
00586          break;
00587        }
00588   
00589        case CMD_TOGGLEOUTPUT: {
00590          outputMode = TRUE - outputMode;
00591          // DBGL( "output mode is " << ( newOutputMode ? "ON" : "OFF" ) );
00592          break;
00593        }
00594   
00595        case CMD_EXIT: {
00596          status = cmd_transaction(FALSE,2);
00597          break;
00598        }
00599   
00600        case CMD_SETCONTAINER: {
00601          status = cmd_setcontainer( strings(0), strings(1), detector, container );
00602          OUTL("contain ", container );
00603          break;
00604        }
00605         
00606        case CMD_STOREVECTOR: {
00607          status = cmd_storevector( detector, container, ints(0), strings, times );
00608          break;
00609        }
00610 
00611        case CMD_STOREVECTORN: {
00612          status = cmd_storevectorn( detector, container, ints(0),
00613                                    strings(0), times(0), times(1) );
00614          break;
00615        }
00616 
00617        case CMD_RANDOM: {
00618          double f = double(random())/LONG_MAX;
00619          if ( f < 0 || f > 1 ) {
00620            cerr << "wrong random number: " << f << endl;
00621            exit(1);
00622          }
00623          long offset = (ints(1)-ints(0))*f;
00624          timeOffset(&offset);
00625          break;
00626        }
00627 
00628        case CMD_OFFSET: {
00629          long offset = ints(0);
00630          if ( offset != 0 ) offset += timeOffset();
00631          timeOffset(&offset);
00632          break;
00633        }
00634 
00635        case CMD_OFFSETLAST: {
00636          long offset;
00637          status = cmd_offsetlast( detector, container, offset );
00638          if ( CMD_SUCCESS == status ) timeOffset(&offset);
00639          break;
00640        }
00641 
00642        case CMD_SCAN: {
00643          status = cmd_scan( detector, container, times(0) );
00644          break;
00645        }
00646 
00647        case CMD_FINDINTERVAL: {
00648          status = cmd_findinterval( detector, container, times(0) );
00649          break;
00650        }
00651 
00652        case CMD_PURGE: {
00653          status = cmd_purge( detector, container, times(0), times(1), ints(0));
00654          break;
00655        }
00656 
00657        case CMD_SETINDEXMODE: {
00658          status = cmd_setindexmode(ints(0));
00659          break;
00660        }
00661 
00662        case CMD_COPY: {
00663          status = cmd_copy(strings(0), strings(1), detector, container);
00664          break;
00665        }
00666 
00667        case CMD_TAG: {
00668          if ( outputMode ) {
00669            cout << strings(0) << endl;
00670          }
00671          break;
00672        }
00673 
00674        case CMD_REMOVEHISTORY: {
00675          status = cmd_removehistory( detector, container );
00676          break;
00677        }
00678 
00679        default: {
00680          cout << "not implemented" << endl;
00681          status = CMD_WARNING;
00682        }
00683   
00684       }
00685   
00686       if ( CMD_ERROR == status ) {
00687         cmd_transaction(TRUE,-1);
00688         return 1;
00689       }
00690 
00691       if ( commands[idx].code == CMD_EXIT ) break;
00692 
00693       if (autoAnalyze && 
00694         status == CMD_SUCCESS &&
00695         commands[idx].ismodifier ) {
00696         cmd_analyze(strings(0), strings(1));
00697       }
00698 
00699     } // not in accumulation mode
00700 
00701   }
00702   return 0;
00703 }
00704 
00705 
00706 
00707 
00708 
00709 RWCString translateOID( const BdbRefAny& ref ) {
00710   ooId id = ref;
00711   d_UShort db   = id.get_DB();
00712   d_UShort cont = id.get_OC();
00713   d_UShort page = id.get_page();
00714   d_UShort slot = id.get_slot();
00715   char s[256];
00716 
00717   sprintf( s, "%d-%d-%d-%d", (int) db, (int) cont, (int) page, (int) slot );
00718   return s;
00719 }
00720 
00721 
00722 RWCString translateSOID( const BdbRefAny& ref ) {
00723   ooId id = ref;
00724   d_UShort page = id.get_page();
00725   d_UShort slot = id.get_slot();
00726   char s[256];
00727 
00728   sprintf( s, "%d-%d", (int) page, (int) slot );
00729   return s;
00730 }
00731 
00732 
00733 int readArg( const char c ) {
00734   int idx = 0;
00735   while ( argument_list[idx].code != ARG_UNKNOWN && argument_list[idx].name != c ) {
00736     ++idx;
00737   }
00738   return idx;
00739 }
00740 
00741 
00742 
00743 int nextArgument( RWCString& argstring, RWCString& argname ) {
00744 
00745   // skip commas
00746   int skip = 0;
00747   const char* p = argstring.data();
00748   while ( p[skip] == ',' ) ++skip;
00749   if ( skip ) argstring = argstring(skip,argstring.length()-skip);
00750   if ( 0 == argstring.length() ) return readArg('\0');
00751 
00752   // get argument type
00753   int argtype = readArg(argstring(0));
00754 
00755   // get argument name
00756   skip = argstring.first(',',1);
00757   if ( RW_NPOS == skip ) skip = argstring.length();
00758   argname = argstring(1,skip-1);
00759   argstring = argstring(skip,argstring.length()-skip);
00760 
00761   return argtype;
00762 }
00763 
00764 
00765 
00766 
00767 
00768 
00769 int readCommand(const cmd*                      commands,
00770                 RWTValOrderedVector<RWCString>& strings,
00771                 RWTValOrderedVector<int>&       ints,
00772                 RWTValOrderedVector<BdbTime>&   times,
00773                 istream&                        in ) {
00774 
00775   static const BdbTime zeroTime(1996,1,1,0,0,0);
00776   static BdbTime lastTime = zeroTime;
00777 
00778   RWCString cmd_name;
00779   if ( ! in ) return 0;
00780 
00781   in >> cmd_name;
00782   int idx = 0;
00783   while ( commands[idx].code != CMD_UNKNOWN &&
00784          commands[idx].name != cmd_name ) {
00785     ++idx;
00786   }
00787   // clean up arguments
00788   ints.clear();
00789   strings.clear();
00790   times.clear();
00791 
00792   if ( commands[idx].code == CMD_UNKNOWN ) {
00793 
00794     cerr << "unknown command:" << cmd_name << endl;
00795     
00796   } else {
00797 
00798     // DBGL("=== next command: " << commands[idx].name );
00799 
00800     // read arguments
00801     RWCString argstring = commands[idx].arguments;
00802     while ( true ) {
00803 
00804       RWCString argname;
00805       int argtype = nextArgument(argstring, argname);
00806       if ( ARG_NOMORE == argument_list[argtype].code ) break;
00807 
00808       // read argument
00809       switch ( argument_list[argtype].code ) {
00810        case ARG_BOOL: { // bool
00811          RWBoolean b;
00812          in >> b;
00813          if ( b != TRUE && b != FALSE ) return CMD_WRONG;
00814          ints.insert(b);
00815          break;
00816        }
00817        case ARG_INT: { // int
00818          int n;
00819          in >> n;
00820          ints.insert(n);
00821          break;
00822        }
00823        case ARG_STRING: { // RWCString
00824          RWCString s;
00825          in >> s;
00826          strings.insert(s);
00827          break;
00828        }
00829        case ARG_TIME:
00830        case ARG_DELTA: {
00831          RWCString s;
00832          in >> s;
00833          int len = 1;
00834          BdbTime t;
00835          switch ( s[0] ) {
00836           case 'm': {
00837             t = BdbTime::minusInfinity;
00838             break;
00839           }
00840           case 'p': {
00841             t = BdbTime::plusInfinity;
00842             break;
00843           }
00844           case 'z': {
00845             t = zeroTime;
00846             break;
00847           }
00848           case 'l': {
00849             t = lastTime;
00850             break;
00851           }
00852           default: {
00853             t = (ARG_TIME == argument_list[argtype].code ? zeroTime : lastTime );
00854             len = 0;
00855           }
00856          } // switch first letter of the time
00857          
00858          if ( s.length() > len ) {
00859            long dt = atol( s.data()+len );
00860            t = t + dt;
00861          }
00862          times.insert(t);
00863          lastTime = t;
00864          break;
00865        }
00866        case ARG_OBJECT1: {
00867          argstring.prepend("s,T,");
00868          break;
00869        }
00870        case ARG_OBJECT2: {
00871          argstring.prepend("s,T,t,");
00872          break;
00873        }
00874        case ARG_VECTOR: {
00875          int n;
00876          in >> n;
00877          if ( n < 1 ) {
00878            cerr << "vector must have positive number of elements." << endl;
00879            return CMD_WRONG;
00880          }
00881          ints.insert(n);
00882          argstring.prepend("t,");
00883          while ( n-- > 1 ) {
00884            argstring.prepend("s,t,");
00885          }
00886          argstring.prepend("s,T,");
00887          break;
00888        }
00889        default: {
00890          cerr << "unknown argument type:" << argument_list[argtype].name << endl;
00891          exit(1);
00892        }
00893 
00894       }
00895 
00896     } // while more arguments needed
00897 
00898   }
00899   return idx;
00900 }
00901 
00902 
00903 
00904 
00905 
00906 
00907 int popCommand(const cmd*                      commands,
00908                RWTValOrderedVector<RWCString>& strings,
00909                RWTValOrderedVector<int>&       ints,
00910                RWTValOrderedVector<BdbTime>&   times,
00911                RWTValOrderedVector<int>&       cmdstack,
00912                RWTValOrderedVector<RWCString>& strstack,
00913                RWTValOrderedVector<int>&       intstack,
00914                RWTValOrderedVector<BdbTime>&   timstack,
00915                int& icmd, int& istr, int& iint, int& itim) {
00916 
00917   if ( icmd >= cmdstack.length() ) {
00918     cerr << "too few commands on stack" << endl;
00919     exit(1);
00920   }
00921 
00922   int idx = cmdstack[icmd++];
00923 
00924   // clean up arguments
00925   ints.clear();
00926   strings.clear();
00927   times.clear();
00928 
00929   if ( commands[idx].code == CMD_UNKNOWN ) {
00930 
00931     abort();
00932 
00933   } else {
00934 
00935     // DBGL("=== next command: " << commands[idx].name );
00936 
00937     // read arguments
00938     RWCString argstring = commands[idx].arguments;
00939     while ( true ) {
00940 
00941       RWCString argname;
00942       int argtype = nextArgument(argstring, argname);
00943       if ( ARG_NOMORE == argument_list[argtype].code ) break;
00944 
00945       // read argument
00946       switch ( argument_list[argtype].code ) {
00947        case ARG_BOOL: { // bool
00948          int b = intstack[iint++];
00949          if ( b != TRUE && b != FALSE ) {
00950            cerr << "should be bool" << endl;
00951            exit(1);
00952          }
00953          ints.insert(b);
00954          break;
00955        }
00956        case ARG_INT: { // int
00957          ints.insert(intstack[iint++]);
00958          break;
00959        }
00960        case ARG_STRING: { // RWCString
00961          strings.insert(strstack[istr++]);
00962          break;
00963        }
00964        case ARG_TIME:
00965        case ARG_DELTA: {
00966          times.insert(timstack[itim++]);
00967          break;
00968        }
00969        case ARG_OBJECT1: {
00970          argstring.prepend("s,T,");
00971          break;
00972        }
00973        case ARG_OBJECT2: {
00974          argstring.prepend("s,T,t,");
00975          break;
00976        }
00977        case ARG_VECTOR: {
00978          int n = intstack[iint++];
00979          ints.insert(n);
00980          argstring.prepend("t,");
00981          while ( n-- > 1 ) {
00982            argstring.prepend("s,t,");
00983          }
00984          argstring.prepend("s,T,");
00985          break;
00986        }
00987        default: {
00988          cerr << "unknown argument type:" << argument_list[argtype].name << endl;
00989          exit(1);
00990        }
00991 
00992       }
00993 
00994     } // while more arguments needed
00995 
00996   }
00997   return idx;
00998 
00999 }
01000 
01001 
01002 
01003 
01004 
01005 
01006 
01007 
01008 result makeObject(const RWCString& detector,
01009                 const RWCString& object,
01010                 BdbHandle(BdbObject)& objH ) {
01011   result status = CMD_SUCCESS;
01012 
01013   if ( "-" == object ) {
01014     objH = NULL;
01015   } else {
01016 
01017     status = CMD_ERROR;
01018 
01019     while ( true ) {
01020       // DBGL( "making object" );
01021 
01022       if ( ! cmd_transaction(TRUE) ) break;
01023 
01024       BdbObsoleteDatabase db(detector);
01025       BdbRefAny hint = db.updatedHint();
01026       if ( BdbIsNull(hint) ) {
01027         cerr << "failed to get a hint" << endl;
01028         break;
01029       }
01030       // DBGL( "got a hint: [" << translateOID(hint) << "]");
01031 
01032       if ( "a" == object ) {
01033         objH = new(hint) EmcFooClassP( EmcFooClass(random()) );
01034       } else if ( "b" == object ) {
01035         objH = new(hint) EmcFooClassP_001( EmcFooClass_001(random(),random()) );
01036       } else if ( "c" == object ) {
01037         objH = new(hint) EmcFooClassP_002( EmcFooClass_002(random(),random(),random()) );
01038       } else {
01039         cerr << "wrong object name " << object << endl;
01040         status = CMD_WARNING;
01041         break;
01042       }
01043 
01044       if ( BdbIsNull(objH) ) {
01045         cerr << "failed to create object" << endl;
01046         break;
01047       }
01048     
01049       // DBGL( "object has been created [" << translateOID(objH) << "]" );
01050       status = CMD_SUCCESS;
01051       break;
01052     }
01053   }
01054   return status;
01055 }
01056 
01057 
01058 
01059 
01060 
01061 
01062 
01063 long timeOffset( long* newoffset ) {
01064   static long offset = 0;
01065   if ( newoffset ) {
01066     offset = *newoffset;
01067     // DBGL("new offset is: " << timeOffset());
01068   }
01069   return offset;
01070 }
01071 
01072 
01073 
01074 
01075 
01076 
01077 
01078 //===========================
01079 //       print help
01080 //===========================
01081 result cmd_help( const cmd* commands ) {
01082   const cmd* p = commands;
01083   cout << "Commands are:" << endl;
01084   while ( p->code != CMD_UNKNOWN ) {
01085     cout << p->name;
01086     RWCString argstring = p->arguments;
01087     while ( true ) {
01088       RWCString argname;
01089       int argtype = nextArgument(argstring, argname);
01090       if ( ARG_NOMORE == argument_list[argtype].code ) break;
01091       cout << " " << argname << "(" << argument_list[argtype].description << ")";
01092     }
01093     cout << endl << p->description << endl;
01094     ++p;
01095   }
01096   return CMD_SUCCESS;
01097 }
01098 
01099 
01100 
01101 
01102 
01103 
01104 
01105 static long getMillis()
01106 {
01107     long millis = 0;
01108 #if (defined OO_SOLARIS)
01109     struct timeval tv;
01110     gettimeofday( &tv, 0 );
01111     millis = tv.tv_sec * 1000 + tv.tv_usec/1000;
01112 #endif
01113     return millis;
01114 }
01115 
01116 
01117 
01118 
01119 
01120 //===========================
01121 //      new transaction
01122 //===========================
01123 result cmd_transaction( RWBoolean updateMode, int ensure ) {
01124   static RWBoolean inTransaction = FALSE;
01125   static RWBoolean mode = FALSE;
01126   static clock_t usecs;
01127   static long msecs;
01128 
01129   BdbConditions* theApp = BdbConditions::instance();
01130 
01131   // abort current transaction
01132   if ( ensure < 0 ) {
01133     if ( inTransaction ) {
01134       DBGL( "-> abort" );
01135       theApp->abort();
01136     }
01137     exit(1);
01138   }
01139 
01140   // finish previous transaction if explicitly asked or update mode needed being in readonly
01141   if ( inTransaction && ( ensure != 0 || updateMode && !mode ) ) {
01142 
01143     // DBGL("finishing the old transaction of mode " << mode );
01144     DBG( "-> transcomm" );
01145 
01146     CLKOUT << "cmd_transaction: committing..." << endl;
01147     if ( BdbcSuccess != theApp->commit() ) {
01148       cerr << " failed to commit transaction" << endl;
01149       return CMD_ERROR;
01150     }
01151     CLKOUT << "cmd_transaction: ...committed" << endl;
01152 
01153     clock_t ttclock = clock() / 1000;
01154     usecs = ttclock - usecs;
01155     msecs = getMillis() - msecs;
01156 
01157     // calibration
01158     clock_t calstart = clock();
01159     clock_t caltime;
01160     int nloop = 0;
01161     while ( (caltime = clock() - calstart) < 1000000 ) {
01162       int j = 0;
01163       for ( int i = 0; i < 1000; i++ ) {
01164         j += 1;
01165       }
01166       nloop += 1;
01167     }
01168 
01169     OUTL("calibr  ", nloop/(((double)caltime)/1000) );
01170     OUTL("ttsystem", usecs );
01171     OUTL("ttastro ", msecs );
01172     OUTL("ttfind  ", timeToFind );
01173 
01174 
01175     // shows the current time since
01176     static time_t ttstartrun = 0;
01177     if ( 0 == ttstartrun ) {
01178       struct tm t;
01179       t.tm_sec = 0;
01180       t.tm_min = 0;
01181       t.tm_hour = 0;
01182       t.tm_mday = 1; // 1st
01183       t.tm_mon = 8;  // september
01184       t.tm_year = 100;
01185       t.tm_isdst = 0;
01186       ttstartrun = mktime( &t );
01187     }
01188     double ttrun = (time(0) - ttstartrun) / (double)86400.0;
01189     OUTL("ttrun   ", ttrun );
01190 
01191     if ( outputMode && !declareTransactionAtStart ) {
01192       cout << "---endtransaction---" << endl;
01193     }
01194 
01195     inTransaction = FALSE;
01196     DBGRES( CMD_SUCCESS );
01197 
01198   }
01199 
01200 
01201   // start new transaction
01202   if ( ensure < 2 && ! inTransaction ) {
01203 
01204     usecs = clock()/1000;
01205     msecs = getMillis();
01206     timeToFind = 0;
01207 
01208     CLKOUT << "cmd_transaction: starting..." << endl;
01209 
01210     BdbStatus status = updateMode ? theApp->startUpdate() : theApp->startRead();
01211     if ( BdbcSuccess != status ) {
01212       cerr << "failed to start a new transaction" << endl;
01213       return CMD_ERROR;
01214     }
01215 
01216     CLKOUT << "cmd_transaction: ...started" << endl;
01217 
01218     mode = updateMode;
01219     inTransaction = TRUE;
01220 
01221     if ( outputMode && declareTransactionAtStart ) {
01222       cout << "---newtransaction---" << endl;
01223     }
01224 
01225   }
01226   return CMD_SUCCESS;
01227 
01228 }
01229 
01230 
01231 
01232 
01233 
01234 
01235 //===========================
01236 //       print containers
01237 //===========================
01238 result cmd_containers( const RWCString& detector, std::vector<RWCString>* containerlist ) {
01239 
01240   DBG( "-> containers" );
01241   result status = cmd_transaction(TRUE);
01242 
01243   do {
01244 
01245     if ( CMD_SUCCESS != status ) {
01246       break;
01247     }
01248 
01249     // list all containers
01250     std::vector<std::string> containers;
01251     BdbObsoleteDatabase db(detector);
01252     /*
01253     if ( BdbcSuccess != db.listAllClasses( containers, true ) ) {
01254       cerr << "cannot list old intervals" << endl;
01255       status = CMD_WARNING;
01256       break;
01257     }
01258      */
01259     if ( BdbcSuccess != db.listAllClasses( containers, false, true ) ) {
01260       cerr << "cannot list new intervals" << endl;
01261       status = CMD_WARNING;
01262       break;
01263     }
01264 
01265     if ( 0 != containerlist ) {
01266       (*containerlist).clear();
01267       for ( int i = 0; i < containers.size(); i++ ) {
01268         (*containerlist).push_back( RWCString( containers[i].c_str()));
01269       }
01270     } else {
01271       cout << "containers are:" << endl;
01272       for ( int i = 0; i < containers.size(); i++ ) {
01273         cout << "   " << containers[i].c_str() << endl;
01274       }
01275     }
01276 
01277   } while ( 0 );
01278 
01279   DBGRES( status );
01280   return status;
01281 }
01282 
01283 
01284 
01285 
01286 
01287 
01288 
01289 //===========================
01290 //       delete database
01291 //===========================
01292 result cmd_delete( const RWCString& detector, const RWCString& container ) {
01293   BdbCondDatabaseMgr mgr;
01294 
01295   DBG( "-> delete" );
01296   result status = cmd_transaction(TRUE);
01297 
01298   do {
01299     
01300     if ( CMD_SUCCESS != status ) {
01301       break;
01302     }
01303 
01304     if ( BdbcSuccess != mgr.removeIntervalContainer(detector, container, true) ) {
01305       cerr << "failed to delete container" << endl;
01306       status = CMD_WARNING;
01307       break;
01308     }
01309 
01310   } while ( 0 );
01311 
01312   DBGRES( status );
01313   return status;
01314 }
01315 
01316 
01317 
01318 
01319 
01320 
01321 
01322 //===========================
01323 //       cleanup database
01324 //===========================
01325 result cmd_cleanup( const RWCString& detector ) {
01326   std::vector<RWCString> containerlist;
01327 
01328   DBG( "-> cleanup" );
01329   result status = cmd_containers(detector, &containerlist);
01330   
01331   do {
01332 
01333     for ( int i = 0; i < containerlist.size() && CMD_SUCCESS == status ; i++ ) {
01334       status = cmd_delete( detector, containerlist[i] );
01335     }
01336 
01337   } while (0);
01338 
01339   DBGRES( status );
01340   return status;
01341 }
01342 
01343 
01344 
01345 
01346 //=====================================
01347 //       copy container
01348 //=====================================
01349 result cmd_setindexmode( long mode ) {
01350 /*
01351   BdbConditions* theApp = BdbConditions::instance();
01352   switch ( mode ) {
01353    case 0:
01354     theApp->setIndexMode( oocExplicitUpdate );
01355     break;
01356    case 1:
01357     theApp->setIndexMode( oocInsensitive );
01358     break;
01359    case 2:
01360     theApp->setIndexMode( oocSensitive );
01361     break;
01362    default:
01363     cerr << "unknown index mode" << endl;
01364     return CMD_WARNING;
01365   }
01366   return CMD_SUCCESS;
01367 */
01368   cerr << "index mode manipulation is not available in the core code of" << endl
01369        << "of the BDB libraries." << endl;
01370   return CMD_WARNING;
01371 }
01372 
01373 
01374 
01375 //=====================================
01376 //       copy container
01377 //=====================================
01378 result cmd_copy(const RWCString& olddetector, const RWCString& oldcontainer,
01379                 const RWCString& newdetector, const RWCString& newcontainer ) {
01380   BdbCondDatabaseMgr mgr;
01381 
01382   DBG( "-> copy from " << olddetector << " " << oldcontainer );
01383   result status = CMD_SUCCESS;
01384 
01385   do {
01386     if ( CMD_SUCCESS != cmd_transaction(TRUE) ) {
01387       status = CMD_ERROR;
01388       break;
01389     }
01390 
01391     if ( BdbcSuccess != mgr.copyIntervalContainer(olddetector,
01392                                                   oldcontainer,
01393                                                   newdetector,
01394                                                   newcontainer) ) {
01395       cerr << "failed to copy container" << endl;
01396       status = CMD_WARNING;
01397       break;
01398     }
01399 
01400   } while ( 0 );
01401 
01402   DBGRES( status );
01403   return status;
01404 }
01405 
01406 
01407 
01408 
01409 //=====================================
01410 // remove history
01411 //=====================================
01412 result cmd_removehistory(const RWCString& detector, const RWCString& container ) {
01413 
01414   DBG( "-> removehistory" );
01415   result status = cmd_transaction(TRUE);
01416 
01417   do {
01418 
01419     if ( CMD_SUCCESS != status ) break;
01420 
01421     BdbCondDatabaseMgr mgr;
01422     if ( BdbcSuccess != mgr.removeHistory( detector, container ) ) {
01423       status = CMD_WARNING;
01424       break;
01425     }
01426 
01427   } while ( 0 );
01428 
01429   DBGRES( status );
01430   return status;
01431 }
01432 
01433 
01434 //=====================================
01435 // set offset to the begin time of the last interval
01436 //=====================================
01437 result cmd_offsetlast(const RWCString& detector, const RWCString& container,
01438                       long& offset ) {
01439   static BdbTime zeroTime(1996,1,1,0,0,0);
01440 
01441   DBG( "-> offsetlast" );
01442   result status = cmd_transaction(TRUE);
01443 
01444   do {
01445     if ( status != CMD_SUCCESS ) {
01446       break;
01447     }
01448     BdbObsoleteDatabase db(detector);
01449     BdbHandle(BdbInterval) last;
01450 
01451     if ( BdbcSuccess != db.lastInterval(last, container ) ) {
01452       status = CMD_WARNING;
01453       break;
01454     }
01455 
01456     offset = last->getBeginTime().getGmtSec() - zeroTime.getGmtSec();
01457 
01458   } while ( 0 );
01459 
01460   DBGRES( status );
01461   return status;
01462 }
01463 
01464 
01465 
01466 //=====================================
01467 //       scan a container
01468 //=====================================
01469 result cmd_purge(const RWCString& detector, const RWCString& container,
01470                  const BdbTime& start, const BdbTime& end,
01471                  long maxtop ) {
01472 
01473   DBG( "-> purge" );
01474   result status = cmd_transaction(TRUE);
01475 
01476   do {
01477 
01478     if ( CMD_SUCCESS != status ) {
01479       break;
01480     }
01481   
01482     BdbCondDatabaseMgr mgr( true );
01483     if ( BdbcSuccess != mgr.purgeIntervalContainer( detector,
01484                                                    container,
01485                                                    start, end,
01486                                                    false,
01487                                                    maxtop ) ) {
01488       cerr << "cannot purge container" << endl;
01489       status = CMD_WARNING;
01490       break;
01491     }
01492 
01493   } while ( 0 );
01494 
01495   DBGRES( status );
01496   return status;
01497 }
01498 
01499 
01500 
01501 //=====================================
01502 //       scan a container
01503 //=====================================
01504 result cmd_scan(const RWCString& detector, const RWCString& container,
01505                 const BdbTime& theTime ) {
01506 
01507   DBG( "-> scan " << theTime.getGmtSec() );
01508   result status = cmd_transaction(TRUE);
01509 
01510   do {
01511 
01512     if ( CMD_SUCCESS != status ) {
01513       break;
01514     }
01515 
01516     BdbObsoleteDatabase db(detector);
01517     BdbHandle(BdbInterval) intH;
01518     if ( BdbcSuccess != db.firstInterval( intH, container ) ) {
01519       status = CMD_WARNING;
01520       break;
01521     } else if ( intH->inInterval(theTime) ) {
01522       status = CMD_SUCCESS;
01523       break;
01524     } else if ( BdbcSuccess != db.lastInterval( intH, container ) ) {
01525       status = CMD_WARNING;
01526       break;
01527     } else if ( intH->inInterval(theTime) ) {
01528       status = CMD_SUCCESS;
01529       break;
01530     } else;
01531     
01532     BdbHandle(BdbContObj) intervalContainerH;
01533     db.getIntervalContH( intervalContainerH );
01534 
01535     d_ULong gmtTime = theTime.getGmtSec();
01536     ooLookupKey lookupKey( ooTypeN(BdbInterval), 2 );
01537 
01538     ooLessThanEqualLookupField lessThanEqual( ooTypeN(BdbInterval),
01539                                              "_beginTime._gmtSec",
01540                                              & gmtTime );
01541     lookupKey.addField( lessThanEqual );
01542 
01543     ooGreaterThanLookupField greaterThan( ooTypeN(BdbInterval),
01544                                          "_endTime._gmtSec",
01545                                          & gmtTime );
01546     lookupKey.addField( greaterThan );
01547 
01548     BdbItr(BdbInterval) theIntervalItr;
01549     theIntervalItr.scan( intervalContainerH, lookupKey, BdbcRead );
01550 
01551   } while ( 0 );
01552 
01553   DBGRES( status );
01554   return status;
01555 }
01556 
01557 
01558 
01559 
01560 //=====================================
01561 //       find interval in a container
01562 //=====================================
01563 result cmd_findinterval(const RWCString& detector, const RWCString& container,
01564                         const BdbTime& theTime ) {
01565 
01566   DBG( "-> findinterval " << theTime.getGmtSec() );
01567   result status = cmd_transaction(TRUE);
01568 
01569   do {
01570 
01571     if ( CMD_SUCCESS != status ) {
01572       break;
01573     }
01574 
01575     BdbObsoleteDatabase db(detector);
01576     BdbHandle(BdbInterval) intH;
01577     if ( BdbcSuccess != db.findInterval( intH,
01578                                          container,
01579                                          theTime ) ) {
01580       status = CMD_WARNING;
01581       break;
01582     }
01583 
01584   } while ( 0 );
01585 
01586   DBGRES( status );
01587   return status;
01588 }
01589 
01590 
01591 
01592 //=====================================
01593 //       store object by start time
01594 //=====================================
01595 result cmd_store(const RWCString& detector, const RWCString& container,
01596                const RWCString& object, const BdbTime& starttime ) {
01597   BdbHandle(BdbObject) objH;
01598 
01599   DBG( "-> store1 " << starttime.getGmtSec() );
01600   result status = makeObject(detector, object, objH);
01601 
01602   do {
01603 
01604     if ( CMD_SUCCESS != status ) break;
01605 
01606     BdbObsoleteDatabase          db( detector );
01607     if ( BdbcSuccess != db.store( objH, container, starttime ) ) {
01608       cerr << "failed to store" << endl;
01609       status = CMD_ERROR;
01610       break;
01611     }
01612 
01613   } while ( 0 );
01614 
01615   DBGRES( status );
01616   return status;
01617 }
01618 
01619 
01620 
01621 
01622 
01623 
01624 //=====================================
01625 //       store2 object by start and end time
01626 //=====================================
01627 result cmd_store2(const RWCString& detector, const RWCString& container,
01628                   const RWCString& object, const BdbTime& starttime,
01629                   const BdbTime& endtime ) {
01630   BdbHandle(BdbObject) objH;
01631 
01632   DBG( "-> store2 " << starttime.getGmtSec() << ":" << endtime.getGmtSec() );
01633   result status = makeObject(detector, object, objH);
01634 
01635   do {
01636 
01637     if ( CMD_SUCCESS != status ) break;
01638 
01639     BdbObsoleteDatabase db( detector );
01640     if ( BdbcSuccess != db.store( objH, container, starttime, endtime ) ) {
01641       cerr << "failed to store2" << endl;
01642       status = CMD_ERROR;
01643       break;
01644     }
01645   } while ( 0 );
01646 
01647   DBGRES( status );
01648   return status;
01649 
01650 }
01651 
01652 
01653 
01654 
01655 
01656 
01657 //=====================================
01658 //       storeAndTruncate object
01659 //=====================================
01660 result cmd_storetrunc(const RWCString& detector, const RWCString& container,
01661                       const RWCString& object, const BdbTime& starttime,
01662                       const BdbTime& endtime ) {
01663   BdbHandle(BdbObject) objH;
01664 
01665   CLKOUT << "cmd_storetrunc: just entered" << endl;
01666   // DBG( "-> storetrunc " << starttime.getGmtSec() << ":" << endtime.getGmtSec() );
01667   result status = makeObject(detector, object, objH);
01668 
01669   CLKOUT << "cmd_storetrunc: object made" << endl;
01670 
01671   do {
01672 
01673     if ( CMD_SUCCESS != status ) break;
01674 
01675     BdbObsoleteDatabase  db( detector );
01676     if ( BdbcSuccess != db.storeAndTruncate( objH, container, starttime, endtime ) ) {
01677       cerr << "failed to storetrunc" << endl;
01678       status = CMD_ERROR;
01679     }
01680 
01681   } while ( 0 );
01682 
01683   DBGRES( status );
01684   CLKOUT << "cmd_storetrunc: leaving..." << endl;
01685   return status;
01686 
01687 }
01688 
01689 
01690 
01691 
01692 
01693 
01694 //=====================================
01695 //           set container
01696 //=====================================
01697 result cmd_setcontainer(const RWCString& newdetector, const RWCString& newcontainer,
01698                         RWCString& detector, RWCString& container ) {
01699   if ( newdetector == "-" ) {
01700     detector = "emc";
01701   } else if ( newdetector.length() != 3 ) {
01702     cerr << "detector must be a 3 letter acronym." << endl;
01703     return CMD_WARNING;
01704   } else {
01705     detector = newdetector;
01706   }
01707   if ( newcontainer == "-" ) {
01708     container = "000000";
01709   } else {
01710     int dir = 0;
01711     if ( newcontainer == ">" ) {
01712       dir = 1;
01713     } else if ( newcontainer == "<" ) {
01714       dir = -1;
01715     }
01716     if ( dir ) {
01717       char tmp[40];
01718       unsigned long newcont = atol(container) + dir;
01719       sprintf(tmp,"%6.6d",newcont);
01720       container = tmp;
01721     } else {
01722       container = newcontainer;
01723     }
01724   }
01725   return CMD_SUCCESS;
01726 }
01727 
01728 
01729 
01730 
01731 
01732 
01733 result analyzeNode(BdbHandle(BdbIntervalR)& basenodeH, RWCString& rack,
01734                    long& currentnodes, long currentheight, long& currentwidth,
01735                    long& maxheight, long& avgheight ) {
01736 
01737   ++currentnodes;      // number of nodes at this baseline chunk
01738   ++currentheight;     // the height of this node
01739 
01740   BdbItr(BdbIntervalR) itr;
01741   
01742   DBG( "[" << translateSOID(basenodeH) << "]" );
01743 
01744   basenodeH.getNextVers(itr);
01745   RWCString thisrack = rack;
01746   thisrack.append(rack[rack.length()-1],1);
01747 
01748   long deltawidth = 0; // the width of that baseline level
01749   while ( itr.next() ) {
01750 
01751     ++deltawidth;
01752     BdbHandle(BdbIntervalR) nodeH( itr );
01753 
01754     if ( CMD_SUCCESS != analyzeNode(nodeH, thisrack,
01755                                     currentnodes, currentheight, currentwidth,
01756                                     maxheight, avgheight ) ) return CMD_WARNING;
01757 
01758 
01759   }
01760 
01761   if ( deltawidth ) {
01762     // there was the next level
01763     currentwidth += deltawidth - 1;
01764   } else {
01765     // nothing above
01766     DBGL( "" );
01767     DBGL( rack );
01768     if ( currentheight > maxheight ) maxheight = currentheight;
01769     avgheight += maxheight;
01770   }
01771 
01772   rack(rack.length()-1) ^= ('0' ^ 'X');
01773   return CMD_SUCCESS;
01774 }
01775 
01776 
01777 
01778 
01779 //=====================================
01780 //       analyze the container
01781 //=====================================
01782 result cmd_analyze(const RWCString& detector, const RWCString& container) {
01783 
01784   DBGL( "-> analyze" );
01785 
01786   if ( CMD_SUCCESS != cmd_transaction() ) {
01787     return CMD_ERROR;
01788   }
01789   clock_t usecs = clock();
01790   long msecs = getMillis();
01791   result status = CMD_WARNING;
01792 
01793   do {
01794 
01795     BdbIntervalItr itr;
01796     if ( BdbcSuccess != itr.setBaseline(detector,container,BdbTime::minusInfinity) ) {
01797       cerr << "failed to set baseline iterator" << endl;
01798       break;
01799     }
01800 
01801     BdbHandle(BdbIntervalR) basenodeH;
01802     RWCString rack("[0");
01803     long nodes = 0;
01804     long maxnodes = 0;
01805     long intervals = 0;
01806     long maxheight = 0;
01807     long avgheight = 0;
01808     long maxwidth = 0;
01809     long avgwidth = 0;
01810     while ( itr.next() ) {
01811 
01812       basenodeH = (BdbHandle(BdbIntervalR)&) itr;
01813       if ( ! nodes ) DBGL( "the first interval is: [" << translateOID(basenodeH) << "]" );
01814 
01815       long currentheight = 0;
01816       long currentwidth = 1;
01817       long currentnodes = 0;
01818       if ( CMD_SUCCESS != analyzeNode(basenodeH, rack, currentnodes, 
01819                                       currentheight, currentwidth,
01820                                       maxheight, avgheight ) ) break;
01821       if ( currentwidth > maxwidth ) {
01822         maxwidth = currentwidth;
01823       }
01824       avgwidth += currentwidth;
01825       nodes += currentnodes;
01826       if ( currentnodes > maxnodes ) maxnodes = currentnodes;
01827       ++intervals;
01828 
01829     }
01830 
01831     status = CMD_SUCCESS;
01832 
01833     if ( intervals > 0 ) {
01834       avgwidth /= intervals;
01835       avgheight /= intervals;
01836     }
01837 
01838     OUTL("baseints", intervals);
01839     OUTL("nodes   ", nodes    );
01840     OUTL("maxnodes", maxnodes );
01841     OUTL("maxh    ", maxheight);
01842     OUTL("maxw    ", maxwidth );
01843     OUTL("avgh    ", avgheight);
01844     OUTL("avgw    ", avgwidth );
01845 
01846   } while ( 0 );
01847 
01848   usecs = (clock() - usecs)/1000;
01849   msecs = getMillis() - msecs;
01850   OUTL("tasystem", usecs);
01851   OUTL("taastro ", msecs );
01852   return status;
01853 }
01854 
01855 
01856 
01857 
01858 
01859 //=====================================
01860 //      store a vector of objects
01861 //=====================================
01862 result cmd_storevectorn(const RWCString& detector, const RWCString& container,
01863                         int elts, const RWCString& object,
01864                         const BdbTime& starttime, const BdbTime& offsettime ) {
01865   std::vector<BdbCondStoreTime> objvector;
01866   BdbHandle(BdbObject) objH;
01867 
01868   int idx = 0;
01869   long d = offsettime.getGmtSec() - starttime.getGmtSec();
01870   DBG("-> storevectorn " << elts << " of " << object << " at " << starttime.getGmtSec() << " " << d);
01871 
01872   result status = CMD_SUCCESS;
01873   while ( CMD_SUCCESS == status && idx < elts ) {
01874     if ( 0 == idx && object == "-" ) {
01875       status = makeObject( detector, "a", objH );
01876     } else {
01877       status = makeObject( detector, object, objH );
01878     }
01879     if ( CMD_SUCCESS == status ) {
01880       objvector.push_back( BdbCondStoreTime(starttime + (long)d*idx, objH) );
01881     }
01882     ++idx;
01883   }
01884   if ( CMD_SUCCESS == status ) {
01885     status = makeObject( detector, "-", objH );
01886     objvector.push_back( BdbCondStoreTime(starttime + d*elts, objH) );
01887 
01888     BdbObsoleteDatabase db( detector );
01889     if ( BdbcSuccess != db.storeVector( objvector, container ) ) {
01890       cerr << "failed to storevectorn" << endl;
01891       status = CMD_ERROR;
01892     }
01893   }
01894 
01895   DBGRES( status );
01896   return status;
01897 }
01898 
01899 
01900 
01901 
01902 //=====================================
01903 //      store a vector of objects
01904 //=====================================
01905 result cmd_storevector(const RWCString& detector, const RWCString& container,
01906                        int elts,
01907                        RWTValOrderedVector<RWCString>& strings,
01908                        RWTValOrderedVector<BdbTime>& times ) {
01909   std::vector<BdbCondStoreTime> objvector;
01910 
01911   BdbHandle(BdbObject) objH;
01912   DBG( "-> storevector of " << elts );
01913 
01914   int idx = 0;
01915   result status;
01916   while ( idx < elts ) {
01917     if ( 0 == idx && "-" == strings(idx) ) {
01918       status = makeObject( detector, "a", objH );
01919     } else {
01920       status = makeObject( detector, strings(idx), objH );
01921     }
01922     if ( CMD_SUCCESS != status ) break;
01923     objvector.push_back( BdbCondStoreTime(times(idx),objH) );
01924     ++idx;
01925   }
01926 
01927   if ( CMD_SUCCESS == status ) {
01928 
01929     // terminator
01930     status = makeObject( detector, "-", objH );
01931     objvector.push_back( BdbCondStoreTime(times(idx),objH) );
01932 
01933     BdbObsoleteDatabase db( detector );
01934     if ( BdbcSuccess != db.storeVector( objvector, container ) ) {
01935       cerr << "failed to store vector" << endl;
01936       status = CMD_ERROR;
01937     }
01938 
01939   }
01940 
01941   DBGRES( status );
01942   return status;
01943 }

 


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

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