Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DbiResultAgg.cxx

Go to the documentation of this file.
00001 // $Id: DbiResultAgg.cxx,v 1.29 2008/05/12 09:59:11 nwest Exp $
00002 
00003 
00004 #include <algorithm>
00005 #include <map>
00006 #include <vector>
00007 
00008 #include "DatabaseInterface/DbiCache.h"
00009 #include "DatabaseInterface/DbiBinaryFile.h"
00010 #include "DatabaseInterface/DbiDBProxy.h"
00011 #include "DatabaseInterface/DbiResultAgg.h"
00012 #include "DatabaseInterface/DbiResultNonAgg.h"
00013 #include "DatabaseInterface/DbiResultKey.h"
00014 #include "DatabaseInterface/DbiResultSet.h"
00015 #include "DatabaseInterface/DbiTableRow.h"
00016 #include "DatabaseInterface/DbiTimerManager.h"
00017 #include "DatabaseInterface/DbiValidityRecBuilder.h"
00018 #include "MessageService/MsgService.h"
00019 #include "Validity/VldRange.h"
00020 #include "Util/UtilString.h"
00021 
00022 ClassImp(DbiResultAgg)
00023 
00024 typedef vector<const DbiResult*>::const_iterator ConstResultItr_t;
00025 
00026 
00027 //   Definition of static data members
00028 //   *********************************
00029 
00030 CVSID("$Id: DbiResultAgg.cxx,v 1.29 2008/05/12 09:59:11 nwest Exp $");
00031 
00032 
00033 //    Definition of all member functions (static or otherwise)
00034 //    *******************************************************
00035 //
00036 //    -  ordered: ctors, dtor, operators then in alphabetical order.
00037 
00038 //.....................................................................
00039 
00040 DbiResultAgg::DbiResultAgg(const string& tableName,
00041                            const DbiTableRow* tableRow,
00042                            DbiCache* cache,
00043                            const DbiValidityRecBuilder* vrecBuilder,
00044                            const DbiDBProxy* proxy,
00045                            const string& sqlQualifiers) :
00046 DbiResult(0,0,sqlQualifiers),
00047 fSize(0)
00048 {
00049 //
00050 //
00051 //  Purpose:  Default constructor
00052 //
00053 //  Arguments: 
00054 //      tableName    in     Name of table.                       
00055 //      tableRow     in     Pointer to a sample tableRow object. 
00056 //                          May be null.
00057 //      cache        in/out Pointer to a cache. May be null.
00058 //      vrecBuilder  in     Pointer to validity record builder containing
00059 //                          aggregated records from query. May be null
00060 //      proxy         in    Pointer to database proxy. May be null.
00061 //      sqlQualifiers in    Extended Context sql qualifiers
00062 //
00063 //  Return:    n/a
00064 //
00065 //  Contact:   N. West
00066 //
00067 //  Specification:-
00068 //  =============
00069 //
00070 //  o Create Result from DbiResultSet generated by query.
00071 
00072 
00073 //  Program Notes:-
00074 //  =============
00075 
00076 //  tableRow is just used to create new subclass DbiTableRow objects.
00077 
00078   LEA_CTOR    //Leak Checker
00079 
00080   typedef map<UInt_t,UInt_t> seqToRow_t;
00081 
00082   MSG("Dbi", Msg::kVerbose) << "Creating DbiResultAgg" << endl;
00083   SetTableName(tableName);
00084   if ( ! tableRow || ! cache || ! vrecBuilder || ! proxy ) return;
00085 
00086 // Unpack the extended context SQL qualifiers.
00087 // Don't use StringTok - it eats null strings 
00088 // e.g. abc;;def gives 2 substrings.
00089 
00090   string::size_type loc  = sqlQualifiers.find(';');
00091   string::size_type loc2 = sqlQualifiers.find(';',loc+1);
00092   string sqlData  = string(sqlQualifiers,loc+1,loc2-loc-1);
00093   string fillOpts = string(sqlQualifiers,loc2+1);
00094 
00095 //Loop over all rows looking to see if they are already in
00096 //the cache, and if not, recording their associated sequence numbers
00097 
00098   vector<UInt_t> reqSeqNos;  // Sequence numbers required from DB.
00099   seqToRow_t seqToRow;       // Map SeqNo - > RowNo.
00100 //  Set up a Default database number, it will be updated if anything
00101 //  needs to be read from the database.
00102   UInt_t dbNo = 0;
00103   Int_t maxRowNo = vrecBuilder->GetNumValidityRec() - 1;
00104 
00105 //Ignore the first entry from the validity rec builder, it will be
00106 //for Agg No = -1, which should not be present for aggregated data.
00107   for ( Int_t rowNo = 1; rowNo <= maxRowNo; ++rowNo ) {
00108     const DbiValidityRec& vrecRow = vrecBuilder->GetValidityRec(rowNo);
00109 
00110 //  If its already in the cache, then just connect it in.
00111     const DbiResult* res = cache->Search(vrecRow,sqlQualifiers);
00112     MSG("Dbi", Msg::kVerbose) << "Checking validity rec " << rowNo
00113                               << " " << vrecRow
00114                               << "SQL qual: " << sqlQualifiers
00115                               << " cache search: " << (void*) res << endl;
00116     if ( res ) {
00117       fResults.push_back(res);
00118       res->Connect();
00119       fSize += res->GetNumRows();
00120     }
00121 
00122 //  If its not in the cache, but represents a gap, then create an empty
00123 //  DbiResult and add it to the cache.
00124     else if ( vrecRow.IsGap() ) {
00125       DbiResult* newRes = new DbiResultNonAgg(0, tableRow, &vrecRow);
00126       cache->Adopt(newRes,false);
00127       fResults.push_back(newRes);
00128       newRes->Connect();
00129     }
00130 
00131 //  Neither in cache, nor a gap, so record its sequence number.
00132     else {
00133       UInt_t seqNo = vrecRow.GetSeqNo();
00134       reqSeqNos.push_back(seqNo);
00135       seqToRow[seqNo] = rowNo;
00136       fResults.push_back(0);
00137 //    All data must come from a single database, so any vrec will
00138 //    do to define which one.
00139       dbNo = vrecRow.GetDbNo();
00140     }
00141   }
00142 
00143 //If there are required sequence numbers, then read them from the
00144 //database and build DbiResults for each.
00145 
00146   if ( reqSeqNos.size() ) {
00147 //  Sort into ascending order; it may simplify the query which will
00148 //  block ranges of sequence numbers together.
00149     sort(reqSeqNos.begin(),reqSeqNos.end());
00150     DbiResultSet* rs = proxy->QuerySeqNos(reqSeqNos,dbNo,sqlData,fillOpts);
00151 //  Flag that data was read from Database.
00152     this->SetResultsFromDb();
00153     DbiTimerManager::gTimerManager.StartSubWatch(1);
00154     while ( ! rs->IsExhausted() ) {
00155       Int_t seqNo;
00156       *rs >> seqNo;
00157       rs->DecrementCurCol();
00158       Int_t rowNo = -2;
00159       if ( seqToRow.find(seqNo) == seqToRow.end() ) {
00160         MSG("Dbi", Msg::kError)
00161           << "Unexpected SeqNo: " << seqNo << endl;
00162       }
00163       else {
00164         rowNo = seqToRow[seqNo];
00165         MSG("Dbi", Msg::kVerbose)
00166           << "Procesing SeqNo: " << seqNo 
00167           << " for row " << rowNo << endl;
00168       }
00169 
00170       const DbiValidityRec& vrecRow = vrecBuilder->GetValidityRec(rowNo);
00171       DbiResultNonAgg* newRes = new DbiResultNonAgg(rs,tableRow,&vrecRow);
00172 //    Don't allow results from Extended Context queries to be reused.
00173       if ( this->IsExtendedContext() ) newRes->SetCanReuse(false);
00174       if ( rowNo == -2 ) {
00175         delete newRes;
00176       }
00177       else {
00178         MSG("Dbi", Msg::kVerbose)
00179           << "SeqNo: " << seqNo 
00180           << " produced " << newRes->GetNumRows() << " rows" << endl;
00181 //      Adopt but don't register key for this component, only the overall DbiResultAgg
00182 //      will have a registered key.
00183         cache->Adopt(newRes,false);
00184         fResults[rowNo-1] = newRes;
00185         newRes->Connect();
00186         fSize += newRes->GetNumRows();
00187       }
00188     }
00189 
00190 //  DbiResultSet fully processed, so delete it.
00191     delete rs;
00192   }
00193 
00194 //All component DbiResultNonAgg objects have now been located and
00195 //connected in, so set up their access keys and determine the validty
00196 //range by ANDing the time windows together.
00197 
00198   fRowKeys.reserve(fSize); 
00199 
00200   DbiValidityRec vRec = vrecBuilder->GetValidityRec(1);
00201   for ( Int_t rowNo = 1; rowNo <= maxRowNo; ++rowNo ) {
00202 
00203     const DbiValidityRec& vrecRow = vrecBuilder->GetValidityRec(rowNo);
00204     VldRange r = vrecRow.GetVldRange();
00205     vRec.AndTimeWindow(r.GetTimeStart(),r.GetTimeEnd());
00206 
00207     const DbiResult* res = fResults[rowNo-1];
00208     if ( res ) {
00209       UInt_t numEnt = res->GetNumRows();
00210       for (UInt_t entNo = 0; entNo < numEnt; ++entNo ) 
00211          fRowKeys.push_back(res->GetTableRow(entNo));
00212     }
00213   }
00214 
00215 // Now that the row look-up table has been built the natural index
00216 // look-up table can be filled in.
00217   this->BuildLookUpTable();
00218 
00219 // Set aggregate number to -1 to show that it has multiple aggregates
00220   vRec.SetAggregateNo(-1);
00221   SetValidityRec(vRec);
00222 
00223   MSG("Dbi",Msg::kDebug) 
00224     << "Aggregate contains " << fSize  << " entries.  vRec:-" << endl
00225     << vRec << endl;
00226 
00227    MSG("Dbi",Msg::kSynopsis) << "Created aggregated result set no. of rows: " 
00228                              << this->GetNumRows() << endl;
00229 
00230 }
00231 
00232 //.....................................................................
00233 
00234 DbiResultAgg::~DbiResultAgg() {
00235 //
00236 //
00237 //  Purpose: Destructor
00238 //
00239 //  Arguments: 
00240 //    None.
00241 //
00242 //  Return:    n/a
00243 //
00244 //  Contact:   N. West
00245 //
00246 //  Specification:-
00247 //  =============
00248 //
00249 //  o  Destroy ResultAgg and disconnect all associated DbiResults,
00250 
00251 
00252 //  Program Notes:-
00253 //  =============
00254 
00255 //  None.
00256 
00257   LEA_DTOR    //Leak Checker
00258 
00259   MSG("Dbi", Msg::kVerbose) << "Destroying DbiResultAgg."  << endl;
00260 
00261   for ( ConstResultItr_t itr = fResults.begin();
00262         itr != fResults.end();
00263         ++itr) if ( *itr ) (*itr)->Disconnect();
00264 
00265 }
00266 //.....................................................................
00267 
00268 DbiResultKey* DbiResultAgg::CreateKey() const {
00269 //
00270 //
00271 //  Purpose:  Create a key that corresponds to this result.
00272 //            Caller must take ownership.
00273 
00274   DbiResultKey* key = 0;
00275   for ( ConstResultItr_t itr = fResults.begin();
00276         itr != fResults.end();
00277         ++itr ) {
00278     const DbiResult* result = *itr;
00279     if ( result ) {
00280       // Create key from first result.
00281       if ( ! key ) key = result->CreateKey();
00282       // Extend key from the remainder.
00283       else {
00284         const DbiValidityRec& vrec = result->GetValidityRec();
00285         key->AddVRecKey(vrec.GetSeqNo(),vrec.GetCreationDate());
00286       }
00287     }
00288   }
00289 
00290 // Should not have an empty set, but just in case.
00291   if ( ! key ) key = new DbiResultKey();
00292 
00293   return key;
00294 
00295 }
00296 //.....................................................................
00297 
00298 const DbiTableRow* DbiResultAgg::GetTableRow(UInt_t row) const {
00299 //
00300 //
00301 //  Purpose:  Return TableRow from last query.
00302 //
00303 //  Arguments: 
00304 //    row         in    Required row.
00305 //
00306 //  Return:    TableRow ptr, or =0 if no row.
00307 //
00308 //  Contact:   N. West
00309 
00310 //  Program Notes:-
00311 //  =============
00312 
00313 //  None.
00314 
00315   return  ( row >= fRowKeys.size() ) ? 0 : fRowKeys[row];
00316 
00317 }
00318 
00319 //.....................................................................
00320 
00321 const DbiValidityRec& DbiResultAgg::GetValidityRec(
00322                                   const DbiTableRow* row) const {
00323 //
00324 //
00325 //  Purpose:  Get DbiValidityRec associated with table or row.
00326 //
00327 //  Arguments: 
00328 //    row          in    The table row
00329 //                       or null (default) to get table DbiValidityRec
00330 //
00331 //  Contact:   N. West
00332 //
00333 
00334 //  Program Notes:-
00335 //  =============
00336 
00337 //  The DbiValidityRec for the in-memory table is effectively an 
00338 //  AND of the DbiValidityRecs of the individual rows.
00339 
00340  if ( ! row ) return this->GetValidityRecGlobal();
00341  DbiResult* owner = row->GetOwner();
00342  return owner ? owner->GetValidityRecGlobal() : this->GetValidityRecGlobal();
00343 
00344 }
00345 //.....................................................................
00346 
00347 Bool_t DbiResultAgg::Satisfies(const string& sqlQualifiers)  {
00348 //
00349 //
00350 //  Purpose:  Return true if result satisfies extended context query.
00351 //
00352 
00353   MSG("Dbi",Msg::kDebug) 
00354     << "Trying to satisfy: SQL: " << sqlQualifiers
00355     << "\n with CanReuse: " << this->CanReuse() 
00356     << " sqlQualifiers: " << this->GetSqlQualifiers() 
00357     << endl;
00358  return    this->CanReuse()
00359         && this->GetSqlQualifiers() == sqlQualifiers;
00360 }
00361 
00362 //.....................................................................
00363 
00364 void DbiResultAgg::Streamer(DbiBinaryFile& bf) {
00365 //
00366 //
00367 //  Purpose:  I/O to binary file
00368 //
00369 //  Specification:-
00370 //  =============
00371 
00372 //  Output constituent non-gap DbiResultNonAgg objects.
00373 
00374   vector<const DbiResult*>::const_iterator itr = fResults.begin();
00375   vector<const DbiResult*>::const_iterator end = fResults.end();
00376 
00377   UInt_t numNonAgg = 0;
00378   for (; itr != end; ++itr) {
00379     const DbiResultNonAgg* rna = dynamic_cast<const DbiResultNonAgg*>(*itr);
00380     if ( rna && ! rna->GetValidityRecGlobal().IsGap() ) ++numNonAgg;
00381   }
00382   bf << numNonAgg;
00383 
00384   
00385   for (itr = fResults.begin(); itr != end; ++itr) {
00386     const DbiResultNonAgg* rna = dynamic_cast<const DbiResultNonAgg*>(*itr);
00387     if ( rna && ! rna->GetValidityRecGlobal().IsGap() ) bf << *rna;
00388   }
00389 }
00390 
00391 /*    Template for New Member Function
00392 
00393 //.....................................................................
00394 
00395 DbiResultAgg:: {
00396 //
00397 //
00398 //  Purpose:  
00399 //
00400 //  Arguments: 
00401 //    xxxxxxxxx    in    yyyyyy
00402 //
00403 //  Return:    
00404 //
00405 //  Contact:   N. West
00406 //
00407 //  Specification:-
00408 //  =============
00409 //
00410 //  o 
00411 
00412 //  Program Notes:-
00413 //  =============
00414 
00415 //  None.
00416 
00417 
00418 }
00419 
00420 */
00421 

Generated on Mon Feb 15 11:06:34 2010 for loon by  doxygen 1.3.9.1