TDbiResultKey.cxx

Go to the documentation of this file.
00001 // $Id: TDbiResultKey.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $
00002 
00003 #include <iostream>
00004 #include <map>
00005 #include <sstream>
00006 
00007 #include "TDbiResultKey.hxx"
00008 #include <TSK_DBI_Log.hxx>
00009 #include <MsgFormat.h>
00010 using std::endl;
00011 
00012 ClassImp(TDbiResultKey)
00013 
00014 
00015 //   Definition of static data members
00016 //   *********************************
00017 
00018 
00019 TDbiResultKey TDbiResultKey::fgEmptyKey;
00020 
00021 //   Global Functions
00022 //   ****************
00023 
00024 std::ostream& operator<<(std::ostream& os, const TDbiResultKey& key) {
00025   os << key.AsString() << endl;
00026   return os;
00027 }
00028 
00029 
00030 // Definition of member functions (alphabetical order)
00031 // ***************************************************
00032 
00033 //.....................................................................
00034 
00035 TDbiResultKey::TDbiResultKey(const TDbiResultKey* that /* =0 */) :
00036 fNumVRecKeys(0)
00037 {
00038 //
00039 //
00040 //  Purpose:  Default constructor
00041 //
00042 
00043 
00044   SK_DBI_Trace( "Creating TDbiResultKey" << "  ");
00045   if ( that ) *this = *that;
00046 }
00047 
00048 ///.....................................................................
00049 
00050 TDbiResultKey::TDbiResultKey(std::string tableName,
00051                            std::string rowName,
00052                            UInt_t seqno,
00053                            TVldTimeStamp ts) :
00054 fTableName(tableName),
00055 fRowName(rowName),
00056 fNumVRecKeys(0)
00057 {
00058 //
00059 //
00060 //  Purpose:  Standard constructor
00061 //
00062 //  Contact:   N. West
00063 //
00064 //  Specification:-
00065 //  =============
00066 //
00067 //  o  Create TDbiResultKey.
00068 
00069 
00070   SK_DBI_Trace( "Creating TDbiResultKey" << "  ");
00071 
00072   this->AddVRecKey(seqno,ts);
00073 }
00074 
00075 //.....................................................................
00076 
00077 TDbiResultKey::~TDbiResultKey() {
00078 //
00079 //
00080 //  Purpose: Destructor
00081 //
00082 //  Contact:   N. West
00083 //
00084 //  Specification:-
00085 //  =============
00086 //
00087 //  o  Destroy TDbiResultKey
00088 
00089 
00090   SK_DBI_Trace( "Destroying TDbiResultKey" << "  ");
00091 
00092 }
00093 
00094 //.....................................................................
00095 
00096 void TDbiResultKey::AddVRecKey(UInt_t seqno, TVldTimeStamp ts) {
00097 //
00098 //
00099 //  Purpose:  Add a TDbiValidityRec key.
00100 //
00101 
00102   fVRecKeys.push_back(VRecKey(seqno,ts));
00103   ++fNumVRecKeys;
00104 
00105 }
00106 
00107 //.....................................................................
00108 
00109 std::string TDbiResultKey::AsString() const {
00110 //
00111 //
00112 //  Purpose:  Return a string that summarises this key giving:-
00113 //            1)  The table and row names.
00114 //            2)  The number of validity records (aggregates)
00115 //            3)  The range of SEQNOs
00116 //            4)  The range of CREATIONDATEs.
00117 
00118   ostringstream os;
00119   os << "Table:" << fTableName << " row:" << fRowName;
00120   if ( fVRecKeys.empty() ) os << " No vrecs";
00121   else {
00122     os << ".  " << fNumVRecKeys << " vrec";
00123     if ( fNumVRecKeys > 1 ) os << "s (seqno min..max;creationdate min..max):";
00124     else                    os << " (seqno;creationdate):";
00125     os << " ";
00126     std::list<VRecKey>::const_iterator itr    = fVRecKeys.begin();
00127     std::list<VRecKey>::const_iterator itrEnd = fVRecKeys.end();
00128     UInt_t seqnoMin    = itr->SeqNo;
00129     UInt_t seqnoMax    = seqnoMin;
00130     TVldTimeStamp tsMin = itr->CreationDate;
00131     TVldTimeStamp tsMax = tsMin;
00132     ++itr;
00133     while ( itr != itrEnd ) {
00134       UInt_t       seqno = itr->SeqNo;
00135       TVldTimeStamp ts    = itr->CreationDate;
00136       if ( seqno < seqnoMin ) seqnoMin = seqno;
00137       if ( seqno > seqnoMax ) seqnoMax = seqno;
00138       if (    ts < tsMin    ) tsMin    = ts;
00139       if (    ts > tsMax    ) tsMax    = ts;
00140       ++itr;
00141     }
00142     os << seqnoMin;
00143     if ( seqnoMin < seqnoMax ) os << ".." << seqnoMax;
00144     os << ";" << tsMin.AsString("s");
00145     if ( tsMin < tsMax ) os << ".." <<  tsMax.AsString("s");
00146   }
00147   return string(os.str());
00148 
00149 }
00150 //.....................................................................
00151 
00152 Float_t TDbiResultKey::Compare(const TDbiResultKey* that) const {
00153 //
00154 //
00155 //  Purpose:  Compare 2 TDbiResultKeys
00156 //
00157 //  Return:    = -2. Table names don't match.
00158 //             = -1. Table names match but row names don't
00159 //                   i.e. contain different TDbiTableRow sub-classes.
00160 //             >= f  Table and row names match and fraction f of the
00161 //                   SEQNOs have same creation date.
00162 //                   So f = 1.  = perfect match.
00163 
00164 //  Program Notes:-
00165 //  =============
00166 
00167 //  None.
00168 
00169   // Check in table and row names.
00170   if ( fTableName != that->fTableName ) return -2.;
00171   if ( fRowName   != that->fRowName   ) return -1.;
00172 
00173   // Pick the key with the most entries and compare the other to it.
00174 
00175   SK_DBI_Debug( "Comparing " << *this << " to "
00176                         << *that << "  ");
00177 
00178   const TDbiResultKey* keyBig   = this;
00179   const TDbiResultKey* keySmall = that;
00180   if ( that->GetNumVrecs() > this->GetNumVrecs() ) {
00181     keyBig   = that;
00182     keySmall = this;
00183   }
00184   int numVrecs = keyBig->GetNumVrecs();
00185   if ( numVrecs == 0 ) return 0.;
00186 
00187   std::map<UInt_t,TVldTimeStamp> seqnoToCreationDate;
00188   std::list<TDbiResultKey::VRecKey>::const_iterator itrEnd = keyBig->fVRecKeys.end();
00189   for (  std::list<TDbiResultKey::VRecKey>::const_iterator itr = keyBig->fVRecKeys.begin();
00190          itr != itrEnd;
00191          ++itr ) seqnoToCreationDate[itr->SeqNo] = itr->CreationDate;
00192   float match = 0;
00193   itrEnd = keySmall->fVRecKeys.end();
00194   for (  std::list<TDbiResultKey::VRecKey>::const_iterator itr = keySmall->fVRecKeys.begin();
00195          itr != itrEnd;
00196          ++itr ) {
00197     SK_DBI_Debug( "Comparing seqno " << itr->SeqNo << " with creation date " << itr->CreationDate
00198                          << " to " <<  seqnoToCreationDate[itr->SeqNo] << "  ");
00199     if ( seqnoToCreationDate[itr->SeqNo] == itr->CreationDate ) ++match;
00200   }
00201   SK_DBI_Debug( "Match results: " << match << " out of " << numVrecs << "  ");
00202 
00203   return match/numVrecs;
00204 
00205 }
00206 
00207 //.....................................................................
00208 
00209 std::string TDbiResultKey::GetTableRowName() const {
00210 //
00211 //
00212 //  Purpose:  Return TableName::RowName
00213 
00214   ostringstream os;
00215   os << fTableName << "::" << fRowName;
00216   return os.str();
00217 
00218 }
00219 /*    Template for New Member Function
00220 
00221 //.....................................................................
00222 
00223 TDbiResultKey:: {
00224 //
00225 //
00226 //  Purpose:
00227 //
00228 //  Arguments:
00229 //    xxxxxxxxx    in    yyyyyy
00230 //
00231 //  Return:
00232 //
00233 //  Contact:   N. West
00234 //
00235 //  Specification:-
00236 //  =============
00237 //
00238 //  o
00239 
00240 //  Program Notes:-
00241 //  =============
00242 
00243 //  None.
00244 
00245 
00246 }
00247 
00248 */
00249 
00250 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1