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

DbiValidityRec.cxx

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

Generated on Thu Nov 1 15:52:19 2007 for loon by  doxygen 1.3.9.1