StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbManagerImpl.cc
1 /***************************************************************************
2  *
3  * $Id: StDbManagerImpl.cc,v 1.53 2021/02/04 16:59:45 dmitry Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Manages access to Servers and passes Query-by-Table to db
9  *
10  ***************************************************************************
11  *
12  * $Log: StDbManagerImpl.cc,v $
13  * Revision 1.53 2021/02/04 16:59:45 dmitry
14  * oops, typo
15  *
16  * Revision 1.52 2021/02/04 16:59:03 dmitry
17  * adding new db domain: FCS
18  *
19  * Revision 1.51 2019/01/08 19:12:54 dmitry
20  * new subsystem: ETOF
21  *
22  * Revision 1.50 2018/11/30 20:04:01 dmitry
23  * new detector added - RHICf
24  *
25  * Revision 1.49 2017/02/24 18:52:25 dmitry
26  * new detector added - EPD
27  *
28  * Revision 1.48 2016/05/24 20:26:48 dmitry
29  * coverity - unreachable delete loop suppression
30  *
31  * Revision 1.47 2015/04/13 19:43:43 dmitry
32  * added new db domain: SST
33  *
34  * Revision 1.46 2014/10/31 16:11:59 dmitry
35  * adding FPS to the database domain list
36  *
37  * Revision 1.45 2013/09/11 17:40:06 dmitry
38  * new database support: IST
39  *
40  * Revision 1.44 2013/06/10 17:45:15 dmitry
41  * unknown db fix - to be backported into old libraries
42  *
43  * Revision 1.43 2013/05/28 18:07:43 dmitry
44  * new db domain: MTD
45  *
46  * Revision 1.42 2013/04/01 14:42:51 dmitry
47  * added new domain - PXL
48  *
49  * Revision 1.41 2012/06/11 14:33:47 fisyak
50  * std namespace
51  *
52  * Revision 1.40 2012/04/09 14:32:26 dmitry
53  * AFS-related patch commented out
54  *
55  * Revision 1.39 2012/04/08 20:48:09 dmitry
56  * added alternate hardcoded location for dbLoadBalancerLocalConfig_BNL.xml
57  *
58  * Revision 1.38 2011/06/16 14:44:00 dmitry
59  * added new domain - FGT
60  *
61  * Revision 1.37 2011/04/04 15:44:24 dmitry
62  * fix to blacklist Calibrations_bla only
63  *
64  * Revision 1.36 2011/02/10 17:30:42 dmitry
65  * added an option to blacklist domains
66  *
67  * Revision 1.35 2009/12/04 16:06:52 dmitry
68  * StDbLib in standalone mode cannot use SafeDelete - proper wrapper added
69  *
70  * Revision 1.34 2009/11/10 20:24:45 fisyak
71  * Use SafeDelete
72  *
73  * Revision 1.33 2009/10/12 15:06:11 dmitry
74  * added new domain: pp2pp
75  *
76  * Revision 1.32 2009/09/11 13:11:49 dmitry
77  * added FMS to domain list
78  *
79  * Revision 1.31 2009/09/10 18:06:07 dmitry
80  * struct alignment fix, does not rely on fixed 4 byte cap anymore - runtime align calculation is now in use
81  *
82  * Revision 1.30 2008/04/08 02:08:34 fine
83  * restore the previouse version
84  *
85  * Revision 1.28 2007/08/24 21:02:11 deph
86  * *** empty log message ***
87  *
88  * Revision 1.27 2007/08/20 18:21:29 deph
89  * New Version of Load Balancer
90  *
91  * Revision 1.26 2007/05/16 22:48:10 deph
92  * Replaced cerr with LOG_ERROR <<endm; for logger
93  *
94  * Revision 1.25 2007/03/08 22:01:44 deph
95  * minor change (removed a break) to allow load balancer to be backward compatible with online migration code
96  *
97  * Revision 1.24 2007/01/09 16:36:57 deph
98  * differnt config file name for seamless autobuild
99  *
100  * Revision 1.23 2007/01/09 16:27:40 deph
101  * Updates for load balancing "added 1)write privilege 2)xml comments 3)camelCase notation
102  *
103  * Revision 1.22 2006/11/16 21:50:40 deph
104  * additional files needed for db load balancing
105  *
106  * Revision 1.21 2006/08/17 02:58:57 deph
107  * updated load balancer - removing hard-coded nodes from API to xml
108  *
109  * Revision 1.20 2005/01/25 22:05:48 deph
110  * Reset precedence for connecting to db. From Low to High: star,home,envVar
111  *
112  * Revision 1.19 2004/08/11 02:09:35 deph
113  * added zdc domain
114  *
115  * Revision 1.18 2004/02/02 00:41:24 deph
116  * Added bbc and tracker domains
117  *
118  * Revision 1.17 2004/01/15 00:02:25 fisyak
119  * Replace ostringstream => StString, add option for alpha
120  *
121  * Revision 1.16 2003/09/16 22:44:17 porter
122  * got rid of all ostrstream objects; replaced with StString+string.
123  * modified rules.make and added file stdb_streams.h for standalone compilation
124  *
125  * Revision 1.15 2003/09/02 17:57:49 perev
126  * gcc 3.2 updates + WarnOff
127  *
128  * Revision 1.14 2002/11/24 01:39:16 porter
129  * added Pmd domain
130  *
131  * Revision 1.13 2002/11/14 17:43:00 porter
132  * added dbEemc to enumerated domain list
133  *
134  * Revision 1.12 2002/10/29 21:40:39 porter
135  * Added dbScalers as a dbType; dbScaler is a dbDomain from before but now
136  * use as a dbType is more in line with how it will be handled
137  *
138  * Revision 1.11 2002/01/30 15:40:47 porter
139  * changed limits on flavor tag & made defaults retrieving more readable
140  *
141  * Revision 1.10 2001/12/21 04:54:45 porter
142  * sped up table definition for emc and changed some ostrstream usage for
143  * insure tests
144  *
145  * Revision 1.9 2001/10/24 04:05:20 porter
146  * added long long type to I/O and got rid of obsolete dataIndex table
147  *
148  * Revision 1.8 2001/04/25 17:18:40 perev
149  * HPcorrs
150  *
151  * Revision 1.7 2001/04/18 19:33:37 porter
152  * added dbRts enumeration for run-time systems domain
153  *
154  * Revision 1.6 2001/03/31 15:03:46 porter
155  * fix bug in StDbManagerImpl::getDbName accidently introduced yesterday
156  * & added new diagnostic message in MysqlDb
157  *
158  * Revision 1.5 2001/03/30 18:48:26 porter
159  * modified code to keep Insure from wigging-out on ostrstream functions.
160  * moved some messaging into a StDbSql method.
161  *
162  * Revision 1.4 2001/02/22 23:01:55 porter
163  * Re-introduced many-to-one name-to-table capability
164  * & robustness for query errors
165  *
166  * Revision 1.3 2001/02/09 23:06:24 porter
167  * replaced ostrstream into a buffer with ostrstream creating the
168  * buffer. The former somehow clashed on Solaris with CC5 iostream (current .dev)
169  *
170  * Revision 1.2 2001/02/08 23:23:56 porter
171  * fixed initialization of schemaID in table & fixed some warnings when
172  * compiled with NODEBUG
173  *
174  * Revision 1.1 2001/01/22 18:37:55 porter
175  * Update of code needed in next year running. This update has little
176  * effect on the interface (only 1 method has been changed in the interface).
177  * Code also preserves backwards compatibility so that old versions of
178  * StDbLib can read new table structures.
179  * -Important features:
180  * a. more efficient low-level table structure (see StDbSql.cc)
181  * b. more flexible indexing for new systems (see StDbElememtIndex.cc)
182  * c. environment variable override KEYS for each database
183  * d. StMessage support & clock-time logging diagnostics
184  * -Cosmetic features
185  * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access
186  * f. removed codes that have been obsolete for awhile (e.g. db factories)
187  * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI
188  * and mysqlAccessor became StDbSql)
189  *
190  * Revision 1.26 2000/08/15 22:51:51 porter
191  * Added Root2DB class from Masashi Kaneta
192  * + made code more robust against requesting data from non-existent databases
193  *
194  * Revision 1.25 2000/06/30 01:57:02 porter
195  * fixed a delete bug & small memory leak found by Akio via Insure++ ,
196  * updated SetTable() method for containing idList, corrected enumeration
197  * map to rhic domain for Conditions_rhic database
198  *
199  * Revision 1.24 2000/06/02 13:37:36 porter
200  * built up list of minor changes:
201  * - made buffer more robust for certain null inputs
202  * - fixed small leak in StDbTables & restructure call to createMemory
203  * - added dbRhic as a database domain in StDbDefs
204  * - added setUser() in StDbManager
205  * - added more diagnostic printouts in mysqlAccessor.cc
206  *
207  * Revision 1.23 2000/05/04 15:13:11 porter
208  * added dbOnl, dbRich, dbMwc domains as standards
209  *
210  * Revision 1.22 2000/04/25 18:26:03 porter
211  * added flavor & production time as settable query fields in
212  * table &/or node. Associated SQL updated in mysqlAccessor.
213  * Flavor key supports "+" as an OR symbol.
214  *
215  * Revision 1.21 2000/03/28 17:03:18 porter
216  * Several upgrades:
217  * 1. configuration by timestamp for Conditions
218  * 2. query by whereClause made more systematic
219  * 3. conflict between db-stored comments & number lists resolved
220  * 4. ensure endtime is correct for certain query falures
221  * 5. dbstl.h->handles ObjectSpace & RogueWave difference (Online vs Offline)
222  *
223  * Revision 1.20 2000/03/01 20:56:16 porter
224  * 3 items:
225  * 1. activated reConnect for server timeouts
226  * 2. activated connection sharing; better resource utilization but poorer
227  * logging
228  * 3. made rollback method in mysqlAccessor more robust (affects writes only)
229  *
230  * Revision 1.19 2000/02/24 20:30:45 porter
231  * fixed padding for uchar; beginTime in mysqlAccessor;
232  * added rollback safety checkes in StDbManger
233  *
234  * Revision 1.18 2000/02/18 16:58:09 porter
235  * optimization of table-query, + whereClause gets timeStamp if indexed
236  * + fix to write multiple rows algorithm
237  *
238  * Revision 1.17 2000/02/15 20:27:44 porter
239  * Some updates to writing to the database(s) via an ensemble (should
240  * not affect read methods & haven't in my tests.
241  * - closeAllConnections(node) & closeConnection(table) method to mgr.
242  * - 'NullEntry' version to write, with setStoreMode in table;
243  * - updated both StDbTable's & StDbTableDescriptor's copy-constructor
244  *
245  * Revision 1.16 2000/01/27 20:27:17 porter
246  * fixed error logic for table, config, or table-list not-found
247  *
248  * Revision 1.15 2000/01/27 05:54:34 porter
249  * Updated for compiling on CC5 + HPUX-aCC + KCC (when flags are reset)
250  * Fixed reConnect()+transaction model mismatch
251  * added some in-code comments
252  *
253  * Revision 1.14 2000/01/19 20:20:05 porter
254  * - finished transaction model needed by online
255  * - fixed CC5 compile problem in StDbNodeInfo.cc
256  * - replace TableIter class by StDbTableIter to prevent name problems
257  *
258  * Revision 1.13 2000/01/14 14:50:52 porter
259  * expanded use of verbose mode & fixed inconsistency in
260  * StDbNodeInfo::getElementID
261  *
262  * Revision 1.12 2000/01/10 20:37:54 porter
263  * expanded functionality based on planned additions or feedback from Online work.
264  * update includes:
265  * 1. basis for real transaction model with roll-back
266  * 2. limited SQL access via the manager for run-log & tagDb
267  * 3. balance obtained between enumerated & string access to databases
268  * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose
269  * 5. restructured Node model for better XML support
270  *
271  * Revision 1.11 1999/12/28 21:31:42 porter
272  * added 'using std::vector' and 'using std::list' for Solaris CC5 compilation.
273  * Also fixed some warnings arising from the CC5 compiles
274  *
275  * Revision 1.10 1999/12/03 22:24:01 porter
276  * expanded functionality used by online, fixed bug in
277  * mysqlAccessor::getElementID(char*), & update StDbDataSet to
278  * conform to changes in Xml reader & writer
279  *
280  * Revision 1.9 1999/10/19 14:30:39 porter
281  * modifications relevant to use with StDbBroker and future merging with
282  * "params" database structure + some docs + suppressing diagnostics messages
283  *
284  * Revision 1.8 1999/09/30 02:06:07 porter
285  * add StDbTime to better handle timestamps, modify SQL content (mysqlAccessor)
286  * allow multiple rows (StDbTable), & Added the comment sections at top of
287  * each header and src file
288  *
289  **************************************************************************/
290 #include "StDbManagerImpl.hh"
291 #include "StDbDefaults.hh"
292 #include "StDbTableFactory.hh"
293 #include "StDbConfigNodeImpl.hh"
294 #include "StDbServerImpl.hh"
295 #include "StDbTable.h"
296 #include "StDbTime.h"
297 #include "StDbTableIter.hh"
298 #include "dbCollection.h"
299 #include "StDbMessenger.hh"
300 
301 #include <cstdlib>
302 
303 #ifndef __STDB_STANDALONE__
304 #include "StMessMgr.h"
305 #else
306 #define LOG_DEBUG cout
307 #define LOG_INFO cout
308 #define LOG_WARN cout
309 #define LOG_ERROR cerr
310 #define LOG_FATAL cerr
311 #define LOG_QA cout
312 #define endm "\n"
313 #endif
314 
315 #include "stdb_streams.h"
316 //#include "StDbDefs.hh"
317 
318 
319 #ifndef NoXmlTreeReader
320 //#include <libxml/nanohttp.h>
321 //#include <sys/stat.h>
322 //#include <sys/types.h>
323 //#include <fstream>
324 #endif
325 
326 #include <string.h>
327 #include <algorithm>
328 
329 #ifdef HPUX
330 #define freeze(i) str()
331 #endif
332 
333 //#include <stdio.h>
334 using namespace std;
335 extern char** environ;
336 
337 #define __CLASS__ "StDbManagerImpl"
338 
339 StDbManagerImpl::StDbManagerImpl(): StDbManager(), dbTypeFree(dbTUser1), dbDomainFree(dbDUser1), mhasServerList(false), mhasDefaultServer(false) {
340 
341  initTypes();
342  initDomains();
343  mfactory = new StDbTableFactory();
344 #ifndef NoXmlTreeReader
345  myServiceBroker = 0;
346 #endif
347 };
348 
350 
351 StDbManagerImpl::~StDbManagerImpl(){
352 
353  deleteServers();
354  deleteDomains();
355  deleteTypes();
356 #ifndef __STDB_STANDALONE__
357  SafeDelete(mfactory);
358  SafeDelete(Messenger);
359 #else
360  if (mfactory) {
361  delete mfactory;
362  mfactory = 0;
363  };
364  if (Messenger) {
365  delete Messenger;
366  Messenger = 0;
367  };
368 
369 #endif
370  mInstance=0;
371 #ifndef NoXmlTreeReader
372 
373 #ifndef __STDB_STANDALONE__
374  SafeDelete(myServiceBroker);
375 #else
376  if (myServiceBroker) {
377  delete myServiceBroker;
378  myServiceBroker = 0;
379  }
380 #endif
381 
382 #endif
383 }
384 
386 //
387 //
388 // protected methods
389 //
390 //
392 
393 void
394 StDbManagerImpl::initTypes(){
395 
396 addDbType(dbStDb,"StarDb");
397 addDbType(dbRunLog,"RunLog");
398 addDbType(dbServer," ");
399 addDbType(dbConditions,"Conditions");
400 addDbType(dbCalibrations,"Calibrations");
401 addDbType(dbGeometry,"Geometry");
402 addDbType(dbRunCatalog,"RunCatalog");
403 addDbType(dbConfigurations,"Configurations");
404 addDbType(dbRunParams,"RunParams");
405 addDbType(dbScalers,"Scalers");
406 addDbType(dbTestScheme,"TestScheme");
407 
408 }
409 
411 
412 void
413 StDbManagerImpl::initDomains(){
414 
415 addDbDomain(dbStar,"Star");
416 addDbDomain(dbTpc,"tpc");
417 addDbDomain(dbFtpc,"ftpc");
418 addDbDomain(dbEmc,"emc");
419 addDbDomain(dbSvt,"svt");
420 addDbDomain(dbCtb,"ctb");
421 addDbDomain(dbTrg,"trg");
422 addDbDomain(dbDaq,"daq");
423 addDbDomain(dbScaler,"scaler");
424 addDbDomain(dbGlobal,"global");
425 addDbDomain(dbL3,"l3");
426 addDbDomain(dbOnl,"onl");
427 addDbDomain(dbRich,"rich");
428 addDbDomain(dbMwc,"mwc");
429 addDbDomain(dbRhic,"rhic");
430 addDbDomain(dbSsd,"ssd");
431 addDbDomain(dbRts,"rts");
432 addDbDomain(dbTof,"tof");
433 addDbDomain(dbFpd,"fpd");
434 addDbDomain(dbEemc,"eemc");
435 addDbDomain(dbPmd,"pmd");
436 addDbDomain(dbBbc,"bbc");
437 addDbDomain(dbTracker,"tracker");
438 addDbDomain(dbZdc,"zdc");
439 addDbDomain(dbFms,"fms");
440 addDbDomain(dbpp2pp,"pp2pp");
441 addDbDomain(dbFgt,"fgt");
442 addDbDomain(dbPxl,"pxl");
443 addDbDomain(dbMtd,"mtd");
444 addDbDomain(dbIst,"ist");
445 addDbDomain(dbFps,"fps");
446 addDbDomain(dbSst,"sst");
447 addDbDomain(dbEpd,"epd");
448 addDbDomain(dbRhicf,"rhicf");
449 addDbDomain(dbEtof,"etof");
450 addDbDomain(dbFcs,"fcs");
451 addDbDomain(dbFst,"fst");
452 addDbDomain(dbFtt,"ftt");
453 }
454 
456 
457 void
458 StDbManagerImpl::addDbType(StDbType type, const char* typeName){
459  mTypes.push_back(new dbType(type,typeName));
460 }
461 
463 
464 void
465 StDbManagerImpl::addDbDomain(StDbDomain domain, const char* domainName){
466  mDomains.push_back(new dbDomain(domain,domainName));
467 }
468 
470 
471 StDbType
472 StDbManagerImpl::newDbType(const char* typeName){
473 
474  StDbType retVal=dbTypeFree;
475  switch(dbTypeFree) {
476  case dbTUser1:
477  {
478  addDbType(dbTUser1,typeName);
479  dbTypeFree = dbTUser2;
480  break;
481  }
482  case dbTUser2:
483  {
484  addDbType(dbTUser2,typeName);
485  dbTypeFree = dbTUser3;
486  break;
487  }
488  case dbTUser3:
489  {
490  addDbType(dbTUser3,typeName);
491  dbTypeFree = dbTEnd;
492  break;
493  }
494  default:
495  {
496  break;
497  }
498  }
499 
500  return retVal;
501 }
502 
504 
505 StDbDomain
506 StDbManagerImpl::newDbDomain(const char* domainName){
507 
508  StDbDomain retVal=dbDomainFree;
509  switch(dbDomainFree) {
510  case dbDUser1:
511  {
512  addDbDomain(dbDUser1,domainName);
513  dbDomainFree = dbDUser2;
514  break;
515  }
516  case dbDUser2:
517  {
518  addDbDomain(dbDUser2,domainName);
519  dbDomainFree = dbDUser3;
520  break;
521  }
522  case dbDUser3:
523  {
524  addDbDomain(dbDUser3,domainName);
525  dbDomainFree = dbDEnd;
526  break;
527  }
528  default:
529  {
530  break;
531  }
532  }
533 
534  return retVal;
535 }
536 
538 
539 void
540 StDbManagerImpl::deleteServers() {
541 
542  if ( !mhasServerList ) return;
543 
544  for( auto &it : mservers ) delete it;
545  mservers.clear();
546 
547 }
548 
550 
551 void
552 StDbManagerImpl::deleteDomains(){
553 
554  for( auto &it : mDomains ) delete it;
555  mDomains.clear();
556 
557 }
558 
560 
561 void
562 StDbManagerImpl::deleteTypes(){
563 
564  for( auto &it : mTypes ) delete it;
565  mTypes.clear();
566 
567 }
568 
569 
572 #define __METHOD__ "lookUpServer()"
573 
574  /* MLK: I considered changing the mode numbering and concluded that its semantics is
575  different. It numbers locations of a particular config file only.
576  We add a different file.
577  */
578 
579 #ifndef NoXmlTreeReader
580 
581  string dbLoadBalancerLocalConfig = "dbLoadBalancerLocalConfig.xml";
582  string dbLoadBalancerGlobalConfig = "dbLoadBalancerGlobalConfig.xml";
583 
584  vector<string> configFileNames;
585  const char* fLocalConfig = getenv("DB_SERVER_LOCAL_CONFIG");
586 
587 
588  if (!fLocalConfig)
589  {
590  LOG_ERROR << "StDbManagerImpl::lookUpServers(): DB_SERVER_LOCAL_CONFIG is undefined! "<<endm;
591  }
592  else
593  {
594  configFileNames.push_back(fLocalConfig);
595  //configFileNames.push_back("/star/data07/dbbackup/dbLoadBalancerLocalConfig_BNL.xml"); // alternate local config, used during AFS outage
596  }
597 
598 
599 
600 /******Removing option to allow LB in $HOME***********************
601  const char* HOME = getenv("HOME");
602 
603  if (!HOME)
604  {
605  LOG_ERROR << "StDbManagerImpl::lookUpServers(): HOME is undefined! "<<endm;
606  }
607  else
608  {
609  configFileNames.push_back((string)HOME+"/"+dbLoadBalancerLocalConfig);
610  }
611 
612 ******************************/
613 
614  const char* STAR = getenv("STAR");
615 
616  if (!STAR)
617  {
618  LOG_ERROR << "StDbManagerImpl::lookUpServers(): STAR is undefined! "<<endm;
619  }
620 // else
621 // {
622 // configFileNames.push_back((string)STAR+"/StDb/servers/"+dbLoadBalancerLocalConfig);
623 // }
624 
625  const char* fGlobalConfig = getenv("DB_SERVER_GLOBAL_CONFIG");
626 
627  if (!fGlobalConfig)
628  {
629  //LOG_ERROR << "StDbManagerImpl::lookUpServers(): DB_SERVER_GLOBAL_CONFIG is undefined! "<<endm;
630  }
631  else
632  {
633  configFileNames.push_back(fGlobalConfig);
634  }
635 
636  if (STAR)
637  {
638  configFileNames.push_back((string)STAR+"/StDb/servers/"+dbLoadBalancerGlobalConfig);
639  }
640 
641 
642  // try opening the files until the first one that opens is found
643 
644  string dbLoadBalancerConfig = "";
645 
646  vector<string>::iterator I= configFileNames.begin();
647  while (I!=configFileNames.end())
648  {
649  fstream dbLbIn;
650  dbLbIn.open((*I).c_str(),ios::in);
651  if(dbLbIn.is_open())
652  {
653  dbLoadBalancerConfig = (*I);
654  dbLbIn.close();
655  break;
656  }
657  else
658  {
659  LOG_ERROR << "StDbManagerImpl::lookUpServers(): could not open "<<(*I)<<endm;
660  }
661  ++I;
662  }
663 
664  LOG_INFO << "StDbManagerImpl::lookUpServers(): config file name is "<<dbLoadBalancerConfig<<endm;
665 
667 
668  myServiceBroker = new StDbServiceBroker(dbLoadBalancerConfig);
669  short SBStatus = myServiceBroker->GetStatus();
670  if (SBStatus == st_db_service_broker::NO_ERROR)
671  {
672  mhasServerList = true;
673  }
674  else
675  {
676  delete myServiceBroker;
677  myServiceBroker = 0;
678  LOG_ERROR << "StDbManagerImpl::lookUpServers() StDbServiceBroker error "<<SBStatus<<" disable XML loadbalancing "<<endm;
679  }
680 
682 
683 #endif
684 
685  char* xmlFile[3]={NULL,NULL,NULL};
686  // dbFindServerMode mode[3]={userHome,serverEnvVar,starDefault};
687  dbFindServerMode mode[3]={serverEnvVar,userHome,starDefault};
688 
689  StString costr;
690  costr<<stendl<<"******** Order of Files searched for dbServers ********* "<<stendl;
691 
692  for(int i=0;i<3; i++){
693  xmlFile[i]=StDbDefaults::Instance()->getServerFileName(mode[i]);
694  if(xmlFile[i]){
695  ifstream is(xmlFile[i]);
696  if(is){
697 
698  cout << " looking at "<<i << " " << xmlFile[i]<< endl;
699  costr<<" "<<i+1<<". "<< xmlFile[i] <<stendl;
700  findServersXml(is);
701  is.close();
702 
703  xmlInputSource = mode[i];
704  // break; // THIS REMOVED TO SUPPORT MANY SERVERS USED IN ONLINE MIGRATION MACROS
705  } else {
706  cout << "There is no "<< i << " " << xmlFile[i] << endl;
707  }
708  delete [] xmlFile[i];
709  xmlFile[i]=NULL;
710  }
711  }
712  costr <<"********************************************************" << stendl;
713  printInfo((costr.str()).c_str(),dbMConnect,__LINE__,__CLASS__,__METHOD__);
714 
715  // cout <<"****************HERE B ****************"<<endl;
716 mhasServerList = true;
717 
718 #undef __METHOD__
719 }
720 
722 void StDbManagerImpl::findServersXml(ifstream& is){
723 
724  char* stardatabase=NULL;
725 
726  /*
727 MLK: changes to undo load-balancing private confuguration.
728 Only basic (as before May 2006) private XML configuration is
729 supported. Central load-balancing configuration file is being introduced with this version.
730  */
731 
732  while(!is.eof()){
733 
734  stardatabase = findServerString(is);
735 
736  if(!stardatabase) continue;
737 
738  // local DTD ...
739  char bserver[32]="<server>"; char eserver[32]="</server>";
740  char bhost[32]="<host>"; char ehost[32]="</host>";
741  char bsock[32]="<socket>"; char esock[32]="</socket>";
742  char bport[32]="<port>"; char eport[32]="</port>";
743  char bdb[32]="<databases>"; char edb[32]="</databases>";
744  // int portNum = 3306;
745  int portNum = 3316;
746 
747  char* servName = mparser.getString(stardatabase,(char*)bserver,(char*)eserver);
748  char* hostName = mparser.getString(stardatabase,(char*)bhost,(char*)ehost);
749  char* uSocket = mparser.getString(stardatabase,(char*)bsock,(char*)esock);
750  char* portNumber = mparser.getString(stardatabase,(char*)bport,(char*)eport);
751 
752  if(portNumber)portNum = atoi(portNumber);
753 #ifndef NoXmlTreeReader
754  StDbServerImpl* myserver = new StDbServerImpl(servName,hostName,uSocket,portNum);
755  myserver->PointMysqlDb(this);
756  StDbServer* server = myserver;
757 #else
758  StDbServer* server = new StDbServerImpl(servName,hostName,uSocket,portNum);
759 #endif
760 
761  if(muserName) server->setUser(muserName,mpWord);
762  delete [] servName;
763  delete [] hostName;
764  delete [] uSocket;
765  if(portNumber)delete [] portNumber;
766  mservers.push_back(server);
767  server->setTimeLogging(misTimeLogged);
768 
769  char* dbNames = mparser.getString(stardatabase,(char*)bdb,(char*)edb);
770 
771 
772  if( !dbNames && !mhasDefaultServer ){
773 
774 
775  server->setIsDefaultServer();
776  mhasDefaultServer = true;
777 
778  } else {
779 
780  // char* p1 = &dbNames[0];
781  char* p1 = dbNames;
782  char* aname;
783  StDbType type; StDbDomain domain;
784  while(p1 && (aname=getNextName(p1))){
785  if(getDataBaseInfo(aname,type,domain))server->addDataBase(type,domain);
786  delete [] aname;
787  }
788  if(dbNames)delete [] dbNames;
789  }
790  if(stardatabase){
791  delete [] stardatabase;
792  stardatabase=0;
793  }
794  }
795 }
796 
798 char* StDbManagerImpl::findServerString(ifstream& is){
799 
800 char tmpline[256];
801 bool done = false;
802 bool started = false;
803 char* id;
804 
805  char* line=NULL;
806  StString os;
807 
808 while(!done){
809 
810  if(is.eof()){
811  done = true;
812  return line;
813  } else {
814  is.getline(tmpline,255);
815 
816  if((id=strstr(tmpline,"//")))continue;
817 
818  if(!started){
819  id= strstr(tmpline,"<StDbServer>");
820  if(id){
821  os<<tmpline;
822  started = true;
823  }
824  } else {
825  os<<tmpline;
826  id=strstr(tmpline,"</StDbServer>");
827  if(id)done=true;
828 
829  } // started check
830  } // eof check
831  } // while loop
832 
833  string osstr=os.str();
834  line= new char[osstr.length()+1];
835  strcpy(line,osstr.c_str());
836 
837 return line;
838 }
839 
841 char* StDbManagerImpl::getNextName(char*& names){
842 
843 char* nextName = 0;
844 if(!names)return nextName;
845 
846 char* id = strstr(names,",");
847 
848 if(!id) {
849  nextName = mparser.removeBlankEnds(names);
850  names = 0;
851 } else {
852  int iloc = id-names;
853  char* saveName = new char[iloc+1];
854  strncpy(saveName,names,iloc);
855  saveName[iloc]='\0';
856  nextName=mparser.removeBlankEnds(saveName);
857  delete [] saveName;
858  names = id; names++;
859 }
860 
861 return nextName;
862 }
864 //
865 // public methods
866 //
868 void StDbManagerImpl::turnOffTimeLogging(){
869  StDbManager::turnOffTimeLogging();
870  if(!mhasServerList)lookUpServers();
871  for(ServerList::iterator itr = mservers.begin();
872  itr != mservers.end(); ++itr)
873  (*itr)->setTimeLogging(misTimeLogged);
874 }
876 
877 StDataBaseI* StDbManagerImpl::findDb(StDbType type, StDbDomain domain){
878  return findServer(type,domain)->useDb();
879 }
880 
881 StDataBaseI* StDbManagerImpl::findDb(const char* dbType, const char* dbDomain){
882  return findServer(dbType,dbDomain)->useDb();
883 }
884 
885 StDataBaseI* StDbManagerImpl::findDb(const char* databaseName){
886  return findServer(databaseName)->useDb();
887 }
888 
889 StDbServer* StDbManagerImpl::findServer(StDbType type, StDbDomain domain){
890 #define __METHOD__
891 
892  if(!mhasServerList)lookUpServers();
893  StDbServer* server = 0;
894 
895  // first check if it exists in list
896  for(ServerList::iterator itr = mservers.begin();
897  itr != mservers.end(); ++itr){
898  if((*itr)->useDb(type,domain)){
899  server = *itr;
900  break;
901  }
902  }
903 
904  // if not build from default server
905  if(!server){
906  server=findDefaultServer();
907  server->addDataBase(type,domain);
908  }
909 return server;
910 #undef __METHOD__
911 }
912 
914 
915 StDbServer* StDbManagerImpl::findServer(StDbNode* node){
916 return findServer(node->getDbType(),node->getDbDomain());
917 }
918 
920 
921 StDbServer* StDbManagerImpl::findServer(const char* typeName, const char* domainName){
922 return findServer(getDbType(typeName),getDbDomain(domainName));
923 }
924 
926 
927 StDbServer* StDbManagerImpl::findServer(const char* databaseName){
928 
929  // if databaseName contains "_" then = 'dbTypeName_dbDomainName'
930  // else = 'dbTypeName' and dbDomainName="Star"
931 
932 char* typeName = new char[strlen(databaseName)+1];
933 strcpy(typeName,databaseName);
934 char* domainName;
935 
936 char* id=strstr(typeName,"_");
937 if(id){
938  *id='\0';
939  id++;
940  domainName=new char[strlen(id)+1];
941  strcpy(domainName,id);
942 } else {
943  domainName=new char[strlen("Star")+1];
944  strcpy(domainName,"Star");
945 }
946 
947 StDbServer* server=findServer(typeName,(const char*) domainName);
948 delete [] domainName;
949 delete [] typeName;
950 
951 return server;
952 }
953 
954 
956 
957 StDbServer* StDbManagerImpl::findDefaultServer(){
958 
959 
960  if(!mhasServerList)
961  {
962  lookUpServers();
963  }
964 
965  StDbServer* server = 0;
966 
967 
968 
969 for(ServerList::iterator itr = mservers.begin(); itr != mservers.end(); ++itr){
970  if((*itr)->isDefault()){
971  server = *itr;
972  break;
973  }
974  }
975 
976 return server;
977 }
978 
980 
981 char* StDbManagerImpl::getDbTypeName(StDbType type){
982 #define __METHOD__ "getDbTypeName(StDbType)"
983 
984 char* name=0;
985  for(dbTypes::iterator itr=mTypes.begin();
986  itr != mTypes.end(); ++itr){
987  if((*itr)->type == type){
988  name = (*itr)->name;
989  break;
990  }
991  }
992 
993  if(name) return mstringDup(name);
994  printInfo(" No dbType name found ",dbMErr,__LINE__,__CLASS__,__METHOD__);
995  return name;
996 #undef __METHOD__
997 }
998 
999 
1001 
1002 char* StDbManagerImpl::getDbDomainName(StDbDomain domain){
1003 #define __METHOD__ "getDbDomainName(StDbDomain)"
1004 
1005 if(domain==dbDomainUnknown)return mstringDup("Star");
1006 
1007 char* name=0;
1008  for(dbDomains::iterator itr=mDomains.begin();
1009  itr != mDomains.end(); ++itr){
1010  if((*itr)->domain == domain){
1011  name = (*itr)->name;
1012  break;
1013  }
1014  }
1015 
1016  if(name) return mstringDup(name);
1017  printInfo(" Domain Name not found",dbMErr,__LINE__,__CLASS__,__METHOD__);
1018  return name;
1019 #undef __METHOD__
1020 }
1021 
1022 
1024 
1025 StDbType StDbManagerImpl::getDbType(const char* typeName){
1026 #define __METHOD__ "getDbType(typeName)"
1027  StDbType retType=dbStDb;
1028  bool found=false;
1029  for(dbTypes::iterator itr=mTypes.begin();
1030  itr != mTypes.end(); ++itr){
1031  if(strcmp((*itr)->name,typeName)==0){
1032  retType = (*itr)->type;
1033  found = true;
1034  break;
1035  }
1036  }
1037 
1038  if(found)return retType;
1039  printInfo("Adding New User dbType=",typeName,dbMDebug,__LINE__,__CLASS__,__METHOD__);
1040 
1041  return newDbType(typeName); //This'll overwrite User db definition
1042 #undef __METHOD__
1043 }
1044 
1045 
1047 
1048 StDbDomain StDbManagerImpl::getDbDomain(const char* domainName){
1049 #define __METHOD__ "getDbDomain(domainName)"
1050  StDbDomain retType=dbDomainUnknown;
1051 
1052  bool found=false;
1053  for(dbDomains::iterator itr=mDomains.begin();
1054  itr != mDomains.end(); ++itr){
1055  if(strcmp((*itr)->name,domainName) ==0){
1056  retType = (*itr)->domain;
1057  found=true;
1058  break;
1059  }
1060  }
1061 
1062  if(found)return retType;
1063  printInfo("Adding New dbDomain=",domainName,dbMWarn,__LINE__,__CLASS__,__METHOD__);
1064 return newDbDomain(domainName);
1065 #undef __METHOD__
1066 }
1067 
1068 void StDbManagerImpl::blacklistDbDomain(const char* domainName) {
1069  std::string domain(domainName);
1070  std::transform(domain.begin(), domain.end(), domain.begin(), ::tolower);
1071  if (mBlacklist.find(domain) == mBlacklist.end()) {
1072  mBlacklist.insert(domain);
1073  }
1074 }
1075 
1077 char* StDbManagerImpl::printDbName(StDbType type, StDbDomain domain){
1078  StDataBaseI* db=findServer(type,domain)->useDb();
1079  if(db) return db->printDbName();
1080  return (char*)"Unknown DataBase";
1081 }
1082 
1084 StDbConfigNode* StDbManagerImpl::initConfig(const char* dbName){
1085  StDbType type; StDbDomain domain;
1086  getDataBaseInfo(dbName,type,domain);
1087  return initConfig(type,domain);
1088 }
1089 
1091 StDbConfigNode* StDbManagerImpl::initConfig(const char* dbName, const char* configName, int opt){
1092 #define __METHOD__ "initConfig(dbName,configName,opt)"
1093  StDbType type; StDbDomain domain;
1094  getDataBaseInfo(dbName,type,domain);
1095 return initConfig(type,domain,configName,opt);
1096 #undef __METHOD__
1097 }
1098 
1100 StDbConfigNode* StDbManagerImpl::initConfig(StDbType type, StDbDomain domain){
1101  // create Config node with appropriate name for db type & domain
1102  char* name = 0;
1103  name = getConfigNodeName(type,domain);
1104  StDbConfigNode* configNode = new StDbConfigNodeImpl(type,domain,name);
1105  configNode->setIsDbNode(true);
1106  delete [] name;
1107  return configNode;
1108 }
1109 
1111 StDbConfigNode* StDbManagerImpl::initConfig(StDbType type, StDbDomain domain, const char* configName, int opt){
1112 
1113  if(misTimeLogged)mnodeLog.start();
1114  StDbConfigNode* configNode=initConfig(type,domain);
1115  configNode->setVersion(configName);
1116  configNode->buildTree(opt);
1117  if(misTimeLogged)mnodeLog.end();
1118 
1119  return configNode;
1120 }
1121 
1123 StDbConfigNode* StDbManagerImpl::initConfig(StDbType type, StDbDomain domain, unsigned int requestTime, int opt){
1124 #define __METHOD__ "initConfig(type,domain,time)"
1125 
1126  StDbConfigNode* configNode=0;
1127  StDbTable* table = new StDbTable("dbCollection");
1128  StDataBaseI* db = findServer(type,domain)->useDb();
1129  if(!db){
1130  printInfo("DataBase Not Found",dbMErr,__LINE__,__CLASS__,__METHOD__);
1131  return configNode;
1132  }
1133 
1134  if(!db->QueryDb(table,requestTime)){
1135  printInfo("Timestamped collection Not found",dbMErr,__LINE__,__CLASS__,__METHOD__);
1136  return configNode;
1137  }
1138  dbCollection* collection = (dbCollection*)table->GetTable();
1139  configNode=initConfig(type,domain,collection->name,opt);
1140  delete table;
1141  return configNode;
1142 #undef __METHOD__
1143 }
1144 
1146 StDbConfigNode* StDbManagerImpl::initConfig(const char* dbName, unsigned int requestTime, int opt){
1147  StDbType type; StDbDomain domain;
1148  getDataBaseInfo(dbName,type,domain); // fill type & domain strings via dbName
1149 return initConfig(type,domain,requestTime,opt);
1150 }
1151 
1153 char* StDbManagerImpl::getConfigNodeName(StDbType type, StDbDomain domain){
1154  // config node name is based on type IF domain is "dbStar",
1155  // else it is based on the domain
1156  if(domain == dbStar) return getDbTypeName(type);
1157  return getDbDomainName(domain);
1158 }
1159 
1161 char* StDbManagerImpl::getExternalVersion(StDbType type, StDbDomain domain){
1162  if (!type || !domain) return 0;
1163  char* dbname = printDbName(type,domain);
1164  if (!dbname) return 0;
1165  return getenv(dbname);
1166 }
1167 
1169 dbEnvList* StDbManagerImpl::getEnvList(const char* name){
1170 
1171  dbEnvList* retVal=new dbEnvList;
1172 
1173  for(int i=0;;i++){
1174  if(!environ[i])break;
1175  if(strstr(environ[i],name)){
1176  char* tmpName=mstringDup(environ[i]);
1177  char* id=strstr(tmpName,"=");
1178  if(id){
1179  *id='\0';id++;
1180  if(strstr(tmpName,name)){
1181  retVal->envVar[retVal->num]=mstringDup(tmpName);
1182  retVal->envDef[retVal->num]=mstringDup(id);
1183  retVal->num++;
1184  } // check of name in env-var side
1185  id--;*id='=';
1186  } // check of "=" in env-var
1187  delete [] tmpName;
1188  } // check of name in env-var
1189  } // loop over all env-vars
1190 
1191  if(retVal->num==0){
1192  delete retVal;
1193  retVal=0;
1194  }
1195 
1196  return retVal;
1197 }
1198 
1200 StDbTable* StDbManagerImpl::newDbTable(const char* dbName, const char* tabName){
1201  return mfactory->newDbTable(dbName,tabName);
1202 }
1203 
1205 StDbTable* StDbManagerImpl::newDbTable(StDbNode* node){
1206  StDbTable* table=mfactory->newDbTable(node->printDbName(),node->printName());
1207  if(table)table->setNodeInfo(node);
1208  return table;
1209 }
1210 
1212 void StDbManagerImpl::setRequestTime(unsigned int time){
1213  mcheckTime.munixTime = time;
1214  updateDateTime(mcheckTime);
1215 }
1216 
1218 void StDbManagerImpl::setRequestTime(const char* time){
1219  mcheckTime.setDateTime(time);
1220  updateUnixTime(mcheckTime);
1221 }
1222 
1224 void StDbManagerImpl::setStoreTime(unsigned int time){
1225  mstoreTime.munixTime = time;
1226  updateDateTime(mstoreTime);
1227 }
1228 
1230 void StDbManagerImpl::setStoreTime(const char* time){
1231  mstoreTime.setDateTime(time);
1232  updateUnixTime(mstoreTime);
1233 }
1234 
1236 void StDbManagerImpl::updateDateTime(StDbTime& t){
1237 #define __METHOD__ "updateDateTime(StDbTime)"
1238  StDbServer* server=findDefaultServer();
1239  StDataBaseI* db=server->useDb();
1240  char* dt;
1241  if(db && (dt=db->getDateTime(t.munixTime))){
1242  t.setDateTime(dt);
1243  delete [] dt;
1244  }
1245 #undef __METHOD__
1246 }
1247 
1249 void StDbManagerImpl::updateUnixTime(StDbTime& t){
1250  StDbServer* server = findDefaultServer();
1251  StDataBaseI* db=server->useDb();
1252  if(db)t.setUnixTime(db->getUnixTime(t.mdateTime));
1253 }
1254 
1256 unsigned int StDbManagerImpl::getUnixCheckTime(){ return mcheckTime.munixTime;}
1257 unsigned int StDbManagerImpl::getUnixRequestTime(){return mcheckTime.munixTime;}
1258 char* StDbManagerImpl::getDateCheckTime() { return mcheckTime.mdateTime; }
1259 char* StDbManagerImpl::getDateRequestTime(){ return mcheckTime.mdateTime; }
1260 
1262 unsigned int StDbManagerImpl::getUnixStoreTime(){ return mstoreTime.munixTime; }
1263 char* StDbManagerImpl::getDateStoreTime(){ return mstoreTime.mdateTime; }
1264 
1266 bool StDbManagerImpl::IsValid(StDbTable* table){
1267  bool retVal = false;
1268  if(table) {
1269  unsigned int time = mcheckTime.munixTime;
1270  if(time >= table->getBeginTime() && time < table->getEndTime())retVal=true;
1271  }
1272  return retVal;
1273 }
1274 
1276 bool StDbManagerImpl::fetchDbTable(StDbTable* table){
1277 
1278  bool retVal = false;
1279  if(!table) return false;
1280  StDataBaseI* db=findDb(table->getDbType(),table->getDbDomain());
1281  if(!db) return false;
1282 
1283  if(misTimeLogged)mdataLog.start();
1284  if(db->QueryDb(table,mcheckTime.munixTime))retVal=true;
1285  if(misTimeLogged)mdataLog.end();
1286 
1287  return retVal;
1288 }
1289 
1290 bool StDbManagerImpl::fetchDbTable(StDbTable* table, char* whereClause){
1291 
1292  if(!table) return false;
1293  StDataBaseI* db=findDb(table->getDbType(),table->getDbDomain());
1294  if(!db) return false;
1295  if(misTimeLogged)mdataLog.start();
1296  bool retVal=(bool) db->QueryDb(table,whereClause);
1297  if(misTimeLogged)mdataLog.end();
1298  return retVal;
1299 }
1300 
1302 bool StDbManagerImpl::fetchAllTables(StDbConfigNode* node){
1303 
1304 bool tables=true;
1305 bool children = true;
1306 bool siblings = true;
1307 
1308  if(node->hasData()){
1309  StDbTableIter* itr = node->getStDbTableIter();
1310  while(!itr->done()) {
1311  StDbTable* tab=itr->next();
1312  tables = (tables && fetchDbTable(tab));
1313  }
1314  delete itr;
1315  }
1316 
1317  if(node->hasChildren())children = fetchAllTables(node->getFirstChildNode());
1318  StDbConfigNode* nextNode;
1319  if((nextNode=node->getNextNode()))siblings = fetchAllTables(nextNode);
1320 
1321 return (tables && children && siblings);
1322 }
1323 
1325 bool StDbManagerImpl::storeDbTable(StDbTable* table, bool commitWhenDone){
1326 #define __METHOD__ "storeDbTable(StDbTable*)"
1327 
1328 if(!table) return (bool) printInfo("Cannot store Null Table",dbMErr,__LINE__,__CLASS__,__METHOD__);
1329 if(mstoreTime.munixTime==0) return (bool) printInfo("Cannot store withou timestamp",dbMErr,__LINE__,__CLASS__,__METHOD__);
1330 
1331  StDataBaseI* db = findDb(table->getDbType(),table->getDbDomain());
1332  if(!db) return false;
1333  bool retVal = (bool)db->WriteDb(table,mstoreTime.munixTime);
1334  if(commitWhenDone)table->commitData();
1335 
1336 return retVal;
1337 #undef __METHOD__
1338 }
1339 
1341 bool StDbManagerImpl::storeAllTables(StDbConfigNode* node, bool commitWhenDone){
1342 #define __METHOD__ "storeAllTable(StDbConfigNode*)"
1343 
1344 if(!node) return (bool) printInfo("Cannot store Null Node",dbMErr,__LINE__,__CLASS__,__METHOD__);
1345 if(mstoreTime.munixTime==0) return (bool) printInfo("Cannot store withno timestamp",dbMErr,__LINE__,__CLASS__,__METHOD__);
1346 
1347  bool tables=true;
1348  if(node->hasData()){
1349  StDbTableIter* itr = node->getStDbTableIter();
1350  while(!itr->done()){
1351  if(tables){
1352  tables = (tables && storeDbTable(itr->next(), false));
1353  } else {
1354  (itr->next())->commitData(); // prevent rollback of non-stored tables
1355  }
1356  }
1357  delete itr;
1358  }
1359 
1360  // end it here if store fails
1361 if(!tables) {
1362  rollBackAllTables(node);
1363  return tables;
1364 }
1365 
1366 // do children & siblings
1367 bool children = true;
1368 bool siblings = true;
1369 
1370  if(node->hasChildren()) children = storeAllTables(node->getFirstChildNode(),false);
1371 
1372  if(!children){
1373  rollBackAllTables(node);
1374  return false;
1375  }
1376 
1377  StDbConfigNode* nextNode;
1378  if((nextNode=node->getNextNode())) siblings = storeAllTables(nextNode,false);
1379 
1380  if(!siblings){
1381  rollBackAllTables(node);
1382  return false;
1383  }
1384  if(commitWhenDone)commitAllTables(node);
1385 return true;
1386 #undef __METHOD__
1387 }
1388 
1390 int
1391 StDbManagerImpl::storeConfig(StDbConfigNode* node, int currentID, int& configID, bool commitWhenDone){
1392 #define __METHOD__ "storeConfig(node,ID,configID,commitFlag)"
1393 
1394  StDataBaseI* db = findDb(node->getDbType(),node->getDbDomain());
1395  if(!db) return 0;
1396  int nodeID, childID, sibID;
1397 
1398 if(!(nodeID=db->WriteDb(node,currentID,configID))) return printInfo(" Write Failed ",dbMErr,__LINE__,__CLASS__,__METHOD__);
1399 
1400  node->addWrittenNode(nodeID);
1401 
1402  if(node->hasChildren())
1403  if(!(childID=storeConfig(node->getFirstChildNode(),nodeID,configID,false))){
1404  rollBack(node);
1405  return printInfo(" Write Failed in Child Node",dbMErr,__LINE__,__CLASS__,__METHOD__);
1406  }
1407 
1408  if(node->getNextNode())
1409  if(!(sibID=storeConfig(node->getNextNode(),currentID,configID,false))) {
1410  rollBackAllNodes(node->getFirstChildNode());
1411  rollBack(node);
1412  return printInfo(" Write Failed in Sibling Node",dbMErr,__LINE__,__CLASS__,__METHOD__);
1413  }
1414 
1415 if(commitWhenDone) commitAllNodes(node);
1416 return nodeID;
1417 #undef __METHOD__
1418 }
1419 
1421 bool
1422 StDbManagerImpl::rollBackAllTables(StDbConfigNode* node){
1423 
1424  bool tables, children, siblings;
1425  tables=children=siblings=true;
1426 
1427  if(node->hasData()){
1428  StDbTableIter* itr = node->getStDbTableIter();
1429  while(!itr->done()){
1430  StDbTable* table=itr->next();
1431  if(table && table->canRollBack())tables = (tables && rollBack(table));
1432  }
1433  delete itr;
1434  }
1435 
1436  if(node->hasChildren())
1437  children = rollBackAllTables(node->getFirstChildNode());
1438  StDbConfigNode* nextNode;
1439  if((nextNode=node->getNextNode()))
1440  siblings = rollBackAllTables(nextNode);
1441 
1442 return (tables && children && siblings);
1443 }
1444 
1446 bool
1448 
1449  bool tables, children, siblings, thisNode;
1450  tables=children=siblings=true;
1451 
1452  if(node->hasData()){
1453  StDbTableIter* itr = node->getStDbTableIter();
1454  while(!itr->done()){
1455  StDbTable* tab = itr->next();
1456  tables=(tables && rollBack((StDbNode*)tab));
1457  }
1458  delete itr;
1459  }
1460 
1461 if(node->hasChildren())
1462  children=rollBackAllNodes(node->getFirstChildNode());
1463  StDbConfigNode* nextNode;
1464 if((nextNode=node->getNextNode()))
1465  siblings=rollBackAllNodes(nextNode);
1466 
1467  thisNode=rollBack(node);
1468 
1469 return (tables && children && siblings && thisNode);
1470 }
1471 
1473 bool
1475 #define __METHOD__ "rollBack(node)"
1476 
1477  if(!node->canRollBack())
1478  return (bool)printInfo(" Cannot rollback Store of Node ",dbMErr,__LINE__,__CLASS__,__METHOD__);
1479 
1480  StDataBaseI* db=findDb(node->getDbType(),node->getDbDomain());
1481  if(!db) return false;
1482  return db->rollBack(node);
1483 #undef __METHOD__
1484 }
1485 
1487 bool
1489 
1490  int numRows;
1491  int* dataRows=table->getWrittenRows(numRows);
1492  if(!dataRows)return true;
1493  StDataBaseI* db=findDb(table->getDbType(),table->getDbDomain());
1494  if(!db) return false;
1495  return db->rollBack(table);
1496 }
1497 
1499 bool
1501 
1502 bool retVal=true;
1503 
1504  if(node->hasData()){
1505  StDbTableIter* itr = node->getStDbTableIter();
1506  while(!itr->done()){
1507  StDbTable* tab = itr->next();
1508  tab->commitData();
1509  }
1510  delete itr;
1511  }
1512 
1513 bool children = true;
1514 bool siblings = true;
1515 
1516  if(node->hasChildren())children = commitAllTables(node->getFirstChildNode());
1517  StDbConfigNode* nextNode;
1518  if((nextNode=node->getNextNode()))siblings = commitAllTables(nextNode);
1519 
1520 return (retVal && children && siblings);
1521 }
1522 
1524 bool
1526 
1527  if(node->hasData()){
1528  StDbTableIter* itr = node->getStDbTableIter();
1529  while(!itr->done()){
1530  StDbNode* ntab= (StDbNode*)itr->next();
1531  ntab->commit();
1532  }
1533  delete itr;
1534  }
1535 if(node->hasChildren())commitAllNodes(node->getFirstChildNode());
1536  StDbConfigNode* nextNode;
1537 if((nextNode=node->getNextNode()))commitAllNodes(nextNode);
1538 node->commit();
1539 
1540 return true;
1541 }
1542 
1544 void
1546 
1547  for(ServerList::iterator itr = mservers.begin();
1548  itr != mservers.end(); ++itr)
1549  (*itr)->closeConnection();
1550 }
1551 
1553 void
1555 
1556 if(!node) return;
1557 
1558  closeConnection(node);
1559  if(node->hasChildren())closeAllConnections(node->getFirstChildNode());
1560  StDbConfigNode* next = node->getNextNode();
1561  if(next)closeAllConnections(next);
1562 
1563 }
1564 
1566 void
1567 StDbManagerImpl::closeConnection(StDbNode* node){
1568 
1569 if(!node) return;
1570 StDbServer* server=findServer(node->getDbType(),node->getDbDomain());
1571 if(server && (server->isConnected())) server->closeConnection();
1572 
1573 }
1574 
1576 void
1577 StDbManagerImpl::printTimeStats(){
1578 #define __METHOD__ "printTimeStats()"
1579 
1580 if(!misTimeLogged){ cout<< "Timing Not Logged"<<stendl; return; }
1581 double queryTimes=0;
1582 double socketTimes=0;
1583  double connectTimes[10];
1584  char* serverID[10];
1585  int j=0;
1586 for(ServerList::iterator itr = mservers.begin();
1587  itr != mservers.end(); ++itr){
1588  queryTimes+=(*itr)->getQueryTimes();
1589  socketTimes+=(*itr)->getSocketTimes();
1590  connectTimes[j]=(*itr)->getConnectTimes();
1591  serverID[j]=(*itr)->printServerName();
1592  j++;
1593 }
1594  double dbTotalConnect=0.;
1595  int i;
1596  for(i=0;i<j;i++)dbTotalConnect+=connectTimes[i];
1597 
1598  double dbTotalTimes=mnodeLog.getTotalTimes() + mdataLog.getTotalTimes();
1599  double dbNodeTotal=mnodeLog.getTotalTimes();// - dbTotalConnect;
1600  double dbNF=100.0*(dbNodeTotal/dbTotalTimes);
1601  double dbDF=100.0*(mdataLog.getTotalTimes()/dbTotalTimes);
1602  double dbQF=100.0*(queryTimes/dbTotalTimes);
1603  double dbSF=100.0*(socketTimes/dbTotalTimes);
1604  // double dbTF=100.0*((socketTimes+queryTimes)/dbTotalTimes);
1605  double dbCF=100.0*(dbTotalConnect/dbTotalTimes);
1606 
1607  StString cos;
1608  cos<<stendl<<"*************************** DataBase ClockTime Stats *************************** "<<stendl;
1609 
1610  cos<<"Total Time in DB-API = "<<dbTotalTimes<<"\t sec"<<stendl;
1611  cos<<" --------------------- In aggregate ------------------"<<stendl;
1612  cos<<" - Prepare Nodes & Servers = "<<dbNodeTotal<<"\t sec --> "<<dbNF<<"% of total"<<stendl;
1613  cos<<" - Select & Retrieve data = "<<mdataLog.getTotalTimes()<<"\t sec --> "<<dbDF<<"% of total"<<stendl;
1614  cos<<" --------------- In MySQL C-API (approximate) ---------"<<stendl;
1615  cos<<" - Connecting to Servers = "<<dbTotalConnect<<"\t sec --> "<<dbCF<<"% of total"<<stendl;
1616  if(j>1){
1617  cos<<" [ Connections per Server; ";
1618  for(i=0;i<j;i++)cos<<serverID[i]<<"="<<connectTimes[i]<<" sec ";
1619  cos<<" ] "<<stendl;
1620  }
1621  cos<<" - SQL Query Times = "<<queryTimes<<"\t sec --> "<<dbQF<<"% of total"<<stendl;
1622  cos<<" - socket transfer Times = "<<socketTimes<<"\t sec --> "<<dbSF<<"% of total"<<stendl;
1623 
1624  cos<<"********************************************************************************"<<stendl;
1625 
1626  printInfo((cos.str()).c_str(),dbMConnect,__LINE__,__CLASS__,__METHOD__);
1627 
1628 #undef __METHOD__
1629 }
1630 
1632 bool
1633 StDbManagerImpl::getDataBaseInfo(const char* dbName, char*& type, char*& domain){
1634 #define __METHOD__ "getDataBaseInfo(dbName,type,domain)"
1635 char* tmpName=mstringDup(dbName);
1636 if(!tmpName) return false;
1637 char* id;
1638  if( !(id=strstr(tmpName,"_"))){
1639  type=tmpName;
1640  domain = mstringDup("Star");
1641  } else {
1642  *id='\0'; id++;
1643  type=mstringDup(tmpName);
1644  domain=mstringDup(id);
1645  id--; *id='_';
1646  delete [] tmpName;
1647  }
1648  StString ni;
1649  ni<<" Found dbType="<<type<<" & dbDomain="<<domain;
1650  ni<<" from DataBase Name="<<dbName;
1651 
1652  printInfo((ni.str()).c_str(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
1653 
1654 return true;
1655 #undef __METHOD__
1656 }
1657 
1658 bool
1659 StDbManagerImpl::getDataBaseInfo(const char* dbName, StDbType& type, StDbDomain& domain){
1660  char* typeName=0; char* domainName=0;
1661  if(!dbName || !(getDataBaseInfo(dbName,typeName,domainName)))return false;
1662  type=getDbType(typeName);
1663  domain=getDbDomain(domainName);
1664  if(typeName)delete [] typeName;
1665  if(domainName) delete [] domainName;
1666 return true;
1667 }
1668 
1670 char*
1671 StDbManagerImpl::getDbName(const char* typeName, const char* domainName){
1672  if (!typeName || !domainName) return 0;
1673  std::string tpName(typeName);
1674  std::string dmName(domainName);
1675  std::string mergedName = tpName + "_" + dmName;
1676  std::string completeName;
1677  std::string blacklisted_domain;
1678 
1679  for (std::set<std::string>::iterator it = mBlacklist.begin(); it != mBlacklist.end(); ++it) {
1680  blacklisted_domain = *it;
1681  completeName = "Calibrations_" + blacklisted_domain;
1682  if (mergedName == completeName) {
1683  mergedName = "blacklist_" + mergedName;
1684  return mstringDup(mergedName.c_str());
1685  }
1686  }
1687 
1688  StString dbname;
1689  dbname<<typeName;
1690  if(strcmp(domainName,"Star")!=0)dbname<<"_"<<domainName;
1691  char* retName = mstringDup((dbname.str()).c_str());
1692 
1693  return retName;
1694 }
1695 
1696 #undef __CLASS__
1697 
1698 
1699 
1700 
1701 
virtual void closeAllConnections()
node commits
virtual bool commitAllNodes(StDbConfigNode *node)
table commits
virtual bool rollBack(StDbNode *node)
for node data
virtual bool commitAllTables(StDbConfigNode *node)
for table data
virtual void lookUpServers()
virtual bool rollBackAllNodes(StDbConfigNode *node)
for table data
StDbMessService * Messenger
where normal=!misVerbose &amp;&amp; !misQuiet
Definition: StDbManager.hh:154