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  

/BdbBitMap/BdbBMQueryCmd.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //         $Id: BdbBMQueryCmd.cc,v 1.4 1999/02/03 19:53:56 gapon Exp $
00004 //
00005 // Description:
00006 //        Command line utility used to make queries to the indexing
00007 //      database.
00008 //
00009 // Environment:
00010 //        Software developed for the BaBar Detector at the SLAC B-Factory.
00011 //
00012 // Author List:
00013 //        Igor A. Gaponenko                Original Author
00014 //
00015 //------------------------------------------------------------------------
00016 
00017 // ---------------
00018 // -- C Headers --
00019 // ---------------
00020 
00021 #include <string.h>
00022 #include <assert.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 
00026 // -----------------
00027 // -- C++ Headers --
00028 // -----------------
00029 
00030 #include <iostream.h>
00031 #include <fstream.h>
00032 
00033 // ---------------------------------
00034 // -- Collaborating Class Headers --
00035 // ---------------------------------
00036 
00037 #include "BdbUtil/Bdb.hh"
00038 #include "BdbApplication/BdbApplication.hh"
00039 #include "BdbAccess/BdbDbTreeSingleton.hh"
00040 #include "BdbTrees/BdbDbRegistrator.hh"
00041 #include "BdbEventStore/BdbEventStore.hh"
00042 #include "BdbIndexing/BdbQuery.hh"
00043 #include "BdbBitMap/BdbBMIndexQueryAPI.hh"
00044 
00045 // -------------------------------------------------------------------------
00046 // -- Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00047 // -------------------------------------------------------------------------
00048 
00049 static const char rcsid[] = "$Id: BdbBMQueryCmd.cc,v 1.4 1999/02/03 19:53:56 gapon Exp $";
00050 
00051 /**
00052  ** usage() - print "usage" information page.
00053  **/
00054 
00055 void usage(const char* message)
00056 {
00057     cout << endl
00058          << "BdbBMQueryCmd: " << message << endl
00059          << endl
00060          << "USAGE: BdbBMQueryCmd help" << endl
00061          << "       BdbBMQueryCmd help sql" << endl
00062          << "       BdbBMQueryCmd help index" << endl
00063          << "       BdbBMQueryCmd estimate <index> [-f <sql-file>]" << endl
00064          << "       BdbBMQueryCmd execute  <index> [-f <sql-file>] -c <collection>" << endl
00065          << endl ;
00066 }
00067 
00068 /**
00069  ** help(), help_sql(), help_index() - print "help" information pages
00070  **/
00071 
00072 void help()
00073 {
00074     cout << endl
00075          << "DESCRIPTION: command line interface to BDB index query processor." << endl
00076          << endl
00077          << "REQUIREMENTS:" << endl
00078          << endl
00079          << "    Environment variable OO_FD_BOOT must point to the Objectivity/DB (TM)" << endl
00080          << "    boot file location." << endl
00081          << endl
00082          << "GENERAL SYNTAX:" << endl
00083          << endl
00084          << "    BdbBMQueryCmd <command-name> [<par1> [<par2> ... ]]" << endl
00085          << endl
00086          << "COMMANDS:" << endl
00087          << endl
00088          << "    help        - print this page or more information on specified topic" << endl
00089          << "    estimate    - perform an estimation (=\"trial\") query request" << endl
00090          << "    execute     - perform a real query and create new events collection" << endl
00091          << endl
00092          << "SYNTAX OF THE COMMANDS:" << endl
00093          << endl
00094          << "    BdbBMQueryCmd help [<topic>]" << endl
00095          << "    BdbBMQueryCmd estimate <index> [-f <sql-file>]" << endl
00096          << "    BdbBMQueryCmd execute  <index> [-f <sql-file>] -c <collection>" << endl
00097          << endl
00098          << "PARAMETERS:" << endl
00099          << endl
00100          << "    index      - index name (see: \"help index\" for more detales" << endl
00101          << "    sql-file   - a file containing SQL query description(use: \"help sql\")" << endl
00102          << "    collection - is Babar Event Store collection name" << endl
00103          << endl ;
00104 }
00105 
00106 void help_sql()
00107 {
00108     cout << endl
00109          << "SQL FILE SYNTAX:" << endl
00110          << endl
00111          << "    ...to be implemented..." << endl
00112          << endl
00113          << "EXAMPLE:" << endl
00114          << endl
00115          << "    ...to be implemented..." << endl
00116          << endl ;
00117 }
00118 
00119 void help_index()
00120 {
00121     cout << endl
00122          << "NAMING CONVENTIONS FOR INDEXES :" << endl
00123          << endl
00124          << "    The indexes are named and physically stored in the database" << endl
00125          << "    according to a role a user plays in the database." << endl
00126          << "    The role is determined by the current authorization level." << endl
00127          << "    The user may change the roles as freely as it is allowed by" << endl
00128          << "    his authorization priveleges." << endl
00129          << endl
00130          << "    In connection with said above, the following general syntax" << endl
00131          << "    for the indexes names is supported by this Utility: " << endl
00132          << endl
00133          << "        /<auth-level>/[<auth-name>/]<index-name>" << endl
00134          << endl
00135          << "    WHERE:" << endl
00136          << endl
00137          << "        <auth-level>  - is  {system|group|user}" << endl
00138          << "        <auth-name>   - is a group name for \"group\" autharization level" << endl
00139          << "                        or is a user name for \"user\" authorization level" << endl
00140          << "        <index-name>  - is an index name itself." << endl
00141          << endl
00142          << "    NOTE:" << endl
00143          << endl
00144          << "        The naming conventions assume that all the names are the" << endl
00145          << "        case-sensitive." << endl
00146          << "        Beware, also, that each index is uniquely identified by a fully" << endl
00147          << "        qualified string, rather then by just its last name." << endl
00148          << endl
00149          << "EXAMPLE:" << endl
00150          << endl
00151          << "    System scope index: /system/Index1" << endl
00152          << "    Group scope index:  /group/students/Index2" << endl
00153          << "    User scope index:   /users/quarrie/Index3" << endl
00154          << endl ;
00155 }
00156 
00157 // --------------------------------------------
00158 // -- Forward declaration of the "executors" --
00159 // --------------------------------------------
00160 
00161 
00162 static int query_estimate(const char* index,
00163                           const char* sql_file = (char*) 0) ;
00164 
00165 static int query_execute(const char* index,
00166                          const char* collection,
00167                          const char* sql_file = (char*) 0) ;
00168 
00169 static int read_from_stdin(char* buf, int buflen) ;
00170 static int read_from_file(char* buf, int buflen, const char* name) ;
00171 
00172 // -------------------
00173 // -- Main Function --
00174 // -------------------
00175 
00176 int
00177 main( int argc, char* argv[] )
00178 {
00179 
00180   // Initialize Objectivity/DB client context.
00181 
00182     ooInit() ;
00183 
00184     BdbDbRegistrator* registrator = new BdbDbRegistrator ;
00185     BdbDbTreeSingleton::instance()->setRegistrator(registrator) ;
00186     BdbEventStore::instance()->configureClustering() ;
00187 
00188     if ( argc < 2 ) {
00189         usage("too few arguments") ;
00190                return 1 ;
00191     }
00192 
00193     while ( 1 ) {
00194 
00195       // --- help --- //
00196 
00197         if ( ! strcmp("help", argv[1])) {
00198 
00199             switch ( argc ) {
00200                    case 2:
00201 
00202                        help() ;
00203                 break ;
00204 
00205                    case 3:
00206 
00207                        if ( ! strcmp("sql", argv[2])) {
00208                     help_sql() ;
00209                     break ;
00210                 }
00211 
00212                 usage("unknown help topic") ;
00213                 return 1 ;
00214 
00215             default:
00216 
00217                 usage("illegal number of parameters for \"help\"") ;
00218                 return 1 ;
00219 
00220             }
00221             break ;
00222         }
00223 
00224       // --- estimate --- //
00225 
00226         if ( ! strcmp("estimate", argv[1])) {
00227 
00228             switch ( argc ) {
00229             case 3:
00230 
00231                 return query_estimate(argv[2]) ;
00232 
00233             case 5:
00234 
00235                 if ( ! strcmp("-f", argv[3])) {
00236                     return query_estimate(argv[2], argv[4]) ;
00237                 } else {
00238                     usage("illegal switch for \"estimate\"") ;
00239                     return 1 ;
00240                 }
00241 
00242             default:
00243 
00244                 usage("illegal number of parameters for \"estimate\"") ;
00245                 return 1 ;
00246 
00247             }
00248             break ;
00249         }
00250 
00251       // --- execute --- //
00252 
00253         if ( ! strcmp("execute", argv[1])) {
00254 
00255             switch ( argc ) {
00256             case 5:
00257 
00258                 return query_execute(argv[2], argv[4]) ;
00259 
00260             case 7:
00261 
00262                 if ( ! strcmp("-f", argv[3])) {
00263                     return query_execute(argv[2], argv[6], argv[4]) ;
00264                 } else {
00265                     usage("illegal switch for \"execute\"") ;
00266                     return 1 ;
00267                 }
00268 
00269             default:
00270 
00271                 usage("illegal number of parameters for \"execute\"") ;
00272                 return 1 ;
00273 
00274             }
00275             break ;
00276         }
00277 
00278         usage("unknonw command specified") ;
00279         return 1 ;
00280     }
00281 
00282     return 0 ;
00283 }
00284 
00285 //-------------------------------------------------
00286 // Perform an estimation query and print results --
00287 //-------------------------------------------------
00288 
00289 static int query_estimate(const char* index,
00290                           const char* sql_file)
00291 {
00292     BdbStatus status ;
00293 
00294   // Load SQL expression.
00295 
00296     const int sql_max_len = 64*1024 ;
00297     char      sql[sql_max_len] ;
00298 
00299     if ( sql_file == (char*) 0 ) {
00300 
00301         cout << "Enter SQL expression below. Complete your input by CONTROL-D" << endl ;
00302 
00303         if ( ! read_from_stdin(sql, sql_max_len)) {
00304             return 1 ;
00305         }
00306 
00307     } else {
00308 
00309         if ( ! read_from_file(sql, sql_max_len, sql_file)) {
00310             return 1 ;
00311         }
00312     }
00313 
00314   // Initialize query processor.
00315   // Translate and verify SQL request.
00316 
00317     BdbBMIndexQueryAPI* qprocessor = new BdbBMIndexQueryAPI() ;
00318     BdbQuery query(qprocessor) ;
00319 
00320     qprocessor->startUpdate() ;
00321 
00322     while ( 1 ) {
00323 
00324         status = qprocessor->connect(index) ;
00325         if ( BdbcSuccess != status ) {
00326             break ;
00327         }
00328 
00329         status = query.translateAndVerify(sql) ;
00330         if ( BdbcSuccess != status ) {
00331             break ;
00332         }
00333 
00334         status = query.estimate() ;
00335         if ( BdbcSuccess != status ) {
00336             break ;
00337         }
00338 
00339       // Print resuling information.
00340 
00341         cout << "Query estimation results ===>" << endl
00342              << endl
00343              << "  * Total number of indexed events:      " << query.getTotalNumEvents() << endl
00344              << endl
00345              << "  * Number of events matching the query: " << query.getNumEvents() << endl
00346              << "  * Number of involved DBs:              " << query.getNumDBs() << endl
00347              << "  * Number of involved Tapes:            " << query.getNumTapes() << endl
00348              << endl ;
00349 
00350         break ;
00351     }
00352     qprocessor->commit() ;
00353 
00354     return ( BdbcSuccess != status ) ;
00355 }
00356 
00357 //------------------------------------------
00358 // Perform a real query and print results --
00359 //------------------------------------------
00360 
00361 static int query_execute(const char* index,
00362                          const char* collection,
00363                          const char* sql_file)
00364 {
00365     BdbStatus status ;
00366 
00367   // Load SQL expression.
00368 
00369     const int sql_max_len = 64*1024 ;
00370     char      sql[sql_max_len] ;
00371 
00372     if ( sql_file == (char*) 0 ) {
00373 
00374         cout << "Enter SQL expression below. Complete your input by CONTROL-D" << endl ;
00375 
00376         if ( ! read_from_stdin(sql, sql_max_len)) {
00377             return 1 ;
00378         }
00379 
00380     } else {
00381 
00382         if ( ! read_from_file(sql, sql_max_len, sql_file)) {
00383             return 1 ;
00384         }
00385     }
00386 
00387   // Initialize query processor.
00388   // Translate and verify SQL request.
00389 
00390     BdbBMIndexQueryAPI* qprocessor = new BdbBMIndexQueryAPI() ;
00391     BdbQuery query(qprocessor) ;
00392 
00393     qprocessor->startUpdate() ;
00394 
00395     while ( 1 ) {
00396 
00397         status = qprocessor->connect(index) ;
00398         if ( BdbcSuccess != status ) {
00399             break ;
00400         }
00401 
00402         status = query.translateAndVerify(sql) ;
00403         if ( BdbcSuccess != status ) {
00404             break ;
00405         }
00406 
00407         status = query.execute(collection) ;
00408         if ( BdbcSuccess != status ) {
00409             break ;
00410         }
00411 
00412       // Print resuling information.
00413 
00414         cout << "Query execution results ===>" << endl
00415              << endl
00416              << "  * Total number of indexed events:      " << query.getTotalNumEvents() << endl
00417              << endl
00418              << "  * Number of events matching the query: " << query.getNumEvents() << endl
00419              << "  * Number of involved DBs:              " << query.getNumDBs() << endl
00420              << "  * Number of involved Tapes:            " << query.getNumTapes() << endl
00421              << endl ;
00422 
00423         break ;
00424     }
00425     qprocessor->commit() ;
00426 
00427     return ( BdbcSuccess != status ) ;
00428 }
00429 
00430 // ---------------------------------------------------------
00431 // Read up to given number of character from the Standard --
00432 // input stream into a buffer.                            --
00433 // ---------------------------------------------------------
00434 
00435 static int read_from_stdin(char* buf, int buflen)
00436 {
00437     int next = 0 ;
00438     int c ;
00439 
00440     do {
00441         c = getc(stdin) ;
00442         buf[next++] = c ;
00443     } while (( next < buflen ) && ( c != EOF )) ; 
00444 
00445     if ( next == buflen ) {
00446 
00447         cout << "BdbBMQueryCmd - SQL expression is longer then "
00448              << buflen << " characters." << endl ;
00449 
00450         return 0 ;
00451 
00452     } else {
00453 
00454         buf[next-1] = '\0' ;
00455 
00456         return 1 ;
00457     }
00458 }
00459 
00460 // --------------------------------------------------
00461 // Open specified file and read up to given number --
00462 // of character into a buffer.                     --
00463 // --------------------------------------------------
00464 
00465 static int read_from_file(char* buf, int buflen, const char* name)
00466 {
00467     FILE* infile = fopen(name, "r") ;
00468     int next = 0 ;
00469     int c ;
00470 
00471     if ( infile == (FILE*) 0 ) {
00472         cout << "BdbBMQueryCmd - input file open error." << endl ;
00473         return 0 ;
00474     }
00475 
00476     do {
00477         c = getc(infile) ;
00478         buf[next++] = c ;
00479     } while (( next < buflen ) && ( c != EOF )) ; 
00480 
00481     if ( next == buflen ) {
00482 
00483         cout << "BdbBMQueryCmd - input file is longer then "
00484              << buflen << " characters." << endl ;
00485 
00486         fclose(infile) ;
00487 
00488         return 0 ;
00489 
00490     } else {
00491 
00492         buf[next-1] = '\0' ;
00493 
00494         fclose(infile) ;
00495 
00496         return 1 ;
00497     }
00498 }
00499 
00500 /////////////////
00501 // End Of File //
00502 /////////////////

 


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

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