00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
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
00214
00215
int schemaID;
00216
if(tableID){
00217
if(!(buff->
ReadScalar(schemaID,
"schemaID") && (schemaID==tableID)) ){
00218
00219
return;
00220 }
00221 }
00222
00223
reSize();
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
00244
int rowpad;
00245
if((rowpad=
offsetToNextEmptyByte%2))rowpad=2-rowpad;
00246
00247
mtableSize =
offsetToNextEmptyByte+rowpad;
00248
00249
00250
00251
misValid=
true;
00252
00253 }
00254
00256
00257
00258
void
00259 StDbTableDescriptor::reSize(){
00260
00261
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
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;
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
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 ));
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
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;
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
00382
00383
#endif
00384
}
00385
00387
00388
StTypeE
00389 StDbTableDescriptor::getType(
char* type) {
00390
00391
StTypeE retVal=
Stchar;
00392
00393
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
00410
00411
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