00001
00002
00003 #include <iostream>
00004 #include <map>
00005 #include <sstream>
00006
00007 #include "DatabaseInterface/DbiResultKey.h"
00008 #include "LeakChecker/Lea.h"
00009 #include "MessageService/MsgService.h"
00010
00011 ClassImp(DbiResultKey)
00012
00013
00014
00015
00016
00017 CVSID("$Id: DbiResultKey.cxx,v 1.7 2007/01/03 15:00:45 west Exp $");
00018
00019 DbiResultKey DbiResultKey::fgEmptyKey;
00020
00021
00022
00023
00024 std::ostream& operator<<(std::ostream& os, const DbiResultKey& key) {
00025 os << key.AsString() << endl;
00026 return os;
00027 }
00028
00029
00030
00031
00032
00033
00034
00035 DbiResultKey::DbiResultKey(const DbiResultKey* that ) :
00036 fNumVRecKeys(0)
00037 {
00038
00039
00040
00041
00042
00043 LEA_CTOR
00044
00045 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultKey" << endl;
00046 if ( that ) *this = *that;
00047 }
00048
00050
00051 DbiResultKey::DbiResultKey(std::string tableName,
00052 std::string rowName,
00053 UInt_t seqno,
00054 VldTimeStamp ts) :
00055 fTableName(tableName),
00056 fRowName(rowName),
00057 fNumVRecKeys(0)
00058 {
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 LEA_CTOR
00071
00072 MSG("Dbi", Msg::kVerbose) << "Creating DbiResultKey" << endl;
00073
00074 this->AddVRecKey(seqno,ts);
00075 }
00076
00077
00078
00079 DbiResultKey::~DbiResultKey() {
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 LEA_DTOR
00092
00093 MSG("Dbi", Msg::kVerbose) << "Destroying DbiResultKey" << endl;
00094
00095 }
00096
00097
00098
00099 void DbiResultKey::AddVRecKey(UInt_t seqno, VldTimeStamp ts) {
00100
00101
00102
00103
00104
00105 fVRecKeys.push_back(VRecKey(seqno,ts));
00106 ++fNumVRecKeys;
00107
00108 }
00109
00110
00111
00112 std::string DbiResultKey::AsString() const {
00113
00114
00115
00116
00117
00118
00119
00120
00121 ostringstream os;
00122 os << "Table:" << fTableName << " row:" << fRowName;
00123 if ( fVRecKeys.empty() ) os << " No vrecs";
00124 else {
00125 os << ". " << fNumVRecKeys << " vrec";
00126 if ( fNumVRecKeys > 1 ) os << "s (seqno min..max;creationdate min..max):";
00127 else os << " (seqno;creationdate):";
00128 os << " ";
00129 std::list<VRecKey>::const_iterator itr = fVRecKeys.begin();
00130 std::list<VRecKey>::const_iterator itrEnd = fVRecKeys.end();
00131 UInt_t seqnoMin = itr->SeqNo;
00132 UInt_t seqnoMax = seqnoMin;
00133 VldTimeStamp tsMin = itr->CreationDate;
00134 VldTimeStamp tsMax = tsMin;
00135 ++itr;
00136 while ( itr != itrEnd ) {
00137 UInt_t seqno = itr->SeqNo;
00138 VldTimeStamp ts = itr->CreationDate;
00139 if ( seqno < seqnoMin ) seqnoMin = seqno;
00140 if ( seqno > seqnoMax ) seqnoMax = seqno;
00141 if ( ts < tsMin ) tsMin = ts;
00142 if ( ts > tsMax ) tsMax = ts;
00143 ++itr;
00144 }
00145 os << seqnoMin;
00146 if ( seqnoMin < seqnoMax ) os << ".." << seqnoMax;
00147 os << ";" << tsMin.AsString("s");
00148 if ( tsMin < tsMax ) os << ".." << tsMax.AsString("s");
00149 }
00150 return string(os.str());
00151
00152 }
00153
00154
00155 Float_t DbiResultKey::Compare(const DbiResultKey* that) const {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 if ( fTableName != that->fTableName ) return -2.;
00174 if ( fRowName != that->fRowName ) return -1.;
00175
00176
00177
00178 MSG("Dbi",Msg::kDebug) << "Comparing " << *this << " to "
00179 << *that << endl;
00180
00181 const DbiResultKey* keyBig = this;
00182 const DbiResultKey* keySmall = that;
00183 if ( that->GetNumVrecs() > this->GetNumVrecs() ) {
00184 keyBig = that;
00185 keySmall = this;
00186 }
00187 int numVrecs = keyBig->GetNumVrecs();
00188 if ( numVrecs == 0 ) return 0.;
00189
00190 std::map<UInt_t,VldTimeStamp> seqnoToCreationDate;
00191 std::list<DbiResultKey::VRecKey>::const_iterator itrEnd = keyBig->fVRecKeys.end();
00192 for ( std::list<DbiResultKey::VRecKey>::const_iterator itr = keyBig->fVRecKeys.begin();
00193 itr != itrEnd;
00194 ++itr ) seqnoToCreationDate[itr->SeqNo] = itr->CreationDate;
00195 float match = 0;
00196 itrEnd = keySmall->fVRecKeys.end();
00197 for ( std::list<DbiResultKey::VRecKey>::const_iterator itr = keySmall->fVRecKeys.begin();
00198 itr != itrEnd;
00199 ++itr ) {
00200 MSG("Dbi",Msg::kDebug) << "Comparing seqno " << itr->SeqNo << " with creation date " << itr->CreationDate
00201 << " to " << seqnoToCreationDate[itr->SeqNo] << endl;
00202 if ( seqnoToCreationDate[itr->SeqNo] == itr->CreationDate ) ++match;
00203 }
00204 MSG("Dbi",Msg::kDebug) << "Match results: " << match << " out of " << numVrecs << endl;
00205
00206 return match/numVrecs;
00207
00208 }
00209
00210
00211
00212 std::string DbiResultKey::GetTableRowName() const {
00213
00214
00215
00216
00217 ostringstream os;
00218 os << fTableName << "::" << fRowName;
00219 return os.str();
00220
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252