00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include<Table.h>
00017 #include<Index.h>
00018 #include<CatalogTables.h>
00019 #include<Lock.h>
00020 #include<Debug.h>
00021 #include<TableImpl.h>
00022 #include<PredicateImpl.h>
00023 DbRetVal TupleIterator::open()
00024 {
00025
00026 if (fullTableScan == scanType_)
00027 {
00028 cIter = new ChunkIterator();
00029 *cIter = ((Chunk*)chunkPtr_)->getIterator();
00030 }else if (hashIndexScan == scanType_)
00031 {
00032 SingleFieldHashIndexInfo *hIdxInfo = (SingleFieldHashIndexInfo*)info;
00033 PredicateImpl *predImpl = (PredicateImpl*) pred_;
00034 bool isPtr = false;
00035 void *keyPtr = (void*)predImpl->valPtrForIndexField(hIdxInfo->fldName);
00036
00037 int bucketNo = HashIndex::computeHashBucket(hIdxInfo->type,
00038 keyPtr, hIdxInfo->noOfBuckets);
00039 Bucket *bucket = &(hIdxInfo->buckets[bucketNo]);
00040 int ret = bucket->mutex_.getLock(procSlot);
00041 if (ret != 0)
00042 {
00043 printError(ErrLockTimeOut,"Unable to acquire bucket Mutex for bucket %d",bucketNo);
00044 return ErrLockTimeOut;
00045 }
00046 HashIndexNode *head = (HashIndexNode*) bucket->bucketList_;
00047 if (!head)
00048 {
00049 bucket->mutex_.releaseLock(procSlot);
00050 bIter = NULL ;
00051 return OK;
00052 }
00053 printDebug(DM_HashIndex, "open:head for bucket %x is :%x", bucket, head);
00054 bIter = new BucketIter(head);
00055 bucket->mutex_.releaseLock(procSlot);
00056 }
00057 return OK;
00058 }
00059
00060
00061 void* TupleIterator::next()
00062 {
00063 PredicateImpl *predImpl = (PredicateImpl*) pred_;
00064 void *tuple = NULL;
00065 DbRetVal rv = OK;
00066 if (fullTableScan == scanType_)
00067 {
00068
00069 if (NULL == pred_ )
00070 {
00071
00072 return cIter->nextElement();
00073 }
00074 else
00075 {
00076
00077 bool result = false;
00078 while (!result)
00079 {
00080 tuple = cIter->nextElement();
00081 if(NULL == tuple) return NULL;
00082 predImpl->setTuple(tuple);
00083 printDebug(DM_Table, "Evaluating the predicate from fullTableScan");
00084 rv = predImpl->evaluate(result);
00085 if (rv != OK) return NULL;
00086 }
00087 }
00088 }else if (hashIndexScan == scanType_)
00089 {
00090 if (NULL == bIter)
00091 {
00092
00093 return NULL;
00094 }
00095
00096 bool result = false;
00097 while (!result)
00098 {
00099 HashIndexNode *node = bIter->next();
00100 if (node == NULL) return NULL;
00101 printDebug(DM_HashIndex, "next: returned HashIndexNode: %x", node);
00102 tuple = node->ptrToTuple_;
00103 if(NULL == tuple) {
00104 printDebug(DM_HashIndex, "next::tuple is null");
00105 return NULL;
00106 }
00107
00108
00109 printDebug(DM_HashIndex, "next: predicate has more than single term");
00110 predImpl->setTuple(tuple);
00111 printDebug(DM_Table, "Evaluating the predicate from hashIndexScan: has more than one term");
00112 rv = predImpl->evaluate(result);
00113 if (rv != OK) return NULL;
00114
00115
00116
00117 }
00118
00119 }
00120 return tuple;
00121 }
00122
00123 DbRetVal TupleIterator::close()
00124 {
00125 if (scanType_ == fullTableScan)
00126 {
00127 delete cIter;
00128 cIter = NULL;
00129 } else if (scanType_ == hashIndexScan)
00130 {
00131 delete bIter;
00132 bIter = NULL;
00133 }
00134 scanType_ = unknownScan;
00135 return OK;
00136 }