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>
27 DbRetVal
TableImpl::bindFld(const char *name
, void *val
)
29 //set it in the field list
30 DbRetVal rv
= fldList_
.updateBindVal(name
, val
);
32 printError(ErrNotExists
, "Field %s does not exist", name
);
38 bool TableImpl::isFldNull(const char *name
){
39 int colpos
= fldList_
.getFieldPosition(name
);
42 printError(ErrNotExists
, "Field %s does not exist", name
);
46 return isFldNull(colpos
);
49 bool TableImpl::isFldNull(int colpos
)
51 if (!curTuple_
) return false;
52 if (colpos
<1 || colpos
> numFlds_
) return false;
53 char *nullOffset
= (char*)curTuple_
- 4;
54 if (isIntUsedForNULL
) {
55 int nullVal
= *(int*)((char*)curTuple_
+ (length_
- 4));
56 if (BITSET(nullVal
, colpos
)) return true;
59 char *nullOffset
= (char*)curTuple_
- os::align(numFlds_
);
60 if (nullOffset
[colpos
-1]) return true;
64 void TableImpl::markFldNull(char const* name
)
66 int colpos
= fldList_
.getFieldPosition(name
);
69 printError(ErrNotExists
, "Field %s does not exist", name
);
75 void TableImpl::markFldNull(int fldpos
)
77 if (fldpos
<1 || fldpos
> numFlds_
) return;
78 if (isIntUsedForNULL
) {
79 if (!BITSET(iNotNullInfo
, fldpos
)) SETBIT(iNullInfo
, fldpos
);
82 if (!BITSET(iNotNullInfo
, fldpos
)) cNullInfo
[fldpos
-1] = 1;
86 void TableImpl::clearFldNull(const char *name
)
88 int colpos
= fldList_
.getFieldPosition(name
);
91 printError(ErrNotExists
, "Field %s does not exist", name
);
98 void TableImpl::clearFldNull(int colpos
)
100 if (colpos
<1 || colpos
> numFlds_
) return;
101 if (isIntUsedForNULL
) {
102 CLEARBIT(iNullInfo
, colpos
);
105 cNullInfo
[colpos
-1] = 0;
110 DbRetVal
TableImpl::execute()
114 printError(ErrAlready
,"Scan already open:Close and re execute");
117 //table ptr is set in predicate because it needs to access the
118 //type and length to evaluate
121 PredicateImpl
*pred
= (PredicateImpl
*) pred_
;
122 pred
->setTable(this);
129 printError(ErrSysInternal
,"Unable to create the plan");
130 return ErrSysInternal
;
133 iter
= new TupleIterator(pred_
, scanType_
, idxInfo
[useIndex_
], chunkPtr_
, sysDB_
->procSlot
);
134 else if (scanType_
== fullTableScan
)
135 iter
= new TupleIterator(pred_
, scanType_
, NULL
, chunkPtr_
, sysDB_
->procSlot
);
138 printError(ErrSysFatal
,"Unable to create tuple iterator");//should never happen
144 printError(ret
,"Unable to open the iterator");
151 DbRetVal
TableImpl::createPlan()
154 //will do early return here. plan is generated only when setPredicate is called.
155 if (scanType_
== unknownScan
) return ErrSysFatal
; //this should never happen
159 //if there are no predicates then go for full scan
160 //if there are no indexes then go for full scan
161 if (NULL
== pred_
|| NULL
== indexPtr_
)
163 scanType_
= fullTableScan
;
164 isPlanCreated
= true;
167 if (NULL
!= indexPtr_
)
169 PredicateImpl
*pred
= (PredicateImpl
*)pred_
;
170 printDebug(DM_Predicate
, "predicate does not involve NOT , OR operator");
171 if (!pred
->isNotOrInvolved())
173 printDebug(DM_Predicate
, "predicate does not involve NOT , OR operator");
174 for (int i
=0; i
< numIndexes_
; i
++)
176 HashIndexInfo
* info
= (HashIndexInfo
*) idxInfo
[i
];
177 FieldIterator iter
= info
->idxFldList
.getIterator();
178 while(iter
.hasElement())
180 FieldDef def
= iter
.nextElement();
181 if (pred
->pointLookupInvolved(def
.fldName_
))
183 printDebug(DM_Predicate
, "point lookup involved for field %s",def
.fldName_
);
184 scanType_
= hashIndexScan
;
185 isPlanCreated
= true;
193 }//while iter.hasElement()
194 if (useIndex_
!= -1) return OK
;
198 scanType_
= fullTableScan
;
199 isPlanCreated
= true;
203 void* TableImpl::fetch()
206 if (NULL
== curTuple_
) return curTuple_
;
207 copyValuesToBindBuffer(curTuple_
);
210 void* TableImpl::fetch(DbRetVal
&rv
)
213 if (NULL
== curTuple_
) return curTuple_
;
214 copyValuesToBindBuffer(curTuple_
);
218 void* TableImpl::fetchNoBind()
222 printError(ErrNotOpen
,"Scan not open or Scan is closed\n");
225 void *prevTuple
= curTuple_
;
226 curTuple_
= iter
->next();
227 if (NULL
== curTuple_
)
231 DbRetVal lockRet
= OK
;
232 if ((*trans
)->isoLevel_
== READ_REPEATABLE
) {
233 lockRet
= lMgr_
->getSharedLock(curTuple_
, trans
);
236 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
237 curTuple_
= prevTuple
;
242 else if ((*trans
)->isoLevel_
== READ_COMMITTED
)
244 //if iso level is read committed, operation duration lock is sufficent
245 //so release it here itself.
247 struct timeval timeout
;
248 timeout
.tv_sec
= Conf::config
.getMutexSecs();
249 timeout
.tv_usec
= Conf::config
.getMutexUSecs();
253 lockRet
= lMgr_
->isExclusiveLocked( curTuple_
, trans
, status
);
256 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
257 curTuple_
= prevTuple
;
262 if (tries
== 0) break;
263 os::select(0, 0, 0, 0, &timeout
);
268 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
269 curTuple_
= prevTuple
;
276 void* TableImpl::fetchNoBind(DbRetVal
&rv
)
281 printError(ErrNotOpen
,"Scan not open or Scan is closed\n");
285 void *prevTuple
= curTuple_
;
286 curTuple_
= iter
->next();
287 if (NULL
== curTuple_
)
291 DbRetVal lockRet
= OK
;
292 if ((*trans
)->isoLevel_
== READ_REPEATABLE
) {
293 lockRet
= lMgr_
->getSharedLock(curTuple_
, trans
);
296 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
298 curTuple_
= prevTuple
;
303 else if ((*trans
)->isoLevel_
== READ_COMMITTED
)
305 //if iso level is read committed, operation duration lock is sufficent
306 //so release it here itself.
308 struct timeval timeout
;
309 timeout
.tv_sec
= Conf::config
.getMutexSecs();
310 timeout
.tv_usec
= Conf::config
.getMutexUSecs();
314 lockRet
= lMgr_
->isExclusiveLocked( curTuple_
, trans
, status
);
317 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
318 curTuple_
= prevTuple
;
324 if (tries
== 0) break;
325 os::select(0, 0, 0, 0, &timeout
);
330 printError(lockRet
, "Unable to get the lock for the tuple %x", curTuple_
);
331 curTuple_
= prevTuple
;
339 DbRetVal
TableImpl::insertTuple()
342 void *tptr
= ((Chunk
*)chunkPtr_
)->allocate(db_
, &ret
);
345 printError(ret
, "Unable to allocate record from chunk");
348 ret
= lMgr_
->getExclusiveLock(tptr
, trans
);
351 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
352 printError(ret
, "Could not get lock for the insert tuple %x", tptr
);
353 return ErrLockTimeOut
;
357 ret
= copyValuesFromBindBuffer(tptr
);
360 printError(ret
, "Unable to copy values from bind buffer");
361 (*trans
)->removeFromHasList(db_
, tptr
);
362 lMgr_
->releaseLock(tptr
);
363 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
371 *(int*)((char*)(tptr
) + (length_
-addSize
)) = iNullInfo
;
375 addSize
= os::align(numFlds_
);
376 os::memcpy(((char*)(tptr
) + (length_
-addSize
)), cNullInfo
, addSize
);
379 //int tupleSize = length_ + addSize;
380 if (NULL
!= indexPtr_
)
384 for (i
= 0; i
< numIndexes_
; i
++)
386 ret
= insertIndexNode(*trans
, indexPtr_
[i
], idxInfo
[i
], tptr
);
387 if (ret
!= OK
) { printError(ret
, "Error in inserting to index"); break;}
389 if (i
!= numIndexes_
)
391 for (int j
= 0; j
< i
; j
++) {
392 printError(ErrWarning
, "Deleting index node");
393 deleteIndexNode(*trans
, indexPtr_
[j
], idxInfo
[j
], tptr
);
395 lMgr_
->releaseLock(tptr
);
396 (*trans
)->removeFromHasList(db_
, tptr
);
397 ((Chunk
*)chunkPtr_
)->free(db_
, tptr
);
398 printError(ret
, "Unable to insert index node for tuple %x", tptr
);
403 ret
= (*trans
)->appendUndoLog(sysDB_
, InsertOperation
, tptr
, length_
);
407 DbRetVal
TableImpl::deleteTuple()
409 if (NULL
== curTuple_
)
411 printError(ErrNotOpen
, "Scan not open: No Current tuple");
414 DbRetVal ret
= lMgr_
->getExclusiveLock(curTuple_
, trans
);
417 printError(ret
, "Could not get lock for the delete tuple %x", curTuple_
);
418 return ErrLockTimeOut
;
421 if (NULL
!= indexPtr_
)
425 for (i
= 0; i
< numIndexes_
; i
++)
427 ret
= deleteIndexNode(*trans
, indexPtr_
[i
], idxInfo
[i
], curTuple_
);
428 if (ret
!= OK
) break;
430 if (i
!= numIndexes_
)
432 for (int j
= 0; j
< i
; j
++)
433 insertIndexNode(*trans
, indexPtr_
[j
], idxInfo
[j
], curTuple_
);
434 lMgr_
->releaseLock(curTuple_
);
435 (*trans
)->removeFromHasList(db_
, curTuple_
);
436 printError(ret
, "Unable to insert index node for tuple %x", curTuple_
);
440 ((Chunk
*)chunkPtr_
)->free(db_
, curTuple_
);
442 ret
= (*trans
)->appendUndoLog(sysDB_
, DeleteOperation
, curTuple_
, length_
);
446 int TableImpl::deleteWhere()
448 int tuplesDeleted
= 0;
451 if (rv
!=OK
) return (int) rv
;
454 if (rv
!= OK
) { tuplesDeleted
= (int)rv
; break; }
455 if (NULL
== curTuple_
) break;
458 printError(rv
, "Error: Could only delete %d tuples", tuplesDeleted
);
465 return tuplesDeleted
;
468 int TableImpl::truncate()
470 //take exclusive lock on the table
471 //get the chunk ptr of the table
472 //traverse the tablechunks and free all the pages except the first one
473 //get the chunk ptr of all its indexes
474 //traverse the indexchunks and free all the pages except the first one
479 Predicate
* tmpPred
= pred_
;
481 isPlanCreated
= false;
482 int tuplesDeleted
= deleteWhere();
483 isPlanCreated
= false;
485 return tuplesDeleted
;
488 DbRetVal
TableImpl::updateTuple()
490 if (NULL
== curTuple_
)
492 printError(ErrNotOpen
, "Scan not open: No Current tuple");
495 DbRetVal ret
= lMgr_
->getExclusiveLock(curTuple_
, trans
);
498 printError(ret
, "Could not get lock for the update tuple %x", curTuple_
);
499 return ErrLockTimeOut
;
501 if (NULL
!= indexPtr_
)
504 //TODO::If it fails while updating index node, we have to undo all the updates
505 //on other indexes on the table.Currently it will leave the database in an
506 //inconsistent state.
507 for (int i
= 0; i
< numIndexes_
; i
++)
509 ret
= updateIndexNode(*trans
, indexPtr_
[i
], idxInfo
[i
], curTuple_
);
512 lMgr_
->releaseLock(curTuple_
);
513 (*trans
)->removeFromHasList(db_
, curTuple_
);
514 printError(ret
, "Unable to update index node for tuple %x", curTuple_
);
520 ret
= (*trans
)->appendUndoLog(sysDB_
, UpdateOperation
, curTuple_
, length_
);
521 if (ret
!= OK
) return ret
;
526 *(int*)((char*)(curTuple_
) + (length_
-addSize
)) |= iNullInfo
;
530 addSize
= os::align(numFlds_
);
531 //TODO::Do not do blind memcpy. It should OR each and every char
532 //os::memcpy(((char*)(curTuple_) + (length_-addSize)), cNullInfo, addSize);
536 DbRetVal rv
= copyValuesFromBindBuffer(curTuple_
, false);
538 lMgr_
->releaseLock(curTuple_
);
539 (*trans
)->removeFromHasList(db_
, curTuple_
);
545 void TableImpl::printInfo()
547 printf(" <TableName> %s </TableName>\n", tblName_
);
548 printf(" <TupleCount> %d </TupleCount>\n", numTuples());
549 printf(" <PagesUsed> %d </PagesUsed>\n", pagesUsed());
550 printf(" <SpaceUsed> %d </SpaceUsed>\n", spaceUsed());
551 printf(" <Indexes> %d <Indexes>\n", numIndexes_
);
552 printf(" <TupleLength> %d </TupleLength>\n", length_
);
553 printf(" <Fields> %d </Fields>\n", numFlds_
);
554 printf(" <Indexes>\n");
555 for (int i
=0; i
<numIndexes_
; i
++)
556 printf("<IndexName> %s </IndexName>\n", CatalogTableINDEX::getName(indexPtr_
[i
]));
557 printf(" </Indexes>\n");
561 DbRetVal
TableImpl::copyValuesFromBindBuffer(void *tuplePtr
, bool isInsert
)
563 //Iterate through the bind list and copy the value here
564 FieldIterator fIter
= fldList_
.getIterator();
565 char *colPtr
= (char*) tuplePtr
;
567 while (fIter
.hasElement())
569 FieldDef def
= fIter
.nextElement();
570 if (def
.isNull_
&& !def
.isDefault_
&& NULL
== def
.bindVal_
&& isInsert
)
572 printError(ErrNullViolation
, "NOT NULL constraint violation for field %s\n", def
.fldName_
);
573 return ErrNullViolation
;
575 if (def
.isDefault_
&& NULL
== def
.bindVal_
&& isInsert
)
577 void *dest
= AllDataType::alloc(def
.type_
, def
.length_
);
578 AllDataType::convert(typeString
, def
.defaultValueBuf_
, def
.type_
, dest
);
579 AllDataType::copyVal(colPtr
, dest
, def
.type_
, def
.length_
);
580 colPtr
= colPtr
+ os::align(AllDataType::size(def
.type_
, def
.length_
));
588 if (NULL
!= def
.bindVal_
)
590 strcpy((char*)colPtr
, (char*)def
.bindVal_
);
591 *(((char*)colPtr
) + (def
.length_
-1)) = '\0';
593 else if (!def
.isNull_
&& isInsert
) setNullBit(fldpos
);
594 colPtr
= colPtr
+ os::align(def
.length_
);
597 if (NULL
!= def
.bindVal_
)
598 os::memcpy((char*)colPtr
, (char*)def
.bindVal_
, def
.length_
);
599 else if (!def
.isNull_
&& isInsert
) setNullBit(fldpos
);
600 colPtr
= colPtr
+ os::align(def
.length_
);
603 if (NULL
!= def
.bindVal_
)
604 AllDataType::copyVal(colPtr
, def
.bindVal_
, def
.type_
);
605 else { if (!def
.isNull_
&& isInsert
) setNullBit(fldpos
); }
606 colPtr
= colPtr
+ os::align(AllDataType::size(def
.type_
));
613 void TableImpl::setNullBit(int fldpos
)
615 if (isIntUsedForNULL
)
616 SETBIT(iNullInfo
, fldpos
);
618 cNullInfo
[fldpos
-1] = 1;
620 DbRetVal
TableImpl::copyValuesToBindBuffer(void *tuplePtr
)
622 //Iterate through the bind list and copy the value here
623 FieldIterator fIter
= fldList_
.getIterator();
624 char *colPtr
= (char*) tuplePtr
;
625 while (fIter
.hasElement())
627 FieldDef def
= fIter
.nextElement();
631 if (NULL
!= def
.bindVal_
)
632 strcpy((char*)def
.bindVal_
, (char*)colPtr
);
633 colPtr
= colPtr
+ os::align(def
.length_
);
636 if (NULL
!= def
.bindVal_
)
637 os::memcpy((char*)def
.bindVal_
, (char*)colPtr
, def
.length_
);
638 colPtr
= colPtr
+ os::align(def
.length_
);
641 if (NULL
!= def
.bindVal_
)
642 AllDataType::copyVal(def
.bindVal_
, colPtr
, def
.type_
);
643 colPtr
= colPtr
+ os::align(AllDataType::size(def
.type_
));
650 //-1 index not supported
651 DbRetVal
TableImpl::insertIndexNode(Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
)
653 INDEX
*iptr
= (INDEX
*)indexPtr
;
655 printDebug(DM_Table
, "Inside insertIndexNode type %d", iptr
->indexType_
);
656 Index
* idx
= Index::getIndex(iptr
->indexType_
);
657 ret
= idx
->insert(this, tr
, indexPtr
, info
, tuple
,undoFlag
);
661 DbRetVal
TableImpl::deleteIndexNode(Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
)
663 INDEX
*iptr
= (INDEX
*)indexPtr
;
665 Index
* idx
= Index::getIndex(iptr
->indexType_
);
666 ret
= idx
->remove(this, tr
, indexPtr
, info
, tuple
, undoFlag
);
669 void TableImpl::printSQLIndexString()
671 CatalogTableINDEXFIELD
cIndexField(sysDB_
);
672 char fName
[IDENTIFIER_LENGTH
];
673 char *fldName
= fName
;
675 for (int i
= 0; i
< numIndexes_
; i
++)
677 INDEX
*iptr
= (INDEX
*) indexPtr_
[i
];
678 cIndexField
.getFieldNameAndType((void*)iptr
, fldName
, type
);
679 printf("CREATE INDEX %s on %s ( %s ) ", iptr
->indName_
, getName(), fldName
);
680 if (((HashIndexInfo
*) idxInfo
[i
])->isUnique
) printf(" UNIQUE;\n"); else printf(";\n");
685 DbRetVal
TableImpl::updateIndexNode(Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
)
687 INDEX
*iptr
= (INDEX
*)indexPtr
;
689 Index
* idx
= Index::getIndex(iptr
->indexType_
);
690 //TODO::currently it updates irrespective of whether the key changed or not
691 //because of this commenting the whole index update code. relook at it and uncomment
693 ret
= idx
->update(this, tr
, indexPtr
, info
, tuple
, undoFlag
);
699 void TableImpl::setTableInfo(char *name
, int tblid
, size_t length
,
700 int numFld
, int numIdx
, void *chunk
)
702 strcpy(tblName_
, name
);
706 numIndexes_
= numIdx
;
710 long TableImpl::spaceUsed()
712 Chunk
*chk
= (Chunk
*)chunkPtr_
;
713 long totSize
= chk
->getTotalDataNodes() * chk
->getSize();
714 totSize
= totSize
+ (chk
->totalPages() * sizeof (PageInfo
));
718 int TableImpl::pagesUsed()
720 Chunk
*chk
= (Chunk
*)chunkPtr_
;
721 return chk
->totalPages();
724 long TableImpl::numTuples()
726 return ((Chunk
*)chunkPtr_
)->getTotalDataNodes();
729 List
TableImpl::getFieldNameList()
732 FieldIterator fIter
= fldList_
.getIterator();
733 while (fIter
.hasElement())
735 FieldDef def
= fIter
.nextElement();
736 Identifier
*elem
= new Identifier();
737 strcpy(elem
->name
, def
.fldName_
);
738 fldNameList
.append(elem
);
742 DbRetVal
TableImpl::close()
746 printError(ErrNotOpen
,"Scan not open");
754 DbRetVal
TableImpl::lock(bool shared
)
760 ret = lMgr_->getSharedLock(chunkPtr_, NULL);
762 ret = lMgr_->getExclusiveLock(chunkPtr_, NULL);
765 printError(ret, "Could not exclusive lock on the table %x", chunkPtr_);
767 //do not append for S to X upgrade
768 if (!ProcessManager::hasLockList.exists(chunkPtr_))
769 ProcessManager::hasLockList.append(chunkPtr_);
774 DbRetVal
TableImpl::unlock()
777 if (!ProcessManager::hasLockList.exists(chunkPtr_)) return OK;
778 DbRetVal ret = lMgr_->releaseLock(chunkPtr_);
781 printError(ret, "Could not release exclusive lock on the table %x", chunkPtr_);
784 ProcessManager::hasLockList.remove(chunkPtr_);
790 TableImpl::~TableImpl()
792 if (NULL
!= iter
) { delete iter
; iter
= NULL
; }
793 if (NULL
!= indexPtr_
) { delete[] indexPtr_
; indexPtr_
= NULL
; }
796 for (int i
= 0; i
< numIndexes_
; i
++) delete idxInfo
[i
];
800 if (numFlds_
> 31 && cNullInfo
!= NULL
) { free(cNullInfo
); cNullInfo
= NULL
; }
802 fldList_
.removeAll();