ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PgPostBankBackupManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PgPostBankBackupManager.cc
1 
10 
11 #include "PgPostApplication.h"
12 #include "PgPostBankBackupLog.h"
14 #include "PgPostBankWrapper.h"
15 #include "PgPostCalBank.h"
16 
17 #include <pdbcalbase/PdbCalChan.h>
18 #include <pdbcalbase/PdbCalBank.h>
19 #include <pdbcalbase/PdbClassMap.h>
20 
21 #include <phool/PHTimeServer.h>
22 #include <phool/PHTimeStamp.h>
23 #include <phool/PHTimer.h>
24 
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>
30 
31 #include <TBuffer.h>
32 #include <TBufferFile.h>
33 #include <TCollection.h> // for TIter
34 #include <TDirectory.h> // for gDirectory, TDirectory (ptr ...
35 #include <TFile.h>
36 #include <TKey.h>
37 #include <TList.h>
38 #include <TObject.h> // for TObject, TObject::kWriteDelete
39 #include <TString.h>
40 
41 #include <algorithm>
42 #include <cassert>
43 #include <cstdlib>
44 #include <exception>
45 #include <fstream>
46 #include <iostream>
47 #include <map>
48 #include <memory>
49 #include <sstream>
50 #include <utility>
51 #include <vector>
52 
53 using namespace std;
54 
56  : verbosity(0)
57  , tag(Tag)
58 {
59  if (tag.length() == 0)
60  {
61  tag = "NO_TAG";
62  }
63 
64  cout
65  << "PgPostBankBackupManager::PgPostBankBackupManager - use backup log tag "
66  << tag << endl;
67 }
68 
70 {
71 }
72 
74 {
75  if (!stmt)
76  {
77  cout << "PgPostBankBackupManager::deleteSQLStatement - WARNING - "
78  << " empty pointer" << endl;
79  return;
80  }
81 
83  if (!ap)
84  {
85  cout << "PgPostBankBackupManager::deleteSQLStatement - ERROR - "
86  << " PgPostApplication instance is nullptr, exiting" << endl;
87  exit(1);
88  }
89  TSQLConnection *con = ap->getConnection();
90  if (!con)
91  {
92  cout << "PgPostBankBackupManager::deleteSQLStatement - ERROR - "
93  << " Cannot get TSQLConnection, exiting" << endl;
94  exit(1);
95  }
96 
97  TList *sl = con->GetListOfStatements();
98  if (!sl)
99  return;
100 
101  TObject *obj = sl->Remove(stmt);
102  if (!obj)
103  {
104  cout << "PgPostBankBackupManager::deleteSQLStatement - ERROR - "
105  << " Cannot find statement in TSQLConnection" << endl;
106  exit(1);
107  }
108 
109  delete stmt;
110  // stmt = nullptr;
111 }
112 
114  TSQLPreparedStatement *stmt)
115 {
116  if (!stmt)
117  {
118  cout
119  << "PgPostBankBackupManager::deleteODBCPreparedStatement - WARNING - "
120  << " empty pointer" << endl;
121  return;
122  }
123 
125  if (!ap)
126  {
127  cout << "PgPostBankBackupManager::deleteODBCPreparedStatement - ERROR - "
128  << " PgPostApplication instance is nullptr, exiting" << endl;
129  exit(1);
130  }
131  TSQLConnection *con = ap->getConnection();
132  if (!con)
133  {
134  cout << "PgPostBankBackupManager::deleteODBCPreparedStatement - ERROR - "
135  << " Cannot get TSQLConnection, exiting" << endl;
136  exit(1);
137  }
138 
139  TList *sl = con->GetListOfStatements();
140  if (!sl)
141  return;
142 
143  // inherance check
144  TObject *o = dynamic_cast<TObject *>(stmt);
145  assert(o);
146 
147  TObject *obj = sl->Remove(o);
148  if (!obj)
149  {
150  cout << "PgPostBankBackupManager::deleteODBCPreparedStatement - ERROR - "
151  << " Cannot find statement in TSQLConnection" << endl;
152  exit(1);
153  }
154  assert(o == obj);
155 
156  // cout << "PgPostBankBackupManager::deleteODBCPreparedStatement - delete the object" << endl;
157  delete stmt;
158  // stmt = nullptr;
159 }
160 
163  const std::string &table_name)
164 {
165  if (!rs)
166  {
167  cout
168  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - Error - null TSQLResultSet"
169  << endl;
170  return nullptr;
171  }
172 
173  if (rs->GetRow() == 0)
174  {
175  cout
176  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - Error - invalid TSQLResultSet with GetRow = 0"
177  << endl;
178  return nullptr;
179  }
180 
181  const int rid = rs->GetInt(8);
182  std::unique_ptr<PgPostCalBank> bw(
183  dynamic_cast<PgPostCalBank *>(rs->GetObject(7)));
184  assert(bw.get());
185  int length = 0;
186  string pdbcalchan_classname;
187  string pgpostcalbank_classname;
188  PdbCalBank *bank_orig = nullptr;
189 
191  if (string(bw->ClassName()) == string("PgPostBankWrapper"))
192  {
193  if (verbosity >= 2)
194  cout
195  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - Processing PgPostBankWrapper "
196  << endl;
197  bank_orig = (static_cast<PgPostBankWrapper *>(bw.get()))->getBank();
198  assert(bank_orig);
199  }
200  else if (string(bw->ClassName()) == string("PgPostCalBank"))
201  {
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());
206  assert(bank_orig);
207  }
208  else if (classMap->find(bw->ClassName()) != classMap->end())
209  {
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());
215  assert(bank_orig);
216  }
217  else
218  {
219  cout << "PgPostBankBackupManager::SQLResultSet2BackupStorage - "
220  << " ERROR - unknown object in database record in class "
221  << bw->ClassName() << endl;
222  exit(1);
223  }
224 
225  // check bank_orig
226  int unwrap_cnt = 1;
227  while (bank_orig)
228  {
229  if (string(bank_orig->ClassName()) == string("PgPostBankWrapper"))
230  {
231  cout
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;
236  bank_orig = static_cast<PgPostBankWrapper *>(bank_orig)->getBank();
237  assert(bank_orig);
238  }
239  else
240  {
241  if (verbosity >= 2)
242  cout << "PgPostBankBackupManager::SQLResultSet2BackupStorage - "
243  << bank_orig->ClassName() << " passed wrapper check" << endl;
244  break; // Done
245  }
246 
247  unwrap_cnt++;
248  if (unwrap_cnt > 10)
249  {
250  cout
251  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - Fatal Error - "
252  << "PgPostBankWrapper object nested inside PgPostBankWrapper for "
253  << unwrap_cnt << " layers: table " << table_name
254  << " where rid = " << rid << endl;
255  exit(1);
256  }
257  }
258 
259  // Check again
260  assert(bank_orig);
261  if (classMap->find(bank_orig->ClassName()) == classMap->end())
262  {
263  cout
264  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - Fatal Error - "
265  << "Calibration bank object of " << bank_orig->ClassName()
266  << " is not supported: table " << table_name << " where rid = " << rid
267  << endl;
268  exit(10);
269  }
270 
271  // Go on to make copies
272  length = bank_orig->getLength();
273  pgpostcalbank_classname = bank_orig->ClassName();
274  pdbcalchan_classname = string("Pdb") + getBankBaseName(pgpostcalbank_classname);
275  if (verbosity >= 2)
276  cout << "PgPostBankBackupManager::SQLResultSet2BackupStorage - Bank "
277  << pgpostcalbank_classname << " contains " << length << " "
278  << pdbcalchan_classname << endl;
279 
280  PdbCalBank *bank_copy = static_cast<PdbCalBank *>(bank_orig->Clone());
282  assert(bs);
283 
284  if (verbosity >= 40)
285  {
286  cout
287  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - obj header 1 "
288  << endl;
289  bs->get_obj_header().Print();
290  }
291 
292  bs->get_database_header().setBankID(rs->GetInt(1));
293  // bs->get_database_header().setBankID2(rs->GetInt(1));
294  bs->get_database_header().setInsertTime(rs->GetLong(2));
295  bs->get_database_header().setStartValTime(rs->GetLong(3));
296  bs->get_database_header().setEndValTime(rs->GetLong(4));
297  bs->get_database_header().setDescription(rs->GetString(5).Data());
298  bs->get_database_header().setUserName(rs->GetString(6).Data());
299  bs->get_database_header().setTableName(table_name);
300  bs->get_database_header().setRId(rid);
301 
302  if (verbosity >= 40)
303  {
304  cout
305  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - obj header 2 "
306  << endl;
307  bs->get_obj_header().Print();
308  }
309 
310  bs->set_obj_info(bw.get());
311  bs->format_name_title();
312 
313  if (verbosity >= 40)
314  {
315  cout
316  << "PgPostBankBackupManager::SQLResultSet2BackupStorage - obj header 3 "
317  << endl;
318  bs->get_obj_header().Print();
319  }
320 
321  return bs;
322 }
323 
327 PgPostBankBackupManager::fetchBank(const std::string &bankName, int rid)
328 {
329  if (verbosity >= 2)
330  cout << "PgPostBankBackupManager::fetchBank - start on fetching "
331  << bankName << " ID " << rid << endl;
332 
334  if (!ap)
335  {
336  cout << "PgPostBankBackupManager::fetchBank - ERROR - "
337  << " PgPostApplication instance is nullptr, exiting" << endl;
338  exit(1);
339  }
340 
341  TSQLConnection *con = ap->getConnection();
342  if (!con)
343  {
344  cout << "PgPostBankBackupManager::fetchBank - ERROR - "
345  << " Cannot get TSQLConnection, exiting" << endl;
346  exit(1);
347  }
348 
349  TSQLStatement *stmt = con->CreateStatement();
350  std::ostringstream tem;
351  // std::ostringstream t2;
352 
353  // cout << bankID.getInternalValue() << endl;
354  tem
355  << "select bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid from "
356  << bankName << " where rid = " << rid;
357 
358  if (verbosity >= 2)
359  cout << "PgPostBankBackupManager::fetchBank - database exe : " << tem.str()
360  << endl;
361 
362  // std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
363  // if (!rs.get())
364  TSQLResultSet *rs(stmt->ExecuteQuery(tem.str().c_str()));
365  if (!rs)
366  {
367  cout << "PgPostBankBackupManager::fetchBank - ERROR - "
368  << " Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
369  exit(1);
370  }
371 
372  if (rs->Next())
373  {
375 
376  if (Verbosity() >= 2)
377  cout << "PgPostBankBackupManager::fetchBank - clear TSQLResultSet"
378  << endl;
379 
380  delete rs;
381  rs = nullptr;
382 
383  if (Verbosity() >= 2)
384  cout << "PgPostBankBackupManager::fetchBank - clear SQLStatement List"
385  << endl;
386  deleteSQLStatement(stmt);
387 
388  return bs;
389  }
390  else
391  {
392  cout << "PgPostBankBackupManager::fetchBank - ERROR - NO Bank found : "
393  << tem.str() << std::endl;
394 
395  if (Verbosity() >= 2)
396  cout << "PgPostBankBackupManager::fetchBank - clear TSQLResultSet"
397  << endl;
398  if (rs)
399  {
400  if (Verbosity() >= 2)
401  cout << "PgPostBankBackupManager::fetchBank - clear TSQLResultSet"
402  << endl;
403 
404  delete rs;
405  rs = nullptr;
406  }
407 
408  return nullptr;
409  }
410 
411  return nullptr;
412 }
413 
418 {
419  if (!bs)
420  {
421  cout << "PgPostBankBackupManager::commit - Error - "
422  << " invalid input pointer" << endl;
423  return false;
424  }
425  if (!bs->isValid())
426  {
427  cout << "PgPostBankBackupManager::commit - Error - "
428  << " invalid input object" << endl;
429  return false;
430  }
431 
432  // std::unique_ptr<PgPostCalBank> bw(bs->createBank());
433  // assert(bw.get());
434 
435  try
436  {
438  assert(ap);
439  TSQLConnection *con = ap->getConnection();
440  assert(con);
441 
442  // Check
444  bs->get_database_header().getRId()))
445  {
446  cout << "PgPostBankBackupManager::commit - Error - "
447  << " RID "
448  << bs->get_database_header().getRId()
449  << " already exist in table "
450  << bs->get_database_header().getTableName() << ". STOP!" << endl;
451  return false;
452  }
453 
454  // Commit
455  ostringstream sqlcmd;
456  sqlcmd << "insert into " << bs->get_database_header().getTableName()
457  << "(bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid) values (?,?,?,?,?,?,?,?);";
458 
459  if (verbosity >= 2)
460  cout << "PgPostBankBackupManager::fetchBank - database commit : "
461  << sqlcmd.str() << endl;
462  TSQLPreparedStatement *pstmt = con->PrepareStatement(
463  sqlcmd.str().c_str());
464  // std::unique_ptr<TSQLPreparedStatement> pstmt(
465  // con->PrepareStatement(sqlcmd.str().c_str()));
466 
467  // deleteODBCPreparedStatement(pstmt);
468 
469  pstmt->SetInt(1, bs->get_database_header().getBankID());
470  pstmt->SetLong(2, (bs->get_database_header().getInsertTime()).getTics());
471  pstmt->SetLong(3,
472  (bs->get_database_header().getStartValTime()).getTics());
473  pstmt->SetLong(4, (bs->get_database_header().getEndValTime()).getTics());
474  pstmt->SetString(5, (bs->get_database_header().getDescription()).c_str());
475  pstmt->SetString(6, (bs->get_database_header().getUserName()).c_str());
476  std::unique_ptr<PgPostCalBank> bw(bs->createBank());
477  assert(bw.get());
478  pstmt->SetObject(7, bw.get());
479  pstmt->SetInt(8, bs->get_database_header().getRId());
480  int res = 0;
481  res = pstmt->ExecuteUpdate();
482  try
483  {
484  con->Commit();
485  }
486  catch (TSQLException &e)
487  {
488  cout << "PgPostBankBackupManager::commit - Error - "
489  << " Exception caught during connection->commit()" << endl;
490  cout << e.GetMessage() << endl;
491  // delete pstmt;
492  return false;
493  }
494  // delete pstmt;
495  if (res == 0)
496  {
497  cout << "PgPostBankBackupManager::commit - Error - "
498  << "DATABASE: commit to "
499  << bs->get_database_header().getTableName() << " failed. "
500  << "Make sure you commit to the master database " << endl;
501  exit(1);
502  }
503  assert(res == 1);
504 
505  if (Verbosity() >= 2)
506  cout << "PgPostBankBackupManager::fetchBank - clear SQLStatement List"
507  << endl;
509 
510  if (Verbosity() >= 1)
511  cout << "PgPostBankBackupManager::commit - Committed " << res << " row"
512  << endl;
513  }
514  catch (std::exception &e)
515  {
516  cout << "PgPostBankBackupManager::commit - Error - "
517  << " Exception caught " << endl;
518  cout << e.what() << endl;
519  // delete pstmt;
520  return false;
521  }
522 
523  return true;
524 }
525 
527 int PgPostBankBackupManager::commitAllBankfromTFile(const std::string &input_file)
528 {
529  if (Verbosity() >= 1)
530  cout << "PgPostBankBackupManager::commitAllBankfromTFile - Loading TFile "
531  << input_file << endl;
532  TFile *f = new TFile(input_file.c_str());
533 
534  if (!f)
535  {
536  cout
537  << "PgPostBankBackupManager::commitAllBankfromTFile - ERROR - can not open TFile "
538  << input_file << endl;
539  return 0;
540  }
541  if (!f->IsOpen())
542  {
543  cout
544  << "PgPostBankBackupManager::commitAllBankfromTFile - ERROR - can not open TFile "
545  << input_file << endl;
546  return 0;
547  }
548 
550  if (!ap)
551  {
552  cout << "PgPostBankBackupManager::commitAllBankfromTFile - ERROR - "
553  << " PgPostApplication instance is nullptr, exiting" << endl;
554  exit(1);
555  }
556 
557  TSQLConnection *con = ap->getConnection();
558  if (!con)
559  {
560  cout << "PgPostBankBackupManager::commitAllBankfromTFile - ERROR - "
561  << " Cannot get TSQLConnection, exiting" << endl;
562  exit(1);
563  }
564 
565  int commit_cnt = 0;
566  int skip_cnt = 0;
567  const int file_cnt = f->GetListOfKeys()->GetSize();
568 
569  PHTimeServer::timer timer_loop(
570  PHTimeServer::get()->insert_new("Record loop"));
571  PHTimeServer::timer timer_db(PHTimeServer::get()->insert_new("Database"));
572  PHTimeServer::timer timer_file(PHTimeServer::get()->insert_new("File write"));
573  PHTimeServer::timer timer_log(
574  PHTimeServer::get()->insert_new("Log database"));
575 
576  bool first_read = true;
577  string table_name;
578  rid_list_t existing_rids;
579  TSQLPreparedStatement *pstmt = nullptr;
581  static_cast<PgPostBankBackupLog *>(nullptr));
582 
583  TIter next(f->GetListOfKeys());
584  TObject *o = nullptr;
585  while ((o = next()))
586  {
587  if (Verbosity() >= 1)
588  {
589  if (((commit_cnt + skip_cnt) % 10000 == 1) || (Verbosity() >= 2))
590  {
591  cout
592  << "PgPostBankBackupManager::commitAllBankfromTFile - processing "
593  << table_name << ": commit/skip/total = " << commit_cnt << "/"
594  << skip_cnt << "/" << file_cnt << " to " << input_file
595  << endl;
596 
597  timer_loop.get()->print_stat();
598  timer_db.get()->print_stat();
599  timer_file.get()->print_stat();
600  timer_log.get()->print_stat();
601  }
602  }
603 
604  timer_loop.get()->restart();
605 
606  TKey *key = dynamic_cast<TKey *>(o);
607  assert(key);
608 
609  if (Verbosity() >= 2)
610  {
611  cout
612  << "PgPostBankBackupManager::commitAllBankfromTFile - Processing "
613  << key->GetName() << " ----------------------------------------"
614  << endl;
615  }
616 
617  try
618  {
619  timer_file.get()->restart();
620 
621  std::unique_ptr<PgPostBankBackupStorage>
622  // PgPostBankBackupStorage *
623  bs(dynamic_cast<PgPostBankBackupStorage *>(key->ReadObj()));
624  timer_file.get()->stop();
625 
626  if (!bs.get())
627  {
628  cout
629  << "PgPostBankBackupManager::commitAllBankfromTFile - ERROR - can not read "
630  << key->GetName() << " from " << input_file << endl;
631  continue;
632  }
633 
634  if (first_read)
635  {
636  first_read = false;
637 
638  table_name = bs->get_database_header().getTableName();
639 
640  existing_rids = PgPostBankBackupManager::getListOfRId(table_name);
641 
642  if (Verbosity() >= 1)
643  cout
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;
648 
649  ostringstream sqlcmd;
650  sqlcmd << "insert into " << table_name
651  << "(bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid) values (?,?,?,?,?,?,?,?);";
652 
653  if (verbosity >= 1)
654  cout
655  << "PgPostBankBackupManager::commitAllBankfromTFile - database commit statement : "
656  << sqlcmd.str() << endl;
657  pstmt = con->PrepareStatement(sqlcmd.str().c_str());
658 
659  bklog = make_shared<PgPostBankBackupLog>(table_name, tag);
660  bklog->Init();
661  } // if (first_read)
662 
663  assert(table_name.length() > 0);
664  assert(pstmt);
665  assert(bklog.get());
666 
667  if (table_name != bs->get_database_header().getTableName())
668  {
669  cout
670  << "PgPostBankBackupManager::commitAllBankfromTFile - ERROR - inconsistent table name for "
671  << key->GetName() << " from " << input_file << ", expect "
672  << table_name << endl;
673 
674  continue;
675  }
676 
677  const int rid = bs->get_database_header().getRId();
678  const bool existing_rid = std::find(existing_rids.begin(),
679  existing_rids.end(), rid) != existing_rids.end();
680  if (existing_rid)
681  {
682  if (Verbosity() >= 2)
683  {
684  cout
685  << "PgPostBankBackupManager::commitAllBankfromTFile - existing records "
686  << key->GetName() << " in database" << endl;
687  }
688 
689  timer_log.get()->restart();
690  bklog->Log(rid, PgPostBankBackupLog::kOptFile2Db_Skip);
691  timer_log.get()->stop();
692  skip_cnt++;
693 
694  } // existing records
695  else
696  { // new records
697 
698  if (Verbosity() >= 2)
699  {
700  cout
701  << "PgPostBankBackupManager::commitAllBankfromTFile - submit new record "
702  << key->GetName() << " to database" << endl;
703  }
704 
705  timer_db.get()->restart();
706  pstmt->SetInt(1, bs->get_database_header().getBankID());
707  pstmt->SetLong(2,
708  (bs->get_database_header().getInsertTime()).getTics());
709  pstmt->SetLong(3,
710  (bs->get_database_header().getStartValTime()).getTics());
711  pstmt->SetLong(4,
712  (bs->get_database_header().getEndValTime()).getTics());
713  pstmt->SetString(5,
714  (bs->get_database_header().getDescription()).c_str());
715  pstmt->SetString(6,
716  (bs->get_database_header().getUserName()).c_str());
717  std::unique_ptr<PgPostCalBank> bw(bs->createBank());
718  assert(bw.get());
719  pstmt->SetObject(7, bw.get());
720  pstmt->SetInt(8, bs->get_database_header().getRId());
721  int res = 0;
722  res = pstmt->ExecuteUpdate();
723  if (res != 1)
724  {
725  cout
726  << "PgPostBankBackupManager::commitAllBankfromTFile - Error - "
727  << "DATABASE: commit to "
729  << " failed with ExecuteUpdate()=" << res
730  << ". Make sure you commit to a writable database "
731  << endl;
732 
733  timer_log.get()->restart();
734  bklog->Log(rid,
736  timer_log.get()->stop();
737  }
738  else
739  {
740  try
741  {
742  con->Commit();
743  }
744  catch (TSQLException &e)
745  {
746  cout
747  << "PgPostBankBackupManager::commitAllBankfromTFile - Error - "
748  << " Exception caught during connection->commit()"
749  << endl;
750  cout << e.GetMessage() << endl;
751  // delete pstmt;
752  exit(1);
753  }
754  timer_log.get()->restart();
755  bklog->Log(rid, PgPostBankBackupLog::kOptFile2Db);
756  timer_log.get()->stop();
757 
758  commit_cnt++;
759  }
760 
761  timer_db.get()->stop();
762  }
763 
764  timer_loop.get()->stop();
765 
766  } // try
767  catch (std::exception &e)
768  {
769  cout << "PgPostBankBackupManager::commit - Error - "
770  << " unhandled exception caught when process record "
771  << key->GetName() << ":" << endl;
772  cout << e.what() << endl;
773  // delete pstmt;
774  }
775 
776  } // main loop of records
777 
778  f->Close();
779 
780  if (Verbosity() >= 1)
781  {
782  if ((commit_cnt + skip_cnt) % 10000 == 1)
783  {
784  cout << "PgPostBankBackupManager::commitAllBankfromTFile - Done "
785  << table_name << ": commit/skip/total = " << commit_cnt << "/"
786  << skip_cnt << "/" << file_cnt << " to " << input_file << endl;
787 
788  timer_loop.get()->print_stat();
789  timer_db.get()->print_stat();
790  timer_file.get()->print_stat();
791  timer_log.get()->print_stat();
792  }
793  }
794 
795  return commit_cnt;
796 }
797 
799 int PgPostBankBackupManager::fetchBank2TFile(const std::string &bankName,
800  const rid_list_t &rid_list, const std::string &out_file)
801 {
802  TDirectory *gd = gDirectory;
803  TFile f(out_file.c_str(), "update");
804 
805  if (f.IsZombie())
806  {
807  cout << "PgPostBankBackupManager::fetchBank2TFile - Error -"
808  << " can not open file" << out_file;
809  return 0;
810  }
811 
812  int cnt = 0;
813  for (rid_list_t::const_iterator it = rid_list.begin(); it != rid_list.end();
814  ++it)
815  {
816  const int rid = *it;
817  if (Verbosity() >= 1)
818  cout << "PgPostBankBackupManager::fetchBank2TFile - Process "
819  << bankName << " at record ID " << rid << " (" << cnt << "/"
820  << rid_list.size() << ")" << endl;
821 
822  PgPostBankBackupStorage *bs = fetchBank(bankName, rid);
823 
824  if (!bs)
825  {
826  cout << "PgPostBankBackupManager::fetchBank2TFile - Error -"
827  << " nullptr PgPostBankBackupStorage object produced" << endl;
828  }
829  else if (!bs->isValid())
830  {
831  cout << "PgPostBankBackupManager::fetchBank2TFile - Error -"
832  << " invalid PgPostBankBackupStorage object produced" << endl;
833  bs->Print();
834  }
835  else
836  {
837  bs->Write(bs->GetName(), TObject::kWriteDelete);
838 
839  delete bs;
840  cnt++;
841  }
842  }
843 
844  f.Close();
845 
846  gDirectory = gd;
847 
848  cout << "PgPostBankBackupManager::fetchBank2TFile - " << cnt << " records in "
849  << bankName << " saved to " << out_file << endl;
850  return cnt;
851 }
852 
854 int PgPostBankBackupManager::fetchAllBank2TFile(const std::string &bankName,
855  const std::string &record_selection, const std::string &out_file_base,
856  int splitting_limit)
857 {
858  if (verbosity >= 2)
859  cout << "PgPostBankBackupManager::fetchBank - start on fetching "
860  << bankName << " for records criteria of [" << record_selection
861  << "] to " << out_file_base << "*.root" << endl;
862 
864  if (!ap)
865  {
866  cout << "PgPostBankBackupManager::fetchAllBank2TFile - ERROR - "
867  << " PgPostApplication instance is nullptr, exiting" << endl;
868  exit(1);
869  }
870 
871  TSQLConnection *con = ap->getConnection();
872  if (!con)
873  {
874  cout << "PgPostBankBackupManager::fetchAllBank2TFile - ERROR - "
875  << " Cannot get TSQLConnection, exiting" << endl;
876  exit(1);
877  }
878 
879  const int row_cnt = getTotalRowCount(bankName);
880  if (row_cnt <= 0)
881  {
882  cout << "PgPostBankBackupManager::fetchAllBank2TFile - WARNING - "
883  << " no data in table " << bankName << endl;
884  // return row_cnt;
885  }
886 
887  TSQLStatement *stmt = con->CreateStatement();
888  assert(stmt);
889 
890  if (verbosity >= 1)
891  cout
892  << "PgPostBankBackupManager::fetchAllBank2TFile - reset output length to row count "
893  << row_cnt << " for " << bankName << endl;
894  stmt->SetMaxRows(row_cnt + 1);
895 
896  std::ostringstream tem;
897  // std::ostringstream t2;
898  tem
899  << "select bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid from "
900  << bankName;
901  if (record_selection.length() > 0)
902  tem << " where " << record_selection;
903  tem << " ORDER BY rid ASC";
904 
905  if (verbosity >= 2)
906  cout << "PgPostBankBackupManager::fetchAllBank2TFile - database exe : "
907  << tem.str() << endl;
908 
909  TDirectory *gd = gDirectory;
910  TFile f;
911  int file_id = 0;
912  int file_rec_cnt = 0;
913  TString file_name;
914 
915  file_name.Form("%s_%04d.root", out_file_base.c_str(), file_id);
916  f.Open(file_name, "recreate");
917  if (f.IsZombie())
918  {
919  cout << "PgPostBankBackupManager::fetchAllBank2TFile - Error -"
920  << " can not open file" << file_name << endl;
921  return 0;
922  }
923  if (verbosity >= 1)
924  cout << "PgPostBankBackupManager::fetchAllBank2TFile - writing new file "
925  << file_name << endl;
926 
927  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
928  if (!rs || !rs.get())
929  // TSQLResultSet * rs(stmt->ExecuteQuery(tem.str().c_str()));
930  // if (!rs)
931  {
932  cout << "PgPostBankBackupManager::fetchAllBank2TFile - ERROR - "
933  << " Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
934  exit(1);
935  }
936 
937  // if (verbosity >= 2)
938  // cout << "PgPostBankBackupManager::fetchAllBank2TFile - received "
939  // << rs->GetRowCount() << " rows"
941  // << endl;
942 
943  PgPostBankBackupLog bklog(bankName, tag);
944  bklog.Init();
945 
946  int cnt = 0;
947 
948  PHTimeServer::timer timer_loop(
949  PHTimeServer::get()->insert_new("Record loop"));
950  PHTimeServer::timer timer_db(PHTimeServer::get()->insert_new("Database"));
951  PHTimeServer::timer timer_file(PHTimeServer::get()->insert_new("File write"));
952  PHTimeServer::timer timer_log(
953  PHTimeServer::get()->insert_new("Log database"));
954 
955  while (rs->Next())
956  {
958 
959  timer_loop.get()->restart();
960 
961  if (verbosity >= 1)
962  {
963  if (cnt % 1000 == 0)
964  cout << "PgPostBankBackupManager::fetchAllBank2TFile - processing "
965  << bankName << ": " << cnt << "/" << row_cnt << " to "
966  << file_name << endl;
967 
968  if (cnt % 10000 == 1)
969  {
970  timer_loop.get()->print_stat();
971  timer_db.get()->print_stat();
972  timer_file.get()->print_stat();
973  timer_log.get()->print_stat();
974  }
975  }
976 
977  if (file_rec_cnt >= splitting_limit)
978  {
979  f.Close();
980  ++file_id;
981  file_rec_cnt = 0;
982  file_name.Form("%s_%04d.root", out_file_base.c_str(), file_id);
983  f.Open(file_name, "recreate");
984  if (f.IsZombie())
985  {
986  cout << "PgPostBankBackupManager::fetchAllBank2TFile - Error -"
987  << " can not open file" << file_name;
988  return 0;
989  }
990  if (verbosity >= 1)
991  cout
992  << "PgPostBankBackupManager::fetchAllBank2TFile - writing new file "
993  << file_name << endl;
994  }
995 
996  timer_db.get()->restart();
997  const int rid = rs->GetInt(8);
999  bankName);
1000  timer_db.get()->stop();
1001 
1002  if (bs)
1003  {
1004  timer_file.get()->restart();
1005  bs->Write(bs->GetName(), TObject::kWriteDelete);
1006  timer_file.get()->stop();
1007 
1008  delete bs;
1009  cnt++;
1010  file_rec_cnt++;
1011  }
1012  else
1013  {
1015 
1016  cout << "PgPostBankBackupManager::fetchAllBank2TFile - Error - "
1017  << "invalid PgPostBankBackupStorage for row " << rs->GetRow()
1018  << endl;
1019  }
1020 
1021  timer_log.get()->restart();
1022  bklog.Log(rid,
1024  timer_log.get()->stop();
1025 
1026  timer_loop.get()->stop();
1027  }
1028 
1029  f.Close();
1030  gDirectory = gd;
1031 
1032  cout << "PgPostBankBackupManager::fetchAllBank2TFile - " << cnt
1033  << " records in " << bankName << " saved to " << out_file_base << "*.root"
1034  << endl;
1035  return cnt;
1036 }
1037 
1038 std::string
1039 PgPostBankBackupManager::getBankBaseName(const std::string &bank_classname)
1040 {
1041  if (bank_classname.length() <= 10)
1042  {
1043  cout
1044  << "PgPostBankBackupManager::getBankBaseName - Error - unknown format for "
1045  << bank_classname << endl;
1046  return "invalid";
1047  }
1048  string prefix = bank_classname.substr(0, 6);
1049  string suffix = bank_classname.substr(bank_classname.length() - 4, 4);
1050 
1051  if (prefix != "PgPost")
1052  {
1053  cout
1054  << "PgPostBankBackupManager::getBankBaseName - Error - unknown prefix format for "
1055  << bank_classname << endl;
1056  return "invalid";
1057  }
1058  if (suffix != "Bank")
1059  {
1060  cout
1061  << "PgPostBankBackupManager::getBankBaseName - Error - unknown suffix format for "
1062  << bank_classname << " with " << suffix << endl;
1063  return "invalid";
1064  }
1065 
1066  return bank_classname.substr(6, bank_classname.length() - 6 - 4);
1067 }
1068 
1070 bool PgPostBankBackupManager::isRIdExist(const std::string &bankName, int rid)
1071 {
1073  assert(ap);
1074  TSQLConnection *con = ap->getConnection();
1075  assert(con);
1076 
1077  // Check
1078  std::ostringstream tem;
1079  // std::ostringstream t2;
1080 
1081  tem << "select bankid, description,rid from " << bankName << " where rid = "
1082  << rid;
1083 
1084  if (verbosity >= 2)
1085  cout << "PgPostBankBackupManager::isRIdExist - database check : "
1086  << tem.str() << endl;
1087 
1088  TSQLStatement *stmt = con->CreateStatement();
1089  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1090  if ((rs) && rs->Next())
1091  {
1092  return true;
1093  }
1094 
1095  return false;
1096 }
1097 
1099 int PgPostBankBackupManager::getTotalRowCount(const std::string &bankName)
1100 {
1102  assert(ap);
1103  TSQLConnection *con = ap->getConnection();
1104  assert(con);
1105 
1106  // Check
1107  std::ostringstream tem;
1108  // std::ostringstream t2;
1109 
1110  tem << "select COUNT(*) AS NumberOfRows from " << bankName;
1111 
1112  if (verbosity >= 2)
1113  cout << "PgPostBankBackupManager::getTotalRowCount - database get : "
1114  << tem.str() << endl;
1115 
1116  TSQLStatement *stmt = con->CreateStatement();
1117  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1118  if ((rs) && rs->Next())
1119  {
1120  return rs->GetInt(1);
1121  }
1122 
1123  return 0;
1124 }
1125 
1129  const string &condition)
1130 {
1131  rid_list_t l;
1132 
1134  assert(ap);
1135  TSQLConnection *con = ap->getConnection();
1136  assert(con);
1137 
1138  // Check
1139  std::ostringstream tem;
1140  // std::ostringstream t2;
1141 
1142  tem << "select rid from " << bankName;
1143 
1144  if (condition.length() > 0)
1145  tem << " where " << condition;
1146 
1147  if (verbosity >= 1)
1148  cout << "PgPostBankBackupManager::getListOfRId - database fetch : "
1149  << tem.str() << endl;
1150 
1151  TSQLStatement *stmt = con->CreateStatement();
1152  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1153  while ((rs) && rs->Next())
1154  {
1155  int rid = rs->GetInt(1);
1156  l.push_back(rid);
1157  }
1158  if (verbosity >= 1)
1159  cout << "PgPostBankBackupManager::getListOfRId - database fetch "
1160  << l.size() << " RIDs" << endl;
1161 
1162  return l;
1163 }
1164 
1166 void PgPostBankBackupManager::dumpTable(const std::string &bankName,
1167  std::ostream &out)
1168 {
1169  if (verbosity >= 2)
1170  cout << "PgPostBankBackupManager::dumpTable - start on fetching "
1171  << bankName << " -> " << bankName << endl;
1172 
1174  if (!ap)
1175  {
1176  cout << "PgPostBankBackupManager::dumpTable - ERROR - "
1177  << " PgPostApplication instance is nullptr, exiting" << endl;
1178  exit(1);
1179  }
1180 
1181  TSQLConnection *con = ap->getConnection();
1182  if (!con)
1183  {
1184  cout << "PgPostBankBackupManager::dumpTable - ERROR - "
1185  << " Cannot get TSQLConnection, exiting" << endl;
1186  exit(1);
1187  }
1188 
1189  TSQLStatement *stmt = con->CreateStatement();
1190  std::ostringstream tem;
1191  // std::ostringstream t2;
1192 
1193  // cout << bankID.getInternalValue() << endl;
1194  tem
1195  << "select bankid,inserttime,startvaltime,endvaltime,description,username,calibrations,rid from "
1196  << bankName << " ORDER BY rid ASC";
1197 
1198  if (verbosity >= 2)
1199  cout << "PgPostBankBackupManager::dumpTable - database exe : " << tem.str()
1200  << endl;
1201 
1202  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1203  if (!rs.get())
1204  {
1205  cout << "PgPostBankBackupManager::dumpTable - ERROR - "
1206  << " Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
1207  exit(1);
1208  }
1209 
1210  PgPostBankBackupLog bklog(bankName, tag);
1211  bklog.Init();
1212  int cnt = 0;
1213  while (rs->Next())
1214  {
1215  std::unique_ptr<PgPostCalBank> bw(
1216  dynamic_cast<PgPostCalBank *>(rs->GetObject(7)));
1217  assert(bw.get());
1218 
1219  cnt++;
1220 
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;
1231 
1232  ULong_t hash = 0x0;
1233  for (size_t i = 0; i < bw->getLength(); i++)
1234  {
1235  PdbCalChan &c = bw->getEntry(i);
1236 
1237  // out << "\t\t" << c.ClassName() << "[" << i << "] = "
1238  // << HashPdbCalChan(c) << endl;
1239 
1240  hash ^= HashPdbCalChan(c);
1241 
1242  if (verbosity >= 4)
1243  {
1244  cout << "PgPostBankBackupManager::dumpTable - " << bankName
1245  << ":" << rid << ", " << c.GetUniqueID() << ", "
1246  << c.TestBits(0xFFFFFFFF) << " -> HASH " << HashPdbCalChan(c)
1247  << ", ";
1248  c.print();
1249  }
1250  }
1251  out << "\tHash[PdbCalChans] = \t" << hash << endl;
1252 
1253  bklog.Log(rid,
1255  }
1256  cout << "PgPostBankBackupManager::dumpTable - processed " << cnt << " records"
1257  << endl;
1258 }
1259 
1261 ULong_t
1263 {
1264  const TObject *ptr = dynamic_cast<const TObject *>(&c);
1265  assert(ptr);
1266  std::unique_ptr<TBuffer> b(new TBufferFile(TBuffer::kWrite));
1267 
1268  b->WriteObject(ptr);
1269 
1270  // cout <<"PgPostBankBackupManager::HashPdbCalChan - buffer dump with size "<<b->Length()<<endl;
1271  // const char * arr = b->Buffer();
1272  // for (int i = 0; i<b->Length(); i++ )
1273  // {
1274  // cout <<i<<"\t = 0x";
1275  // cout <<std::hex<<static_cast<unsigned int>(arr[i])<<endl;
1276  // }
1277  // cout <<std::dec;
1278 
1279  return TString::Hash(b->Buffer(), b->Length());
1280 }
1281 
1282 int PgPostBankBackupManager::CleanTable(const std::string &bankName,
1283  const PHTimeStamp &min_save_time, const bool do_delete,
1284  const std::string &log_file_name)
1285 {
1286  const bool do_log = log_file_name.size();
1287 
1288  if (verbosity >= 1)
1289  {
1290  cout << "PgPostBankBackupManager::CleanTable - start on fetching "
1291  << bankName << " -> " << bankName
1292  << " with preservation limit of T>" << min_save_time;
1293  if (do_log)
1294  cout << ". Log to file " << log_file_name;
1295  cout << endl;
1296  }
1297 
1298  fstream flog;
1299  if (do_log)
1300  flog.open(log_file_name.c_str(), ios_base::out);
1301 
1303  if (!ap)
1304  {
1305  cout << "PgPostBankBackupManager::CleanTable - ERROR - "
1306  << " PgPostApplication instance is nullptr, exiting" << endl;
1307  exit(1);
1308  }
1309 
1310  TSQLConnection *con = ap->getConnection();
1311  if (!con)
1312  {
1313  cout << "PgPostBankBackupManager::CleanTable - ERROR - "
1314  << " Cannot get TSQLConnection, exiting" << endl;
1315  exit(1);
1316  }
1317 
1318  vector<int> bankids;
1319  { // get bank IDs
1320  TSQLStatement *stmt = con->CreateStatement();
1321  std::ostringstream tem;
1322 
1323  tem << "select bankid from " << bankName
1324  << " group by bankid order by bankid";
1325 
1326  if (verbosity >= 1)
1327  cout << "PgPostBankBackupManager::CleanTable - database exe : "
1328  << tem.str() << endl;
1329 
1330  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1331  if (!rs.get())
1332  {
1333  cout << "PgPostBankBackupManager::CleanTable - ERROR - "
1334  << " Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
1335  exit(1);
1336  }
1337 
1338  while (rs->Next())
1339  {
1340  const int id = rs->GetInt(1);
1341 
1342  if (verbosity >= 2)
1343  cout << "\tBankID = \t" << id << endl;
1344 
1345  bankids.push_back(id);
1346  }
1347 
1348  if (verbosity >= 1)
1349  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1350  << ": received " << bankids.size() << " bank IDs" << endl;
1351  } // get bank IDs
1352 
1353  vector<int> rid_to_drop;
1354  int cnt_total = 0;
1355  for (vector<int>::const_iterator it = bankids.begin(); it != bankids.end();
1356  ++it)
1357  {
1358  const int bankid = *it;
1359 
1360  if (verbosity >= 1)
1361  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1362  << ": process bankID " << bankid << ". Processed " << cnt_total
1363  << " records / delete " << rid_to_drop.size() << endl;
1364 
1365  TSQLStatement *stmt = con->CreateStatement();
1366  std::ostringstream tem;
1367 
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";
1371 
1372  if (verbosity >= 1)
1373  cout << "PgPostBankBackupManager::CleanTable - database exe : "
1374  << tem.str() << endl;
1375 
1376  std::unique_ptr<TSQLResultSet> rs(stmt->ExecuteQuery(tem.str().c_str()));
1377  if (!rs.get())
1378  {
1379  cout << "PgPostBankBackupManager::CleanTable - ERROR - "
1380  << " Cannot get TSQLResultSet from ExecuteQuery, exiting" << endl;
1381  exit(1);
1382  }
1383 
1384  typedef map<int, int> covered_period_t;
1385  covered_period_t covered_period;
1386 
1387  while (rs->Next())
1388  {
1389  cnt_total++;
1390 
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;
1396 
1397  if (verbosity >= 2)
1398  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1399  << " : " << bankid << " : " << rid
1400  << " : process record with insert " << inserttime << " covers ("
1401  << startvaltime << ", " << endvaltime << ")" << endl;
1402 
1403  // move this part to DB query part
1404  // if (inserttime >= min_save_time.getTics())
1405  // {
1406  // if (verbosity >= 2)
1407  // {
1408  // cout << "PgPostBankBackupManager::CleanTable - " << bankName
1409  // << " : " << bankid << " : " << rid
1410  // << " : preserve record since insert time" << inserttime
1411  // << " > threshold " << min_save_time.getTics()
1412  // << ". Drop it \n";
1413  // }
1414  // }
1415 
1416  if (startvaltime > endvaltime)
1417  {
1418  if (verbosity >= 2)
1419  {
1420  cout << "PgPostBankBackupManager::CleanTable - ERROR -"
1421  << bankName << " : " << bankid << " : " << rid
1422  << ": wrong record start " << startvaltime << " > end "
1423  << endvaltime << ". Drop it \n";
1424  drop_record = 1;
1425  }
1426  } // if (startvaltime > endvaltime)
1427  else
1428  {
1429  int last_starttime = -2;
1430  // int last_endtime = -1;
1431  covered_period_t::iterator piter = covered_period.begin();
1432  while (piter != covered_period.end() && drop_record == 0)
1433  {
1434  int covered_startvaltime = piter->first;
1435  int covered_endvaltime = piter->second;
1436 
1437  // assert(last_starttime<last_endtime);
1438  assert(last_starttime < covered_startvaltime);
1439 
1440  const bool contain_startvaltime = startvaltime >= covered_startvaltime && startvaltime <= covered_endvaltime;
1441  const bool contain_endvaltime = endvaltime >= covered_startvaltime && endvaltime <= covered_endvaltime;
1442 
1443  if (contain_startvaltime && contain_endvaltime)
1444  {
1445  if (verbosity >= 2)
1446  cout << "PgPostBankBackupManager::CleanTable -"
1447  << bankName << " : " << bankid << " : " << rid
1448  << " : - fully covered record (" << startvaltime
1449  << ", " << endvaltime << ") <-> ("
1450  << covered_startvaltime << ", "
1451  << covered_endvaltime << ")" << endl;
1452 
1453  drop_record = 2;
1454  ++piter;
1455  } // if (contain_endvaltime)
1456  else if (
1457 
1458  contain_startvaltime && !contain_endvaltime)
1459  {
1460  if (verbosity >= 2)
1461  cout << "PgPostBankBackupManager::CleanTable -"
1462  << bankName << " : " << bankid << " : " << rid
1463  << " : - afterward merge record (" << startvaltime
1464  << ", " << endvaltime << ") <-> ("
1465  << covered_startvaltime << ", "
1466  << covered_endvaltime << ")" << endl;
1467 
1468  assert(endvaltime > covered_endvaltime);
1469 
1470  // $covered_period{covered_startvaltime} = endvaltime;
1471  piter->second = endvaltime;
1472  startvaltime = covered_startvaltime;
1473 
1474  ++piter;
1475 
1476  // $drop_record = 0;
1477  } //else if (contain_startvaltime && !contain_endvaltime)
1478  else if (!contain_startvaltime && contain_endvaltime)
1479  {
1480  if (verbosity >= 2)
1481 
1482  cout << "PgPostBankBackupManager::CleanTable -"
1483  << bankName << " : " << bankid << " : " << rid
1484  << " : - forward merge record (" << startvaltime
1485  << ", " << endvaltime << ") <-> ("
1486  << covered_startvaltime << ", "
1487  << covered_endvaltime << ")" << endl;
1488 
1489  assert(startvaltime < covered_startvaltime);
1490 
1491  // delete( covered_period{covered_startvaltime} );
1492  covered_period_t::iterator piter_tmp = piter;
1493  ++piter;
1494  covered_period.erase(piter_tmp);
1495 
1496  covered_startvaltime = startvaltime;
1497  covered_period[covered_startvaltime] = covered_endvaltime;
1498  endvaltime = covered_endvaltime;
1499 
1500  // $drop_record = 0;
1501 
1502  } // else if (!contain_startvaltime && (contain_endvaltime || endvaltime == covered_startvaltime - 1))
1503  else if (startvaltime < covered_startvaltime && endvaltime > covered_endvaltime)
1504  {
1505  if (verbosity >= 2)
1506 
1507  cout << "PgPostBankBackupManager::CleanTable -"
1508  << bankName << " : " << bankid << " : " << rid
1509  << " : - over merge record (" << startvaltime
1510  << ", " << endvaltime << ") <-> ("
1511  << covered_startvaltime << ", "
1512  << covered_endvaltime << ")" << endl;
1513 
1514  // delete( $covered_period{covered_startvaltime} );
1515  covered_period_t::iterator piter_tmp = piter;
1516  ++piter;
1517  covered_period.erase(piter_tmp);
1518 
1519  covered_startvaltime = startvaltime;
1520  covered_endvaltime = endvaltime;
1521  covered_period[covered_startvaltime] = covered_endvaltime;
1522 
1523  // $drop_record = 0;
1524 
1525  } // if ( startvaltime < covered_startvaltime && endvaltime > covered_endvaltime )
1526  else
1527  {
1528  if (verbosity >= 2)
1529 
1530  cout << "PgPostBankBackupManager::CleanTable -"
1531  << bankName << " : " << bankid << " : " << rid
1532  << " : - not related records (" << startvaltime
1533  << ", " << endvaltime << ") <-> ("
1534  << covered_startvaltime << ", "
1535  << covered_endvaltime << ")" << endl;
1536 
1537  ++piter;
1538  } // else
1539 
1540  last_starttime = covered_startvaltime;
1541  // last_endtime = covered_endvaltime;
1542  } // covered_period loop
1543 
1544  } //if (startvaltime > endvaltime) - else
1545 
1546  if (drop_record > 0)
1547  {
1548  if (verbosity >= 2)
1549  {
1550  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1551  << " : " << bankid << " : " << rid << " : - drop record"
1552  << endl;
1553  }
1554 
1555  rid_to_drop.push_back(rid);
1556  }
1557  else
1558  {
1559  if (verbosity >= 2)
1560  {
1561  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1562  << " : " << bankid << " : " << rid << " : - keep record"
1563  << endl;
1564  }
1565  covered_period[startvaltime] = endvaltime;
1566  } // if ($drop_record) - else
1567 
1568  if (verbosity >= 3)
1569  {
1570  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1571  << " : " << bankid << " : " << rid
1572  << " : - print covered period";
1573  for (covered_period_t::iterator piter = covered_period.begin();
1574  piter != covered_period.end(); ++piter)
1575  {
1576  const int covered_startvaltime = piter->first;
1577  const int covered_endvaltime = piter->second;
1578 
1579  cout << ", (" << covered_startvaltime << ", "
1580  << covered_endvaltime << ")";
1581  }
1582  cout << endl;
1583  } // if ( $verbosity >= 3 )
1584 
1585  } // while (rs->Next()) - record loop
1586 
1587  } // bankid loop
1588 
1589  // do the deleting
1590  {
1591  if (verbosity >= 1)
1592  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1593  << ": will delete " << rid_to_drop.size() << " records." << endl;
1594 
1595  ostringstream sqlcmd;
1596  sqlcmd << "delete from " << bankName << " where rid = ?";
1597 
1598  if (verbosity >= 1)
1599  cout << "PgPostBankBackupManager::CleanTable - database commit : "
1600  << sqlcmd.str() << endl;
1601 
1602  TSQLPreparedStatement *pstmt = con->PrepareStatement(
1603  sqlcmd.str().c_str());
1604 
1605  PgPostBankBackupLog bklog(bankName, tag);
1606  bklog.Init();
1607 
1608  PHTimeServer::timer timer_loop(
1609  PHTimeServer::get()->insert_new("Record loop"));
1610  PHTimeServer::timer timer_db(PHTimeServer::get()->insert_new("Database"));
1611  PHTimeServer::timer timer_log(
1612  PHTimeServer::get()->insert_new("Log database"));
1613 
1614  int cnt = 0;
1615  for (vector<int>::const_iterator it = rid_to_drop.begin();
1616  it != rid_to_drop.end(); ++it)
1617  {
1618  timer_loop.get()->restart();
1619 
1620  const int rid = *it;
1621 
1622  if (verbosity >= 2)
1623  cout << "PgPostBankBackupManager::CleanTable -" << bankName
1624  << ": delete rid " << rid << ", execute flag (do_delete) = "
1625  << do_delete << endl;
1626 
1627  if (verbosity >= 1)
1628  {
1629  if (cnt % 100 == 0)
1630  cout << "PgPostBankBackupManager::CleanTable - " << bankName
1631  << " delete : " << cnt << "/" << rid_to_drop.size() << endl;
1632 
1633  if (cnt % 100 == 1)
1634  {
1635  timer_loop.get()->print_stat();
1636  timer_db.get()->print_stat();
1637  timer_log.get()->print_stat();
1638  }
1639  }
1640 
1641  if (do_delete)
1642  {
1643  timer_db.get()->restart();
1644  pstmt->SetInt(1, rid);
1645  int res = 0;
1646  res = pstmt->ExecuteUpdate();
1647  try
1648  {
1649  con->Commit();
1650  }
1651  catch (TSQLException &e)
1652  {
1653  cout << "PgPostBankBackupManager::CleanTable - Error - "
1654  << " Exception caught during connection->commit()"
1655  << endl;
1656  cout << e.GetMessage() << endl;
1657  // delete pstmt;
1658  exit(1);
1659  }
1660  timer_db.get()->stop();
1661 
1662  if (res == 0)
1663  {
1664  cout << "PgPostBankBackupManager::CleanTable - Error - "
1665  << "DATABASE: delete " << bankName << " failed. "
1666  << "Make sure you commit to the master database " << endl;
1667  exit(1);
1668  }
1669  assert(res == 1);
1670 
1671  if (do_log)
1672  {
1673  flog << "Deleted Entry - " << bankName << " RId " << rid
1674  << endl;
1675  }
1676 
1677  timer_log.get()->restart();
1678  bklog.Log(rid,
1680  timer_log.get()->stop();
1681  }
1682 
1683  cnt++;
1684  timer_loop.get()->stop();
1685  }
1686 
1687  timer_loop.get()->print_stat();
1688  timer_db.get()->print_stat();
1689  timer_log.get()->print_stat();
1690  } // do the deleting
1691 
1692  return rid_to_drop.size();
1693 }