src/server/TupleIterator.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
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             //no predicates
00072             return cIter->nextElement();
00073         }
00074         else
00075         {
00076             //evaluate till it succeeds
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             //if there are no nodes in bucket bIter will be null
00093             return NULL;
00094         }
00095         //evaluate till it succeeds
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             //if (!predImpl->isSingleTerm()) {
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             //else 
00116             //    return tuple;
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 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7