1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
17 #include<CatalogTables.h>
23 #include<PredicateImpl.h>
26 #include<AggTableImpl.h> //for AggType
28 void Table::getFieldNameAlone(char *fname
, char *name
) {
30 char *fullname
= fname
;
31 while(*fullname
!= '\0')
33 if (*fullname
== '.') { dotFound
= true; break; }
36 if (dotFound
) strcpy(name
, ++fullname
); else strcpy(name
, fname
);
39 void Table::getTableNameAlone(char *fname
, char *name
) {
42 bool dotFound
= false;
45 if (*name
== '.') { *name
='\0'; dotFound
= true; break; }
48 if (!dotFound
) strcpy(start
, "");
52 DbRetVal
TableImpl::bindFld(const char *name
, void *val
)
54 if (name
[0] == '*' ) return OK
;
55 //set it in the field list
56 char fieldName
[IDENTIFIER_LENGTH
];
57 getFieldNameAlone((char*)name
, fieldName
);
58 DbRetVal rv
= fldList_
.updateBindVal(fieldName
, val
);
60 printError(ErrNotExists
, "Field %s does not exist", fieldName
);
66 bool TableImpl::isFldNull(const char *name
){
67 if (name
[0] == '*') return false;
68 if ( strncasecmp(name
,"COUNT",5) == 0 || strncasecmp(name
,"AVG",3) == 0 ||
69 strncasecmp(name
,"MIN",3) == 0 || strncasecmp(name
,"MAX",3) == 0 ||
70 strncasecmp(name
,"SUM",3) == 0 ) return false;
71 char fieldName
[IDENTIFIER_LENGTH
];
72 getFieldNameAlone((char*)name
, fieldName
);
73 int colpos
= fldList_
.getFieldPosition(fieldName
);
76 printError(ErrNotExists
, "Field %s does not exist", name
);
80 return isFldNull(colpos
);
83 int TableImpl::getFldPos(char *name
)
85 return fldList_
.getFieldPosition(name
);
87 void TableImpl::setAliasName(char *name
)
89 strcpy(aliasName
, name
);
91 bool TableImpl::isFldNull(int colpos
)
93 if (!curTuple_
) return false;
94 if (colpos
<1 || colpos
> numFlds_
) return false;
95 if (isIntUsedForNULL
) {
96 int nullVal
= *(int*)((char*)curTuple_
+ (length_
- 4));
97 if (BITSET(nullVal
, colpos
)) return true;
100 char *nullOffset
= (char*)curTuple_
+ (length_
- os::align(numFlds_
));
101 if (nullOffset
[colpos
-1]) return true;
105 void TableImpl::resetNullinfo()
107 if (isIntUsedForNULL
) {
112 while(i
< numFlds_
) { cNullInfo
[i
++] = 0;}
115 DbRetVal
TableImpl::markFldNull(char const* name
)
118 int colpos
= fldList_
.getFieldPosition(name
);
121 printError(ErrNotExists
, "Field %s does not exist", name
);
124 rv
= markFldNull(colpos
);
128 DbRetVal
TableImpl::markFldNull(int fldpos
)
130 if (fldpos
<1 || fldpos
> numFlds_
) return ErrBadArg
;
131 bool isBitSet
= false;
132 if (isIntUsedForNULL
) {
133 if (!BITSET(iNotNullInfo
, fldpos
)) {
134 SETBIT(iNullInfo
, fldpos
);
138 printError(ErrNullViolation
, "NOT NULL constraint violation");
139 return ErrNullViolation
;
143 if (!cNotNullInfo
[fldpos
-1]) cNullInfo
[fldpos
-1] = 1;
145 printError(ErrNullViolation
, "NOT NULL constraint violation");
146 return ErrNullViolation
;
152 void TableImpl::clearFldNull(const char *name
)
154 int colpos
= fldList_
.getFieldPosition(name
);
157 printError(ErrNotExists
, "Field %s does not exist", name
);
160 clearFldNull(colpos
);
163 void TableImpl::clearFldNull(int colpos
)
165 if (colpos
<1 || colpos
> numFlds_
) return;
166 if (isIntUsedForNULL
) {
167 CLEARBIT(iNullInfo
, colpos
);
170 cNullInfo
[colpos
-1] = 0;
174 bool TableImpl::hasIndex(char* fName
)
176 if (NULL
== indexPtr_
) return false;
177 for (int i
=0; i
< numIndexes_
; i
++)
179 HashIndexInfo
* info
= (HashIndexInfo
*) idxInfo
[i
];
180 FieldIterator iter
= info
->idxFldList
.getIterator();
181 if(iter
.hasElement())
183 FieldDef
*def
= iter
.nextElement();
184 if(strcmp(def
->fldName_
, fName
) == 0)
185 if(!iter
.hasElement())//neglet if it is composite index
192 IndexType
TableImpl::getIndexType(char *fName
, int *pos
)
194 if (NULL
== indexPtr_
) return unknownIndex
;
195 for (int i
=0; i
< numIndexes_
; i
++)
197 HashIndexInfo
* info
= (HashIndexInfo
*) idxInfo
[i
];
198 FieldIterator iter
= info
->idxFldList
.getIterator();
199 if(iter
.hasElement())
201 FieldDef
*def
= iter
.nextElement();
202 if(strcmp(def
->fldName_
, fName
) == 0)
203 if(!iter
.hasElement()) {//neglet if it is composite index
205 return info
->indType
;
212 void TableImpl::addPredicate(char *fName
, ComparisionOp op
, void *buf
)
214 char fieldName
[IDENTIFIER_LENGTH
];
215 Table::getFieldNameAlone(fName
, fieldName
);
216 PredicateImpl
*pred
= (PredicateImpl
*) pred_
;
217 PredicateImpl
*newPred
= new PredicateImpl();
218 newPred
->setTerm(fName
, op
, buf
);
219 if (NULL
== pred
) { pred_
= newPred
; predList
.append(newPred
); return; }
220 if (pred
->isSingleTerm())
222 bool res
= pred
->appendIfSameFld(fName
, op
, buf
);
228 PredicateImpl
*bothPred
= new PredicateImpl();
229 bothPred
->setTerm(pred
, OpAnd
, newPred
);
230 predList
.append(bothPred
);
234 DbRetVal
TableImpl::optimize()
236 //table ptr is set in predicate because it needs to access the
237 //type and length to evaluate
240 PredicateImpl
*pred
= (PredicateImpl
*) pred_
;
241 pred
->setTable(this);
242 pred
->setProjectionList(NULL
);
243 pred
->setOffsetAndType();
245 DbRetVal rv
= createPlan();
246 if (rv
!= OK
) return rv
;
247 if (iter
) { iter
->close(); delete iter
; iter
= NULL
; }
249 iter
= new TupleIterator(pred_
, scanType_
, idxInfo
[useIndex_
], chunkPtr_
, sysDB_
->procSlot
,isBetween
,isPointLook
,shouldNullSearch
);
250 else if (scanType_
== fullTableScan
)
251 iter
= new TupleIterator(pred_
, scanType_
, NULL
, chunkPtr_
, sysDB_
->procSlot
,isBetween
,isPointLook
,shouldNullSearch
);
254 printError(ErrSysFatal
,"Unable to create tuple iterator");
255 //should never happen
262 DbRetVal
TableImpl::execute()
264 if (iter
&& !iter
->isIterClosed())
266 //printError(ErrAlready,"Scan already open:Close and re execute");
270 if (!isPlanCreated
) ret
= optimize();
273 printError(ErrSysInternal
,"Unable to create the plan");
274 return ErrSysInternal
;
279 printError(ret
,"Unable to open the iterator");
286 DbRetVal
TableImpl::createPlan()
289 //will do early return here. plan is generated only when setPredicate is called.
290 if (scanType_
== unknownScan
) return ErrSysFatal
; //this should never happen
297 FieldIterator fIter
= fldList_
.getIterator();
298 FieldDef
*def
= NULL
;
299 while ((def
= fIter
.nextElement())!= NULL
) {
300 if (NULL
!= def
->bindVal_
) bindList_
.append(def
);
302 numBindFlds_
= bindList_
.size();
303 if (bindListArray_
) { ::free(bindListArray_
); bindListArray_
= NULL
; }
304 bindListArray_
= (void **) malloc(numBindFlds_
* sizeof (void *));
307 ListIterator it
= bindList_
.getIterator();
308 while ((elem
= it
.nextElement()) != NULL
) bindListArray_
[i
++] = elem
;
309 scanType_
= fullTableScan
;
310 isPlanCreated
= true;
312 //if there are no predicates then go for full scan
313 //if there are no indexes then go for full scan
314 if (NULL
== pred_
|| NULL
== indexPtr_
)
318 if (NULL
!= indexPtr_
)
320 PredicateImpl
*pred
= (PredicateImpl
*)pred_
;
321 //If searching for IS NULL or IS NOT NULL then fullscan
322 if(pred
->isIsNullInvolved())
324 scanType_
= fullTableScan
;
325 shouldNullSearch
=true;
328 printDebug(DM_Predicate
, "predicate does not involve NOT , OR operator");
329 if (!pred
->isNotOrInvolved())
331 printDebug(DM_Predicate
, "predicate does not involve NOT , OR operator");
332 bool isAllFldPointLookup
= true;
333 for (int i
=0; i
< numIndexes_
; i
++)
335 HashIndexInfo
* info
= (HashIndexInfo
*) idxInfo
[i
];
336 FieldIterator iter
= info
->idxFldList
.getIterator();
337 while(iter
.hasElement())
339 FieldDef
*def
= iter
.nextElement();
340 if (pred
->pointLookupInvolved(def
->fldName_
))
342 if (!isAllFldPointLookup
) break;
343 printDebug(DM_Predicate
, "point lookup involved for field %s",def
->fldName_
);
344 if(hashIndex
== info
->indType
) scanType_
= hashIndexScan
;
345 else scanType_
= treeIndexScan
;
349 else if (pred
->isBetweenInvolved(def
->fldName_
))
351 if (treeIndex
== info
->indType
)
353 scanType_
= treeIndexScan
;
356 break; //no composite index for tree index
357 } else isAllFldPointLookup
= false;
359 else if (pred
->rangeQueryInvolved(def
->fldName_
))
361 printDebug(DM_Predicate
, "range lookup involved for field %s",def
->fldName_
);
362 if (treeIndex
== info
->indType
)
364 scanType_
= treeIndexScan
;
366 break; //no composite index for tree index
367 } else isAllFldPointLookup
=false;
370 isAllFldPointLookup
= false;
373 }//while iter.hasElement()
374 if (!isAllFldPointLookup
&& useIndex_
!= -1) return OK
;
378 scanType_
= fullTableScan
;
382 void* TableImpl::fetch()
385 if (NULL
== curTuple_
) return curTuple_
;
386 copyValuesToBindBuffer(curTuple_
);
389 void* TableImpl::fetch(DbRetVal
&rv
)
392 if (NULL
== curTuple_
) return curTuple_
;
393 copyValuesToBindBuffer(curTuple_
);
397 void* TableImpl::fetchNoBind()
401 printError(ErrNotOpen
,"Scan not open or Scan is closed\n");
404 void *prevTuple
= curTuple_
;
405 curTuple_
= iter
->next();
406 if (NULL
== curTuple_
)
410 DbRetVal lockRet
= OK
;
412 if ((*trans
)->isoLevel_
== READ_COMMITTED
)
414 //if iso level is read committed, operation duration lock is sufficent
415 //so release it here itself.
416 int tries
= Conf::config
.getMutexRetries();
417 struct timeval timeout
;
418 timeout
.tv_sec
= Conf::config
.getMutexSecs();
419 timeout
.tv_usec
= Conf::config
.getMutexUSecs();
423 lockRet
= lMgr_
->isExclusiveLocked( curTuple_
, trans
, status
);
426 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
427 curTuple_
= prevTuple
;
432 if (tries
== 0) break;
433 os::select(0, 0, 0, 0, &timeout
);
437 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
438 curTuple_
= prevTuple
;
442 else if ((*trans
)->isoLevel_
== READ_REPEATABLE
) {
443 if (OK
!= trySharedLock(curTuple_
, trans
))
445 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
446 curTuple_
= prevTuple
;
454 DbRetVal
TableImpl::trySharedLock(void *curTuple
, Transaction
**trans
)
456 DbRetVal lockRet
= OK
;
457 int tries
= Conf::config
.getMutexRetries();
458 while((lockRet
= lMgr_
->getSharedLock(curTuple_
, trans
)) == ErrLockTimeOut
)
461 if (tries
<=0) break;
465 DbRetVal
TableImpl::tryExclusiveLock(void *curTuple
, Transaction
**trans
)
467 DbRetVal lockRet
= OK
;
468 int tries
= Conf::config
.getMutexRetries();
469 while((lockRet
= lMgr_
->getExclusiveLock(curTuple_
, trans
)) == ErrLockTimeOut
)
472 if (tries
<=0) break;
477 void* TableImpl::fetchNoBind(DbRetVal
&rv
)
482 printError(ErrNotOpen
,"Scan not open or Scan is closed\n");
486 void *prevTuple
= curTuple_
;
487 curTuple_
= iter
->next();
488 if (NULL
== curTuple_
)
492 DbRetVal lockRet
= OK
;
494 if ((*trans
)->isoLevel_
== READ_REPEATABLE
) {
495 lockRet
= lMgr_
->getSharedLock(curTuple_
, trans
);
498 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
500 curTuple_
= prevTuple
;
505 else if ((*trans
)->isoLevel_
== READ_COMMITTED
)
507 //if iso level is read committed, operation duration lock is sufficent
508 //so release it here itself.
509 int tries
= Conf::config
.getMutexRetries();
510 //struct timeval timeout;
511 //timeout.tv_sec = Conf::config.getMutexSecs();
512 //timeout.tv_usec = Conf::config.getMutexUSecs();
516 lockRet
= lMgr_
->isExclusiveLocked( curTuple_
, trans
, status
);
519 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
520 curTuple_
= prevTuple
;
526 if (tries
== 0) break;
527 //os::select(0, 0, 0, 0, &timeout);
531 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
532 curTuple_
= prevTuple
;
540 DbRetVal
TableImpl::fetchAgg(const char * fldName
, AggType aType
, void *buf
, bool &noRec
)
542 FieldInfo
*info
= new FieldInfo();
543 DbRetVal rv
= getFieldInfo(fldName
, info
);
544 if (OK
!= rv
) return rv
;
546 if (AGG_MIN
== aType
|| AGG_MAX
== aType
) {
548 IndexType iType
= getIndexType((char*)fldName
, &pos
);
549 if(treeIndex
== iType
&& pos
>=0) {
550 if (AGG_MIN
== aType
) {
551 HashIndexInfo
* hInfo
= (HashIndexInfo
*) idxInfo
[pos
];
552 CINDEX
*iptr
= (CINDEX
*) hInfo
->indexPtr
;
553 TreeNode
*fstNode
=(TreeNode
*)iptr
->hashNodeChunk_
;
556 TreeNode
*start
= (TreeNode
*)*((char**)((char*)fstNode
+ sizeof(TreeNode
)));
557 iter
= new TreeIter(start
,(TreeNode
*)iptr
->hashNodeChunk_
,sysDB_
->procSlot
);
559 iter
= new TreeIter(NULL
,(TreeNode
*)iptr
->hashNodeChunk_
,sysDB_
->procSlot
);
561 char *tuple
= (char*) iter
->getFirstElement();
563 AllDataType::copyVal(buf
,(void*)(tuple
+info
->offset
),
564 info
->type
, info
->length
);
568 delete iter
; iter
= NULL
;
570 else if (AGG_MAX
== aType
) {
571 HashIndexInfo
* hInfo
= (HashIndexInfo
*) idxInfo
[pos
];
572 CINDEX
*iptr
= (CINDEX
*) hInfo
->indexPtr
;
573 TreeNode
*fstNode
=(TreeNode
*)iptr
->hashNodeChunk_
;
576 TreeNode
*start
= (TreeNode
*)*((char**)((char*)fstNode
+ sizeof(TreeNode
)));
577 iter
= new TreeIter(start
,(TreeNode
*)iptr
->hashNodeChunk_
,sysDB_
->procSlot
);
579 iter
= new TreeIter(NULL
,(TreeNode
*)iptr
->hashNodeChunk_
,sysDB_
->procSlot
);
581 char *tuple
= (char*) iter
->getLastElement();
583 AllDataType::copyVal(buf
,(void*)(tuple
+info
->offset
),
584 info
->type
, info
->length
);
585 delete iter
; iter
= NULL
;
588 delete iter
; iter
=NULL
;
591 }else if (AGG_COUNT
== aType
) {
596 DataType type
= info
->type
;
597 int length
= info
->length
;
598 int offset
= info
->offset
;
599 int colPos
= fldList_
.getFieldPosition(fldName
);
600 bool isNullable
= true;
601 if (info
->isNull
|| info
->isPrimary
|| info
->isDefault
|| info
->isAutoIncrement
) {
604 int nullOffset
= length_
-4;
605 if (aType
== AGG_COUNT
) {
606 length
= sizeof(int);
609 if (NULL
== pred_
&& typeInt
== type
&& aType
!= AGG_AVG
)
611 ChunkIterator cIter
= ((Chunk
*)chunkPtr_
)->getIterator();
612 char *tuple
=(char*)cIter
.nextElement();
620 if (isIntUsedForNULL
) {
621 if (BITSET(*(int*)(tuple
+nullOffset
), colPos
)) count
=0;
625 if(isFldNull(colPos
)) count
=0;
628 if (aType
!= AGG_COUNT
)
629 AllDataType::copyVal(buf
, (void*) (tuple
+offset
), type
, length
);
632 cIter
.pageSize
= PAGE_SIZE
;
635 tuple
= (char*)cIter
.nextElementInt();
636 if (NULL
== tuple
) break;
638 if (isIntUsedForNULL
) {
639 if (BITSET(*(int*)(tuple
+nullOffset
), colPos
)) continue;
643 if(isFldNull(colPos
)) continue;
646 if (aType
== AGG_MIN
)
648 if (*(int*)buf
>= *((int*)(tuple
+offset
)))
649 *(int*)buf
= *((int*)(tuple
+offset
));
651 else if (aType
== AGG_MAX
)
653 if (*(int*)buf
<= *((int*)(tuple
+offset
)))
654 *(int*)buf
= *((int*)(tuple
+offset
));
656 else if (aType
== AGG_SUM
)
658 *(int*)buf
+= *((int*)(tuple
+offset
));
660 else if (aType
== AGG_AVG
)
662 *(int*)buf
= *(int*)buf
+ *((int*)(tuple
+offset
));
665 else if (aType
== AGG_COUNT
)
671 if( AGG_AVG
== aType
) AllDataType::divVal(buf
, &count
, type
);
672 else if (AGG_COUNT
== aType
) (*(int*)buf
) = count
;
677 char *tuple
= (char*) fetchNoBind(rv
);
678 if ( NULL
== tuple
) { noRec
= true; return OK
; }
681 while(isFldNull(colPos
)) {
682 tuple
= (char*) fetchNoBind(rv
);
683 if (aType
== AGG_COUNT
) count
++;
686 if ( NULL
== tuple
) { noRec
= true; return OK
; }
688 if (aType
== AGG_AVG
) {
689 AllDataType::convertToDouble(buf
, (void*) (tuple
+offset
), type
);
690 } else if (aType
!= AGG_COUNT
) {
691 AllDataType::copyVal(buf
, (void*) (tuple
+offset
), type
, length
);
694 tuple
= (char*) fetchNoBind(rv
);
695 if (NULL
== tuple
) break;
697 if (isIntUsedForNULL
) {
698 if (BITSET(*(int*)(tuple
+nullOffset
), colPos
)) continue;
702 if(isFldNull(colPos
)) continue;
708 res
= AllDataType::compareVal(buf
, (void*) (tuple
+offset
),
711 if (res
) AllDataType::copyVal(buf
, (void*) (tuple
+offset
),
717 res
= AllDataType::compareVal(buf
, (void*) (tuple
+offset
),
720 if (res
) AllDataType::copyVal(buf
, (void*) (tuple
+offset
),
726 AllDataType::addVal(buf
, (void*) (tuple
+offset
),
733 AllDataType::convertToDouble(&tmpBuf
, (void*) (tuple
+offset
), type
);
734 AllDataType::addVal(buf
, &tmpBuf
, typeDouble
);
748 AllDataType::divVal((double *)buf
, count
, type
);
753 (*(int*)buf
) = count
;
760 DbRetVal
TableImpl::insertTuple()
763 void *tptr
= NULL
;// ((Chunk*)chunkPtr_)->allocate(db_, &ret);
765 int totalTries
= Conf::config
.getMutexRetries();
766 while (tries
< totalTries
)
769 tptr
= ((Chunk
*)chunkPtr_
)->allocate(db_
, &ret
);
770 if (tptr
!=NULL
) break;
771 if (ret
!= ErrLockTimeOut
)
773 printError(ret
, "Unable to allocate record from chunk");
780 printError(ret
, "Unable to allocate record from chunk after %d retries", tries
);
785 TableImpl
*fkTbl
=NULL
;
786 ListIterator tblIter
= tblList
.getIterator();
788 while (tblIter
.hasElement()){
789 fkTbl
= (TableImpl
*) tblIter
.nextElement();
790 bool pkRec
= isPkTableHasRecord(fkTbl
->getName(),fkTbl
,true);
792 printError(ErrForeignKeyInsert
, "Unable to insert into foreign Key table.Check PK table");
793 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
794 return ErrForeignKeyInsert
;
800 //ret = lMgr_->getExclusiveLock(tptr, trans);
801 if (OK
!= tryExclusiveLock(tptr
, trans
))
803 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
804 printError(ret
, "Could not get lock for the insert tuple %x", tptr
);
805 return ErrLockTimeOut
;
809 ret
= copyValuesFromBindBuffer(tptr
);
812 printError(ret
, "Unable to copy values from bind buffer");
814 (*trans
)->removeFromHasList(db_
, tptr
);
815 lMgr_
->releaseLock(tptr
);
817 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
824 *(int*)((char*)(tptr
) + (length_
-addSize
)) = iNullInfo
;
828 addSize
= os::align(numFlds_
);
829 os::memcpy(((char*)(tptr
) + (length_
-addSize
)), cNullInfo
, addSize
);
832 //int tupleSize = length_ + addSize;
833 if (NULL
!= indexPtr_
)
837 for (i
= 0; i
< numIndexes_
; i
++)
839 ret
= insertIndexNode(*trans
, indexPtr_
[i
], idxInfo
[i
], tptr
);
840 if (ret
!= OK
) { printError(ret
, "Error in inserting to index %x", tptr
); break;}
844 for (int j
= 0; j
< i
; j
++) {
845 printError(ErrWarning
, "Undo:Deleting index node");
846 deleteIndexNode(*trans
, indexPtr_
[j
], idxInfo
[j
], tptr
);
849 (*trans
)->removeFromHasList(db_
, tptr
);
850 lMgr_
->releaseLock(tptr
);
852 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
857 ret
= (*trans
)->appendUndoLog(sysDB_
, InsertOperation
, tptr
, length_
);
859 printError(ret
, "Unable to create undo log for %x %d", tptr
, *(int*)tptr
);
860 for (int j
= 0; j
< numIndexes_
; j
++) {
861 printError(ErrWarning
, "Deleting index node");
862 deleteIndexNode(*trans
, indexPtr_
[j
], idxInfo
[j
], tptr
);
865 (*trans
)->removeFromHasList(db_
, tptr
);
866 lMgr_
->releaseLock(tptr
);
868 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
873 DbRetVal
TableImpl::deleteTuple()
875 if (NULL
== curTuple_
)
877 printError(ErrNotOpen
, "Scan not open: No Current tuple");
881 TableImpl
*fkTbl
=NULL
;
882 ListIterator tblIter
= tblFkList
.getIterator();
884 while (tblIter
.hasElement()){
885 fkTbl
= (TableImpl
*) tblIter
.nextElement();
886 bool pkRec
= isFkTableHasRecord(fkTbl
->getName(),fkTbl
);
888 printError(ErrForeignKeyDelete
, "A Relation Exists. Delete from child table first");
889 return ErrForeignKeyDelete
;
896 //ret = lMgr_->getExclusiveLock(curTuple_, trans);
897 if (OK
!= tryExclusiveLock(curTuple_
, trans
))
899 printError(ret
, "Could not get lock for the delete tuple %x",
901 return ErrLockTimeOut
;
905 if (NULL
!= indexPtr_
)
909 for (i
= 0; i
< numIndexes_
; i
++)
911 ret
= deleteIndexNode(*trans
, indexPtr_
[i
], idxInfo
[i
], curTuple_
);
912 if (ret
!= OK
) break;
914 if (i
!= numIndexes_
)
916 printError(ErrWarning
, "Inserting back index node");
917 for (int j
= 0; j
< i
; j
++)
918 insertIndexNode(*trans
, indexPtr_
[j
], idxInfo
[j
], curTuple_
);
920 lMgr_
->releaseLock(curTuple_
);
921 (*trans
)->removeFromHasList(db_
, curTuple_
);
923 printError(ret
, "Unable to insert index node for tuple %x", curTuple_
);
928 ret
= (*trans
)->appendUndoLog(sysDB_
, DeleteOperation
, curTuple_
, 0);
930 printError(ret
, "Unable to create undo log for %x ", curTuple_
);
931 for (int j
= 0; j
< numIndexes_
; j
++) {
932 printError(ErrWarning
, "Inserting back index node");
933 insertIndexNode(*trans
, indexPtr_
[j
], idxInfo
[j
], curTuple_
);
936 (*trans
)->removeFromHasList(db_
, curTuple_
);
937 lMgr_
->releaseLock(curTuple_
);
940 ((Chunk
*)chunkPtr_
)->free(db_
, curTuple_
);
946 int TableImpl::deleteWhere()
948 int tuplesDeleted
= 0;
951 if (rv
!=OK
) return (int) rv
;
954 if (rv
!= OK
) { tuplesDeleted
= (int)rv
; break; }
955 if (NULL
== curTuple_
) break;
958 printError(rv
, "Error: Could only delete %d tuples", tuplesDeleted
);
965 return tuplesDeleted
;
968 int TableImpl::truncate()
970 //take exclusive lock on the table
971 //get the chunk ptr of the table
972 //traverse the tablechunks and free all the pages except the first one
973 //get the chunk ptr of all its indexes
974 //traverse the indexchunks and free all the pages except the first one
979 Predicate
* tmpPred
= pred_
;
981 isPlanCreated
= false;
982 int tuplesDeleted
= deleteWhere();
983 isPlanCreated
= false;
985 return tuplesDeleted
;
988 DbRetVal
TableImpl::updateTuple()
990 if (NULL
== curTuple_
)
992 printError(ErrNotOpen
, "Scan not open: No Current tuple");
996 TableImpl
*fkTbl
=NULL
;
997 ListIterator tblIter
= tblList
.getIterator();
999 while (tblIter
.hasElement()){
1000 fkTbl
= (TableImpl
*) tblIter
.nextElement();
1001 bool pkRec
= isPkTableHasRecord(fkTbl
->getName(),fkTbl
,false);
1003 printError(ErrForeignKeyInsert
, "Unable to insert into foreign Key table.Check PK table");
1004 return ErrForeignKeyInsert
;
1012 //ret = lMgr_->getExclusiveLock(curTuple_, trans);
1013 if (OK
!= tryExclusiveLock(curTuple_
, trans
))
1015 printError(ret
, "Could not get lock for the update tuple %x", curTuple_
);
1016 return ErrLockTimeOut
;
1019 if (NULL
!= indexPtr_
)
1022 //TODO::If it fails while updating index node, we have to undo all the updates
1023 //on other indexes on the table.Currently it will leave the database in an
1024 //inconsistent state.
1025 for (int i
= 0; i
< numIndexes_
; i
++)
1027 ret
= updateIndexNode(*trans
, indexPtr_
[i
], idxInfo
[i
], curTuple_
);
1031 lMgr_
->releaseLock(curTuple_
);
1032 (*trans
)->removeFromHasList(db_
, curTuple_
);
1034 printError(ret
, "Unable to update index node for tuple %x", curTuple_
);
1040 ret
= (*trans
)->appendUndoLog(sysDB_
, UpdateOperation
, curTuple_
, length_
);
1043 lMgr_
->releaseLock(curTuple_
);
1044 (*trans
)->removeFromHasList(db_
, curTuple_
);
1049 int iNullVal
=iNullInfo
;
1053 iNullInfo
= *(int*)((char*)(curTuple_
) + (length_
- addSize
));
1057 *(int*)((char*)(curTuple_
) + (length_
-addSize
)) |= iNullInfo
;
1060 DbRetVal rv
= copyValuesFromBindBuffer(curTuple_
, false);
1061 if (rv
!= OK
&& !loadFlag
) {
1062 lMgr_
->releaseLock(curTuple_
);
1063 (*trans
)->removeFromHasList(db_
, curTuple_
);
1070 *(int*)((char*)(curTuple_
) + (length_
-addSize
)) = iNullInfo
;
1073 else iNullInfo
=iNullVal
;
1077 addSize
= os::align(numFlds_
);
1078 //TODO::Do not do blind memcpy. It should OR each and every char
1080 char *null
=(char*)(curTuple_
) + (length_
-addSize
);
1081 while(i
< numFlds_
) {
1082 if(cNullInfo
[i
]) null
[i
] |= cNullInfo
[i
];
1085 //os::memcpy(((char*)(curTuple_) + (length_-addSize)), cNullInfo, addSize);
1092 void TableImpl::printInfo()
1094 printf(" <TableName> %s </TableName>\n", tblName_
);
1095 printf(" <TupleCount> %d </TupleCount>\n", numTuples());
1096 printf(" <PagesUsed> %d </PagesUsed>\n", pagesUsed());
1097 printf(" <SpaceUsed> %d </SpaceUsed>\n", spaceUsed());
1098 printf(" <Indexes> %d <Indexes>\n", numIndexes_
);
1099 printf(" <TupleLength> %d </TupleLength>\n", length_
);
1100 printf(" <Fields> %d </Fields>\n", numFlds_
);
1101 printf(" <Indexes>\n");
1102 for (int i
=0; i
<numIndexes_
; i
++)
1103 printf("<IndexName> %s </IndexName>\n", CatalogTableINDEX::getName(indexPtr_
[i
]));
1104 printf(" </Indexes>\n");
1108 DbRetVal
TableImpl::copyValuesFromBindBuffer(void *tuplePtr
, bool isInsert
)
1110 //Iterate through the bind list and copy the value here
1111 FieldIterator fIter
= fldList_
.getIterator();
1112 char *colPtr
= (char*) tuplePtr
;
1114 while (fIter
.hasElement())
1116 FieldDef
*def
= fIter
.nextElement();
1117 if(def
->isAutoIncrement_
&& isInsert
)
1119 void *dest
= AllDataType::alloc(def
->type_
, def
->length_
);
1120 AllDataType::copyVal(dest
,ptrToAuto
, def
->type_
, def
->length_
);
1121 if(def
->bindVal_
==NULL
)
1123 AllDataType::increment(colPtr
, dest
, def
->type_
);
1124 AllDataType::copyVal(ptrToAuto
,colPtr
, def
->type_
, def
->length_
);
1125 colPtr
= colPtr
+ def
->length_
;
1130 if(AllDataType::compareVal(def
->bindVal_
, dest
, OpGreaterThan
, def
->type_
)){
1131 AllDataType::copyVal(ptrToAuto
,def
->bindVal_
, def
->type_
, def
->length_
);
1136 if (def
->isNull_
&& !def
->isDefault_
&& NULL
== def
->bindVal_
&& isInsert
)
1138 printError(ErrNullViolation
, "NOT NULL constraint violation for field %s", def
->fldName_
);
1139 return ErrNullViolation
;
1141 if (def
->isDefault_
&& NULL
== def
->bindVal_
&& isInsert
)
1143 void *dest
= AllDataType::alloc(def
->type_
, def
->length_
);
1144 AllDataType::convert(typeString
, def
->defaultValueBuf_
, def
->type_
, dest
, def
->length_
);
1145 AllDataType::copyVal(colPtr
, dest
, def
->type_
, def
->length_
);
1146 colPtr
= colPtr
+ def
->length_
;
1154 if (NULL
!= def
->bindVal_
)
1156 if(!isInsert
&& isFldNull(fldpos
)){clearNullBit(fldpos
);}
1157 strncpy((char*)colPtr
, (char*)def
->bindVal_
, def
->length_
);
1158 *(((char*)colPtr
) + (def
->length_
-1)) = '\0';
1160 else if (!def
->isNull_
&& !def
->bindVal_
&& isInsert
) setNullBit(fldpos
);
1161 colPtr
= colPtr
+ def
->length_
;
1164 if (NULL
!= def
->bindVal_
)
1166 if(!isInsert
&& isFldNull(fldpos
)){clearNullBit(fldpos
);}
1167 DbRetVal rv
= AllDataType::strToValue(colPtr
, (char *) def
->bindVal_
, def
->type_
, def
->length_
);
1168 if (rv
!= OK
) return ErrBadArg
;
1170 else if (!def
->isNull_
&& isInsert
&& !def
->bindVal_
) setNullBit(fldpos
);
1171 colPtr
= colPtr
+ def
->length_
;
1174 if (NULL
!= def
->bindVal_
){
1175 if(!isInsert
&& isFldNull(fldpos
)){clearNullBit(fldpos
);}
1176 AllDataType::copyVal(colPtr
, def
->bindVal_
, def
->type_
);}
1177 else { if (!def
->isNull_
&& isInsert
) setNullBit(fldpos
); }
1178 colPtr
= colPtr
+ def
->length_
;
1185 void TableImpl::clearNullBit(int fldpos
)
1187 if (isIntUsedForNULL
){
1188 CLEARBIT(iNullInfo
, fldpos
);}
1190 cNullInfo
[fldpos
-1] = 0;
1192 void TableImpl::setNullBit(int fldpos
)
1194 if (isIntUsedForNULL
)
1195 SETBIT(iNullInfo
, fldpos
);
1197 cNullInfo
[fldpos
-1] = 1;
1199 DbRetVal
TableImpl::copyValuesToBindBuffer(void *tuplePtr
)
1201 //Iterate through the bind list and copy the value here
1202 char *colPtr
= (char*) tuplePtr
;
1203 FieldDef
*def
= NULL
;
1204 for (int i
= 0; i
< numBindFlds_
; i
++) {
1205 def
= (FieldDef
*) bindListArray_
[i
];
1206 colPtr
= (char *) tuplePtr
+ def
->offset_
;
1207 AllDataType::copyVal(def
->bindVal_
, colPtr
, def
->type_
, def
->length_
);
1212 //-1 index not supported
1213 DbRetVal
TableImpl::insertIndexNode(Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
)
1215 CINDEX
*iptr
= (CINDEX
*)indexPtr
;
1217 printDebug(DM_Table
, "Inside insertIndexNode type %d", iptr
->indexType_
);
1218 Index
* idx
= Index::getIndex(iptr
->indexType_
);
1219 ret
= idx
->insert(this, tr
, indexPtr
, info
, tuple
,loadFlag
);
1223 DbRetVal
TableImpl::deleteIndexNode(Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
)
1225 CINDEX
*iptr
= (CINDEX
*)indexPtr
;
1227 Index
* idx
= Index::getIndex(iptr
->indexType_
);
1228 ret
= idx
->remove(this, tr
, indexPtr
, info
, tuple
, loadFlag
);
1231 void TableImpl::printSQLIndexString(FILE *fp
, int fd
)
1233 if (fp
== NULL
) fp
= stdout
;
1234 CatalogTableINDEXFIELD
cIndexField(sysDB_
);
1235 char fName
[IDENTIFIER_LENGTH
];
1236 char idxName
[IDENTIFIER_LENGTH
];
1237 char *fldName
= fName
;
1239 for (int i
= 0; i
< numIndexes_
; i
++)
1241 CINDEX
*iptr
= (CINDEX
*) indexPtr_
[i
];
1242 sprintf(idxName
,"%s_idx_Auto_increment",getName());
1243 if(strcmp(iptr
->indName_
,idxName
)==0){ continue; }
1244 if (Conf::config
.useDurability()) {
1246 strcpy(obj
.name
, iptr
->indName_
);
1247 if (iptr
->indexType_
== hashIndex
) {
1249 obj
.bucketChunk
= ((Chunk
*)iptr
->chunkPtr_
)->getFirstPage();
1250 obj
.firstPage
= ((Chunk
*)iptr
->hashNodeChunk_
)->getFirstPage();
1251 obj
.curPage
= ((Chunk
*)iptr
->hashNodeChunk_
)->getCurrentPage();
1252 } else if (iptr
->indexType_
== treeIndex
) {
1254 obj
.firstPage
= ((Chunk
*)iptr
->chunkPtr_
)->getFirstPage();
1255 obj
.curPage
= ((Chunk
*)iptr
->chunkPtr_
)->getCurrentPage();
1256 long nodes
= ((Chunk
*)iptr
->chunkPtr_
)->getTotalDataNodes();
1258 ChunkIterator cIter
= ((Chunk
*)iptr
->chunkPtr_
)->getIterator();
1259 obj
.bucketChunk
= cIter
.nextElement();
1260 } else obj
.bucketChunk
= NULL
;
1263 write(fd
, buf
, sizeof(obj
));
1265 fprintf(fp
, "CREATE INDEX %s on %s ( ", iptr
->indName_
, getName());
1267 cIndexField
.getFieldInfo(iptr
, fldList
);
1268 FieldIterator fIter
= fldList
.getIterator();
1269 bool firstFld
= true;
1270 while(fIter
.hasElement())
1272 FieldDef
*def
= fIter
.nextElement();
1273 if (firstFld
) { fprintf(fp
, " %s ", def
->fldName_
); firstFld
= false; }
1274 else fprintf(fp
, " ,%s ", def
->fldName_
);
1276 fldList
.removeAll();
1278 if (iptr
->indexType_
== hashIndex
) fprintf(fp
, " HASH ");
1279 else fprintf(fp
, " TREE ");
1280 if (((HashIndexInfo
*) idxInfo
[i
])->isUnique
) fprintf(fp
, " UNIQUE");
1281 if(((HashIndexInfo
*) idxInfo
[i
])->noOfBuckets
!= 1009 ) fprintf(fp
, " SIZE %d ",((HashIndexInfo
*) idxInfo
[i
])->noOfBuckets
);
1287 DbRetVal
TableImpl::updateIndexNode(Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
)
1289 CINDEX
*iptr
= (CINDEX
*)indexPtr
;
1291 Index
* idx
= Index::getIndex(iptr
->indexType_
);
1292 //TODO::currently it updates irrespective of whether the key changed or not
1293 //because of this commenting the whole index update code. relook at it and uncomment
1295 ret
= idx
->update(this, tr
, indexPtr
, info
, tuple
, loadFlag
);
1301 void TableImpl::setTableInfo(char *name
, int tblid
, size_t length
,
1302 int numFld
, int numIdx
, void *chunk
)
1304 strcpy(tblName_
, name
);
1308 numIndexes_
= numIdx
;
1312 long TableImpl::spaceUsed()
1314 Chunk
*chk
= (Chunk
*)chunkPtr_
;
1315 long totSize
= chk
->getTotalDataNodes() * chk
->getSize();
1316 totSize
= totSize
+ (chk
->totalPages() * sizeof (PageInfo
));
1320 int TableImpl::pagesUsed()
1322 Chunk
*chk
= (Chunk
*)chunkPtr_
;
1323 return chk
->totalPages();
1326 long TableImpl::numTuples()
1328 return ((Chunk
*)chunkPtr_
)->getTotalDataNodes();
1331 List
TableImpl::getFieldNameList()
1334 FieldIterator fIter
= fldList_
.getIterator();
1335 char fieldName
[IDENTIFIER_LENGTH
];
1336 while (fIter
.hasElement())
1338 FieldDef
*def
= fIter
.nextElement();
1339 Identifier
*elem
= new Identifier();
1340 Table::getFieldNameAlone(def
->fldName_
, fieldName
);
1341 sprintf(elem
->name
, "%s.%s", getName(), fieldName
);
1342 fldNameList
.append(elem
);
1346 DbRetVal
TableImpl::close()
1348 if (iter
) { iter
->close(); delete iter
; iter
= NULL
; }
1349 TableImpl
*fkTbl
=NULL
;
1350 ListIterator tblIter
= tblList
.getIterator();
1352 while (tblIter
.hasElement()){
1353 fkTbl
= (TableImpl
*) tblIter
.nextElement();
1357 tblIter
= tblFkList
.getIterator();
1359 while (tblIter
.hasElement()){
1360 fkTbl
= (TableImpl
*) tblIter
.nextElement();
1364 printDebug(DM_Database
,"Closing table handle: %x", this);
1367 ListIterator pIter
= predList
.getIterator();
1368 while (pIter
.hasElement())
1370 PredicateImpl
*pImpl
= (PredicateImpl
*) pIter
.nextElement();
1375 logFinest(Conf::logger
, "Closing Table");
1379 DbRetVal
TableImpl::closeScan()
1381 //do not throw scan not open error
1382 //this function will be called by table handle
1388 DbRetVal
TableImpl::lock(bool shared
)
1394 ret = lMgr_->getSharedLock(chunkPtr_, NULL);
1396 ret = lMgr_->getExclusiveLock(chunkPtr_, NULL);
1399 printError(ret, "Could not exclusive lock on the table %x", chunkPtr_);
1401 //do not append for S to X upgrade
1402 if (!ProcessManager::hasLockList.exists(chunkPtr_))
1403 ProcessManager::hasLockList.append(chunkPtr_);
1408 DbRetVal
TableImpl::unlock()
1411 if (!ProcessManager::hasLockList.exists(chunkPtr_)) return OK;
1412 DbRetVal ret = lMgr_->releaseLock(chunkPtr_);
1415 printError(ret, "Could not release exclusive lock on the table %x", chunkPtr_);
1418 ProcessManager::hasLockList.remove(chunkPtr_);
1424 TableImpl::~TableImpl()
1426 if (NULL
!= iter
) { delete iter
; iter
= NULL
; }
1427 if (NULL
!= indexPtr_
) { delete[] indexPtr_
; indexPtr_
= NULL
; }
1428 if (NULL
!= idxInfo
)
1430 for (int i
= 0; i
< numIndexes_
; i
++) delete idxInfo
[i
];
1434 if (numFlds_
> 31 && cNullInfo
!= NULL
) { free(cNullInfo
); cNullInfo
= NULL
; }
1435 if (bindList_
.size()) bindList_
.reset();
1436 if (bindListArray_
) { free (bindListArray_
); bindListArray_
= NULL
; }
1437 fldList_
.removeAll();
1441 void *TableImpl::getBindFldAddr(const char *name
)
1443 return fldList_
.getBindField(name
);
1445 bool TableImpl::isTableInvolved(char *tblName
)
1447 //printf("Table isTableInvolved called for %s with %s\n", tblName, getName());
1448 if (0 == strcmp(getName(), tblName
)) return true; else return false;
1450 bool TableImpl::pushPredicate(Predicate
*pred
)
1453 PredicateImpl
*pImpl
= (PredicateImpl
*) pred
;
1454 char tableName
[IDENTIFIER_LENGTH
];
1455 Table::getTableNameAlone(pImpl
->getFldName1(), tableName
);
1456 //printf("predicate tbl name %s\n", tableName);
1458 //if predicate is of form t1.f1=t2.f1 then do not push here
1459 if (0 != strcmp(pImpl
->getFldName2(),"")) return ret
;
1461 if (0 == strcmp(getName(), tableName
))
1464 //printf("PRABA::pushed predicate in tablehdl %s\n", getName());
1470 void TableImpl::setCondition(Condition
*p
)
1472 isPlanCreated
= false;
1473 ListIterator pIter
= predList
.getIterator();
1474 while (pIter
.hasElement())
1476 PredicateImpl
*pImpl
= (PredicateImpl
*) pIter
.nextElement();
1481 if (p
) pred_
= p
->getPredicate(); else pred_
= NULL
;
1484 void TableImpl::setPredicate(Predicate
*pred
)
1486 if (NULL
== pred_
) { pred_
= pred
; return; }
1488 Predicate
*curPred
= pred_
;
1489 PredicateImpl
*newPred
= new PredicateImpl();
1490 newPred
->setTerm(curPred
, OpAnd
, pred
);
1491 newPred
->setTable(this);
1495 void TableImpl::printPlan(int space
)
1497 char spaceBuf
[IDENTIFIER_LENGTH
];
1498 memset(spaceBuf
, 32, IDENTIFIER_LENGTH
);
1499 spaceBuf
[space
] = '\0';
1500 printf("%s <TABLE-NODE>\n", spaceBuf
);
1501 printf("%s <NAME> %s </NAME>\n", spaceBuf
, getName());
1502 printf("%s <ScanType> %s </ScanType>\n", spaceBuf
, ScanTypeNames
[scanType_
]);
1503 PredicateImpl
*pred
= (PredicateImpl
*)pred_
;
1504 if (pred
) pred
->print(space
+2);
1505 printf("%s </TABLE-NODE>\n", spaceBuf
);
1507 void TableImpl::printSQLForeignString()
1510 FieldNameList pkFieldList
,fkFieldList
;
1512 void *tFkptr
= NULL
;
1513 void *chunkPk
= NULL
;
1514 CatalogTableTABLE
cTable(sysDB_
);
1515 TableImpl
*fkTbl
=NULL
;
1516 ListIterator tblIter
= tblList
.getIterator();
1519 while (tblIter
.hasElement()){
1520 fkTbl
= (TableImpl
*) tblIter
.nextElement();
1521 rv
= cTable
.getChunkAndTblPtr(fkTbl
->getName(), chunkPk
, tPkptr
);
1522 if ( OK
!= rv
){return ;}
1523 rv
= cTable
.getChunkAndTblPtr(getName(), chunkPk
, tFkptr
);
1524 if ( OK
!= rv
){return ;}
1525 CatalogTableFK
cFk(sysDB_
);
1526 rv
= cFk
.getPkFkFieldInfo(tPkptr
,tFkptr
,pkFieldList
,fkFieldList
);
1527 if ( OK
!= rv
){return;}
1528 pkFieldList
.resetIter();
1529 fkFieldList
.resetIter();
1530 char *fldName
= NULL
;
1531 bool firstField
=true;
1532 if(!firstFK
) printf(", ");
1533 printf(", FOREIGN KEY ( ");
1534 while((fldName
= fkFieldList
.nextFieldName())!= NULL
)
1537 printf("%s",fldName
);
1541 printf(",%s",fldName
);
1543 printf(" ) REFERENCES %s ( ",fkTbl
->getName());
1545 while((fldName
= pkFieldList
.nextFieldName())!= NULL
)
1548 printf("%s",fldName
);
1552 printf(",%s",fldName
);
1556 pkFieldList
.removeAll();
1557 fkFieldList
.removeAll();
1561 bool TableImpl::isPkTableHasRecord(char *pkTableName
, TableImpl
*fkTbl
,bool isInsert
)
1564 bool isRecExist
=false;
1565 FieldNameList pkFieldList
,fkFieldList
;
1567 void *tFkptr
= NULL
;
1568 void *chunkPk
= NULL
;
1569 CatalogTableTABLE
cTable(sysDB_
);
1570 rv
= cTable
.getChunkAndTblPtr(pkTableName
, chunkPk
, tPkptr
);
1571 if ( OK
!= rv
){return false;}
1572 rv
= cTable
.getChunkAndTblPtr(getName(), chunkPk
, tFkptr
);
1573 if ( OK
!= rv
){return false;}
1574 CatalogTableFK
cFk(sysDB_
);
1575 rv
= cFk
.getPkFkFieldInfo(tPkptr
,tFkptr
,pkFieldList
,fkFieldList
);
1576 if ( OK
!= rv
){return false;}
1577 int totFld
= pkFieldList
.size();
1578 Condition
*condition
= new Condition
[totFld
];
1579 char *pkFldName
= NULL
;
1580 char *fkFldName
= NULL
;
1583 pkFieldList
.resetIter();
1584 fkFieldList
.resetIter();
1586 while((pkFldName
= pkFieldList
.nextFieldName())!= NULL
)
1588 fkFldName
= fkFieldList
.nextFieldName();
1589 FieldIterator fIter
= fldList_
.getIterator();
1590 while (fIter
.hasElement())
1592 def
= fIter
.nextElement();
1593 if (strcmp(def
->fldName_
, fkFldName
) == 0)
1595 if(NULL
== def
->bindVal_
&& isInsert
) { return true; }
1596 if(NULL
== def
->bindVal_
) {
1597 val
= (char*)curTuple_
+ def
->offset_
;
1599 val
= def
->bindVal_
;
1601 if(def
->type_
==typeString
)
1602 condition
[i
].setTerm(pkFldName
,OpEquals
,&val
);
1604 condition
[i
].setTerm(pkFldName
,OpEquals
,val
);
1610 pkFieldList
.removeAll();
1611 fkFieldList
.removeAll();
1612 Condition
*cond
= NULL
;
1613 if(i
== 0 && !isInsert
)return true;
1615 cond
= new Condition
[i
-1];
1619 for(j
=0;j
<totcon
-1;j
++)
1622 cond
[j
].setTerm(condition
[i
++].getPredicate(),OpAnd
,condition
[i
++].getPredicate());
1624 cond
[j
].setTerm(cond
[j
-1].getPredicate(), OpAnd
, condition
[i
++].getPredicate());
1626 fkTbl
->setCondition(&cond
[j
-1]);
1629 fkTbl
->setCondition(&condition
[i
-1]);
1643 bool TableImpl::isFkTableHasRecord(char *pkTableName
, TableImpl
*fkTbl
)
1646 FieldNameList pkFieldList
,fkFieldList
;
1648 void *tFkptr
= NULL
;
1649 void *chunkPk
= NULL
;
1650 CatalogTableTABLE
cTable(sysDB_
);
1651 rv
= cTable
.getChunkAndTblPtr(getName(), chunkPk
, tPkptr
);
1652 if ( OK
!= rv
){return false;}
1653 rv
= cTable
.getChunkAndTblPtr(pkTableName
, chunkPk
, tFkptr
);
1654 if ( OK
!= rv
){return false;}
1655 CatalogTableFK
cFk(sysDB_
);
1656 rv
= cFk
.getPkFkFieldInfo(tPkptr
,tFkptr
,pkFieldList
,fkFieldList
);
1657 if ( OK
!= rv
){return false;}
1658 int totFld
= pkFieldList
.size();
1659 Condition
*condition
= new Condition
[totFld
];
1660 char *pkFldName
= NULL
;
1661 char *fkFldName
= NULL
;
1664 pkFieldList
.resetIter();
1665 fkFieldList
.resetIter();
1666 while((pkFldName
= pkFieldList
.nextFieldName())!= NULL
)
1668 fkFldName
= fkFieldList
.nextFieldName();
1669 FieldIterator fIter
= fldList_
.getIterator();
1670 while (fIter
.hasElement())
1672 def
= fIter
.nextElement();
1673 void *val
= (char*)curTuple_
+ def
->offset_
;
1674 if (strcmp(def
->fldName_
, pkFldName
) == 0)
1676 if(def
->type_
==typeString
)
1677 condition
[i
].setTerm(fkFldName
,OpEquals
,&val
);//((char*)curTuple_+def->offset_));
1679 condition
[i
].setTerm(fkFldName
,OpEquals
,val
);//((char*)curTuple_+def->offset_));
1685 pkFieldList
.removeAll();
1686 fkFieldList
.removeAll();
1687 if(i
== 0 )return true;
1688 Condition
*cond
= new Condition
[i
-1];
1691 for(j
=0;j
<totFld
-1;j
++)
1694 cond
[j
].setTerm(condition
[i
++].getPredicate(),OpAnd
,condition
[i
++].getPredicate());
1696 cond
[j
].setTerm(cond
[j
-1].getPredicate(), OpAnd
, condition
[i
++].getPredicate());
1699 fkTbl
->setCondition(&condition
[totFld
-1]);
1701 fkTbl
->setCondition(&cond
[j
-1]);
1713 DbRetVal
TableImpl::compact()
1716 int ret
=((Chunk
*)chunkPtr_
)->compact(db_
->procSlot
);
1718 return ErrLockTimeOut
;
1720 if (NULL
!= indexPtr_
)
1724 for (i
= 0; i
< numIndexes_
; i
++)
1726 rv
= compactIndexNode(indexPtr_
[i
]);
1727 if (rv
!= OK
) { printError(rv
, "Error in compacting index Node"); break;}
1733 DbRetVal
TableImpl::compactIndexNode( void *indexPtr
)
1735 CINDEX
*iptr
= (CINDEX
*)indexPtr
;
1737 printDebug(DM_Table
, "Inside insertIndexNode type %d", iptr
->indexType_
);
1738 if( hashIndex
== (iptr
->indexType_
) )
1740 ret1
=((Chunk
*)iptr
->hashNodeChunk_
)->compact(db_
->procSlot
);
1742 return ErrLockTimeOut
;
1746 ret1
=((Chunk
*)iptr
->chunkPtr_
)->compact(db_
->procSlot
);
1748 return ErrLockTimeOut
;