Adding new tool cacheverify to the build system
[csql.git] / src / server / TupleIterator.cxx
blobda60869657d2e308708d2b6c9e72df635eae69d9
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<Table.h>
17 #include<Index.h>
18 #include<CatalogTables.h>
19 #include<Lock.h>
20 #include<Debug.h>
21 #include<TableImpl.h>
22 #include<PredicateImpl.h>
23 DbRetVal TupleIterator::open()
26 if (fullTableScan == scanType_)
28 cIter = new ChunkIterator();
29 *cIter = ((Chunk*)chunkPtr_)->getIterator();
30 }else if (hashIndexScan == scanType_)
32 SingleFieldHashIndexInfo *hIdxInfo = (SingleFieldHashIndexInfo*)info;
33 PredicateImpl *predImpl = (PredicateImpl*) pred_;
34 bool isPtr = false;
35 void *keyPtr = (void*)predImpl->valPtrForIndexField(hIdxInfo->fldName);
37 int bucketNo = HashIndex::computeHashBucket(hIdxInfo->type,
38 keyPtr, hIdxInfo->noOfBuckets);
39 Bucket *bucket = &(hIdxInfo->buckets[bucketNo]);
40 int ret = bucket->mutex_.getLock(procSlot);
41 if (ret != 0)
43 printError(ErrLockTimeOut,"Unable to acquire bucket Mutex for bucket %d",bucketNo);
44 return ErrLockTimeOut;
46 HashIndexNode *head = (HashIndexNode*) bucket->bucketList_;
47 if (!head)
49 bucket->mutex_.releaseLock(procSlot);
50 bIter = NULL ;
51 return OK;
53 printDebug(DM_HashIndex, "open:head for bucket %x is :%x", bucket, head);
54 bIter = new BucketIter(head);
55 bucket->mutex_.releaseLock(procSlot);
57 return OK;
61 void* TupleIterator::next()
63 PredicateImpl *predImpl = (PredicateImpl*) pred_;
64 void *tuple = NULL;
65 DbRetVal rv = OK;
66 if (fullTableScan == scanType_)
69 if (NULL == pred_ )
71 //no predicates
72 return cIter->nextElement();
74 else
76 //evaluate till it succeeds
77 bool result = false;
78 while (!result)
80 tuple = cIter->nextElement();
81 if(NULL == tuple) return NULL;
82 predImpl->setTuple(tuple);
83 printDebug(DM_Table, "Evaluating the predicate from fullTableScan");
84 rv = predImpl->evaluate(result);
85 if (rv != OK) return NULL;
88 }else if (hashIndexScan == scanType_)
90 if (NULL == bIter)
92 //if there are no nodes in bucket bIter will be null
93 return NULL;
95 //evaluate till it succeeds
96 bool result = false;
97 while (!result)
99 HashIndexNode *node = bIter->next();
100 if (node == NULL) return NULL;
101 printDebug(DM_HashIndex, "next: returned HashIndexNode: %x", node);
102 tuple = node->ptrToTuple_;
103 if(NULL == tuple) {
104 printDebug(DM_HashIndex, "next::tuple is null");
105 return NULL;
108 //if (!predImpl->isSingleTerm()) {
109 printDebug(DM_HashIndex, "next: predicate has more than single term");
110 predImpl->setTuple(tuple);
111 printDebug(DM_Table, "Evaluating the predicate from hashIndexScan: has more than one term");
112 rv = predImpl->evaluate(result);
113 if (rv != OK) return NULL;
114 //}
115 //else
116 // return tuple;
120 return tuple;
123 DbRetVal TupleIterator::close()
125 if (scanType_ == fullTableScan)
127 delete cIter;
128 cIter = NULL;
129 } else if (scanType_ == hashIndexScan)
131 delete bIter;
132 bIter = NULL;
134 scanType_ = unknownScan;
135 return OK;