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

StDbTableDescriptor.cc

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * 00003 * $Id: StDbTableDescriptor.cc,v 1.25 2005/09/07 22:04:02 deph Exp $ 00004 * 00005 * Author: R. Jeff Porter 00006 *************************************************************************** 00007 * 00008 * Description: Class implement table-descriptor (memory/name of data-elements) 00009 * this descriptor is loaded from database 00010 * 00011 *************************************************************************** 00012 * 00013 * $Log: StDbTableDescriptor.cc,v $ 00014 * Revision 1.25 2005/09/07 22:04:02 deph 00015 * update to correct padding issue for packed tables 00016 * 00017 * Revision 1.24 2004/01/15 00:02:25 fisyak 00018 * Replace ostringstream => StString, add option for alpha 00019 * 00020 * Revision 1.23 2003/10/28 20:58:31 perev 00021 * Linux ==> __linux__ 00022 * 00023 * Revision 1.22 2003/09/16 22:44:18 porter 00024 * got rid of all ostrstream objects; replaced with StString+string. 00025 * modified rules.make and added file stdb_streams.h for standalone compilation 00026 * 00027 * Revision 1.21 2003/09/02 17:57:50 perev 00028 * gcc 3.2 updates + WarnOff 00029 * 00030 * Revision 1.20 2002/01/30 15:40:48 porter 00031 * changed limits on flavor tag & made defaults retrieving more readable 00032 * 00033 * Revision 1.19 2001/12/05 17:16:35 porter 00034 * stand-alone make file no longer had "D__linux__" in compile but this is still needed 00035 * and returned. Also retrieve elementID list in query by whereClause for plotting 00036 * many row instances. 00037 * 00038 * Revision 1.18 2001/10/24 04:05:20 porter 00039 * added long long type to I/O and got rid of obsolete dataIndex table 00040 * 00041 * Revision 1.17 2001/02/08 23:23:56 porter 00042 * fixed initialization of schemaID in table & fixed some warnings when 00043 * compiled with NODEBUG 00044 * 00045 * Revision 1.16 2001/01/22 18:38:00 porter 00046 * Update of code needed in next year running. This update has little 00047 * effect on the interface (only 1 method has been changed in the interface). 00048 * Code also preserves backwards compatibility so that old versions of 00049 * StDbLib can read new table structures. 00050 * -Important features: 00051 * a. more efficient low-level table structure (see StDbSql.cc) 00052 * b. more flexible indexing for new systems (see StDbElememtIndex.cc) 00053 * c. environment variable override KEYS for each database 00054 * d. StMessage support & clock-time logging diagnostics 00055 * -Cosmetic features 00056 * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access 00057 * f. removed codes that have been obsolete for awhile (e.g. db factories) 00058 * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI 00059 * and mysqlAccessor became StDbSql) 00060 * 00061 * Revision 1.15 2000/03/28 17:03:19 porter 00062 * Several upgrades: 00063 * 1. configuration by timestamp for Conditions 00064 * 2. query by whereClause made more systematic 00065 * 3. conflict between db-stored comments & number lists resolved 00066 * 4. ensure endtime is correct for certain query falures 00067 * 5. dbstl.h->handles ObjectSpace & RogueWave difference (Online vs Offline) 00068 * 00069 * Revision 1.14 2000/03/06 17:11:49 porter 00070 * - WriteDb(table) returns true if no data is in table 00071 * - fixed memory leak introduced in 2/18/00 update. 00072 * - modified descriptor algorythm for OnlRunDescriptor. 00073 * 00074 * Revision 1.13 2000/03/01 20:56:16 porter 00075 * 3 items: 00076 * 1. activated reConnect for server timeouts 00077 * 2. activated connection sharing; better resource utilization but poorer 00078 * logging 00079 * 3. made rollback method in mysqlAccessor more robust (affects writes only) 00080 * 00081 * Revision 1.12 2000/02/24 20:30:47 porter 00082 * fixed padding for uchar; beginTime in mysqlAccessor; 00083 * added rollback safety checkes in StDbManger 00084 * 00085 * Revision 1.11 2000/02/15 20:27:45 porter 00086 * Some updates to writing to the database(s) via an ensemble (should 00087 * not affect read methods & haven't in my tests. 00088 * - closeAllConnections(node) & closeConnection(table) method to mgr. 00089 * - 'NullEntry' version to write, with setStoreMode in table; 00090 * - updated both StDbTable's & StDbTableDescriptor's copy-constructor 00091 * 00092 * Revision 1.10 2000/01/27 05:54:35 porter 00093 * Updated for compiling on CC5 + HPUX-aCC + KCC (when flags are reset) 00094 * Fixed reConnect()+transaction model mismatch 00095 * added some in-code comments 00096 * 00097 * Revision 1.9 2000/01/19 20:20:07 porter 00098 * - finished transaction model needed by online 00099 * - fixed CC5 compile problem in StDbNodeInfo.cc 00100 * - replace TableIter class by StDbTableIter to prevent name problems 00101 * 00102 * Revision 1.8 2000/01/10 20:37:55 porter 00103 * expanded functionality based on planned additions or feedback from Online work. 00104 * update includes: 00105 * 1. basis for real transaction model with roll-back 00106 * 2. limited SQL access via the manager for run-log & tagDb 00107 * 3. balance obtained between enumerated & string access to databases 00108 * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose 00109 * 5. restructured Node model for better XML support 00110 * 00111 * Revision 1.7 1999/12/07 21:25:25 porter 00112 * some fixes for __linux__ warnings 00113 * 00114 * Revision 1.6 1999/12/03 19:02:01 porter 00115 * modified descriptor to accept tableDescriptor once this St_base object 00116 * has been updated to have longer name lengths. 00117 * 00118 * Revision 1.5 1999/10/19 14:30:40 porter 00119 * modifications relevant to use with StDbBroker and future merging with 00120 * "params" database structure + some docs + suppressing diagnostics messages 00121 * 00122 * Revision 1.4 1999/09/30 02:06:10 porter 00123 * add StDbTime to better handle timestamps, modify SQL content (mysqlAccessor) 00124 * allow multiple rows (StDbTable), & Added the comment sections at top of 00125 * each header and src file 00126 * 00127 **************************************************************************/ 00128 #include "StDbTableDescriptor.h" 00129 #include <stdlib.h> 00130 #include <math.h> 00131 #include "stdb_streams.h" 00132 00133 int StDbTableDescriptor::rowSizeTT = 0; 00134 00136 00137 StDbTableDescriptor::StDbTableDescriptor(){ init(); } 00138 00140 00141 StDbTableDescriptor::StDbTableDescriptor(int structID, int schemaID){ 00142 00143 init(); 00144 mstructID=structID; 00145 mschemaID=schemaID; 00146 00147 }; 00148 00150 00151 void 00152 StDbTableDescriptor::init(){ 00153 00154 mtableSize=0; 00155 mCur = 0; 00156 mMax = 100; 00157 offsetToNextEmptyByte = 0; 00158 offsetToLast4Bytes = -4; 00159 mnumElements = 0; 00160 lastType=Stdouble; 00161 padsize = 0; 00162 mcols = new tableDescriptor[mMax]; 00163 memset(mcols,0,mMax*sizeof(tableDescriptor)); 00164 mschemaID=mstructID=0; 00165 misValid=false; 00166 mhasDouble=false; 00167 00168 } 00169 00171 00172 StDbTableDescriptor::StDbTableDescriptor(StDbTableDescriptor& d){ 00173 00174 mCur = (int)d.getNumElements()-1; 00175 mMax = d.getCurrentInternalSize(); 00176 offsetToNextEmptyByte = 0; 00177 offsetToLast4Bytes = 0; 00178 mcols = d.getTableDescriptor(); 00179 mnumElements=d.getNumElements(); 00180 mtableSize = d.getTotalSizeInBytes(); 00181 mschemaID=d.getSchemaID(); 00182 mstructID=d.getStructID(); 00183 misValid=d.IsValid(); 00184 mhasDouble=d.mhasDouble; 00185 00186 } 00187 00189 00190 tableDescriptor* 00191 StDbTableDescriptor::getTableDescriptor() const { 00192 00193 tableDescriptor* dScr = new tableDescriptor[mMax]; 00194 memset(dScr,0,(mMax)*sizeof(tableDescriptor)); 00195 memcpy(dScr,mcols,(mMax)*sizeof(tableDescriptor)); 00196 00197 return dScr; 00198 } 00200 00201 StTableDescriptorI* 00202 StDbTableDescriptor::getCpy(){ 00203 00204 StTableDescriptorI* dScr = new StDbTableDescriptor(*this); 00205 return dScr; 00206 00207 } 00209 00210 void 00211 StDbTableDescriptor::fillElement(StDbBuffer* buff, int tableID){ 00212 00213 //bool ClientMode; 00214 // if(!(ClientMode=buff->IsClientMode()))buff->SetClientMode(); 00215 int schemaID; 00216 if(tableID){ // mask off elements if tableID is non-zero 00217 if(!(buff->ReadScalar(schemaID,"schemaID") && (schemaID==tableID)) ){ 00218 // skip this one 00219 return; 00220 } 00221 } 00222 00223 reSize(); // increase array if needed 00224 int i = mCur; 00225 char* mtype = 0; 00226 char* mname = 0; 00227 buff->ReadScalar(mname,"name"); 00228 if(mname)strcpy(mcols[i].name,mname); 00229 if(mname) delete [] mname; 00230 if(buff->ReadScalar(mtype,"type")){ 00231 mcols[i].type = getType(mtype); 00232 if(mtype)delete [] mtype; 00233 char* length=0; 00234 if(buff->ReadScalar(length,"length"))fillSizeAndOffset(length,i); 00235 if(length) delete [] length; 00236 } 00237 00238 if(mcols[i].type==Stlonglong || mcols[i].type==Stdouble ) mhasDouble=true; 00239 00240 mCur++; 00241 mnumElements++; 00242 00243 // for multiple rows, add padding as needed at end of structure (tableSize) 00244 int rowpad; 00245 if((rowpad=offsetToNextEmptyByte%2))rowpad=2-rowpad; 00246 00247 mtableSize = offsetToNextEmptyByte+rowpad; 00248 00249 //if(!ClientMode)buff->SetStorageMode(); // reset to StorageMode 00250 00251 misValid=true; 00252 00253 } 00254 00256 00257 00258 void 00259 StDbTableDescriptor::reSize(){ 00260 00261 // simply add 10 elements 00262 00263 if(mCur<mMax) return; 00264 00265 int newMax = mMax+10; 00266 tableDescriptor* dScr = new tableDescriptor[newMax]; 00267 memcpy(dScr,mcols,(mMax)*sizeof(tableDescriptor)); 00268 if(mcols)delete [] mcols; 00269 mcols=dScr; 00270 mMax=newMax; 00271 00272 } 00273 00275 00276 void 00277 StDbTableDescriptor::fillLengths(char* length, int elementNum){ 00278 00279 int ip = 0; 00280 int i=elementNum; 00281 char* id= strstr(length,","); 00282 00283 // preset lengths to 1; 00284 00285 int k= (int)(sizeof(mcols[i].dimensionlen)/sizeof(ip)); 00286 for(int j=0;j<k;j++)mcols[i].dimensionlen[j]=1; 00287 00288 while (id && ip<3) { 00289 id[0]='\0'; 00290 mcols[i].dimensionlen[ip] = atoi(length); 00291 ip++; 00292 id++; 00293 length=id; 00294 id= strstr(length,","); 00295 } 00296 mcols[i].dimensionlen[ip]=atoi(length); 00297 00298 } 00299 00301 00302 void 00303 StDbTableDescriptor::fillSizeAndOffset(char* length, int elementNum){ 00304 00305 00306 int i = elementNum; 00307 fillLengths(length,i); 00308 int space = 4-(offsetToNextEmptyByte-offsetToLast4Bytes); 00309 00310 int j,jj; 00311 StTypeE type = mcols[i].type; 00312 j=0; 00313 mcols[i].size = getSize(mcols[i].type); 00314 int k= (int)(sizeof(mcols[i].dimensionlen)/sizeof(j)); 00315 for(j=0;j<k;j++)mcols[i].size*=mcols[i].dimensionlen[j]; 00316 00317 if(offsetToLast4Bytes < 0){ 00318 00319 mcols[i].offset = 0; 00320 offsetToNextEmptyByte = mcols[i].offset+mcols[i].size; 00321 j = 4* ((int) floor ( (float) (mcols[i].size-1)/4 )); 00322 offsetToLast4Bytes = mcols[i].offset + j;// + 4; 00323 00324 00325 } else if(type==Stchar || type==Stuchar){ 00326 00327 mcols[i].offset=offsetToNextEmptyByte; 00328 offsetToNextEmptyByte = mcols[i].offset+mcols[i].size; 00329 jj=mcols[i].offset+mcols[i].size-offsetToLast4Bytes; 00330 if(jj>1) 00331 j = 4* ((int) floor ((float) (jj-1)/4 )); 00332 // j= number of bytes (in 4s) past last offset 00333 if(j) 00334 offsetToLast4Bytes = offsetToLast4Bytes+j; 00335 00336 } else if( (space>=2) && (type == Stshort || type == Stushort) ){ 00337 00338 mcols[i].offset=offsetToLast4Bytes+2; 00339 j = 4* ((int) floor ((float) (mcols[i].size-2-1)/4 )); // note the 2 00340 offsetToNextEmptyByte=mcols[i].offset+mcols[i].size; 00341 jj=mcols[i].offset+mcols[i].size-offsetToLast4Bytes; 00342 if(jj>1) 00343 j = 4* ((int) floor ((float) (jj-1)/4 )); 00344 // j= number of bytes (in 4s) past last offset 00345 if(j) 00346 offsetToLast4Bytes = offsetToLast4Bytes+j; 00347 00348 } else { 00349 00350 00351 if( (type==Stdouble || type==Stlonglong) && 00352 (lastType != Stdouble) && (lastType != Stlonglong) && padsize < 8){ 00353 offsetToLast4Bytes+=padsize; 00354 offsetToNextEmptyByte+=padsize; 00355 } 00356 00357 mcols[i].offset=offsetToLast4Bytes+4; 00358 offsetToNextEmptyByte = mcols[i].offset + mcols[i].size; 00359 j = 4* ((int) floor ((float) (mcols[i].size-1)/4 )); 00360 offsetToLast4Bytes = mcols[i].offset + j;// + 4; 00361 00362 } 00363 00364 if(offsetToLast4Bytes<0)offsetToLast4Bytes=0; 00365 00366 #ifdef __linux__ 00367 lastType=Stdouble; 00368 #else 00369 if(type==Stdouble || type==Stlonglong )padsize = 0; 00370 lastType=type; 00371 00372 unsigned int onesize = getSize(mcols[i].type); 00373 for(j=0;j<k;j++){ 00374 if(mcols[i].dimensionlen[j]==1 && j>0)continue; 00375 for(int jk=0; jk< (int)mcols[i].dimensionlen[j]; jk++){ 00376 padsize=padsize+(int)onesize; 00377 if(padsize>=8)padsize=padsize-8; 00378 } 00379 } 00380 if(padsize>0 && padsize<=4) padsize=4; 00381 // padsize = 4* ((int) floor ((float) (padsize)/4 )); 00382 00383 #endif 00384 } 00385 00387 00388 StTypeE 00389 StDbTableDescriptor::getType(char* type) { 00390 00391 StTypeE retVal=Stchar; 00392 00393 //char* typenames[] = {"Stchar","Stuchar","Stshort","Stushort","Stint","Stuint","Stlong","Stulong","Stfloat","Stdouble","Stascii","Ststring"}; 00394 const char* typenames[] = {"char","uchar","short","ushort","int","uint","long","ulong","longlong","float","double","ascii","string"}; 00395 00396 for(int i=0; i<12;i++){ 00397 if(strcmp(type,typenames[i])==0){ 00398 retVal=(StTypeE)i; 00399 break; 00400 } 00401 } 00402 00403 return retVal; 00404 } 00405 00407 void StDbTableDescriptor::endRowPadding(){ 00408 00409 //simple item for solaris & use of doubles & longlong. 00410 // if the struct contains such an entity, it's size must 00411 // be divisable by 8. 00412 00413 #ifndef __linux__ 00414 if(mhasDouble){ 00415 int checkPadding=mtableSize%8; 00416 if(checkPadding>0 && checkPadding<5)mtableSize+=4; 00417 } 00418 #endif 00419 00420 }; 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439

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