372 #include "StDbTable.h"
373 #include "StDbBuffer.h"
374 #include "typeAcceptor.hh"
375 #include "StTableDescriptorI.h"
377 #ifndef __STDB_STANDALONE__
378 #include "StMessMgr.h"
380 #define LOG_DEBUG cout
381 #define LOG_INFO cout
382 #define LOG_WARN cout
383 #define LOG_ERROR cerr
384 #define LOG_FATAL cerr
389 #include "StDbDefaults.hh"
390 #include "StDbManager.hh"
392 #include "stdb_streams.h"
400 #define __CLASS__ "StDbTable"
403 StDbTable::StDbTable(
const char* tableName):
StDbNode(tableName) { init();};
405 StDbTable::StDbTable(
const char* tableName,
int schemaID):
StDbNode(tableName) { init(); mschemaID=schemaID; }
411 mflavor=table.getFlavor();
412 mdefaultFlavor = table.defaultFlavor();
413 mprodTime = table.getProdTime();
415 mschemaID = table.getSchemaID();
416 mrows = table.GetNRows();
417 mhasDescriptor=table.hasDescriptor();
418 mdescriptor=table.getDescriptorCpy();
420 mbeginTime.setDateTime(table.getBeginDateTime());
421 mbeginTime.setUnixTime(table.getBeginTime());
422 mendTime.setDateTime(table.getEndDateTime());
423 mendTime.setUnixTime(table.getEndTime());
425 char* tmp = table.GetTable();
426 if(mrows==0) mrows = 1;
428 unsigned int size = mrows*table.getTableSize();
429 mdata =
new char[size];
430 memcpy(mdata,tmp,size);
434 setCstructName(table.printCstructName());
435 setDataTable(table.printDataTable());
439 void StDbTable::init() {
451 mendTime.munixTime = 0;
454 mprodTime = StDbDefaults::Instance()->getProdTime();
461 void StDbTable::setNodeInfo(
StDbNode* node){
463 mdbType=node->getDbType();
464 mdbDomain=node->getDbDomain();
465 setDbName(node->printDbName());
466 setVersion(node->printVersion());
467 setNodeType(node->printNodeType());
468 mnodeID=node->getNodeID();
472 void StDbTable::setCstructName(
const char* name){ mstructName=mstrDup(name); }
475 char* StDbTable::getCstructName() {
return mstrDup(mstructName); }
478 void StDbTable::setDataTable(
const char* name){ mdataTable=mstrDup(name); }
481 char* StDbTable::getDataTable() {
return mstrDup(mdataTable); }
485 StDbTable::setDefaultFlavor(){ setFlavor(StDbDefaults::Instance()->printFlavor());
489 void StDbTable::setFlavor(
const char* flavor) {
491 if(mflavor)
delete [] mflavor;
492 mflavor=
new char[strlen(flavor)+1];
493 strcpy(mflavor,flavor);
494 mdefaultFlavor = StDbDefaults::Instance()->IsDefaultFlavor(mflavor);
497 char* StDbTable::getFlavor() {
return mstrDup(mflavor); }
502 StDbTable::addWrittenRows(
int* dataID,
int numRows,
bool canRollBack){
504 for(
int i=0; i<numRows; i++)
mstoredData.addWrittenRow(dataID[i]);
505 if(canRollBack)mcanRollBack=
true;
510 StDbTable::getWrittenRows(
int& nRows){
513 void StDbTable::commitData() {
mstoredData.commit(); }
514 void StDbTable::clearStoreInfo() {
mstoredData.resetStoreInfo(); }
518 StDbTable::getDescriptorCpy()
const {
return mdescriptor->getCpy(); }
524 if(mdescriptor)
delete mdescriptor;
525 mdescriptor=descriptor;
533 char* StDbTable::GetTable() {
if(!
mdata)createMemory();
return mdata;};
537 StDbTable::GetTableCpy() {
539 if(!
mdata)
return (
void*)GetTable();
541 int len = mrows*getTableSize();
542 char* c = (
char*)calloc(mrows,getTableSize());
559 setElementID(idList,nrows);
561 int len = nrows*getTableSize();
569 StDbTable::AddRows(
char* c,
int nrows) {
571 char* tmpData = duplicateData();
572 int len1 = mrows*getTableSize();
573 int len2 = nrows*getTableSize();
575 int newRows = nrows+mrows;
581 createMemory(newRows);
583 char* ptr= &
mdata[0];
584 memcpy(
mdata,tmpData,len1);
595 StDbTable::getDataValue(
const char* name,
int rowNumber){
598 int saveRowNum=mrowNumber;
599 mrowNumber=rowNumber;
600 int max = mdescriptor->getNumElements();
606 for(
int i=0;i<max;i++){
607 getElementSpecs(i,ptr,ename,length,type);
609 if ( strcmp(name,ename) == 0 )
break;
615 mrowNumber=saveRowNum;
616 if(!ename)
return retVal;
624 StDbTable::duplicateData() {
627 int len1 = mrows*getTableSize();
630 memcpy(dup,
mdata,len1);
638 StDbTable::createMemory(
int nrows) {
651 if(mdescriptor && mdescriptor->getNumElements()>0){
655 if (!mdescriptor->getTrowSize()){
656 len = mrows*mdescriptor->getTotalSizeInBytes();
658 len = mrows*mdescriptor->getTrowSize();
664 int max = mdescriptor->getNumElements();
669 for(
int i=0; i<max;i++){
670 getElementSpecs(i,ptr,name,length,type);
671 if (type==Stchar) { *ptr=
'\0'; }
676 if(!mname){mname=mstrDup(
"Unknown");}
685 StDbTable::createMemory() {
686 if(
mdata)
return true;
687 if(mrows==0) mrows=1;
688 return createMemory(mrows);
694 StDbTable::getElementName() {
return mstrDup(melementName); };
697 StDbTable::setElementName(
const char* name) { melementName=mstrDup(name);};
700 StDbTable::setElementID(
int* elements,
int nrows) {
704 if(melementID)
delete [] melementID;
709 melementID =
new int[nrows];
710 memcpy(melementID, elements, nrows*
sizeof(
int));
714 void StDbTable::resizeNumRows(
int nrows){
719 unsigned int rowsize;
720 if (!mdescriptor->getTrowSize()) {
721 rowsize=mdescriptor->getTotalSizeInBytes();
723 rowsize=mdescriptor->getTrowSize();
725 unsigned int len = mrows*rowsize;
726 unsigned int newlen = nrows*rowsize;
729 char* oldData=
new char[len];
730 memcpy(oldData,
mdata,len);
732 mdata =
new char[newlen];
734 memcpy(
mdata,oldData,newlen);
736 memcpy(
mdata,oldData,len);
748 StDbTable::addNRows(
int numRows){
750 if(!mdescriptor)
return;
752 int newRows = numRows+mrows;
754 unsigned int rowsize;
755 if(!mdescriptor->getTrowSize()) {
756 rowsize=mdescriptor->getTotalSizeInBytes();
758 rowsize=mdescriptor->getTrowSize();
760 unsigned int len = newRows*rowsize;
761 char* newData =
new char[len];
762 memset(newData,0,len);
766 memset(p1,0,numRows*rowsize);
770 resizeElementID(newRows);
775 StDbTable::resizeElementID(
int numRows){
777 int * newElements=
new int[numRows];
779 memcpy(newElements,melementID,mrows*
sizeof(
int));
780 delete [] melementID;
782 melementID=newElements;
789 StDbTable::addNElements(
int* elements,
int newRows){
792 if(!melementID)
return;
798 for(i=k;i<mrows;i++){
799 melementID[i]=elements[j];
807 StDbTable::StreamAccessor(
typeAcceptor* accept,
bool isReading){
810 accept->pass((
char*)
"schemaID",mschemaID,len);
814 if(mversion)
delete [] mversion;
815 if(melementID)
delete [] melementID;
818 melementID =
new int[mrows];
819 for(
int i=0;i<mrows;i++)melementID[i]=i;
822 accept->pass((
char*)
"beginTime",
mbeginTime.mdateTime,len);
823 accept->pass((
char*)
"version",mversion,len);
824 accept->pass((
char*)
"elementID",melementID, mrows);
829 StDbTable::StreamAccessor(
StDbBufferI* buff,
bool isReading){
832 if(!(ClientMode=buff->IsClientMode()))buff->SetClientMode();
836 melementID =
new int[mrows];
837 for(
int i=0;i<mrows;i++)melementID[i]=i;
841 buff->ReadScalar(rowID,
"elementID");
842 melementID[mrowNumber]=rowID;
845 buff->ReadScalar(mschemaID,
"schemaID");
846 if(mversion)
delete [] mversion;
847 buff->ReadScalar(mversion,
"version");
850 buff->ReadScalar(bTime,
"beginTime");
856 buff->WriteScalar(mschemaID,
"schemaID");
857 buff->WriteScalar(
mbeginTime.munixTime,
"beginTime");
858 if(mversion)buff->WriteScalar(mversion,
"version");
859 rowID = melementID[mrowNumber];
860 buff->WriteScalar(rowID,
"elementID");
863 if(!ClientMode)buff->SetStorageMode();
869 StDbTable::getElementSpecs(
int elementNum,
char*& c,
char*& name,
unsigned int& length,StTypeE& type){
871 int tRow = mdescriptor->getTrowSize();
872 unsigned int tSize=mdescriptor->getTotalSizeInBytes();
873 unsigned int rowIndex;
875 rowIndex = ((
unsigned int)mrowNumber)*tSize;
877 rowIndex = ((
unsigned int)mrowNumber)*tRow;
880 c = &
mdata[rowIndex];
881 int current = mdescriptor->getElementOffset(i);
883 name = mdescriptor->getElementName(i);
884 length = mdescriptor->getElementLength(i);;
885 type = mdescriptor->getElementType(i);
892 StDbTable::dbStreamer(
StDbBufferI* buff,
bool isReading){
894 int max = mdescriptor->getNumElements();
901 if(!(ClientMode=buff->IsClientMode()))buff->SetClientMode();
903 if(createMemory() && mrowNumber < mrows){
905 for(
int i=0; i<max; i++){
906 getElementSpecs(i,ptr,name,length,type);
908 ReadElement(ptr,name,length,type,(
StDbBuffer*)buff);
910 WriteElement(ptr,name,length,type,(
StDbBuffer*)buff);
916 if(isReading)mhasData=
true;
919 LOG_ERROR <<
"dbStreamer:: more rows delivered than allocated " << endm;
921 if(!ClientMode)buff->SetStorageMode();
929 int max = mdescriptor->getNumElements();
940 if(mrowNumber<mrows){
941 for(
int i=0; i<max; i++){
942 getElementSpecs(i,ptr,name,length,type);
943 WriteElement(ptr,name,length,type,(
StDbBuffer*)buff);
953 StDbTable::dbTableStreamer(
StDbBufferI* buff,
const char* name,
bool isReading){
955 int max = mdescriptor->getNumElements();
957 StTypeE type = mdescriptor->getElementType(0);
958 unsigned int length = (
unsigned int) mrows*max;
963 if(!(ClientMode=buff->IsClientMode()))buff->SetClientMode();
965 if(createMemory() && mrowNumber < mrows){
970 ReadElement(ptr,(
char *) name,length,type,(
StDbBuffer*)buff);
972 WriteElement(ptr,(
char *) name,length,type,(
StDbBuffer*)buff);
975 if(isReading) mhasData=
true;
978 if(!ClientMode)buff->SetStorageMode();
985 StDbTable::dbStreamer(
typeAcceptor* accept,
bool isReading){
987 int max = mdescriptor->getNumElements();
993 if(createMemory() && mrowNumber < mrows){
996 for(
int i=0; i<max; i++){
997 getElementSpecs(i,ptr,name,length,type);
998 PassInElement(ptr,name,length,type,accept);
1003 for(
int i=0; i<max; i++){
1004 getElementSpecs(i,ptr,name,length,type);
1005 PassOutElement(ptr,name,length,type,accept);
1018 StDbTable::ReadElement(
char*& ptr,
char* name,
int len, StTypeE type,
StDbBuffer* buff){
1020 char* mchar;
unsigned char* muchar;
short* mshort;
unsigned short* mushort;
1021 int* mint;
unsigned int* muint;
long* mlong;
unsigned long* mulong;
1022 long long* mlonglong;
1023 float* mfloat;
double* mdouble;
1031 cn<<name<<
".text";
const char* commentName = (cn.str()).c_str();
1033 if(!buff->ReadScalar(mchar,commentName))buff->ReadScalar(mchar,name);
1035 int len1=strlen(mchar);
1036 if(len>len1) len=len1;
1037 strncpy(ptr,mchar,len);
1041 printNoDataReturned(name);
1047 if(buff->ReadArray(muchar,blen,name)){
1048 if(len>blen)len=blen;
1049 memcpy(ptr,muchar,len*
sizeof(
unsigned char));
1051 }
else { printNoDataReturned(name); }
1056 if(buff->ReadArray(mshort,blen,name)){
1057 if(len>blen)len=blen;
1058 memcpy(ptr,mshort,len*
sizeof(
short));
1060 }
else { printNoDataReturned(name); }
1065 if(buff->ReadArray(mushort,blen,name)){
1066 if(len>blen)len=blen;
1067 memcpy(ptr,mushort,len*
sizeof(
unsigned short));
1069 }
else { printNoDataReturned(name); }
1074 if(buff->ReadArray(mint,blen,name)){
1075 if(len>blen)len=blen;
1076 memcpy(ptr,mint,len*
sizeof(
int));
1078 }
else { printNoDataReturned(name); }
1083 if(buff->ReadArray(muint,blen,name)){
1084 if(len>blen)len=blen;
1085 memcpy(ptr,muint,len*
sizeof(
unsigned int));
1087 }
else { printNoDataReturned(name); }
1092 if(buff->ReadArray(mlong,blen,name)){
1093 if(len>blen)len=blen;
1094 memcpy(ptr,mlong,len*
sizeof(
long));
1096 }
else { printNoDataReturned(name); }
1101 if(buff->ReadArray(mulong,blen,name)){
1102 if(len>blen)len=blen;
1103 memcpy(ptr,mulong,len*
sizeof(
unsigned long));
1105 }
else { printNoDataReturned(name); }
1110 if(buff->ReadArray(mlonglong,blen,name)){
1111 if(len>blen)len=blen;
1112 memcpy(ptr,mlonglong,len*
sizeof(
long long));
1113 delete [] mlonglong;
1114 }
else { printNoDataReturned(name); }
1119 if(buff->ReadArray(mfloat,blen,name)){
1120 if(len>blen)len=blen;
1121 memcpy(ptr,mfloat,len*
sizeof(
float));
1123 }
else { printNoDataReturned(name); }
1128 if(buff->ReadArray(mdouble,blen,name)){
1129 if(len>blen)len=blen;
1130 memcpy(ptr,mdouble,len*
sizeof(
double));
1132 }
else { printNoDataReturned(name); }
1136 printNoDataReturned(name);
1145 StDbTable::WriteElement(
char* ptr,
char* name,
int len, StTypeE type,
StDbBuffer* buff){
1151 buff->WriteScalar(mchar,name);
1156 unsigned char* muchar = (
unsigned char*)ptr;
1157 buff->WriteArray(muchar,len,name);
1162 short* mshort = (
short*) ptr;
1163 buff->WriteArray(mshort ,len,name);
1168 unsigned short* mushort = (
unsigned short*) ptr;
1169 buff->WriteArray(mushort,len,name);
1174 int* mint = (
int*)ptr;
1175 buff->WriteArray(mint,len,name);
1180 unsigned int* muint = (
unsigned int*) ptr;
1181 buff->WriteArray(muint,len,name);
1186 long* mlong = (
long*) ptr;
1188 buff->WriteArray(mlong,len,name);
1193 unsigned long* mulong = (
unsigned long*) ptr;
1194 buff->WriteArray(mulong,len,name);
1199 long long* mlonglong = (
long long*) ptr;
1200 buff->WriteArray(mlonglong,len,name);
1205 float* mfloat = (
float*) ptr;
1207 buff->WriteArray(mfloat,len,name);
1212 double* mdouble = (
double*) ptr;
1213 buff->WriteArray(mdouble,len,name);
1224 StDbTable::PassInElement(
char* ptr,
char* name,
int len, StTypeE type,
typeAcceptor* accept){
1231 accept->pass(name,data,len);
1232 memcpy(ptr,data,len);
1238 unsigned char* data;
1239 accept->pass(name,data,len);
1240 memcpy(ptr,data,len);
1247 accept->pass(name,data,len);
1248 memcpy(ptr,data,len*
sizeof(
short));
1254 unsigned short* data;
1255 accept->pass(name,data,len);
1256 memcpy(ptr,data,len*
sizeof(
short));
1263 accept->pass(name,data,len);
1264 memcpy(ptr,data,len*
sizeof(
int));
1271 accept->pass(name,data,len);
1272 memcpy(ptr,data,len*
sizeof(
int));
1279 accept->pass(name,data,len);
1280 memcpy(ptr,data,len*
sizeof(
long));
1286 unsigned long* data;
1287 accept->pass(name,data,len);
1288 memcpy(ptr,data,len*
sizeof(
long));
1295 accept->pass(name,data,len);
1296 memcpy(ptr,data,len*
sizeof(
long long));
1303 accept->pass(name,data,len);
1304 memcpy(ptr,data,len*
sizeof(
float));
1311 accept->pass(name,data,len);
1312 memcpy(ptr,data,len*
sizeof(
double));
1325 StDbTable::PassOutElement(
char* ptr,
char* name,
int len, StTypeE type,
typeAcceptor* accept){
1330 accept->pass(name,ptr,len);
1335 unsigned char* muchar = (
unsigned char*)ptr;
1336 accept->pass(name, muchar,len);
1341 short* mshort = (
short*)ptr;
1343 accept->pass(name, *mshort ,len);
1345 accept->pass(name,mshort,len);
1351 unsigned short* mushort = (
unsigned short*)ptr;
1353 accept->pass(name, *mushort ,len);
1355 accept->pass(name,mushort,len);
1361 int* mint = (
int*)ptr;
1363 accept->pass(name, *mint ,len);
1365 accept->pass(name,mint,len);
1371 unsigned int* muint = (
unsigned int*)ptr;
1373 accept->pass(name, *muint ,len);
1375 accept->pass(name,muint,len);
1381 long* mlong = (
long*)ptr;
1383 accept->pass(name, *mlong ,len);
1385 accept->pass(name,mlong,len);
1391 unsigned long* mulong = (
unsigned long*)ptr;
1393 accept->pass(name, *mulong ,len);
1395 accept->pass(name,mulong,len);
1401 long long* mlonglong = (
long long*)ptr;
1403 accept->pass(name, *mlonglong ,len);
1405 accept->pass(name,mlonglong,len);
1411 float* mfloat = (
float*)ptr;
1413 accept->pass(name, *mfloat ,len);
1415 accept->pass(name,mfloat,len);
1421 double* mdouble = (
double*)ptr;
1423 accept->pass(name, *mdouble ,len);
1425 accept->pass(name,mdouble,len);
1437 StDbTable::checkDescriptor(){
1439 int i = mdescriptor->getNumElements();
1440 unsigned int size = mdescriptor->getTotalSizeInBytes();
1441 cout <<
"Descriptor for Table = " << mname<<endl;
1442 cout <<
" number of elements = "<<i<<
" with size = " << size <<
" TrowSize = " << mdescriptor->getTrowSize()<< endl;
1443 for(
int k=0; k<i;k++){
1444 cout <<
"Name = " << mdescriptor->getElementName(k);
1445 cout <<
" size = " << mdescriptor->getElementSize(k);
1446 cout <<
" offset = " <<mdescriptor->getElementOffset(k);
1447 cout <<
" type = " <<(int)mdescriptor->getElementType(k) << endl;
1452 void StDbTable::printNoDataReturned(
const char* elementName){
1455 emess<<
" No data return from table="<<printName()<<
" column="<<elementName;
1456 StDbManager::Instance()->printInfo((emess.str()).c_str(),dbMWarn,__LINE__,__CLASS__,
"ReadElement(ptr,name,len,type,buffer)");
StDbStoreInfo mstoredData
for rolling back stores
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc'd version of data for StRoot
bool mhasDescriptor
c-struct descriptor information
StDbTime mbeginTime
validity interval
char * mdata
data & num of rows
static StDbManager * Instance()
strdup(..) is not ANSI