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

DbiValidityRec.cxx

Go to the documentation of this file.
00001 // $Id: DbiValidityRec.cxx,v 1.21 2006/08/08 10:51:32 west Exp $
00002 
00003 #include "Conventions/Detector.h"
00004 #include "Conventions/SimFlag.h"
00005 #include "DatabaseInterface/DbiBinaryFile.h"
00006 #include "DatabaseInterface/DbiResultSet.h"
00007 #include "DatabaseInterface/DbiOutRowStream.h"
00008 #include "DatabaseInterface/DbiValidityRec.h"
00009 #include "MessageService/MsgService.h"
00010 #include "LeakChecker/Lea.h"
00011 #include "Validity/VldRange.h"
00012 #include "Validity/VldTimeStamp.h"
00013 
00014 #include "TString.h"
00015 
00016 #include <sstream>
00017 #include <iostream>
00018 
00019 ClassImp(DbiValidityRec)
00020 
00021 
00022 //   Definition of static data members
00023 //   *********************************
00024 
00025 CVSID("$Id: DbiValidityRec.cxx,v 1.21 2006/08/08 10:51:32 west Exp $\n  \
00026       CVSID_DBIRESULTPTR ");
00027 
00028 //  Instantiate associated Result Pointer class.
00029 //  *******************************************
00030 
00031 #include "DatabaseInterface/DbiResultPtr.tpl"
00032 template class  DbiResultPtr<DbiValidityRec>;
00033 
00034 //  Global functions
00035 //  *****************
00036 
00037 //.....................................................................
00038 
00039 ostream& operator<<(ostream& os, const DbiValidityRec& vRec) {
00040 //
00041 //
00042 //  Purpose:  Output validity record to ostream.
00043 //
00044 //  Arguments: 
00045 //    os           in    output stream
00046 //    vRec         in    Validity record to be output
00047 //
00048 //  Return:        updated ostream   
00049 //
00050 //  Contact:   N. West
00051 //
00052 //  Specification:-
00053 //  =============
00054 //
00055 //  o Output validity record to ostream.
00056 
00057 //  Program Notes:-
00058 //  =============
00059 
00060 //  None.
00061 
00062   const VldRange&           range(vRec.GetVldRange());
00063   
00064   ostringstream os2;  //Use local to avoid manipulators from making permanent change to os.
00065   os2 << "SeqNo: " << vRec.GetSeqNo()  
00066       << "  AggNo: " << vRec.GetAggregateNo()
00067       << "  DbNo: " << vRec.GetDbNo();
00068   if ( vRec.IsGap() ) os2 << " (gap)";
00069   os2  << "  VldRange: |" 
00070        << setprecision(3) << hex << setfill ('0') << "0x" <<  setw(3) << range.GetDetectorMask() << "|"
00071        << "0x" << setw(3) << range.GetSimMask() << "|"
00072        << " " << range.GetTimeStart().AsString("s") << " .. "
00073        << range.GetTimeEnd().AsString("s") 
00074        << " from: " << range.GetDataSource();
00075   os << os2.str();
00076   return os;
00077 
00078 }
00079 
00080 // Definition of member functions (alphabetical order)
00081 // ***************************************************
00082 
00083 
00084 //.....................................................................
00085 
00086 void DbiValidityRec::AndTimeWindow(const VldTimeStamp& startOther,
00087                                    const VldTimeStamp& endOther) {
00088 //
00089 //
00090 //  Purpose:  And in supplied start and end times.
00091 //
00092 //  Arguments: 
00093 //    startOther   in    Start time to be ANDed in.
00094 //    endOther     in    End time to be ANDed in.
00095 //
00096 //  Return:   None. 
00097 //
00098 //  Contact:   N. West
00099 //
00100 //  Specification:-
00101 //  =============
00102 //
00103 //  o Trim time windown on current record to be the overlap (and)
00104 //    with the supplied time window.
00105 
00106 //  Program Notes:-
00107 //  =============
00108 
00109 //  None.
00110 
00111   VldTimeStamp start = fVldRange.GetTimeStart();
00112   VldTimeStamp end   = fVldRange.GetTimeEnd();
00113 
00114   SetTimeWindow( start > startOther ? start : startOther,
00115                  end   < endOther   ? end   : endOther );
00116 }
00117 
00118 //.....................................................................
00119 
00120 DbiValidityRec::DbiValidityRec(Int_t dbNo,Bool_t isGap) :
00121 
00122 fAggregateNo(-2),
00123 fDbNo(dbNo),
00124 fIsGap(isGap),
00125 fTask(0),
00126 fSeqNo(0),
00127 fTableProxy(0)
00128 {
00129 //
00130 //
00131 //  Purpose:  Default constructor
00132 //
00133 //  Arguments:.  
00134 //    dbNo     in    Cascade no. of database holding record.
00135 //    isGap    in    = kTRUE if a gap.
00136 //
00137 //  Return:    n/a
00138 //
00139 //  Contact:   N. West
00140 //
00141 //  Specification:-
00142 //  =============
00143 //
00144 //  o Create ValidityRec.
00145 
00146 
00147 //  Program Notes:-
00148 //  =============
00149 
00150 //  None.
00151 
00152   LEA_CTOR    //Leak Checker
00153 
00154   MSG("Dbi", Msg::kVerbose) << "Creating DbiValidityRec" << endl;
00155 }
00156 //.....................................................................
00157 
00158 DbiValidityRec::DbiValidityRec(const DbiValidityRec& from) : DbiTableRow(from)
00159 {
00160 //
00161 //
00162 //  Purpose:  Copy constructor
00163 //
00164 //  Arguments:  
00165 //    from    in    Object to be copied from.
00166 //
00167 //  Return:    n/a
00168 //
00169 //  Contact:   N. West
00170 //
00171 //  Specification:-
00172 //  =============
00173 //
00174 //  o Create ValidityRec.
00175 
00176 
00177 //  Program Notes:-
00178 //  =============
00179 
00180 //  Explicit just for leak checking.
00181 
00182   LEA_CTOR    //Leak Checker
00183 
00184   MSG("Dbi", Msg::kVerbose) << "Creating DbiValidityRec" << endl;
00185 
00186   *this = from;
00187 
00188 }
00189 //.....................................................................
00190 
00191 DbiValidityRec::DbiValidityRec(const VldRange& range,
00192                                Dbi::Task task,
00193                                Int_t aggNo,
00194                                UInt_t seqNo,
00195                                Int_t dbNo,
00196                                Bool_t isGap,
00197                                VldTimeStamp time) :
00198 fAggregateNo(aggNo),
00199 fCreationDate(time),
00200 fDbNo(dbNo),
00201 fIsGap(isGap),
00202 fTask(task),
00203 fSeqNo(seqNo),
00204 fTableProxy(0),
00205 fVldRange(range)
00206 {
00207 //
00208 //
00209 //  Purpose:  Constructor from components
00210 //
00211 //  Arguments:  
00212 //    range    in   component range
00213 //    task     in   component task 
00214 //    aggNo    in   component aggregate number
00215 //    seqNo    in   component sequence number
00216 //    dbNo     in    Cascade no. of database holding record.
00217 //    isGap    in    = kTRUE if a gap.
00218 //
00219 //
00220 //
00221 // 
00222 //  Return:    n/a
00223 //
00224 //  Contact:   N. West
00225 //
00226 //  Specification:-
00227 //  =============
00228 //
00229 //  o Create ValidityRec.
00230 
00231 
00232 //  Program Notes:-
00233 //  =============
00234 
00235 //  None.
00236 
00237   LEA_CTOR    //Leak Checker
00238 
00239   MSG("Dbi", Msg::kVerbose) << "Creating DbiValidityRec" << endl;
00240 }
00241 
00242 //.....................................................................
00243 
00244 DbiValidityRec::~DbiValidityRec() {
00245 //
00246 //
00247 //  Purpose: Destructor
00248 //
00249 //  Arguments: 
00250 //    None.
00251 //
00252 //  Return:    n/a
00253 //
00254 //  Contact:   N. West
00255 //
00256 //  Specification:-
00257 //  =============
00258 //
00259 //  o  Destroy ValidityRec.
00260 
00261 
00262 //  Program Notes:-
00263 //  =============
00264 
00265 //  None.
00266 
00267   LEA_DTOR    //Leak Checker
00268 
00269   MSG("Dbi", Msg::kVerbose) << "Destroying DbiValidityRec" << endl;
00270 
00271 }
00272 
00273 //.....................................................................
00274 
00275 void DbiValidityRec::Fill(DbiResultSet& rs,
00276                           const DbiValidityRec* /* vrec */) {
00277 //
00278 //
00279 //  Purpose:  Fill oject from Result Set
00280 //
00281 //  Arguments: 
00282 //    rs           in    Result Set used to fill object
00283 //    vrec         in    =0.  If filling other table rows it points
00284 //                       to the associated validity record.
00285 //
00286 //  Return:    
00287 //
00288 //  Contact:   N. West
00289 //
00290 //  Specification:-
00291 //  =============
00292 //
00293 //  o Fill object from current row of Result Set.
00294 
00295 //  Program Notes:-
00296 //  =============
00297 
00298 //  None.
00299 
00300     VldTimeStamp start, end;
00301     Int_t detMask, simMask;
00302 
00303 //  Establish source DbiTableProxy and cascade no.
00304 
00305     fTableProxy = rs.GetTableProxy();
00306     fDbNo       = rs.GetDbNo();
00307 
00308 //  Read data from row.
00309 
00310     rs >> fSeqNo >> start >> end >> detMask >> simMask >> fTask 
00311        >> fAggregateNo    >> fCreationDate  >> fInsertDate;    
00312 
00313     fIsGap = kFALSE;
00314 
00315     VldRange vr(detMask, simMask, start, end, "From Database");
00316     fVldRange = vr;   
00317 
00318     MSG("Dbi", Msg::kVerbose) 
00319          << "DbiValidityRec for row " << rs.CurRowNum()
00320          << ": " << fVldRange.AsString()
00321          << " seq num: " << fSeqNo
00322          << " agg no: "  << fAggregateNo
00323          << " task: " << endl;
00324 }
00325 
00326 //.....................................................................
00327 
00328 std::string DbiValidityRec::GetL2CacheName() const {
00329 //
00330 //
00331 //  Purpose:  Return the associated Level 2 Cache Name.
00332 //
00333 //  Specification:-
00334 //  =============
00335 //
00336 //  o See GetL2CacheName(UInt_t seqLo, UInt_t seqHi, VldTimeStamp ts);
00337 
00338   return DbiValidityRec::GetL2CacheName(this->GetSeqNo(),
00339                                         this->GetSeqNo(),
00340                                         this->GetCreationDate());
00341 
00342 }
00343 //.....................................................................
00344 
00345 std::string DbiValidityRec::GetL2CacheName(UInt_t seqLo, 
00346                                            UInt_t seqHi, 
00347                                            VldTimeStamp ts) {
00348 //
00349 //
00350 //  Purpose:  Return the associated Level 2 Cache Name.
00351 //
00352 //  Specification:-
00353 //  =============
00354 //
00355 //  o For unaggregated queries name format: <Seqno>_<CreationDate>
00356 //
00357 //  o For aggregated queries name format:   <LowSeqno>_<HighSeqNo>_<MaxCreationDate>
00358 
00359 //  Program Notes:-
00360 //  =============
00361 
00362 //  The static routine centralises the definition of cache names both for
00363 //  DbiValidityRec and DbiValidityRecBuilder.
00364 
00365   ostringstream oss;
00366   oss << seqLo << "_";
00367   if ( seqLo != seqHi )  oss << seqHi << "_";
00368   oss << ts.AsString("s");
00369   std::string str(oss.str());
00370   // Convert white space to underscore.
00371   int i = str.size();
00372   while ( i-- ) if ( str[i] == ' ' ) str[i] = '_';
00373   return str;
00374 
00375 }
00376 
00377 //.....................................................................
00378 
00379 Bool_t DbiValidityRec::HasExpired(const DbiValidityRec& other) const {
00380 //
00381 //
00382 //  Purpose: See if validity record has expired i.e. is compatible with
00383 //           this one except for date.
00384 //
00385 //  Arguments: 
00386 //    other        in    Other DbiValidityRec to be compared with
00387 //
00388 //  Return:    
00389 //
00390 //  Contact:   N. West
00391 //
00392 //  Specification:-
00393 //  =============
00394 //
00395 //  o See if validity record has expired i.e. is compatible with
00396 //    this one except for date.
00397 
00398 //  Program Notes:-
00399 //  =============
00400 
00401 //  This method is used by DbiCache to select candidates for
00402 //  purging.
00403 
00404   const VldRange& otherVR =other.GetVldRange();
00405  
00406   return 
00407      (       other.GetTask() == fTask  
00408         && ( otherVR.GetDetectorMask() & fVldRange.GetDetectorMask() )
00409         && ( otherVR.GetSimMask()      & fVldRange.GetSimMask() )
00410         && (    otherVR.GetTimeStart() >= fVldRange.GetTimeEnd()
00411              || otherVR.GetTimeEnd()   <= fVldRange.GetTimeStart() )
00412      );
00413 
00414 }
00415 //.....................................................................
00416 
00417 
00418 Bool_t DbiValidityRec::HasExpired(const VldContext& vc,
00419                                   const Dbi::Task& task) const {
00420 //
00421 //
00422 //  Purpose: See if validity record has expired i.e. is compatible with
00423 //           this context query except for date.
00424 //
00425 //  Arguments: 
00426 //    vc           in    Context of query
00427 //    task         in    Task of query
00428 //
00429 //
00430 //  Contact:   N. West
00431 
00432 //  Program Notes:-
00433 //  =============
00434 
00435 //  This method is used by DbiResult to identify when ready for
00436 //  purging.
00437 
00438   VldTimeStamp    ts = vc.GetTimeStamp();
00439   const VldRange& vr = this->GetVldRange();
00440 
00441   return 
00442      (       task             == this->GetTask()  
00443         && ( vc.GetDetector() &  vr.GetDetectorMask() )
00444         && ( vc.GetSimFlag()  &  vr.GetSimMask() )
00445         && (    ts            >= vr.GetTimeEnd()
00446              || ts            <  vr.GetTimeStart() )
00447      );
00448 
00449 }
00450 
00451 //.....................................................................
00452 
00453 
00454 Bool_t DbiValidityRec::IsCompatible(const VldContext& vc,
00455                                     const Dbi::Task& task) const {
00456 //
00457 //
00458 //  Purpose: See if result is compatible with query. 
00459 //
00460 //  Arguments: 
00461 //    vc           in    Context of new query
00462 //    task         in    Task of new query
00463 //
00464 //  Return:    
00465 //
00466 //  Contact:   N. West
00467 //
00468 //  Specification:-
00469 //  =============
00470 //
00471 //  o See if result is compatible with query.
00472 
00473 //  Program Notes:-
00474 //  =============
00475 
00476 //  None.
00477 
00478    MSG("Dbi",Msg::kDebug) 
00479      << " DbiValidityRec::IsCompatible : tasks:"
00480      << task << "," << fTask
00481      << " is compat: " << fVldRange.IsCompatible(vc) << endl
00482      << "   range " << fVldRange.AsString() << endl
00483      << "   context " << vc.AsString() << endl;
00484 
00485   return task == fTask  && fVldRange.IsCompatible(vc);
00486 
00487 }
00488 
00489 void DbiValidityRec::SetTimeWindow(const VldTimeStamp& start,
00490                                    const VldTimeStamp& end) {
00491 //
00492 //
00493 //  Purpose:  Set supplied start and end times.
00494 //
00495 //  Arguments: 
00496 //    start        in    Start time to be set,
00497 //    end          in    End time to be set.
00498 //
00499 //  Return:   None. 
00500 //
00501 //  Contact:   N. West
00502 //
00503 //  Specification:-
00504 //  =============
00505 //
00506 //  o Set time windown on current record.
00507 
00508 //  Program Notes:-
00509 //  =============
00510 
00511 //  The method here is rather clumsy, but I don't know of anything
00512 //  better!
00513 
00514   VldRange  range(fVldRange.GetDetectorMask(),
00515                   fVldRange.GetSimMask(),
00516                   start,
00517                   end,
00518                   fVldRange.GetDataSource() );
00519   fVldRange = range;
00520 
00521 }
00522 
00523 //.....................................................................
00524 
00525 void DbiValidityRec::Store(DbiOutRowStream& ors,
00526                            const DbiValidityRec* /* vrec */) const {
00527 //
00528 //
00529 //  Purpose:  Stream object to output row stream
00530 //
00531 //  Arguments: 
00532 //    ors          in    Output row stream.
00533 //    vrec         in    =0.  If filling other table rows it points
00534 //                       to the associated validity record.
00535 //
00536 //  Return:    
00537 //
00538 //  Contact:   N. West
00539 //
00540 //  Specification:-
00541 //  =============
00542 //
00543 //  o  Stream object to output row stream.
00544 
00545 //  Program Notes:-
00546 //  =============
00547 
00548 //  None.
00549 
00550 //  Write data to row.
00551 
00552     ors << fSeqNo 
00553         << fVldRange.GetTimeStart() 
00554         << fVldRange.GetTimeEnd() 
00555         << fVldRange.GetDetectorMask()
00556         << fVldRange.GetSimMask() 
00557         << fTask 
00558         << fAggregateNo    
00559         << fCreationDate  
00560         << fInsertDate;       
00561 }
00562 //.....................................................................
00563 
00564 void DbiValidityRec::Streamer(DbiBinaryFile& file) {
00565 //
00566 //
00567 //  Purpose:  I/O to binary file
00568 //
00569 //  Program Notes:-
00570 //  =============
00571 
00572 //  Don't store the pointer fTableProxy (don't want to get into storing
00573 //  pointers!); the DbiCache will fix it up on input
00574 
00575   if ( file.IsReading() ) {
00576     file >> fAggregateNo
00577          >> fCreationDate
00578          >> fDbNo
00579          >> fInsertDate
00580          >> fIsGap
00581          >> fTask
00582          >> fSeqNo
00583          >> fVldRange;
00584     fTableProxy = 0;
00585   }
00586   else if ( file.IsWriting() ) {
00587     file << fAggregateNo
00588          << fCreationDate
00589          << fDbNo
00590          << fInsertDate
00591          << fIsGap
00592          << fTask
00593          << fSeqNo
00594          << fVldRange;
00595   }
00596 }
00597 
00598 //.....................................................................
00599 
00600 void DbiValidityRec::Trim(const VldTimeStamp& queryTime,
00601                           const DbiValidityRec& other) {
00602 //
00603 //
00604 //  Purpose:  Trim this validity record so that represents 
00605 //            best validity record for query.
00606 //
00607 //  Arguments: 
00608 //    queryTime    in    Time of query
00609 //    other        in    DbiValidity record satisfying query
00610 //
00611 //  Return:    None.
00612 //
00613 //  Contact:   N. West
00614 //
00615 //  Specification:-
00616 //  =============
00617 //
00618 //  o Update this validity record so that it remains the best
00619 //    validity record taking into account the supplied record.
00620 
00621 
00622 //  Program Notes:-
00623 //  =============
00624 
00625 //  This is the function that deal with validity management.
00626 //  It takes into account that several validity records may
00627 //  overlap and that the best one is the one with the latest
00628 //  creation date that brackets the query date.  Other entries
00629 //  with later creation dates may trim start or end times.
00630 
00631 //  Assumptions:-
00632 //  ===========
00633 //
00634 //  That entries are submitted in strict descending priority i.e.:-
00635 //
00636 //  1)  Entries for a higher priority database precede those from a
00637 //      lower priority one.
00638 //
00639 //  2)  Within a database entries are in descending creation date order.
00640 
00641 
00642 //  Ignore other records that are either gaps or have wrong
00643 //  aggregate number.
00644 
00645   if ( fAggregateNo != other.fAggregateNo || other.IsGap() ) return;
00646   
00647 //  If this record is not a gap then the other record can be ignore
00648 //  as it is of lower priority.
00649 
00650   if ( ! IsGap() ) return;
00651 
00652   VldTimeStamp start      = fVldRange.GetTimeStart();
00653   VldTimeStamp end        = fVldRange.GetTimeEnd();
00654   VldTimeStamp startOther = other.GetVldRange().GetTimeStart();
00655   VldTimeStamp endOther   = other.GetVldRange().GetTimeEnd();
00656   
00657 // If entry brackets query date, then use it but with a validity that
00658 // is trimmed by the current record.
00659 
00660   if ( startOther <= queryTime && endOther > queryTime ) {
00661     if ( start < startOther ) start = startOther;
00662     if ( end   > endOther   ) end   = endOther;
00663     *this = other;
00664     SetTimeWindow(start,end);
00665   }
00666 
00667 // It doesn't bracket, so use it to trim the window
00668 
00669   else {
00670 
00671     if ( endOther <= queryTime ) {
00672       if ( start < endOther   ) SetTimeWindow(endOther,end);
00673     }
00674     else if ( startOther > queryTime ) {
00675       if ( end   > startOther ) SetTimeWindow(start, startOther);
00676     }
00677   }
00678 
00679 }
00680 
00681 /*    Template for New Member Function
00682 
00683 //.....................................................................
00684 
00685 DbiValidityRec:: {
00686 //
00687 //
00688 //  Purpose:  
00689 //
00690 //  Arguments: 
00691 //    xxxxxxxxx    in    yyyyyy
00692 //
00693 //  Return:    
00694 //
00695 //  Contact:   N. West
00696 //
00697 //  Specification:-
00698 //  =============
00699 //
00700 //  o 
00701 
00702 //  Program Notes:-
00703 //  =============
00704 
00705 //  None.
00706 
00707 
00708 }
00709 
00710 */
00711 

Generated on Fri Mar 28 15:30:38 2008 for loon by  doxygen 1.3.9.1