17 #include <pdbcalbase/PdbCalChan.h>
18 #include <pdbcalbase/PdbCalBank.h>
19 #include <pdbcalbase/PdbClassMap.h>
25 #include <RDBC/TSQL.h>
26 #include <RDBC/TSQLConnection.h>
27 #include <RDBC/TSQLPreparedStatement.h>
28 #include <RDBC/TSQLResultSet.h>
29 #include <RDBC/TSQLStatement.h>
32 #include <TBufferFile.h>
33 #include <TCollection.h>
34 #include <TDirectory.h>
59 if (
tag.length() == 0)
65 <<
"PgPostBankBackupManager::PgPostBankBackupManager - use backup log tag "
77 cout <<
"PgPostBankBackupManager::deleteSQLStatement - WARNING - "
78 <<
" empty pointer" << endl;
85 cout <<
"PgPostBankBackupManager::deleteSQLStatement - ERROR - "
86 <<
" PgPostApplication instance is nullptr, exiting" << endl;
92 cout <<
"PgPostBankBackupManager::deleteSQLStatement - ERROR - "
93 <<
" Cannot get TSQLConnection, exiting" << endl;
97 TList *sl = con->GetListOfStatements();
101 TObject *obj = sl->Remove(stmt);
104 cout <<
"PgPostBankBackupManager::deleteSQLStatement - ERROR - "
105 <<
" Cannot find statement in TSQLConnection" << endl;
114 TSQLPreparedStatement *stmt)
119 <<
"PgPostBankBackupManager::deleteODBCPreparedStatement - WARNING - "
120 <<
" empty pointer" << endl;
127 cout <<
"PgPostBankBackupManager::deleteODBCPreparedStatement - ERROR - "
128 <<
" PgPostApplication instance is nullptr, exiting" << endl;
134 cout <<
"PgPostBankBackupManager::deleteODBCPreparedStatement - ERROR - "
135 <<
" Cannot get TSQLConnection, exiting" << endl;
139 TList *sl = con->GetListOfStatements();
150 cout <<
"PgPostBankBackupManager::deleteODBCPreparedStatement - ERROR - "
151 <<
" Cannot find statement in TSQLConnection" << endl;
163 const std::string &table_name)
168 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - Error - null TSQLResultSet"
173 if (rs->GetRow() == 0)
176 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - Error - invalid TSQLResultSet with GetRow = 0"
181 const int rid = rs->GetInt(8);
182 std::unique_ptr<PgPostCalBank> bw(
183 dynamic_cast<PgPostCalBank *>(rs->GetObject(7)));
186 string pdbcalchan_classname;
187 string pgpostcalbank_classname;
191 if (
string(bw->ClassName()) ==
string(
"PgPostBankWrapper"))
195 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - Processing PgPostBankWrapper "
200 else if (
string(bw->ClassName()) ==
string(
"PgPostCalBank"))
202 cout <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - WARNING - "
203 <<
"empty PgPostCalBank object in database, table " << table_name
204 <<
" where rid = " << rid << endl;
205 bank_orig =
dynamic_cast<PdbCalBank *
>(bw.get());
208 else if (classMap->
find(bw->ClassName()) != classMap->
end())
210 cout <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - WARNING - "
211 <<
"Direct stream of " << bw->ClassName()
212 <<
" object without wrapper in database, table " << table_name
213 <<
" where rid = " << rid << endl;
214 bank_orig =
dynamic_cast<PdbCalBank *
>(bw.get());
219 cout <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - "
220 <<
" ERROR - unknown object in database record in class "
221 << bw->ClassName() << endl;
229 if (
string(bank_orig->ClassName()) ==
string(
"PgPostBankWrapper"))
232 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - WARNING - "
233 <<
"PgPostBankWrapper object nested inside PgPostBankWrapper layer "
234 << unwrap_cnt <<
". Discard secondary wrapper: table "
235 << table_name <<
" where rid = " << rid << endl;
242 cout <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - "
243 << bank_orig->ClassName() <<
" passed wrapper check" << endl;
251 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - Fatal Error - "
252 <<
"PgPostBankWrapper object nested inside PgPostBankWrapper for "
253 << unwrap_cnt <<
" layers: table " << table_name
254 <<
" where rid = " << rid << endl;
261 if (classMap->
find(bank_orig->ClassName()) == classMap->
end())
264 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - Fatal Error - "
265 <<
"Calibration bank object of " << bank_orig->ClassName()
266 <<
" is not supported: table " << table_name <<
" where rid = " << rid
273 pgpostcalbank_classname = bank_orig->ClassName();
274 pdbcalchan_classname = string(
"Pdb") +
getBankBaseName(pgpostcalbank_classname);
276 cout <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - Bank "
277 << pgpostcalbank_classname <<
" contains " << length <<
" "
278 << pdbcalchan_classname << endl;
287 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - obj header 1 "
305 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - obj header 2 "
316 <<
"PgPostBankBackupManager::SQLResultSet2BackupStorage - obj header 3 "
330 cout <<
"PgPostBankBackupManager::fetchBank - start on fetching "
331 << bankName <<
" ID " << rid << endl;
336 cout <<
"PgPostBankBackupManager::fetchBank - ERROR - "
337 <<
" PgPostApplication instance is nullptr, exiting" << endl;
344 cout <<
"PgPostBankBackupManager::fetchBank - ERROR - "
345 <<
" Cannot get TSQLConnection, exiting" << endl;
349 TSQLStatement *stmt = con->CreateStatement();
350 std::ostringstream tem;
355 <<
"select bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid from "
356 << bankName <<
" where rid = " << rid;
359 cout <<
"PgPostBankBackupManager::fetchBank - database exe : " << tem.str()
364 TSQLResultSet *rs(stmt->ExecuteQuery(tem.str().c_str()));
367 cout <<
"PgPostBankBackupManager::fetchBank - ERROR - "
368 <<
" Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
377 cout <<
"PgPostBankBackupManager::fetchBank - clear TSQLResultSet"
384 cout <<
"PgPostBankBackupManager::fetchBank - clear SQLStatement List"
392 cout <<
"PgPostBankBackupManager::fetchBank - ERROR - NO Bank found : "
393 << tem.str() << std::endl;
396 cout <<
"PgPostBankBackupManager::fetchBank - clear TSQLResultSet"
401 cout <<
"PgPostBankBackupManager::fetchBank - clear TSQLResultSet"
421 cout <<
"PgPostBankBackupManager::commit - Error - "
422 <<
" invalid input pointer" << endl;
427 cout <<
"PgPostBankBackupManager::commit - Error - "
428 <<
" invalid input object" << endl;
446 cout <<
"PgPostBankBackupManager::commit - Error - "
449 <<
" already exist in table "
455 ostringstream sqlcmd;
457 <<
"(bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid) values (?,?,?,?,?,?,?,?);";
460 cout <<
"PgPostBankBackupManager::fetchBank - database commit : "
461 << sqlcmd.str() << endl;
462 TSQLPreparedStatement *pstmt = con->PrepareStatement(
463 sqlcmd.str().c_str());
476 std::unique_ptr<PgPostCalBank> bw(bs->
createBank());
478 pstmt->SetObject(7, bw.get());
481 res = pstmt->ExecuteUpdate();
486 catch (TSQLException &
e)
488 cout <<
"PgPostBankBackupManager::commit - Error - "
489 <<
" Exception caught during connection->commit()" << endl;
490 cout << e.GetMessage() << endl;
497 cout <<
"PgPostBankBackupManager::commit - Error - "
498 <<
"DATABASE: commit to "
500 <<
"Make sure you commit to the master database " << endl;
506 cout <<
"PgPostBankBackupManager::fetchBank - clear SQLStatement List"
511 cout <<
"PgPostBankBackupManager::commit - Committed " << res <<
" row"
514 catch (std::exception &
e)
516 cout <<
"PgPostBankBackupManager::commit - Error - "
517 <<
" Exception caught " << endl;
518 cout << e.what() << endl;
530 cout <<
"PgPostBankBackupManager::commitAllBankfromTFile - Loading TFile "
531 << input_file << endl;
532 TFile *
f =
new TFile(input_file.c_str());
537 <<
"PgPostBankBackupManager::commitAllBankfromTFile - ERROR - can not open TFile "
538 << input_file << endl;
544 <<
"PgPostBankBackupManager::commitAllBankfromTFile - ERROR - can not open TFile "
545 << input_file << endl;
552 cout <<
"PgPostBankBackupManager::commitAllBankfromTFile - ERROR - "
553 <<
" PgPostApplication instance is nullptr, exiting" << endl;
560 cout <<
"PgPostBankBackupManager::commitAllBankfromTFile - ERROR - "
561 <<
" Cannot get TSQLConnection, exiting" << endl;
567 const int file_cnt = f->GetListOfKeys()->GetSize();
576 bool first_read =
true;
579 TSQLPreparedStatement *pstmt =
nullptr;
581 static_cast<PgPostBankBackupLog *>(
nullptr));
583 TIter next(f->GetListOfKeys());
589 if (((commit_cnt + skip_cnt) % 10000 == 1) || (
Verbosity() >= 2))
592 <<
"PgPostBankBackupManager::commitAllBankfromTFile - processing "
593 << table_name <<
": commit/skip/total = " << commit_cnt <<
"/"
594 << skip_cnt <<
"/" << file_cnt <<
" to " << input_file
606 TKey *key =
dynamic_cast<TKey *
>(o);
612 <<
"PgPostBankBackupManager::commitAllBankfromTFile - Processing "
613 << key->GetName() <<
" ----------------------------------------"
621 std::unique_ptr<PgPostBankBackupStorage>
623 bs(dynamic_cast<PgPostBankBackupStorage *>(key->ReadObj()));
629 <<
"PgPostBankBackupManager::commitAllBankfromTFile - ERROR - can not read "
630 << key->GetName() <<
" from " << input_file << endl;
644 <<
"PgPostBankBackupManager::commitAllBankfromTFile - wrtting table "
645 << table_name <<
" from TFile " << input_file
646 <<
". TFile record size = " << file_cnt
647 <<
" database size = " << existing_rids.size() << endl;
649 ostringstream sqlcmd;
650 sqlcmd <<
"insert into " << table_name
651 <<
"(bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid) values (?,?,?,?,?,?,?,?);";
655 <<
"PgPostBankBackupManager::commitAllBankfromTFile - database commit statement : "
656 << sqlcmd.str() << endl;
657 pstmt = con->PrepareStatement(sqlcmd.str().c_str());
659 bklog = make_shared<PgPostBankBackupLog>(table_name,
tag);
663 assert(table_name.length() > 0);
670 <<
"PgPostBankBackupManager::commitAllBankfromTFile - ERROR - inconsistent table name for "
671 << key->GetName() <<
" from " << input_file <<
", expect "
672 << table_name << endl;
678 const bool existing_rid = std::find(existing_rids.begin(),
679 existing_rids.end(), rid) != existing_rids.end();
685 <<
"PgPostBankBackupManager::commitAllBankfromTFile - existing records "
686 << key->GetName() <<
" in database" << endl;
701 <<
"PgPostBankBackupManager::commitAllBankfromTFile - submit new record "
702 << key->GetName() <<
" to database" << endl;
717 std::unique_ptr<PgPostCalBank> bw(bs->
createBank());
719 pstmt->SetObject(7, bw.get());
722 res = pstmt->ExecuteUpdate();
726 <<
"PgPostBankBackupManager::commitAllBankfromTFile - Error - "
727 <<
"DATABASE: commit to "
729 <<
" failed with ExecuteUpdate()=" << res
730 <<
". Make sure you commit to a writable database "
744 catch (TSQLException &
e)
747 <<
"PgPostBankBackupManager::commitAllBankfromTFile - Error - "
748 <<
" Exception caught during connection->commit()"
750 cout << e.GetMessage() << endl;
767 catch (std::exception &
e)
769 cout <<
"PgPostBankBackupManager::commit - Error - "
770 <<
" unhandled exception caught when process record "
771 << key->GetName() <<
":" << endl;
772 cout << e.what() << endl;
782 if ((commit_cnt + skip_cnt) % 10000 == 1)
784 cout <<
"PgPostBankBackupManager::commitAllBankfromTFile - Done "
785 << table_name <<
": commit/skip/total = " << commit_cnt <<
"/"
786 << skip_cnt <<
"/" << file_cnt <<
" to " << input_file << endl;
800 const rid_list_t &rid_list,
const std::string &out_file)
802 TDirectory *gd = gDirectory;
803 TFile
f(out_file.c_str(),
"update");
807 cout <<
"PgPostBankBackupManager::fetchBank2TFile - Error -"
808 <<
" can not open file" << out_file;
813 for (rid_list_t::const_iterator
it = rid_list.begin();
it != rid_list.end();
818 cout <<
"PgPostBankBackupManager::fetchBank2TFile - Process "
819 << bankName <<
" at record ID " << rid <<
" (" << cnt <<
"/"
820 << rid_list.size() <<
")" << endl;
826 cout <<
"PgPostBankBackupManager::fetchBank2TFile - Error -"
827 <<
" nullptr PgPostBankBackupStorage object produced" << endl;
831 cout <<
"PgPostBankBackupManager::fetchBank2TFile - Error -"
832 <<
" invalid PgPostBankBackupStorage object produced" << endl;
837 bs->Write(bs->GetName(), TObject::kWriteDelete);
848 cout <<
"PgPostBankBackupManager::fetchBank2TFile - " << cnt <<
" records in "
849 << bankName <<
" saved to " << out_file << endl;
855 const std::string &record_selection,
const std::string &out_file_base,
859 cout <<
"PgPostBankBackupManager::fetchBank - start on fetching "
860 << bankName <<
" for records criteria of [" << record_selection
861 <<
"] to " << out_file_base <<
"*.root" << endl;
866 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - ERROR - "
867 <<
" PgPostApplication instance is nullptr, exiting" << endl;
874 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - ERROR - "
875 <<
" Cannot get TSQLConnection, exiting" << endl;
882 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - WARNING - "
883 <<
" no data in table " << bankName << endl;
887 TSQLStatement *stmt = con->CreateStatement();
892 <<
"PgPostBankBackupManager::fetchAllBank2TFile - reset output length to row count "
893 << row_cnt <<
" for " << bankName << endl;
894 stmt->SetMaxRows(row_cnt + 1);
896 std::ostringstream tem;
899 <<
"select bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid from "
901 if (record_selection.length() > 0)
902 tem <<
" where " << record_selection;
903 tem <<
" ORDER BY rid ASC";
906 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - database exe : "
907 << tem.str() << endl;
909 TDirectory *gd = gDirectory;
912 int file_rec_cnt = 0;
915 file_name.Form(
"%s_%04d.root", out_file_base.c_str(), file_id);
916 f.Open(file_name,
"recreate");
919 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - Error -"
920 <<
" can not open file" << file_name << endl;
924 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - writing new file "
925 << file_name << endl;
927 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
928 if (!rs || !rs.get())
932 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - ERROR - "
933 <<
" Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
964 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - processing "
965 << bankName <<
": " << cnt <<
"/" << row_cnt <<
" to "
966 << file_name << endl;
968 if (cnt % 10000 == 1)
977 if (file_rec_cnt >= splitting_limit)
982 file_name.Form(
"%s_%04d.root", out_file_base.c_str(), file_id);
983 f.Open(file_name,
"recreate");
986 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - Error -"
987 <<
" can not open file" << file_name;
992 <<
"PgPostBankBackupManager::fetchAllBank2TFile - writing new file "
993 << file_name << endl;
997 const int rid = rs->GetInt(8);
1005 bs->Write(bs->GetName(), TObject::kWriteDelete);
1016 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - Error - "
1017 <<
"invalid PgPostBankBackupStorage for row " << rs->GetRow()
1032 cout <<
"PgPostBankBackupManager::fetchAllBank2TFile - " << cnt
1033 <<
" records in " << bankName <<
" saved to " << out_file_base <<
"*.root"
1041 if (bank_classname.length() <= 10)
1044 <<
"PgPostBankBackupManager::getBankBaseName - Error - unknown format for "
1045 << bank_classname << endl;
1048 string prefix = bank_classname.substr(0, 6);
1049 string suffix = bank_classname.substr(bank_classname.length() - 4, 4);
1051 if (prefix !=
"PgPost")
1054 <<
"PgPostBankBackupManager::getBankBaseName - Error - unknown prefix format for "
1055 << bank_classname << endl;
1058 if (suffix !=
"Bank")
1061 <<
"PgPostBankBackupManager::getBankBaseName - Error - unknown suffix format for "
1062 << bank_classname <<
" with " << suffix << endl;
1066 return bank_classname.substr(6, bank_classname.length() - 6 - 4);
1078 std::ostringstream tem;
1081 tem <<
"select bankid, description,rid from " << bankName <<
" where rid = "
1085 cout <<
"PgPostBankBackupManager::isRIdExist - database check : "
1086 << tem.str() << endl;
1088 TSQLStatement *stmt = con->CreateStatement();
1089 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1090 if ((rs) && rs->Next())
1107 std::ostringstream tem;
1110 tem <<
"select COUNT(*) AS NumberOfRows from " << bankName;
1113 cout <<
"PgPostBankBackupManager::getTotalRowCount - database get : "
1114 << tem.str() << endl;
1116 TSQLStatement *stmt = con->CreateStatement();
1117 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1118 if ((rs) && rs->Next())
1120 return rs->GetInt(1);
1139 std::ostringstream tem;
1142 tem <<
"select rid from " << bankName;
1144 if (condition.length() > 0)
1145 tem <<
" where " << condition;
1148 cout <<
"PgPostBankBackupManager::getListOfRId - database fetch : "
1149 << tem.str() << endl;
1151 TSQLStatement *stmt = con->CreateStatement();
1152 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1153 while ((rs) && rs->Next())
1155 int rid = rs->GetInt(1);
1159 cout <<
"PgPostBankBackupManager::getListOfRId - database fetch "
1160 << l.size() <<
" RIDs" << endl;
1170 cout <<
"PgPostBankBackupManager::dumpTable - start on fetching "
1171 << bankName <<
" -> " << bankName << endl;
1176 cout <<
"PgPostBankBackupManager::dumpTable - ERROR - "
1177 <<
" PgPostApplication instance is nullptr, exiting" << endl;
1184 cout <<
"PgPostBankBackupManager::dumpTable - ERROR - "
1185 <<
" Cannot get TSQLConnection, exiting" << endl;
1189 TSQLStatement *stmt = con->CreateStatement();
1190 std::ostringstream tem;
1195 <<
"select bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid from "
1196 << bankName <<
" ORDER BY rid ASC";
1199 cout <<
"PgPostBankBackupManager::dumpTable - database exe : " << tem.str()
1202 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1205 cout <<
"PgPostBankBackupManager::dumpTable - ERROR - "
1206 <<
" Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
1215 std::unique_ptr<PgPostCalBank> bw(
1216 dynamic_cast<PgPostCalBank *>(rs->GetObject(7)));
1221 const int rid = rs->GetInt(8);
1222 out <<
"New Entry - " << bankName << endl;
1223 out <<
"\tRId = \t" << rid << endl;
1224 out <<
"\tBankID = \t" << (rs->GetInt(1)) << endl;
1225 out <<
"\tInsertTime = \t" << (rs->GetLong(2)) << endl;
1226 out <<
"\tStartValTime = \t" << (rs->GetLong(3)) << endl;
1227 out <<
"\tEndValTime = \t" << (rs->GetLong(4)) << endl;
1228 out <<
"\tDescription = \t" << (rs->GetString(5).Data()) << endl;
1229 out <<
"\tUserName = \t" << (rs->GetString(6).Data()) << endl;
1230 out <<
"\tN PdbCalChan = \t" << (bw->
getLength()) << endl;
1233 for (
size_t i = 0; i < bw->
getLength(); i++)
1244 cout <<
"PgPostBankBackupManager::dumpTable - " << bankName
1245 <<
":" << rid <<
", " << c.GetUniqueID() <<
", "
1251 out <<
"\tHash[PdbCalChans] = \t" << hash << endl;
1256 cout <<
"PgPostBankBackupManager::dumpTable - processed " << cnt <<
" records"
1266 std::unique_ptr<TBuffer>
b(
new TBufferFile(TBuffer::kWrite));
1268 b->WriteObject(ptr);
1279 return TString::Hash(b->Buffer(), b->Length());
1283 const PHTimeStamp &min_save_time,
const bool do_delete,
1284 const std::string &log_file_name)
1286 const bool do_log = log_file_name.size();
1290 cout <<
"PgPostBankBackupManager::CleanTable - start on fetching "
1291 << bankName <<
" -> " << bankName
1292 <<
" with preservation limit of T>" << min_save_time;
1294 cout <<
". Log to file " << log_file_name;
1300 flog.open(log_file_name.c_str(), ios_base::out);
1305 cout <<
"PgPostBankBackupManager::CleanTable - ERROR - "
1306 <<
" PgPostApplication instance is nullptr, exiting" << endl;
1313 cout <<
"PgPostBankBackupManager::CleanTable - ERROR - "
1314 <<
" Cannot get TSQLConnection, exiting" << endl;
1318 vector<int> bankids;
1320 TSQLStatement *stmt = con->CreateStatement();
1321 std::ostringstream tem;
1323 tem <<
"select bankid from " << bankName
1324 <<
" group by bankid order by bankid";
1327 cout <<
"PgPostBankBackupManager::CleanTable - database exe : "
1328 << tem.str() << endl;
1330 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1333 cout <<
"PgPostBankBackupManager::CleanTable - ERROR - "
1334 <<
" Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
1340 const int id = rs->GetInt(1);
1343 cout <<
"\tBankID = \t" <<
id << endl;
1345 bankids.push_back(
id);
1349 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1350 <<
": received " << bankids.size() <<
" bank IDs" << endl;
1353 vector<int> rid_to_drop;
1355 for (vector<int>::const_iterator
it = bankids.begin();
it != bankids.end();
1358 const int bankid = *
it;
1361 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1362 <<
": process bankID " << bankid <<
". Processed " << cnt_total
1363 <<
" records / delete " << rid_to_drop.size() << endl;
1365 TSQLStatement *stmt = con->CreateStatement();
1366 std::ostringstream tem;
1368 tem <<
"select inserttime,startvaltime,endvaltime,rid FROM " << bankName
1369 <<
" where bankid = " << bankid <<
" and inserttime < "
1370 << min_save_time.
getTics() <<
" order by inserttime desc, rid desc";
1373 cout <<
"PgPostBankBackupManager::CleanTable - database exe : "
1374 << tem.str() << endl;
1376 std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1379 cout <<
"PgPostBankBackupManager::CleanTable - ERROR - "
1380 <<
" Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
1384 typedef map<int, int> covered_period_t;
1385 covered_period_t covered_period;
1391 const int inserttime = rs->GetInt(1);
1392 int startvaltime = rs->GetInt(2);
1393 int endvaltime = rs->GetInt(3);
1394 const int rid = rs->GetInt(4);
1395 int drop_record = 0;
1398 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1399 <<
" : " << bankid <<
" : " << rid
1400 <<
" : process record with insert " << inserttime <<
" covers ("
1401 << startvaltime <<
", " << endvaltime <<
")" << endl;
1416 if (startvaltime > endvaltime)
1420 cout <<
"PgPostBankBackupManager::CleanTable - ERROR -"
1421 << bankName <<
" : " << bankid <<
" : " << rid
1422 <<
": wrong record start " << startvaltime <<
" > end "
1423 << endvaltime <<
". Drop it \n";
1429 int last_starttime = -2;
1431 covered_period_t::iterator
piter = covered_period.begin();
1432 while (piter != covered_period.end() && drop_record == 0)
1434 int covered_startvaltime = piter->first;
1435 int covered_endvaltime = piter->second;
1438 assert(last_starttime < covered_startvaltime);
1440 const bool contain_startvaltime = startvaltime >= covered_startvaltime && startvaltime <= covered_endvaltime;
1441 const bool contain_endvaltime = endvaltime >= covered_startvaltime && endvaltime <= covered_endvaltime;
1443 if (contain_startvaltime && contain_endvaltime)
1446 cout <<
"PgPostBankBackupManager::CleanTable -"
1447 << bankName <<
" : " << bankid <<
" : " << rid
1448 <<
" : - fully covered record (" << startvaltime
1449 <<
", " << endvaltime <<
") <-> ("
1450 << covered_startvaltime <<
", "
1451 << covered_endvaltime <<
")" << endl;
1458 contain_startvaltime && !contain_endvaltime)
1461 cout <<
"PgPostBankBackupManager::CleanTable -"
1462 << bankName <<
" : " << bankid <<
" : " << rid
1463 <<
" : - afterward merge record (" << startvaltime
1464 <<
", " << endvaltime <<
") <-> ("
1465 << covered_startvaltime <<
", "
1466 << covered_endvaltime <<
")" << endl;
1468 assert(endvaltime > covered_endvaltime);
1471 piter->second = endvaltime;
1472 startvaltime = covered_startvaltime;
1478 else if (!contain_startvaltime && contain_endvaltime)
1482 cout <<
"PgPostBankBackupManager::CleanTable -"
1483 << bankName <<
" : " << bankid <<
" : " << rid
1484 <<
" : - forward merge record (" << startvaltime
1485 <<
", " << endvaltime <<
") <-> ("
1486 << covered_startvaltime <<
", "
1487 << covered_endvaltime <<
")" << endl;
1489 assert(startvaltime < covered_startvaltime);
1492 covered_period_t::iterator piter_tmp =
piter;
1494 covered_period.erase(piter_tmp);
1496 covered_startvaltime = startvaltime;
1497 covered_period[covered_startvaltime] = covered_endvaltime;
1498 endvaltime = covered_endvaltime;
1503 else if (startvaltime < covered_startvaltime && endvaltime > covered_endvaltime)
1507 cout <<
"PgPostBankBackupManager::CleanTable -"
1508 << bankName <<
" : " << bankid <<
" : " << rid
1509 <<
" : - over merge record (" << startvaltime
1510 <<
", " << endvaltime <<
") <-> ("
1511 << covered_startvaltime <<
", "
1512 << covered_endvaltime <<
")" << endl;
1515 covered_period_t::iterator piter_tmp =
piter;
1517 covered_period.erase(piter_tmp);
1519 covered_startvaltime = startvaltime;
1520 covered_endvaltime = endvaltime;
1521 covered_period[covered_startvaltime] = covered_endvaltime;
1530 cout <<
"PgPostBankBackupManager::CleanTable -"
1531 << bankName <<
" : " << bankid <<
" : " << rid
1532 <<
" : - not related records (" << startvaltime
1533 <<
", " << endvaltime <<
") <-> ("
1534 << covered_startvaltime <<
", "
1535 << covered_endvaltime <<
")" << endl;
1540 last_starttime = covered_startvaltime;
1546 if (drop_record > 0)
1550 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1551 <<
" : " << bankid <<
" : " << rid <<
" : - drop record"
1555 rid_to_drop.push_back(rid);
1561 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1562 <<
" : " << bankid <<
" : " << rid <<
" : - keep record"
1565 covered_period[startvaltime] = endvaltime;
1570 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1571 <<
" : " << bankid <<
" : " << rid
1572 <<
" : - print covered period";
1573 for (covered_period_t::iterator
piter = covered_period.begin();
1576 const int covered_startvaltime =
piter->first;
1577 const int covered_endvaltime =
piter->second;
1579 cout <<
", (" << covered_startvaltime <<
", "
1580 << covered_endvaltime <<
")";
1592 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1593 <<
": will delete " << rid_to_drop.size() <<
" records." << endl;
1595 ostringstream sqlcmd;
1596 sqlcmd <<
"delete from " << bankName <<
" where rid = ?";
1599 cout <<
"PgPostBankBackupManager::CleanTable - database commit : "
1600 << sqlcmd.str() << endl;
1602 TSQLPreparedStatement *pstmt = con->PrepareStatement(
1603 sqlcmd.str().c_str());
1615 for (vector<int>::const_iterator
it = rid_to_drop.begin();
1616 it != rid_to_drop.end(); ++
it)
1620 const int rid = *
it;
1623 cout <<
"PgPostBankBackupManager::CleanTable -" << bankName
1624 <<
": delete rid " << rid <<
", execute flag (do_delete) = "
1625 << do_delete << endl;
1630 cout <<
"PgPostBankBackupManager::CleanTable - " << bankName
1631 <<
" delete : " << cnt <<
"/" << rid_to_drop.size() << endl;
1644 pstmt->SetInt(1, rid);
1646 res = pstmt->ExecuteUpdate();
1651 catch (TSQLException &
e)
1653 cout <<
"PgPostBankBackupManager::CleanTable - Error - "
1654 <<
" Exception caught during connection->commit()"
1656 cout << e.GetMessage() << endl;
1664 cout <<
"PgPostBankBackupManager::CleanTable - Error - "
1665 <<
"DATABASE: delete " << bankName <<
" failed. "
1666 <<
"Make sure you commit to the master database " << endl;
1673 flog <<
"Deleted Entry - " << bankName <<
" RId " << rid
1692 return rid_to_drop.size();