DbiResultAgg Class Reference

#include <DbiResultAgg.h>

Inheritance diagram for DbiResultAgg:
DbiResult

List of all members.

Public Member Functions

 DbiResultAgg (const string &tableName="Unknown", const DbiTableRow *tableRow=0, DbiCache *cache=0, const DbiValidityRecBuilder *vrecBuilder=0, const DbiDBProxy *proxy=0, const string &sqlQualifiers="")
virtual ~DbiResultAgg ()
virtual DbiResultKeyCreateKey () const
virtual UInt_t GetNumAggregates () const
virtual UInt_t GetNumRows () const
virtual const DbiTableRowGetTableRow (UInt_t row) const
virtual const DbiValidityRecGetValidityRec (const DbiTableRow *row=0) const
virtual Bool_t Satisfies (const string &sqlQualifiers)
virtual void Streamer (DbiBinaryFile &bf)

Private Attributes

std::vector< const DbiResult * > fResults
std::vector< const DbiTableRow * > fRowKeys
 Look-up: Row no. -> TableRow.
UInt_t fSize
 Current set size.

Detailed Description

Id
DbiResultAgg.h,v 1.15 2006/08/08 10:51:32 west Exp

Definition at line 53 of file DbiResultAgg.h.


Constructor & Destructor Documentation

DbiResultAgg::DbiResultAgg ( const string &  tableName = "Unknown",
const DbiTableRow tableRow = 0,
DbiCache cache = 0,
const DbiValidityRecBuilder vrecBuilder = 0,
const DbiDBProxy proxy = 0,
const string &  sqlQualifiers = "" 
)

Definition at line 40 of file DbiResultAgg.cxx.

References DbiValidityRec::AndTimeWindow(), DbiResult::Connect(), DbiValidityRec::GetDbNo(), DbiResultNonAgg::GetNumRows(), DbiResult::GetNumRows(), DbiValidityRec::GetSeqNo(), DbiResult::GetTableRow(), VldRange::GetTimeEnd(), VldRange::GetTimeStart(), DbiValidityRec::GetVldRange(), DbiTimerManager::gTimerManager, DbiValidityRec::IsGap(), Msg::kDebug, Msg::kError, Msg::kSynopsis, Msg::kVerbose, LEA_CTOR, MSG, DbiValidityRec::SetAggregateNo(), DbiResult::SetCanReuse(), and DbiTimerManager::StartSubWatch().

00045                                                         :
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 }

DbiResultAgg::~DbiResultAgg (  )  [virtual]

Definition at line 234 of file DbiResultAgg.cxx.

References fResults, Msg::kVerbose, LEA_DTOR, and MSG.

00234                             {
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 }


Member Function Documentation

DbiResultKey * DbiResultAgg::CreateKey (  )  const [virtual]

Implements DbiResult.

Definition at line 268 of file DbiResultAgg.cxx.

References DbiResultKey::AddVRecKey(), DbiResult::CreateKey(), fResults, DbiValidityRec::GetCreationDate(), DbiValidityRec::GetSeqNo(), and DbiResult::GetValidityRec().

00268                                             {
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 }

virtual UInt_t DbiResultAgg::GetNumAggregates (  )  const [inline, virtual]

Implements DbiResult.

Definition at line 74 of file DbiResultAgg.h.

References fResults.

00074                                                         { 
00075                                           return 1 + fResults.size(); };

virtual UInt_t DbiResultAgg::GetNumRows (  )  const [inline, virtual]

Implements DbiResult.

Definition at line 76 of file DbiResultAgg.h.

References fSize.

00076                                                   { 
00077                                                   return fSize; }

const DbiTableRow * DbiResultAgg::GetTableRow ( UInt_t  row  )  const [virtual]

Implements DbiResult.

Definition at line 298 of file DbiResultAgg.cxx.

References fRowKeys.

00298                                                              {
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 }

const DbiValidityRec & DbiResultAgg::GetValidityRec ( const DbiTableRow row = 0  )  const [virtual]

Reimplemented from DbiResult.

Definition at line 321 of file DbiResultAgg.cxx.

References DbiTableRow::GetOwner(), and DbiResult::GetValidityRecGlobal().

00322                                                                 {
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 }

Bool_t DbiResultAgg::Satisfies ( const string &   )  [virtual]

Not all DbiResult classes can satisfy these types of query so those that do must override.

Reimplemented from DbiResult.

Definition at line 347 of file DbiResultAgg.cxx.

References DbiResult::CanReuse(), DbiResult::GetSqlQualifiers(), Msg::kDebug, and MSG.

00347                                                            {
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 }

void DbiResultAgg::Streamer ( DbiBinaryFile bf  )  [virtual]

Reimplemented from DbiResult.

Definition at line 364 of file DbiResultAgg.cxx.

References fResults, DbiResult::GetValidityRecGlobal(), and DbiValidityRec::IsGap().

00364                                              {
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 }


Member Data Documentation

std::vector<const DbiResult*> DbiResultAgg::fResults [private]

Array of DbiResults (vector<DbiResult*>). The array index is NOT the aggregate number; it simply matches the rows are supplied by the DbiValidityRecBuilder with first row of DbiValidityRecBuilder excluded i.e. row n of builder creates a DbiResult pointed to by fResults[n-1];

Definition at line 97 of file DbiResultAgg.h.

Referenced by CreateKey(), GetNumAggregates(), Streamer(), and ~DbiResultAgg().

std::vector<const DbiTableRow*> DbiResultAgg::fRowKeys [private]

Look-up: Row no. -> TableRow.

Definition at line 100 of file DbiResultAgg.h.

Referenced by GetTableRow().

UInt_t DbiResultAgg::fSize [private]

Current set size.

Definition at line 103 of file DbiResultAgg.h.

Referenced by GetNumRows().


The documentation for this class was generated from the following files:

Generated on 2 Nov 2017 for loon by  doxygen 1.6.1