Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

StDbConfigNodeImpl.cc

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * 00003 * $Id: StDbConfigNodeImpl.cc,v 1.7 2004/01/15 00:02:25 fisyak Exp $ 00004 * 00005 * Author: R. Jeff Porter 00006 *************************************************************************** 00007 * 00008 * Description: Payload Implementatoin of pure virtual Node (directory) class 00009 * StDbConfigNode --> to hold list of dbtables 00010 * 00011 *************************************************************************** 00012 * 00013 * $Log: StDbConfigNodeImpl.cc,v $ 00014 * Revision 1.7 2004/01/15 00:02:25 fisyak 00015 * Replace ostringstream => StString, add option for alpha 00016 * 00017 * Revision 1.6 2003/09/16 22:44:17 porter 00018 * got rid of all ostrstream objects; replaced with StString+string. 00019 * modified rules.make and added file stdb_streams.h for standalone compilation 00020 * 00021 * Revision 1.5 2003/09/02 17:57:49 perev 00022 * gcc 3.2 updates + WarnOff 00023 * 00024 * Revision 1.4 2003/01/10 04:19:20 porter 00025 * added feature of getting timestamp list (but no data) for a table. 00026 * fixed 2 features sometimes used in online in query-by-whereclause. 00027 * removed a stray 'cout' in a routine that is rarely accessed 00028 * 00029 * Revision 1.3 2001/10/26 16:35:28 porter 00030 * improved directory search 00031 * 00032 * Revision 1.2 2001/02/09 23:06:24 porter 00033 * replaced ostrstream into a buffer with ostrstream creating the 00034 * buffer. The former somehow clashed on Solaris with CC5 iostream (current .dev) 00035 * 00036 * Revision 1.1 2001/01/22 18:37:52 porter 00037 * Update of code needed in next year running. This update has little 00038 * effect on the interface (only 1 method has been changed in the interface). 00039 * Code also preserves backwards compatibility so that old versions of 00040 * StDbLib can read new table structures. 00041 * -Important features: 00042 * a. more efficient low-level table structure (see StDbSql.cc) 00043 * b. more flexible indexing for new systems (see StDbElememtIndex.cc) 00044 * c. environment variable override KEYS for each database 00045 * d. StMessage support & clock-time logging diagnostics 00046 * -Cosmetic features 00047 * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access 00048 * f. removed codes that have been obsolete for awhile (e.g. db factories) 00049 * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI 00050 * and mysqlAccessor became StDbSql) 00051 * 00052 * 00053 **************************************************************************/ 00054 #include "stdb_streams.h" 00055 #include <string.h> 00056 #include "StDbConfigNodeImpl.hh" 00057 #include "StDbManagerImpl.hh" // could be StDbManager.hh but for dbEnvList def 00058 #include "StDbTableIterImpl.hh" 00059 #include "StDbElementIndex.hh" 00060 00061 #define __CLASS__ "StDbConfigNodeImpl" 00062 00064 00065 StDbConfigNodeImpl::StDbConfigNodeImpl(StDbConfigNode* parent, const char* nodeName, const char* configName): StDbConfigNode(parent,nodeName,configName), mindexRef(-1) { 00066 melementIndex = new StDbElementIndex(); 00067 setConfigured(false); 00068 mcanRollBack=false; 00069 updateDbInfo(); 00070 } 00071 00073 00074 StDbConfigNodeImpl::StDbConfigNodeImpl(StDbConfigNode* parent, StDbNode& node): StDbConfigNode(parent,node), mindexRef(-1) { 00075 melementIndex = new StDbElementIndex(); 00076 updateDbInfo(); 00077 } 00078 00080 00081 StDbConfigNodeImpl::StDbConfigNodeImpl(StDbType type, StDbDomain domain, const char* nodeName, const char* configName): StDbConfigNode(type,domain,nodeName,configName), mindexRef(-1) { 00082 melementIndex = new StDbElementIndex(); 00083 updateDbInfo(); 00084 } 00085 00087 00088 StDbConfigNodeImpl::~StDbConfigNodeImpl(){ 00089 deleteTables(); 00090 delete melementIndex; 00091 }; 00092 00094 void 00095 StDbConfigNodeImpl::updateDbInfo(){ 00096 #define __METHOD__ "updateDbInfo()" 00097 00098 if(mparentNode)melementIndex->addElementIndex(mparentNode->getElementIndex()); 00099 00100 // If StarDb Type, then name holds map to real Db type 00101 // while the domain may still be StarDb 00102 // Else if StarDb domain, then name holds map to real domain type 00103 // Else both type & domain are that of parent (which maybe User defined) 00104 00105 misConfigured = false; 00106 if(mparentNode){ //top level node will have type&domain set by the manager. 00107 if(mdbType==dbStDb){ 00108 mdbType=StDbManager::Instance()->getDbType(mname); 00109 } else if(mdbDomain==dbStar) { 00110 mdbDomain=StDbManager::Instance()->getDbDomain(mname); 00111 } 00112 } 00113 00114 if(!mnodeType) mnodeType=mstrDup("DB"); 00115 // replace nodetype of DB to Config moving from 1 db to another 00116 // & allow overwriting of versionkey with environment variable 00117 if(strcmp(mnodeType,"DB")==0){ 00118 setIsDbNode(true); 00119 delete [] mnodeType; 00120 mnodeType=mstrDup("Config"); 00121 setBranchID(0); 00122 char* version=StDbManager::Instance()->getExternalVersion(mdbType,mdbDomain); 00123 if(version && strcmp(version,mversion)){ 00124 StString nm; 00125 nm<<" Overriding Key="<<mversion<<" with Environment Var Key="<<version; 00126 nm<<" for DataBase="; 00127 nm<<StDbManager::Instance()->printDbName(mdbType,mdbDomain); 00128 StDbManager::Instance()->printInfo((nm.str()).c_str(),dbMWarn,__LINE__,__CLASS__,__METHOD__); 00129 setVersion(version); 00130 } 00131 } 00132 00133 #undef __METHOD__ 00134 } 00135 00137 int 00138 StDbConfigNodeImpl::buildTree(int opt){ 00139 #define __METHOD__ "buildTree(int opt)" 00140 00141 StDataBaseI *db = StDbManager::Instance()->findDb(mdbType, mdbDomain); 00142 00143 if(!db) 00144 return StDbManager::Instance()->printInfo(" No DB found for Node=",mname,dbMErr,__LINE__,__CLASS__,__METHOD__); 00145 00146 if(db->QueryDb(this)){ 00147 // check if we should connect via environment variables 00148 if(isDbNode() && mdbDomain==dbStar){ 00149 dbEnvList* elist = StDbManager::Instance()->getEnvList(mname); 00150 if(elist){ 00151 addChildren(elist); 00152 delete elist; 00153 } 00154 } 00155 // load descriptors & element structure as needed 00156 updateDbTables(opt); 00157 if(mfirstChildNode)mfirstChildNode->buildTree(opt); 00158 } 00159 00160 if(mnextNode)mnextNode->buildTree(opt); 00161 00162 return 1; 00163 #undef __METHOD__ 00164 } 00165 00167 void StDbConfigNodeImpl::updateDbTables(int opt){ 00168 00169 if(!mhasData)return; 00170 StDataBaseI* db=StDbManager::Instance()->findDb(mdbType, mdbDomain); 00171 if(!db) return; 00172 00173 StDbTableIter* itr = getStDbTableIter(); 00174 StDbTable* table; 00175 while((itr) && !(itr->done())){ 00176 table=itr->next(); 00177 updateDbTable(table,opt); 00178 } 00179 if(itr)delete itr; 00180 00181 } 00183 void StDbConfigNodeImpl::updateDbTable(StDbTable* table, int opt){ 00184 00185 if(!table) return; 00186 StDataBaseI* db=StDbManager::Instance()->findDb(mdbType, mdbDomain); 00187 if(!db)return; 00188 00189 int nRows; 00190 char* elements=table->printElementName(); 00191 if(elements){ 00192 int* eList=db->selectElements(elements,melementIndex,nRows); 00193 if(eList){ 00194 table->setElementID(eList,nRows); 00195 delete [] eList; 00196 } 00197 } 00198 if(!opt) db->QueryDescriptor(table); 00199 } 00200 00202 00203 StDbTable* 00204 StDbConfigNodeImpl::addDbTable(const char* tableName, const char* version){ 00205 00206 // just like addTable but also loads the descriptor from the database 00207 StDbTable* table = addTable(tableName,version); 00208 updateDbTable(table); 00209 return table; 00210 } 00211 00213 00214 StDbTable* 00215 StDbConfigNodeImpl::addTable(const char* tableName, const char* version){ 00216 00217 StDbTable* table = StDbManager::Instance()->newDbTable(mdbName,tableName); 00218 if(table) mTables.push_back(table); 00219 table->setVersion((char*)version); 00220 table->setNodeType("table"); 00221 00222 StDataBaseI* db = StDbManager::Instance()->findDb(mdbType, mdbDomain); 00223 if(db && table && db->QueryDb((StDbNode*)table))mhasData=true; 00224 return table; 00225 } 00226 00228 StDbTable* 00229 StDbConfigNodeImpl::addTable(StDbNode* node){ 00230 00231 StDbTable* table = StDbManager::Instance()->newDbTable(node); 00232 if(table){ 00233 mTables.push_back(table); 00234 mhasData=true; 00235 } 00236 00237 return table; 00238 } 00239 00241 StDbTable* 00242 StDbConfigNodeImpl::findTable(const char* name, const char* subPath){ 00243 00244 if(subPath && (strcmp(subPath,"/")==0))return findLocalTable(name); 00245 00246 StDbConfigNode* node = findConfigNode(subPath); 00247 if(node)return node->findLocalTable(name); 00248 00249 // if we got here then we can't find it 00250 StDbTable* table=0; 00251 return table; 00252 } 00253 00255 00256 StDbTable* 00257 StDbConfigNodeImpl::findLocalTable(const char* name){ 00258 00259 TableList::iterator itr; 00260 StDbTable* table=0; 00261 for(itr = mTables.begin(); itr!=mTables.end(); ++itr){ 00262 if((*itr)->checkName(name)){ 00263 table=*itr; 00264 break; 00265 } 00266 } 00267 return table; 00268 } 00269 00271 void 00272 StDbConfigNodeImpl::removeTable(StDbTable* table){ 00273 00274 if(!table)return; 00275 TableList::iterator itr; 00276 StDbTable* myTable=0; 00277 00278 for(itr = mTables.begin(); itr!=mTables.end(); ++itr){ 00279 myTable=*itr; 00280 if(myTable && compareTables(myTable,table)){ 00281 mTables.erase(itr); break; 00282 } 00283 myTable=0; 00284 } 00285 } 00286 00288 void 00289 StDbConfigNodeImpl::setTablesFlavor(const char* flavor){ 00290 00291 TableList::iterator itr; 00292 for(itr = mTables.begin(); itr!=mTables.end(); ++itr){ 00293 if((*itr))(*itr)->setFlavor(flavor); 00294 } 00295 } 00296 00298 void 00299 StDbConfigNodeImpl::setTablesProdTime(unsigned int ptime){ 00300 00301 TableList::iterator itr; 00302 for(itr = mTables.begin(); itr!=mTables.end(); ++itr){ 00303 if((*itr))(*itr)->setProdTime(ptime); 00304 } 00305 } 00306 00308 bool 00309 StDbConfigNodeImpl::compareTables(StDbTable* tab1, StDbTable* tab2){ 00310 00311 bool retVal=false; 00312 // compare name & version 00313 if((strcmp(tab1->printName(),tab2->printName())!=0)) return retVal; 00314 if((strcmp(tab1->printVersion(),tab2->printVersion())!=0)) return retVal; 00315 // compare each row identifier 00316 int nRows1; int nRows2; 00317 int* elements1 = tab1->getElementID(nRows1); 00318 int* elements2 = tab2->getElementID(nRows2); 00319 if(nRows1 != nRows2) return retVal; 00320 int i; 00321 for(i=0;i<nRows1;i++)if(elements1[i] != elements2[i])break; 00322 if(i==nRows1)retVal=true; 00323 return retVal; 00324 } 00325 00327 StDbElementIndex* StDbConfigNodeImpl::getElementIndex(){return melementIndex; } 00328 00330 void 00331 StDbConfigNodeImpl::setElementIndexInfo(const char* indexName, int indexID){ 00332 mindexRef=melementIndex->addNameValuePair(indexName,indexID); 00333 } 00334 00336 void 00337 StDbConfigNodeImpl::getElementIndexInfo(char*& indexName, int& indexID){ 00338 00339 if(mindexRef < 0){ indexName=0; return; } 00340 indexName=melementIndex->getIndexName(mindexRef); 00341 indexID =melementIndex->getIndexVal(mindexRef); 00342 } 00343 00345 int 00346 StDbConfigNodeImpl::getNumIndeces() const { return melementIndex->getNumIndeces();} 00347 00349 StDbTableIter* 00350 StDbConfigNodeImpl::getStDbTableIter(){ 00351 StDbTableIter* itr = new StDbTableIterImpl(this); 00352 return itr; 00353 } 00354 00355 00357 void 00358 StDbConfigNodeImpl::resetConfig(const char* configName, int opt){ 00359 00360 if(mfirstChildNode)mfirstChildNode->deleteTree(); 00361 if(mhasData)deleteTables(); 00362 setVersion(configName); 00363 buildTree(opt); 00364 } 00365 00366 00368 void 00369 StDbConfigNodeImpl::deleteTables(){ 00370 00371 TableList::iterator itr; 00372 StDbTable* table; 00373 do { for(itr = mTables.begin(); itr!=mTables.end(); ++itr){ 00374 table=*itr; 00375 mTables.erase(itr); 00376 delete table; 00377 break; 00378 } 00379 } while (mTables.begin() != mTables.end()); 00380 mhasData=false; 00381 } 00382 00384 void 00385 StDbConfigNodeImpl::addChildren(dbEnvList* elist){ 00386 #define __METHOD__ "addChildren(dbEnvList*)" 00387 00388 for(int i=0; i<elist->num; i++){ 00389 char* id=strstr(elist->envVar[i],"_"); 00390 if(id){ 00391 id++; 00392 if(strcmp(id,mname) && !findChildConfigNode(id)){ 00393 StString nm; 00394 nm<<" Adding DataBase="<<elist->envVar[i]<<" with KEY="; 00395 nm<<elist->envDef[i]<<" from Environment variable definition"; 00396 StDbManager::Instance()->printInfo((nm.str()).c_str(),dbMWarn,__LINE__,__CLASS__,__METHOD__); 00397 //VP delete [] nMes; 00398 new StDbConfigNodeImpl(this,id,elist->envDef[i]); 00399 } 00400 } 00401 } 00402 #undef __METHOD__ 00403 } 00404 00406 void 00407 StDbConfigNodeImpl::printTables(int depth){ 00408 00409 if(StDbManager::Instance()->IsVerbose()){ 00410 StString os; 00411 for(int k=0;k<depth;k++)os<<" "; 00412 00413 string pdepth=os.str(); 00414 TableList::iterator itr; 00415 for(itr = mTables.begin(); itr!=mTables.end(); ++itr){ 00416 cout<<pdepth<<"Table="<<(*itr)->printName()<<", Version="<<(*itr)->printVersion(); 00417 int nRows; 00418 int* eIDs=(*itr)->getElementID(nRows); 00419 cout <<", numRows="<<nRows; 00420 if(nRows==1)cout<<", elementID="<<eIDs[0]; 00421 cout<<endl; 00422 } 00423 //VP delete [] pdepth; 00424 } 00425 } 00426 00427 void 00428 StDbConfigNodeImpl::printNumberStats() { 00429 #define __METHOD__ "printNumberStats()" 00430 00431 unsigned int numNodes, numTables, numBytes; 00432 numNodes=numTables=numBytes=0; 00433 getNumberStats(numNodes,numTables, numBytes); 00434 double kbs = ((double)numBytes)/1000.0; 00435 StString cos; 00436 cos<<"******************** Number Stats ******************** "<<stendl; 00437 cos<<"Total Number of Nodes = "<<numNodes <<stendl; 00438 cos<<"Total Number of Tables = "<<numTables <<stendl; 00439 cos<<"Total Size of Data in Tables = "<<kbs<<" kBytes" <<stendl; 00440 cos<<"******************************************************" <<stendl; 00441 00442 StDbManager::Instance()->printInfo((cos.str()).c_str(),dbMConnect,__LINE__,__CLASS__,__METHOD__); 00443 //VP delete [] nstats; 00444 00445 #undef __METHOD__ 00446 } 00447 00449 void 00450 StDbConfigNodeImpl::getNumberStats(unsigned int& numNodes, unsigned int& numTables, unsigned int& numBytes){ 00451 00452 numNodes++; 00453 StDbTableIter* itr = getStDbTableIter(); 00454 StDbTable* table; 00455 while((itr) && !(itr->done())){ 00456 table=itr->next(); 00457 numTables++; 00458 numBytes+=(table->getTableSize()*table->GetNRows()); 00459 } 00460 if(itr)delete itr; 00461 if(mfirstChildNode)mfirstChildNode->getNumberStats(numNodes,numTables,numBytes); 00462 if(mnextNode)mnextNode->getNumberStats(numNodes,numTables,numBytes); 00463 } 00464 #undef __CLASS__ 00465

Generated on Thu Aug 24 14:45:25 2006 for Doxygen by doxygen 1.3.7