From 944eee670af05514dff23502894352360e1e029e Mon Sep 17 00:00:00 2001 From: prabatuty Date: Sun, 3 Jul 2011 04:47:01 +0000 Subject: [PATCH] code reorg --- src/storage/Expression.cxx | 645 ----------------------------- src/storage/FieldList.cxx | 408 ------------------ src/storage/PredicateEvalImpl.cxx | 419 ------------------- src/storage/PredicateImpl.cxx | 851 -------------------------------------- src/storage/TupleIterator.cxx | 292 ------------- 5 files changed, 2615 deletions(-) delete mode 100644 src/storage/Expression.cxx delete mode 100644 src/storage/FieldList.cxx delete mode 100644 src/storage/PredicateEvalImpl.cxx delete mode 100644 src/storage/PredicateImpl.cxx delete mode 100644 src/storage/TupleIterator.cxx diff --git a/src/storage/Expression.cxx b/src/storage/Expression.cxx deleted file mode 100644 index e909fff2..00000000 --- a/src/storage/Expression.cxx +++ /dev/null @@ -1,645 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by www.databasecache.com * - * Contact: praba_tuty@databasecache.com * - * * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * ***************************************************************************/ -#include -#include -#include - -void Expression::setTable(Table *tbl ) -{ - if(NULL!=lhs) - lhs->setTable(tbl); - if(NULL!=rhs) - rhs->setTable(tbl); - - table=(TableImpl*) tbl; -} - -void Expression::setTuple(void *tpl) -{ - if(NULL!=lhs) - lhs->setTuple(tpl); - if(NULL!=rhs) - rhs->setTuple(tpl); - - tuple = tpl; -} - -void Expression::setFunctionType(FunctionType type) -{ - if(NULL!=lhs) - lhs->setFunctionType(type); - if(NULL!=rhs) - rhs->setFunctionType(type); - fType = type; -} - -void Expression::setExpr(Expression* exp1, FunctionType type,Expression* exp2) -{ - lhs = exp1; - rhs = exp2; - setFunctionType(type); - arOp=unKnownOperator; -} - -void Expression::setExpr(void *cVal,bool flag) -{ - arOp=unKnownOperator; - constVal=cVal; - lhs = rhs = NULL; -} - -void Expression::setExpr(char const *name,ArithOperator op,void *cVal) -{ - strcpy(fldName, name); - arOp = op; - constVal = cVal; - lhs = rhs = NULL; -} - -void Expression::setExpr(char const *name) -{ - strcpy(fldName, name); - arOp=unKnownOperator; - constVal=NULL; - lhs = rhs = NULL; -} - -void Expression::setExpr(Expression *exp1, ArithOperator op, Expression *exp2) -{ - lhs = exp1; - rhs = exp2; - arOp = op; -} - -void *Expression::evaluate(DataType type,bool &result) -{ - calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH); - AllDataType::memoryset(calVal,type); - char *rhsResult = NULL , *lhsResult = NULL; - if (NULL != lhs) - { - lhsResult =(char *) lhs->evaluate(type,result); - if (NULL == lhsResult) return lhsResult; - } - if (NULL != rhs) - { - rhsResult = (char *)rhs->evaluate(type,result); - if (NULL == rhsResult) return rhsResult; - } - if(result){return tuple;} - if (0==strcmp(fldName,"NULL")){ - result=true;return tuple; - } - int offset; - char *val=NULL; - if(NULL==lhs && NULL == rhs) - { - if(strcmp(fldName,"\0")!=0) - { - DataType srcType = table->getFieldType(fldName); - if(srcType > 12) return NULL; - else - { - offset=table->getFieldOffset(fldName); - val= ((char*) tuple) + offset; - if(table->isFldNull(fldName)) - { - result=true; - return tuple; - } - - } - } - if(constVal!= NULL && strcmp(fldName,"\0")!=0) - { - AllDataType::convert(table->getFieldType(fldName), val, - type, calVal, table->getFieldLength(fldName)); - solve(calVal, constVal, type, arOp); - } - else if(constVal!= NULL && 0==strcmp(fldName,"\0")) - { - AllDataType::copyVal(calVal, constVal, type, IDENTIFIER_LENGTH); - } - else if( NULL==constVal && strcmp(fldName,"\0")!=0) - { - AllDataType::convert(table->getFieldType(fldName), val, - type, calVal, table->getFieldLength(fldName)); - } - return calVal; - } - if(NULL!=lhsResult && NULL!=rhsResult) - { - solve(lhsResult, rhsResult, type, arOp); - return lhsResult; - } - return NULL; - -} - -void *Expression::evaluateForFunction(DataType type) -{ - void *rhsResult = NULL , *lhsResult = NULL; - if (NULL != lhs) - { - lhsResult =(char *) lhs->evaluateForFunction(type); - if (NULL == lhsResult) return lhsResult; - } - if (NULL != rhs) - { - rhsResult = (char *)rhs->evaluateForFunction(type); - if (NULL == rhsResult) return rhsResult; - } - int offset; - char *val=NULL; - if(NULL==lhs && NULL == rhs) - { - if(strcmp(fldName,"\0")!=0) - { - DataType srcType = table->getFieldType(fldName); - offset=table->getFieldOffset(fldName); - setDataType(srcType); - val= ((char*) tuple) + offset; - if(table->isFldNull(fldName)) - { - return NULL; - }else { - calVal=AllDataType::alloc(srcType,IDENTIFIER_LENGTH); - AllDataType::copyVal(calVal,val, srcType,IDENTIFIER_LENGTH); - return calVal; - } - - } - calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH); - AllDataType::memoryset(calVal,type); - if(constVal!= NULL && strcmp(fldName,"\0")==0) - { - copyFunctionVal(calVal, constVal, fType, IDENTIFIER_LENGTH); - return calVal; - } - } - if(NULL!=lhsResult && NULL!=rhsResult) - { - if(calVal==NULL){ - calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH); - AllDataType::memoryset(calVal,type); - } - return evaluateAndGetValPtr(lhsResult,rhsResult); - } - if(NULL!=lhsResult && NULL==rhsResult) - { - if(calVal==NULL){ - calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH); - AllDataType::memoryset(calVal,type); - } - dType = lhs->getDataType(); - if(dType == typeTimeStamp) - { - if(fType == EXTRACTYEARFROMDAY || fType == EXTRACTHOURFROMTIME - || fType == EXTRACTMINFROMTIME || fType ==EXTRACTSECFROMTIME - || fType == EXTRACTMONFROMDAY || fType ==EXTRACTDAYFROMDAY) - { - fType = (FunctionType)((int)(fType)+3); - } - } - return evaluateAndGetValPtr(lhsResult,rhsResult); - } - return NULL; -} - -void *Expression::evaluateAndGetValPtr( void *lhsResult, void *rhsResult) -{ - switch(fType) - { - case DATEDIFF: - { - *(int *)calVal = (*(Date*)lhsResult - *(Date*)rhsResult); - return calVal; - } - case DATEADDWITHYEAR: - { - ((Date*)lhsResult)->addYear(*(int*)rhsResult); - return lhsResult; - } - case DATEADDWITHMON: - { - ((Date*)lhsResult)->addMonth(*(int*)rhsResult); - return lhsResult; - } - case DATEADDWITHDAY: - { - ((Date*)lhsResult)->addDay(*(int*)rhsResult); - return lhsResult; - } - case DATESUBWITHYEAR: - { - ((Date*)lhsResult)->subYear(*(int*)rhsResult); - return lhsResult; - } - case DATESUBWITHMON: - { - ((Date*)lhsResult)->subMonth(*(int*)rhsResult); - return lhsResult; - } - case DATESUBWITHDAY: - { - ((Date*)lhsResult)->subDay(*(int*)rhsResult); - return lhsResult; - } - case TIMEDIFF: - { - *(int *)calVal = (*(Time*)lhsResult - *(Time*)rhsResult); - return calVal; - } - case TIMEADDWITHHOUR: - { - ((Time*)lhsResult)->addHour(*(int*)rhsResult); - return lhsResult; - } - case TIMEADDWITHMIN: - { - ((Time*)lhsResult)->addMin(*(int*)rhsResult); - return lhsResult; - } - case TIMEADDWITHSEC: - { - ((Time*)lhsResult)->addSec(*(int*)rhsResult); - return lhsResult; - } - case TIMESUBWITHHOUR: - { - ((Time*)lhsResult)->subHour(*(int*)rhsResult); - return lhsResult; - } - case TIMESUBWITHMIN: - { - ((Time*)lhsResult)->subMin(*(int*)rhsResult); - return lhsResult; - } - case TIMESUBWITHSEC: - { - ((Time*)lhsResult)->subSec(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPADDWITHYEAR: - { - ((TimeStamp*)lhsResult)->addYear(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPADDWITHMON: - { - ((TimeStamp*)lhsResult)->addMonth(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPADDWITHDAY: - { - ((TimeStamp*)lhsResult)->addDay(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPSUBWITHYEAR: - { - ((TimeStamp*)lhsResult)->subYear(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPSUBWITHMON: - { - ((TimeStamp*)lhsResult)->subMonth(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPSUBWITHDAY: - { - ((TimeStamp*)lhsResult)->subDay(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPADDWITHHOUR: - { - ((TimeStamp*)lhsResult)->addHour(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPADDWITHMIN: - { - ((TimeStamp*)lhsResult)->addMin(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPADDWITHSEC: - { - ((TimeStamp*)lhsResult)->addSec(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPSUBWITHHOUR: - { - ((TimeStamp*)lhsResult)->subHour(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPSUBWITHMIN: - { - ((TimeStamp*)lhsResult)->subMin(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPSUBWITHSEC: - { - ((TimeStamp*)lhsResult)->subSec(*(int*)rhsResult); - return lhsResult; - } - case TIMESTAMPDIFFYEAR: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->yearDiff((*(TimeStamp*)rhsResult)); - return calVal; - } - case TIMESTAMPDIFFMON: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->monthDiff((*(TimeStamp*)rhsResult)); - return calVal; - } - case TIMESTAMPDIFFDAY: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->dayDiff((*(TimeStamp*)rhsResult)); - return calVal; - } - case TIMESTAMPDIFFHOUR: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->hourDiff((*(TimeStamp*)rhsResult)); - return calVal; - } - case TIMESTAMPDIFFMIN: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->minDiff((*(TimeStamp*)rhsResult)); - return calVal; - } - case TIMESTAMPDIFFSEC: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->secDiff((*(TimeStamp*)rhsResult)); - return calVal; - } - case EXTRACTYEARFROMDAY: - { - *(int *)calVal = ((Date*)lhsResult)->year(); - return calVal; - } - case EXTRACTMONFROMDAY: - { - *(int *)calVal = ((Date*)lhsResult)->month(); - return calVal; - } - case EXTRACTDAYFROMDAY: - { - *(int *)calVal = ((Date*)lhsResult)->dayOfMonth(); - return calVal; - } - case EXTRACTHOURFROMTIME: - { - *(int *)calVal = ((Time*)lhsResult)->hours(); - return calVal; - } - case EXTRACTMINFROMTIME: - { - *(int *)calVal = ((Time*)lhsResult)->minutes(); - return calVal; - } - case EXTRACTSECFROMTIME: - { - *(int *)calVal = ((Time*)lhsResult)->seconds(); - return calVal; - } - case EXTRACTYEARFROMTIMESTAMP: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->year(); - return calVal; - } - case EXTRACTMONFROMTIMESTAMP: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->month(); - return calVal; - } - case EXTRACTDAYFROMTIMESTAMP: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->dayOfMonth(); - return calVal; - } - case EXTRACTHOURFROMTIMESTAMP: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->hours(); - return calVal; - } - case EXTRACTMINFROMTIMESTAMP: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->minutes(); - return calVal; - } - case EXTRACTSECFROMTIMESTAMP: - { - *(int *)calVal = ((TimeStamp*)lhsResult)->seconds(); - return calVal; - } - case DATEFROMTIMESTAMP: - { - ((TimeStamp*)lhsResult)->getDate(*(Date *)calVal); - return calVal; - } - case TIMEFROMTIMESTAMP: - { - ((TimeStamp*)lhsResult)->getTime(*(Time *)calVal); - return calVal; - } - default: return NULL; - } - -} - -void Expression::copyFunctionVal(void *dest,void *src, FunctionType type, int length) -{ - switch(type) - { - case DATEDIFF: - { - //Date date; - //date.parseFrom((char*)src); - AllDataType::copyVal(dest, src, typeDate,length); - return; - } - case DATEADDWITHYEAR: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case DATEADDWITHMON: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case DATEADDWITHDAY: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case DATESUBWITHYEAR: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case DATESUBWITHMON: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case DATESUBWITHDAY: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case TIMEDIFF: - { - AllDataType::copyVal(dest, src, typeTime,length); - return; - } - case TIMEADDWITHHOUR: - case TIMEADDWITHMIN: - case TIMEADDWITHSEC: - case TIMESUBWITHHOUR: - case TIMESUBWITHMIN: - case TIMESUBWITHSEC: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case TIMESTAMPADDWITHYEAR: - case TIMESTAMPADDWITHMON: - case TIMESTAMPADDWITHDAY: - case TIMESTAMPSUBWITHYEAR: - case TIMESTAMPSUBWITHMON: - case TIMESTAMPSUBWITHDAY: - case TIMESTAMPADDWITHHOUR: - case TIMESTAMPADDWITHMIN: - case TIMESTAMPADDWITHSEC: - case TIMESTAMPSUBWITHHOUR: - case TIMESTAMPSUBWITHMIN: - case TIMESTAMPSUBWITHSEC: - { - AllDataType::copyVal(dest, src,typeInt,length); - return; - } - case TIMESTAMPDIFFYEAR: - case TIMESTAMPDIFFMON: - case TIMESTAMPDIFFDAY: - case TIMESTAMPDIFFHOUR: - case TIMESTAMPDIFFMIN: - case TIMESTAMPDIFFSEC: - { - AllDataType::copyVal(dest, src,typeTimeStamp,length); - return; - } - case EXTRACTYEARFROMDAY: - case EXTRACTMONFROMDAY: - case EXTRACTDAYFROMDAY: - { - AllDataType::copyVal(dest, src,typeDate,length); - return; - } - case EXTRACTHOURFROMTIME: - case EXTRACTMINFROMTIME: - case EXTRACTSECFROMTIME: - { - AllDataType::copyVal(dest, src,typeTime,length); - return; - } - case EXTRACTYEARFROMTIMESTAMP: - case EXTRACTMONFROMTIMESTAMP: - case EXTRACTDAYFROMTIMESTAMP: - case EXTRACTHOURFROMTIMESTAMP: - case EXTRACTMINFROMTIMESTAMP: - case EXTRACTSECFROMTIMESTAMP: - { - AllDataType::copyVal(dest, src,typeTimeStamp,length); - return; - - } - case DATEFROMTIMESTAMP: - case TIMEFROMTIMESTAMP: - { - AllDataType::copyVal(dest, src,typeTimeStamp,length); - return; - - } - default: return; - } - -} - -void Expression::solve(void *opand1, void *opand2, DataType type, ArithOperator arOp) -{ - switch(arOp) - { - case addition: - AllDataType::addVal(opand1, opand2, type ); - break; - case subtraction: - AllDataType::subVal(opand1, opand2, type); - break; - case multiplication: - AllDataType::mulVal(opand1, opand2, type); - break; - case division: - AllDataType::divVal(opand1, opand2, type); - break; - case modulus: - AllDataType::mudVal(opand1, opand2, type); - break; - default: - break; - } - return; -} - -bool Expression::isSingleTerm() -{ - if (NULL==lhs && NULL==rhs ) return true; - else false; -} - -void Expression::memFree() -{ - if(lhs!=NULL) - lhs->memFree(); - if(rhs!=NULL) - rhs->memFree(); - free(calVal); -} - -void Expression::convertStrToVal(DataType type) -{ - if(lhs!=NULL) - lhs->convertStrToVal(type); - if(rhs!=NULL) - rhs->convertStrToVal(type); - if(constVal !=NULL) - { - void *parseString=constVal; - constVal=AllDataType::alloc(type); - AllDataType::strToValue(constVal,(char*)parseString, type); - free(parseString); - } - -} - -void Expression::freeVal() -{ - if(lhs!=NULL) - lhs->freeVal(); - if(rhs!=NULL) - rhs->freeVal(); - if(constVal !=NULL) - free(constVal); -} diff --git a/src/storage/FieldList.cxx b/src/storage/FieldList.cxx deleted file mode 100644 index cc87307b..00000000 --- a/src/storage/FieldList.cxx +++ /dev/null @@ -1,408 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by www.databasecache.com * - * Contact: praba_tuty@databasecache.com * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - ***************************************************************************/ -#include -#include -#include -#include -#include - -//does not check for duplicates -DbRetVal FieldList::append(FieldDef fDef) -{ - FieldNode *newNode = new FieldNode(); - newNode->fldDef = fDef; - newNode->next = NULL; - //If this is the first node, set it as head - if (NULL == head) { head = newNode; return OK; } - - FieldNode *iter = head; - while (NULL != iter->next) iter = iter->next; - iter->next = newNode; - return OK; -} - - -DbRetVal FieldList::remove(const char* fldName) -{ - if (NULL == head) - { - printError(ErrNotExists, "There are no elements in the list. Empty list"); - return ErrNotExists; - } - FieldNode *iter = head, *prev = head; - while (iter->next != NULL) - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - prev->next = iter->next; - delete iter; - } - prev = iter; - iter = iter->next; - } - if( iter == head) // there is only one node in the list - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - delete head; - head = NULL; - return OK; - } - - } - if( prev == head) // there are only two node in the list - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - head->next = NULL; - delete iter; - return OK; - } - } - printError(ErrNotFound, "There are no elements in the list"); - return ErrNotFound; -} - -DbRetVal FieldList::removeAll() -{ - if (NULL == head) return OK; - FieldNode *iter = head, *next = head; - while (iter->next != NULL) - { - next = iter->next; - delete iter; - iter = next; - } - delete iter; //deleting the last element - head = NULL; - return OK; -} - -int FieldList::size() -{ - int size = 0; - FieldNode *iter = head; - while (iter!= NULL) - { - size++; - iter = iter->next; - } - return size; -} - -//-1->if val is passed NULL -//-2->if fld is not present -DbRetVal FieldList::updateBindVal(const char *fldName, void *val, - bool isNullExplicit) -{ - if (NULL == val && isNullExplicit == false) - { - printError(ErrBadArg, "Value passed is NULL"); - return ErrBadArg; - } - FieldNode *iter = head; - while(NULL != iter) - { - if (strcmp(iter->fldDef.fldName_, fldName) == 0) - { - if (NULL == val) iter->fldDef.isNullExplicit_ = true; - else iter->fldDef.bindVal_ = val; - return OK; - } - iter = iter ->next; - } - printError(ErrNotFound, "Field not present in the list"); - return ErrNotFound; -} - -void *FieldList::getBindField(const char *fldName) -{ - FieldNode *iter = head; - while(NULL != iter) - { - if (strcmp(iter->fldDef.fldName_, fldName) == 0) - { - return iter->fldDef.bindVal_; - } - iter = iter ->next; - } - printError(ErrNotFound, "Field not present in the list"); - return NULL; -} - -void FieldList::fillFieldInfo(int fldpos, void *inp) -{ - int pos=0; - FieldNode *iter = head; - while (pos next; pos++; } - FieldInfoValue *info = (FieldInfoValue*) inp; - strcpy(info->fldName , iter->fldDef.fldName_); - info->length = iter->fldDef.length_; - info->type = iter->fldDef.type_; - info->offset = iter->fldDef.offset_; - info->isNullable = iter->fldDef.isNull_; - info->isPrimary = iter->fldDef.isPrimary_; - info->isUnique = iter->fldDef.isUnique_; - info->isAutoIncrement = iter->fldDef.isAutoIncrement_; -} - -DbRetVal FieldList::getFieldInfo(const char *fldName, FieldInfo *&info) -{ - - FieldNode *iter = head; - if ('*' == fldName[0]) - { - //the above is for count(*) - strcpy(info->fldName , iter->fldDef.fldName_); - if (info->type == typeString || info->type == typeVarchar) - info->length = iter->fldDef.length_ -1; - else - info->length = iter->fldDef.length_; - info->type = iter->fldDef.type_; - info->offset = iter->fldDef.offset_; - info->isDefault = iter->fldDef.isDefault_; - if (info->isDefault) - strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_); - info->isNull = iter->fldDef.isNull_; - info->isPrimary = iter->fldDef.isPrimary_; - info->isUnique = iter->fldDef.isUnique_; - info->isAutoIncrement = iter->fldDef.isAutoIncrement_; - return OK; - - } - while(iter != NULL) - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - strcpy(info->fldName , iter->fldDef.fldName_); - if (info->type == typeString || info->type == typeVarchar) - info->length = iter->fldDef.length_ -1; - else - info->length = iter->fldDef.length_; - info->type = iter->fldDef.type_; - info->offset = iter->fldDef.offset_; - info->isDefault = iter->fldDef.isDefault_; - strcpy(info->defaultValueBuf, iter->fldDef.defaultValueBuf_); - info->isNull = iter->fldDef.isNull_; - info->isPrimary = iter->fldDef.isPrimary_; - info->isUnique = iter->fldDef.isUnique_; - info->isAutoIncrement = iter->fldDef.isAutoIncrement_; - return OK; - } - iter = iter ->next; - } - return ErrNotFound; -} - -int FieldList::getFieldOffset(const char *fldName) -{ - FieldNode *iter = head; - int offset = 0; - while(iter != NULL) - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - return offset; - } - if (iter->fldDef.type_ != typeVarchar) - offset = offset + iter->fldDef.length_; - else - offset = offset + sizeof(void *); - iter = iter ->next; - } - return -1; -} - -int FieldList::getFieldOffset(int fldpos) -{ - if (fldpos < 1) return -1; - FieldNode *iter = head; - int offset = 0; - int counter =0; - while(iter != NULL) - { - if (counter == fldpos -1) - { - return offset; - } - if (iter->fldDef.type_ != typeVarchar) - offset = offset + iter->fldDef.length_; - else offset = offset + sizeof(void *); - iter = iter ->next; - counter++; - } - return -1; -} - -//Returns position of field in the list -//Count starting from 1 -//-1 if field not found in the list -int FieldList::getFieldPosition(const char *fldName) -{ - char onlyFldName[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone((char*)fldName, onlyFldName); - int position = 1; - FieldNode *iter = head; - while(iter != NULL) - { - if (0 == strcmp(iter->fldDef.fldName_, onlyFldName)) - return position; - position++; - iter = iter->next; - } - - return -1; -} - -int FieldList::getTupleSize() -{ - FieldNode *iter = head; - int offset = 0; - while(iter != NULL) - { - if (iter->fldDef.type_ == typeVarchar) - offset += sizeof(void *); - else - offset = offset + iter->fldDef.length_; - iter = iter->next; - } - return offset; -} - - - -DataType FieldList::getFieldType(const char *fldName) -{ - FieldNode *iter = head; - int offset = 0; - while(iter != NULL) - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - return iter->fldDef.type_; - } - iter = iter ->next; - } - return typeUnknown; -} - -//-1->if field not present in list -size_t FieldList::getFieldLength(const char *fldName) -{ - FieldNode *iter = head; - int offset = 0; - while(iter != NULL) - { - if (0 == strcmp(iter->fldDef.fldName_, fldName)) - { - return iter->fldDef.length_; - } - iter = iter ->next; - } - return -1; -} - - -//No check for duplicates -//TODO::User exposed so check for duplicates -DbRetVal FieldNameList::append(const char *name) -{ - FieldNameNode *newNode = new FieldNameNode(); - strcpy(newNode->fldName, name); - newNode->next = NULL; - //If this is the first node, set it as head - if (NULL == head) { head = newNode; return OK; } - - FieldNameNode *it = head; - while (NULL != it->next) it = it->next; - it->next = newNode; - return OK; -} - -//-1 -> if there is nothing in list -//-2 -> if it is not present in list -DbRetVal FieldNameList::remove(const char* name) -{ - if (NULL == head) - { - printError(ErrNotExists, "List is empty"); - return ErrNotExists; - } - FieldNameNode *ite = head, *prev = head; - while (ite->next != NULL) - { - if (0 == strcmp(ite->fldName, name)) - { - prev->next = ite->next; - delete ite; - } - prev = ite; - ite = ite->next; - } - if( ite == head) // there is only one node in the list - { - if (0 == strcmp(ite->fldName, name)) - { - delete head; - head = NULL; - return OK; - } - - } - if( prev == head) // there are only two node in the list - { - if (0 == strcmp(ite->fldName, name)) - { - head->next = NULL; - delete ite; - return OK; - } - } - printError(ErrNotFound, "Field name %s not present in the list", name); - return ErrNotFound; -} - -DbRetVal FieldNameList::removeAll() -{ - if (NULL == head) return OK; - FieldNameNode *iter = head, *next = head; - while (iter->next != NULL) - { - next = iter->next; - delete iter; - iter = next; - } - delete iter; //deleting the last element - head = NULL; - return OK; -} - -char* FieldNameList::nextFieldName() -{ - if (iter == NULL) return NULL; - FieldNameNode *node = iter; - iter = iter ->next; - return node->fldName; -} - -int FieldNameList::size() -{ - FieldNameNode *it = head; - if (NULL == it) return 0; - int count = 1; - while (NULL != it->next) {it = it->next; count++;} - return count; -} diff --git a/src/storage/PredicateEvalImpl.cxx b/src/storage/PredicateEvalImpl.cxx deleted file mode 100644 index e7bc57ec..00000000 --- a/src/storage/PredicateEvalImpl.cxx +++ /dev/null @@ -1,419 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by www.databasecache.com * - * Contact: praba_tuty@databasecache.com * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - ***************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -DbRetVal PredicateImpl::evaluateLogical(bool &result) -{ - bool rhsResult = false, lhsResult=false; - DbRetVal retCode =OK; - result = false; - if (NULL != lhs) - { - retCode = lhs->evaluate(lhsResult); - if (retCode != OK) return ErrInvalidExpr; - }else lhsResult = true; - if (NULL != rhs) - { - retCode = rhs->evaluate(rhsResult); - if (retCode != OK) return ErrInvalidExpr; - } else rhsResult = true; - if (NULL != lhs) - { - //Means it involves only Logical operator - if (OpAnd == logicalOp) { - if (lhsResult && rhsResult) result = true; - }else if (OpOr == logicalOp) { - if (lhsResult || rhsResult) result = true; - }else if (OpNot == logicalOp){ - if (lhsResult) result = false; else result = true; - } - printDebug(DM_Predicate, "result is %d", result); - } - return OK; -} - -DbRetVal PredicateImpl::evaluateForHaving(bool &result, AggTableImpl *aImpl, void *aggElement) -{ - bool rhsResult = false, lhsResult=false; - DbRetVal retCode =OK; - result = false; - if (NULL != lhs) - { - retCode = lhs->evaluateForHaving(lhsResult, aImpl, aggElement); - if (retCode != OK) return ErrInvalidExpr; - }else lhsResult = true; - if (NULL != rhs) - { - retCode = rhs->evaluateForHaving(rhsResult, aImpl, aggElement); - if (retCode != OK) return ErrInvalidExpr; - } else rhsResult = true; - if (NULL != lhs) - { - if (OpAnd == logicalOp) { - if (lhsResult && rhsResult) result = true; - }else if (OpOr == logicalOp) { - if (lhsResult || rhsResult) result = true; - }else if (OpNot == logicalOp){ - if (lhsResult) result = false; else result = true; - } - printDebug(DM_Predicate, "result is %d", result); - return OK; - } - - void *val1 = NULL, *val2 =NULL; - int offset = aImpl->getAggOffset(fldName1, aggType); - val1 = (void*)((char*)aggElement + offset); - if (!isBindBufSet) { - //sets the type and length when it is called first time - FieldInfo *info = new FieldInfo(); - DbRetVal rv = aImpl->getFieldInfo(fldName1, info); - if (aggType == AGG_AVG) { - type = typeDouble; - length = sizeof(double); - } else if (aggType == AGG_COUNT) { - type = typeInt; - length = sizeof(int); - } else { - type = info->type; - length = info->length; - } - delete info; - isBindBufSet = true; - } - if(operand != NULL && operandPtr == NULL) - { - val2 = (char*) operand; - } - else if(operand == NULL && operandPtr != NULL) - { - val2 = *(char**)operandPtr; - } - if (aggType == AGG_AVG) { - double dVal2 = 0; - AllDataType::convertToDouble(&dVal2, val2, type); - result = AllDataType::compareVal(val1, &dVal2, compOp, typeDouble, length); - } - else if (aggType == AGG_COUNT) { - int dVal2 = 0; - AllDataType::convertToInt(&dVal2, val2, type); - result = AllDataType::compareVal(val1, &dVal2, compOp, typeInt, length); - } - else - result = AllDataType::compareVal(val1, val2, compOp, type, length); - return OK; -} - -DbRetVal PredicateImpl::evaluateLogicalForTable(bool &result, char *tuple) -{ - bool rhsResult = false, lhsResult=false; - DbRetVal retCode =OK; - result = false; - if (NULL != lhs) - { - lhs->evaluateForTable(lhsResult, tuple); - }else lhsResult = true; - if (NULL != rhs) - { - rhs->evaluateForTable(rhsResult, tuple); - } else rhsResult = true; - if (NULL != lhs) - { - //Means it involves only Logical operator - if (OpAnd == logicalOp) { - if (lhsResult && rhsResult) result = true; - }else if (OpOr == logicalOp) { - if (lhsResult || rhsResult) result = true; - }else if (OpNot == logicalOp){ - if (lhsResult) result = false; else result = true; - } - printDebug(DM_Predicate, "result is %d", result); - } - return OK; -} - -void PredicateImpl::evaluateForTable(bool &result, char *tuple) -{ - if (!isNoLeftRight) { - bool rhsResult = false; - if (NULL != rhs) - { - rhs->evaluateForTable(rhsResult, tuple); - if(rhsResult == false && OpAnd == logicalOp) {//do early return - result = false; - return; - } - } else rhsResult = true; - bool lhsResult = false; - if (NULL != lhs) - { - lhs->evaluateForTable(lhsResult, tuple); - }else lhsResult = true; - if (NULL != lhs) - { - //Means it involves only Logical operator - if (OpAnd == logicalOp) { - if (lhsResult && rhsResult) result = true; - }else if (OpOr == logicalOp) { - if (lhsResult || rhsResult) result = true; - }else if (OpNot == logicalOp){ - if (lhsResult) result = false; else result = true; - } - printDebug(DM_Predicate, "result is %d", result); - return ; - } - } - //Table null check of condition - if(lExp || rExp){ - void* val=NULL; - void* rval = NULL; - TableImpl *tImpl = (TableImpl*) table; - if(lExp){ - lExp->setTable(tImpl); - lExp->setTuple(tuple); - val = lExp->evaluateForFunction(AllDataType::getCsqlTypeFromFunctionType(lExp->getFunctionType())); - } - if(rExp) { - rExp->setTable(tImpl); - rExp->setTuple(tuple); - rval = rExp->evaluateForFunction(AllDataType::getCsqlTypeFromFunctionType(rExp->getFunctionType())); - } - if( val && rval){ - result = AllDataType::compareVal(val, rval, compOp, AllDataType::getCsqlTypeFromFunctionTypeForComparision(lExp->getFunctionType()),length); - }else if( val && operandPtr!=NULL){ - val2 = *(char**)operandPtr; - result = AllDataType::compareVal(val, val2, compOp, AllDataType::getCsqlTypeFromFunctionTypeForComparision(lExp->getFunctionType()),length); - }else if(val && (offset2 != -1 && operand == NULL && operandPtr == NULL)){ - val2 = tuple + offset2; - result = AllDataType::compareVal(val, val2, compOp, AllDataType::getCsqlTypeFromFunctionTypeForComparision(lExp->getFunctionType()),length); - }else{ - result =false; - } - - return; - } - if (isNullable) { - TableImpl *tImpl = (TableImpl*) table; - tImpl->setCurTuple(tuple); - bool isValueNull = table->isFldNull(fldPos); - if(compOp == OpIsNull) - { - if( (isValueNull && isNull) || (!isValueNull && !isNull) ) - result = true; - else - result = false; - return; - } - if(isValueNull) - { - result=false; - return; - } - } - //the below code works only for single table - val1= tuple + offset1; - if(offset2 != -1 && operand == NULL && operandPtr == NULL) - val2 = tuple + offset2; - if (!isBindBufSet) { - //Assumes that fldName2 data type is also same for expr f1 evaluate(lhsResult); - if (retCode < OK) return ErrInvalidExpr; - }else lhsResult = true; - if (NULL != rhs) - { - retCode = rhs->evaluate(rhsResult); - if (retCode < OK) return ErrInvalidExpr; - } else rhsResult = true; - if (NULL != lhs) - { - //Means it involves only Logical operator - if (OpAnd == logicalOp) { - if (lhsResult && rhsResult) result = true; - }else if (OpOr == logicalOp) { - if (lhsResult || rhsResult) result = true; - }else if (OpNot == logicalOp){ - if (lhsResult) result = false; else result = true; - if ( ErrNullValues == retCode) result = false; - } - printDebug(DM_Predicate, "result is %d", result); - return OK; - } - } - //Means it is relational expression - //first operand is always field identifier - //get the value in the tuple - if (projList) { - if (dontEvaluate) {result= true; return OK; } - if (!isBindBufSet) - { - //for join node evaluation - ListIterator fIter = projList->getIterator(); - JoinProjFieldInfo *def; - //char *val1, *val2; - while (fIter.hasElement()) - { - def = (JoinProjFieldInfo*) fIter.nextElement(); - if (NULL != def->bindBuf) { - if (0 == strcmp(fldName1, def->tabFieldName)) - { - val1 = (char*)def->bindBuf; - type = def->type; - length = def->length; - break; - } - }else{ - printError(ErrNotExists, "Field not binded %s.%s\n", - def->tableName, def->fieldName); - return ErrNotExists; - } - } - if (operand == NULL && operandPtr == NULL) - { - char fieldName2[IDENTIFIER_LENGTH]; - memset(fieldName2, 0, IDENTIFIER_LENGTH); - Table::getFieldNameAlone(fldName2, fieldName2); - if (fieldName2) { - fIter.reset(); - while (fIter.hasElement()) - { - def = (JoinProjFieldInfo*) fIter.nextElement(); - if (NULL != def->bindBuf) { - if (0 == strcmp(fldName2, def->tabFieldName)) - { - val2 = (char*)def->bindBuf; - break; - } - }else{ - printError(ErrNotExists, "Field not binded %s.%s\n", - def->tableName, def->fieldName); - return ErrNotExists; - } - } - } - } - else if(operand != NULL && operandPtr == NULL) - { - val2 = (char*) operand; - } - else if(operand == NULL && operandPtr != NULL) - { - val2 = *(char**)operandPtr; - } - isBindBufSet = true; - } - JoinTableImpl *jTable = (JoinTableImpl*) table; - if (jTable->isFldNullInt(fldName1) || jTable->isFldNullInt(fldName2)) - { - result=false; - return ErrNullValues; - } - result = AllDataType::compareVal(val1, val2, compOp, type, - length); - return OK; - - } - printf("FATAL::wrong method call\n"); - //the below code works only for single table - val1= (char*)tuple + offset1; - if(offset2 != -1 && operand == NULL && operandPtr == NULL) - val2 = ((char*)tuple) + offset2; - if (!isBindBufSet) { - //Assumes that fldName2 data type is also same for expr f1 -#include -#include -#include -#include -#include -#include -#include -#include -#include -static char aggNames[][10] = -{ - "MIN", "MAX", "SUM", "AVG", "COUNT", "" -}; - -PredicateImpl::~PredicateImpl() -{ -// if (lhs) {delete lhs; lhs = NULL; } -// if (rhs) { delete rhs; rhs = NULL; } -} - -void PredicateImpl::print(int space) -{ - char spaceBuf[IDENTIFIER_LENGTH]; - memset(spaceBuf, 32, IDENTIFIER_LENGTH); - spaceBuf[space] = '\0'; - - printf("%s \n", spaceBuf); - if (0 != strcmp(fldName1, "")) { - if (aggType == AGG_UNKNOWN) - printf("%s %s \n", spaceBuf, fldName1); - else - printf("%s %s(%s) \n", spaceBuf, - aggNames[aggType-1], fldName1); - } - if (0 != strcmp(fldName2, "")) - printf("%s %s \n", spaceBuf, fldName2); - if (compOp != OpInvalidComparisionOp) - printf("%s %s \n", spaceBuf, CompOpNames[compOp]); - if (logicalOp != OpInvalidLogicalOp) - printf("%s %s \n", spaceBuf, LogOpNames[logicalOp]); - if (operand) printf("%s VALUE \n", spaceBuf); - if (operandPtr) printf("%s VALUE \n", spaceBuf); - if (comp2Op != OpInvalidComparisionOp) - printf("%s %s \n", spaceBuf, CompOpNames[comp2Op]); - if (operand2) printf("%s VALUE \n", spaceBuf); - if (operand2Ptr) printf("%s VALUE \n", spaceBuf); - //TEMP - //printf(" %d \n", isPushedDown); - - if (lhs) { - printf("%s \n", spaceBuf); - lhs->print(space+2); - printf("%s \n", spaceBuf); - } - if (rhs) - { - printf("%s \n", spaceBuf); - rhs->print(space+2); - printf("%s \n", spaceBuf); - } - printf("%s \n", spaceBuf); - -} - -void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, - const char *fName2) -{ - strcpy(fldName1, fName1); - strcpy(fldName2, fName2); - compOp = op; - operand = NULL; - operandPtr = NULL; - lhs = rhs = NULL; - parent = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; -} - -//Operand should be of the same type of the field.This is must -void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void *opnd) -{ - strcpy(fldName1, fName1); - compOp = op; - operand = opnd; - operandPtr = NULL; - lhs = rhs = NULL; - parent = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; -} - -void PredicateImpl::setTerm(Expression *exp, ComparisionOp op, void **opnd) -{ - compOp = op; - lhs = rhs = NULL; - operandPtr = opnd; - operand = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; - lExp = exp; -} - -void PredicateImpl::setTerm(Expression *exp1, ComparisionOp op, Expression *exp2) -{ - compOp = op; - lhs = rhs = NULL; - operandPtr = NULL; - operand = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; - lExp = exp1; - rExp = exp2; -} - -void PredicateImpl::setTerm(Expression *exp, ComparisionOp op, const char *fName2 ) -{ - strcpy(fldName2, fName2); - compOp = op; - operand = NULL; - operandPtr = NULL; - lhs = rhs = NULL; - parent = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; - lExp = exp; - return; -} - -void PredicateImpl::setTerm(const char* fName1, ComparisionOp op,bool nullFlag) -{ - strcpy(fldName1, fName1); - compOp = op; - isNull = nullFlag; - lhs = rhs = NULL; - operandPtr = NULL; - operand = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; -} - -void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void **opnd) -{ - strcpy(fldName1, fName1); - compOp = op; - operand = NULL; - operandPtr = opnd; - lhs = rhs = NULL; - parent = NULL; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; -} - -void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void **opnd, AggType aType) -{ - strcpy(fldName1, fName1); - compOp = op; - operand = NULL; - operandPtr = opnd; - lhs = rhs = NULL; - parent = NULL; - aggType = aType; - logicalOp = OpInvalidLogicalOp; - comp2Op = OpInvalidComparisionOp; - operand2 =NULL; - operand2Ptr = NULL; -} - -void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void **opnd, - ComparisionOp op2, void **opnd2) -{ - strcpy(fldName1, fName1); - compOp = op; - operand = NULL; - operandPtr = opnd; - lhs = rhs = NULL; - parent = NULL; - logicalOp = OpInvalidLogicalOp; - - comp2Op = op2; - operand2=NULL; - operand2Ptr = opnd2; -} - -void PredicateImpl::setParent(PredicateImpl *pImpl) -{ - //if (parent != NULL) printf("Parent already set\n"); - parent = pImpl; - return; -} - -void PredicateImpl::setTerm(Predicate *p1, LogicalOp op, Predicate *p2 ) -{ - if (p2 == NULL && op != OpNot || op == OpNot && p2 != NULL) - { - //TODO::printError - printError(ErrBadArg, "Wrong argument passed\n"); - return; - } - lhs = (PredicateImpl*)p1; - rhs = (PredicateImpl*)p2; - logicalOp = op; - compOp = OpInvalidComparisionOp; - if (lhs != NULL) lhs->setParent(this); - if (rhs != NULL) rhs->setParent(this); - return; -} - -void PredicateImpl::setTable(Table *tbl) -{ - if (NULL != lhs) - lhs->setTable(tbl); - if (NULL != rhs) - rhs->setTable(tbl); - table = tbl; -} - -void PredicateImpl::setIfNoLeftRight() -{ - if (NULL != lhs) - lhs->setIfNoLeftRight(); - if (NULL != rhs) - rhs->setIfNoLeftRight(); - if(NULL == lhs && NULL == rhs) isNoLeftRight=true; - return; -} - -void PredicateImpl::setTuple(void *tpl) -{ - if (isNoLeftRight) { - tuple=tpl; - return; - } - if (NULL != lhs) - lhs->setTuple(tpl); - if (NULL != rhs) - rhs->setTuple(tpl); - tuple = tpl; -} - -void PredicateImpl::setProjectionList(List *lst) -{ - if (NULL != lhs) - lhs->setProjectionList(lst); - if (NULL != rhs) - rhs->setProjectionList(lst); - projList = lst; - isBindBufSet = false; -} - -bool PredicateImpl::isSingleTerm() -{ - if (NULL == lhs && NULL == rhs && comp2Op == OpInvalidComparisionOp) - return true; - return false; -} - -bool PredicateImpl::appendIfSameFld(char *fName, ComparisionOp op, void *buf) -{ - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - if (strcmp(fName,fieldName1) == 0) - { - printDebug(DM_Predicate, "Field name matched"); - /* - //switching so that in case of joins, first other conditions are - //evaluated first and then matching tuples for join is evaluated - //otherwise it may give wrong result set - if (operand) {operand2 = operand; operand2Ptr = NULL; } - if (operandPtr) {operand2Ptr = operandPtr; operand2 = NULL; } - comp2Op = compOp; - compOp = op; - operand = buf; - operandPtr = NULL; - */ - comp2Op = op; - operand2 = buf; - - return true; - } - return false; -} - -bool PredicateImpl::isIsNullInvolved() -{ - bool lhsResult = true, rhsResult = true; - if (NULL != lhs) - { - lhsResult = lhs->isIsNullInvolved(); - } - if (NULL != rhs) - { - rhsResult = rhs->isIsNullInvolved(); - } - if (NULL != lhs) - { - if (lhsResult || rhsResult) return true; - if(compOp == isNull) return true; - } - return false; -} - -bool PredicateImpl::isNotOrInvolved() -{ - bool lhsResult = true, rhsResult = true; - if (NULL != lhs) - { - lhsResult = lhs->isNotOrInvolved(); - } - if (NULL != rhs) - { - rhsResult = rhs->isNotOrInvolved(); - } - if (NULL != lhs) - { - //Means it involves only Logical operator - switch(logicalOp) - { - case OpAnd: - if (lhsResult || rhsResult) return true; else return false; - break; - case OpOr: - return true; - break; - case OpNot: - default: - return true; - break; - } - } - return false; -} - -void* PredicateImpl::getValIfPointLookupOnInt(int &offset) -{ //perf opt - if (NULL != lhs && NULL != rhs) return NULL; - if(typeInt != type || comp2Op !=OpInvalidComparisionOp) return NULL; - if (compOp != OpEquals) return NULL; - offset = offset1; - void *val =NULL; - if(operand == NULL && operandPtr != NULL) - { - val = *(void**)operandPtr; - } else if(operand != NULL && operandPtr == NULL) - { - val = (void*) operand; - } - return val; -} - -void* PredicateImpl::getVal1IfBetweenOnInt(int &offset) -{ //perf opt - if (NULL != lhs && NULL != rhs) return NULL; - if(typeInt != type) return NULL; - if (compOp != OpGreaterThanEquals || - comp2Op !=OpLessThanEquals) return NULL; - offset = offset1; - void *val =NULL; - if(operand == NULL && operandPtr != NULL) - { - val = *(void**)operandPtr; - } else if(operand != NULL && operandPtr == NULL) - { - val = (void*) operand; - } - return val; -} - -void* PredicateImpl::getVal2IfBetweenOnInt(int &offset) -{ //perf opt - if (NULL != lhs && NULL != rhs) return NULL; - if(typeInt != type) return NULL; - if (compOp != OpGreaterThanEquals || - comp2Op !=OpLessThanEquals) return NULL; - offset = offset1; - void *val =NULL; - if(operand2 == NULL && operand2Ptr != NULL) - { - val = *(void**)operand2Ptr; - } else if(operand2 != NULL && operand2Ptr == NULL) - { - val = (void*) operand2; - } - return val; -} - -void PredicateImpl::solveForProjList(Table *tab) -{ - if (NULL != lhs) - { - lhs->solveForProjList(tab); - } - if (NULL != rhs) - { - rhs->solveForProjList(tab); - } - table = tab; - if (NULL != lhs) return ; - bool isExist1=false; - bool isExist2=false; - if (projList) - { - ListIterator fIter = projList->getIterator(); - JoinProjFieldInfo *def; - while (fIter.hasElement()) - { - def = (JoinProjFieldInfo*) fIter.nextElement(); - if (!isExist1 && 0 == strcmp(fldName1, def->tabFieldName)) - { - isExist1=true; - } - if (!isExist2 && 0 == strcmp(fldName2, def->tabFieldName) ) - { - isExist2=true; - } - } - if (!isExist1) - { - tab->bindFld(fldName1, NULL); - } - if (!isExist2 && strcmp(fldName2, "")!=0) - { - tab->bindFld(fldName2, NULL); - } - } -} - -void PredicateImpl::setOffsetAndType() -{ - if (NULL != lhs) - { - lhs->setOffsetAndType(); - } - if (NULL != rhs) - { - rhs->setOffsetAndType(); - } - char fieldName1[IDENTIFIER_LENGTH]; - char fieldName2[IDENTIFIER_LENGTH]; - memset(fieldName1, 0, IDENTIFIER_LENGTH); - memset(fieldName2, 0, IDENTIFIER_LENGTH); - Table::getFieldNameAlone(fldName1, fieldName1); - Table::getFieldNameAlone(fldName2, fieldName2); - //this function is called only from TableImpl - TableImpl *tImpl = (TableImpl*) table; - if(fieldName1){ - FieldInfo *info = new FieldInfo(); - tImpl->getFieldInfo(fieldName1, info); - offset1 = tImpl->getFieldOffset(fieldName1); - fldPos = tImpl->getFldPos(fieldName1); - type = info->type; - length = info->length; - isNullable = true; - if (info->isNull || info->isPrimary || info->isAutoIncrement) - isNullable = false; - //printf("isNullable is set to %d\n", isNullable); - delete info; - } - - if(fieldName2){ - offset2 = tImpl->getFieldOffset(fieldName2); - if(typeUnknown == type) - type = tImpl->getFieldType(fieldName2); - } - -} - -bool PredicateImpl::pointLookupInvolved(const char *fname) -{ - bool rhsResult, lhsResult; - if (NULL != lhs) - { - lhsResult = lhs->pointLookupInvolved(fname); - } - if (NULL != rhs) - { - rhsResult = rhs->pointLookupInvolved(fname); - } - if (NULL != lhs) - { - //Means it involves only Logical operator - switch(logicalOp) - { - case OpAnd: - //return lhsResult; - if (lhsResult || rhsResult) return true; else return false; - break; - case OpOr: - return false; - break; - case OpNot: - default: - return false; - break; - } - } - //Means it is relational expression - //first operand is always field identifier - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - if (OpEquals == compOp) - { - //for expressions f1 == f2 use full scan, so return false - if(NULL == operand && NULL == operandPtr) return false; - if(0 == strcmp(fieldName1, fname)) - { - return true; - } - } - return false; -} - -bool PredicateImpl::isBetweenInvolved(const char *fname) -{ - bool rhsResult, lhsResult; - if (NULL != lhs) - { - lhsResult = lhs->isBetweenInvolved(fname); - } - if (NULL != rhs) - { - rhsResult = rhs->isBetweenInvolved(fname); - } - if (NULL != lhs) - { - switch(logicalOp) - { - case OpAnd: - if (lhsResult && rhsResult) return true; else return false; - break; - default: - return false; - break; - } - } - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - if ( OpGreaterThan == compOp || OpGreaterThanEquals == compOp) - { - if(0 == strcmp(fieldName1, fname)) - { - return true; - } - } - return false; -} - -bool PredicateImpl::rangeQueryInvolved(const char *fname) -{ - bool rhsResult, lhsResult; - if (NULL != lhs) - { - lhsResult = lhs->rangeQueryInvolved(fname); - } - if (NULL != rhs) - { - rhsResult = rhs->rangeQueryInvolved(fname); - } - if (NULL != lhs) - { - switch(logicalOp) - { - case OpAnd: - if (lhsResult || rhsResult) return true; else return false; - break; - case OpOr: - return false; - break; - case OpNot: - default: - return false; - break; - } - } - //Means it is relational expression - //first operand is always field identifier - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - if (OpLessThan == compOp || OpLessThanEquals == compOp || - OpGreaterThan == compOp || OpGreaterThanEquals == compOp) - { - //for expressions f1 == f2 use full scan, so return false - if(NULL == operand && NULL == operandPtr) return false; - if(0 == strcmp(fieldName1, fname)) - { - return true; - } - } - return false; -} - -void* PredicateImpl::opAndValPtrForIndexField(const char *fname, bool isUnique,ComparisionOp &op) -{ - ComparisionOp lhsOp= OpInvalidComparisionOp, rhsOp= OpInvalidComparisionOp; - void *lhsRet=NULL, *rhsRet=NULL; - if (NULL != lhs) - { - lhsRet = lhs->opAndValPtrForIndexField(fname, isUnique, lhsOp); - } - if (NULL != rhs) - { - rhsRet = rhs->opAndValPtrForIndexField(fname, isUnique,rhsOp); - } - if (lhsRet && lhsOp == OpEquals) { op = lhsOp; return lhsRet;} - if (rhsRet && rhsOp == OpEquals) { op = rhsOp; return rhsRet;} - if (NULL != lhs) - { - if( lhsRet) { op = lhsOp; return lhsRet; } - if( rhsRet) { op = rhsOp; return rhsRet; } - } - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - //Means it is relational expression - //first operand is always field identifier - if(0 == strcmp(fieldName1, fname)) - { - op = compOp; - if (isUnique && compOp != OpLessThan && - compOp != OpLessThanEquals && - compOp != OpNotEquals ) isPushedDown = true; - if (operand) return operand; else return *(void**)operandPtr; - } - op = OpInvalidComparisionOp; - return NULL; - -} - -//called only in case of hash index scan -void* PredicateImpl::valPtrForIndexField(const char *fname, bool isUnique) -{ - void *lhsRet=NULL, *rhsRet=NULL; - if (NULL != lhs) - { - lhsRet = lhs->valPtrForIndexField(fname, isUnique); - if ( lhsRet != NULL) return lhsRet; - } - if (NULL != rhs) - { - rhsRet = rhs->valPtrForIndexField(fname, isUnique); - if ( rhsRet != NULL) return rhsRet; - } - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - //Means it is relational expression - //first operand is always field identifier - if (OpEquals == compOp) - { - if(0 == strcmp(fieldName1, fname)) - { - if (isUnique) isPushedDown = true; - if (operand) return operand; else return *(void**)operandPtr; - } - } - return NULL; -} - -ComparisionOp PredicateImpl::opForIndexField(const char *fname) -{ - ComparisionOp lhsRet= OpInvalidComparisionOp, rhsRet= OpInvalidComparisionOp; - if (NULL != lhs) - { - lhsRet = lhs->opForIndexField(fname); - if ( lhsRet != OpInvalidComparisionOp) return lhsRet; - - } - if (NULL != rhs) - { - rhsRet = rhs->opForIndexField(fname); - if ( rhsRet != OpInvalidComparisionOp) return rhsRet; - } - char fieldName1[IDENTIFIER_LENGTH]; - Table::getFieldNameAlone(fldName1, fieldName1); - if(0 == strcmp(fieldName1, fname)) - { - return compOp; - } - return OpInvalidComparisionOp; -} - -PredicateImpl* PredicateImpl::getTablePredicate() -{ - PredicateImpl *lhsRet = NULL, *rhsRet = NULL; - if (NULL != lhs) - { - lhsRet = lhs->getTablePredicate(); - if ( lhsRet != NULL) return lhsRet; - } - if (NULL != rhs) - { - rhsRet = rhs->getTablePredicate(); - if ( rhsRet != NULL) return rhsRet; - } - if (operand || operandPtr ) - { - //printf("PRABA::getTablePredicate returning %s %d\n", fldName1, compOp); - if (parent) - { - if (this == parent->lhs) { - parent->lhs = NULL; - } - else { - parent->rhs = NULL; - } - parent = NULL; - } - return this; - } - return NULL; -} - -PredicateImpl* PredicateImpl::getJoinPredicate() -{ - PredicateImpl *lhsRet = NULL, *rhsRet = NULL; - if (NULL != lhs) - { - lhsRet = lhs->getJoinPredicate(); - if ( lhsRet != NULL) return lhsRet; - } - if (NULL != rhs) - { - rhsRet = rhs->getJoinPredicate(); - if ( rhsRet != NULL) return rhsRet; - } - if (0 != strcmp(fldName2, "")) - { - //printf("PRABA::getJoinPredicate returning %s %s\n", fldName1, fldName2); - if (parent) - { - if (this == parent->lhs) - parent->lhs = NULL; - else - parent->rhs = NULL; - parent = NULL; - } - return this; - } - return NULL; -} - -void PredicateImpl::removeIfNotNecessary() -{ - if (NULL != lhs) - { - lhs->removeIfNotNecessary(); - } - if (NULL != rhs) - { - rhs->removeIfNotNecessary(); - } - if (logicalOp != OpAnd) return; - if (NULL == lhs && NULL == rhs) - { - if (NULL == parent) - { - return; - } - if (this == parent->rhs) parent->rhs = NULL; - else if (this == parent->lhs) parent->lhs = NULL; - //TODO::PRABA::fix the leak below. if uncommented dumps core - //delete this; - //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG - //current object is deleted. do not any code here - return; - } - else if (NULL == lhs ) - { - //left side of the node is empty means we can remove this AND node - //and place it as left or right of my parent where i am currently placed - if (NULL == parent) - { - return; - } - if (this == parent->rhs) parent->rhs=this->rhs; - else if (this == parent->lhs) parent->lhs = this->rhs; - //TODO::PRABA::fix the leak below. if uncommented dumps core - //delete this; - //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG - //current object is deleted. do not any code here - return; - } - else if (NULL == rhs ) - { - //right side of the node is empty means we can remove this AND node - //and place it as left or right of my parent where i am currently placed - if (NULL == parent) - { - return; - } - if (this == parent->rhs) parent->rhs=this->lhs; - else if (this == parent->lhs) parent->lhs = this->lhs; - //TODO::PRABA::fix the leak below. if uncommented dumps core - //delete this; - //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG - //current object is deleted. do not any code here - return; - } - return; -} - -bool PredicateImpl::isDummyPredicate() -{ - if (NULL == lhs && NULL == rhs && NULL == parent - && NULL == operand && NULL == operandPtr && - (0 == strcmp(fldName1, "")) && (0==strcmp(fldName2, ""))) - return true; - else - return false; -} - -PredicateImpl* PredicateImpl::getIfOneSidedPredicate() -{ - if (logicalOp != OpAnd) return NULL; - if (NULL == lhs && NULL !=rhs) - { - return rhs; - } - if (NULL != lhs && NULL ==rhs) - { - return lhs; - } - return NULL; -} diff --git a/src/storage/TupleIterator.cxx b/src/storage/TupleIterator.cxx deleted file mode 100644 index c3170ece..00000000 --- a/src/storage/TupleIterator.cxx +++ /dev/null @@ -1,292 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2007 by www.databasecache.com * - * Contact: praba_tuty@databasecache.com * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - ***************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -DbRetVal TupleIterator::setPlan() -{ - PredicateImpl *predImpl = (PredicateImpl*) pred_; - if (treeIndexScan == scanType_) - { - HashIndexInfo *hIdxInfo = (HashIndexInfo*)info; - FieldIterator iter = hIdxInfo->idxFldList.getIterator(); - if(iter.hasElement()) - { - FieldDef *def = iter.nextElement(); - keyPtr = (char*)predImpl->opAndValPtrForIndexField(def->fldName_, hIdxInfo->isUnique,op); - } - CINDEX *iptr = (CINDEX*) hIdxInfo->indexPtr; - TreeNode *fstNode=(TreeNode *)iptr->hashNodeChunk_; - if(fstNode!=NULL){ - TreeNode *start = (TreeNode *)*((char**)((char*)fstNode + sizeof(TreeNode))); - tIter->set(start,(TreeNode*)iptr->hashNodeChunk_,procSlot); - }else{ - tIter->set(NULL,(TreeNode*)iptr->hashNodeChunk_,procSlot); - } - tIter->setSearchKey(keyPtr, op); - if (hIdxInfo->isUnique) tIter->setUnique(); - tIter->setFldOffset(hIdxInfo->fldOffset); - tIter->setTypeLength(hIdxInfo->type, hIdxInfo->compLength); - } - if(predImpl) predImpl->setIfNoLeftRight(); - return OK; -} - -DbRetVal TupleIterator::open() -{ - PredicateImpl *predImpl = (PredicateImpl*) pred_; - if (fullTableScan == scanType_) - { - *cIter = ((Chunk*)chunkPtr_)->getIterator(); - }else if (hashIndexScan == scanType_) - { - HashIndexInfo *hIdxInfo = (HashIndexInfo*)info; - FieldIterator iter = hIdxInfo->idxFldList.getIterator(); - int offset = hIdxInfo->fldOffset; - if(!keyBuffer) keyBuffer = (char*) malloc(hIdxInfo->compLength); - void *keyPtr = NULL; - char *keyBufferIter = keyBuffer; - while(iter.hasElement()) - { - FieldDef *def = iter.nextElement(); - //keyPtr = (void*)predImpl->valPtrForIndexField(def->fldName_,hIdxInfo->isUnique); - //TODO::PRABA::the below opt should be done for hash also - keyPtr = (void*)predImpl->valPtrForIndexField(def->fldName_,false); - if (NULL == keyPtr) { - printError(ErrSysFatal, "Fatal: Should not come here"); - continue; - } - AllDataType::copyVal(keyBufferIter, keyPtr, def->type_, def->length_); - keyBufferIter = keyBufferIter + def->length_; - } - int bucketNo = HashIndex::computeHashBucket(hIdxInfo->type, - keyBuffer, hIdxInfo->noOfBuckets, hIdxInfo->compLength); - Bucket *bucket = &(hIdxInfo->buckets[bucketNo]); - IndexNode *head = (IndexNode*) bucket->bucketList_; - if (!head) - { - bIter->setHead(head); - return OK; - } - printDebug(DM_HashIndex, "open:head for bucket %x is :%x", bucket, head); - bIter->setHead(head); - }else if (trieIndexScan == scanType_) - { - HashIndexInfo *indInfo = (HashIndexInfo*)info; - char hashValue[TRIE_MAX_LENGTH]; - FieldIterator iter = indInfo->idxFldList.getIterator(); - FieldDef *def = iter.nextElement(); - void* keyPtr = (void*)predImpl->valPtrForIndexField(def->fldName_,false); - if (NULL == keyPtr) { - printError(ErrSysFatal, "Fatal: Should not come here"); - } - TrieIndex::computeHashValues(indInfo->type, keyPtr, hashValue, indInfo->compLength); - TrieNode* start = (TrieNode*)indInfo->buckets; - if (NULL == start) - { - bIter->setHead(NULL); - return OK; - } - char **next = NULL; - int cnt = 0; - while(-1 != hashValue[cnt+1]) { - next = (char**)&(start->next_[hashValue[cnt]]); - if (! *next) - { - printError(ErrNotFound, "No trie node found \n"); - return ErrNotFound; - } - //traverse till the end - start = (TrieNode*) *next; - cnt++; - } - void **ptr = (void**)&(start->head_[hashValue[cnt]]); - IndexNode *head = (IndexNode*) *ptr; - if (!head) - { - bIter->setHead(head); - return OK; - } - bIter->setHead(head); - - }else if (treeIndexScan == scanType_) - { - HashIndexInfo *hIdxInfo = (HashIndexInfo*)info; - CINDEX *iptr = (CINDEX*) hIdxInfo->indexPtr; - TreeNode *fstNode=(TreeNode *)iptr->hashNodeChunk_; - if(fstNode!=NULL){ - TreeNode *start = (TreeNode *)*((char**)((char*)fstNode + sizeof(TreeNode))); - tIter->set(start,(TreeNode*)iptr->hashNodeChunk_,procSlot); - }else{ - tIter->set(NULL,(TreeNode*)iptr->hashNodeChunk_,procSlot); - } - if (hIdxInfo->isUnique) tIter->setUnique(); - } - isClosed = false; - return OK; -} - -//not returing previous tuple for all iterators and for tree iterator. -//it just decrements the nodeOffset for tree iterator. -void* TupleIterator::prev() -{ - PredicateImpl *predImpl = (PredicateImpl*) pred_; - void *tuple = NULL; - if (treeIndexScan == scanType_) - { - if (NULL == tIter) return NULL; - tuple = tIter->prev(); - predImpl->setTuple(tuple); - if(NULL == tuple) { - printDebug(DM_HashIndex, "prev::tuple is null"); - } - //TODO::evaluate as it is done in next() before returning - } - return tuple; -} - -void* TupleIterator::next() -{ - PredicateImpl *predImpl = (PredicateImpl*) pred_; - void *tuple = NULL; - DbRetVal rv = OK; - if (fullTableScan == scanType_) - { - if (NULL == pred_) - { - //no predicates - return cIter->nextElement(); - } - else - { - int offset=0; - bool isLargeSizeAllocator = cIter->isLargeSize(); - void *val = predImpl->getValIfPointLookupOnInt(offset); - char *tup = NULL; - if (val != NULL) { - int value = *(int*)val; - if (isLargeSizeAllocator) { - while (true) - { - tup = (char*)cIter->nextElement(); - if(NULL == tup) return NULL; - if (value == *((int*)(tup+offset))) break; - } - return tup; - }else { - tup = (char*)cIter->nextElementIntMatch(value, offset); - return tup; - } - } - val = predImpl->getVal1IfBetweenOnInt(offset); - if (val != NULL) { - void *val2 = predImpl->getVal2IfBetweenOnInt(offset); - int value1 = *(int*)val; - int value2 = *(int*)val2; - while (true) - { - if(isLargeSizeAllocator) - tup = (char*)cIter->nextElement(); - else - tup = (char*)cIter->nextElementInt(); - if(NULL == tup) return NULL; - if (*((int*)(tup+offset)) >= value1 && - *((int*)(tup+offset)) <= value2) break; - } - return tup; - } - - //evaluate till it succeeds - bool result = false; - while (!result) - { - if(isLargeSizeAllocator) - tuple = cIter->nextElement(); - else - tuple = cIter->nextElementInt(); - if(NULL == tuple) return NULL; - //predImpl->setTuple(tuple); - printDebug(DM_Table, "Evaluating the predicate from fullTableScan"); - predImpl->evaluateForTable(result, (char*)tuple); - } - } - }else if (hashIndexScan == scanType_ || trieIndexScan == scanType_) - { - //evaluate till it succeeds - bool result = false; - while (!result) - { - IndexNode *node = bIter->next(); - if (node == NULL) return NULL; - printDebug(DM_HashIndex, "next: returned IndexNode: %x", node); - tuple = node->ptrToTuple_; - if(NULL == tuple) { - printDebug(DM_HashIndex, "next::tuple is null"); - return NULL; - } - - //if (!predImpl->isSingleTerm()) { - printDebug(DM_HashIndex, "next: predicate has more than single term"); - //predImpl->setTuple(tuple); - printDebug(DM_Table, "Evaluating the predicate from hashIndexScan: has more than one term"); - predImpl->evaluateForTable(result, (char*)tuple); - //} - //else - // return tuple; - } - - }else if (treeIndexScan == scanType_) - { - if (NULL == tIter) return NULL; - bool result = false; - while (!result) - { - tuple = tIter->next(); - if(NULL == tuple) { - printDebug(DM_HashIndex, "next::tuple is null"); - return NULL; - } - //predImpl->setTuple(tuple); - predImpl->evaluateForTable(result, (char*)tuple); - if(!result && (isBetween || isPointLook)) tIter->nextNode(); - } - } - return tuple; -} - -DbRetVal TupleIterator::close() -{ - if (isClosed) return OK; - reset(); - isClosed = true; - return OK; -} - -void TupleIterator::reset() -{ - DbRetVal rv = OK; - if (scanType_ == fullTableScan) { - if (cIter) *cIter = ((Chunk*)chunkPtr_)->getIterator(); - } - else if (scanType_ == hashIndexScan) { - if(bIter) bIter->reset(); - } - else if (scanType_ == treeIndexScan) { if(tIter) tIter->reset(); } -} -- 2.11.4.GIT