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 ***************************************************************************/
18 #include<CatalogTables.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 HashIndexInfo
*hIdxInfo
= (HashIndexInfo
*)info
;
33 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
35 FieldIterator iter
= hIdxInfo
->idxFldList
.getIterator();
37 keyBuffer
= (char*) malloc(hIdxInfo
->compLength
);
38 void *keyStartBuffer
= (void*) keyBuffer
, *keyPtr
;
39 while(iter
.hasElement())
41 FieldDef def
= iter
.nextElement();
42 keyPtr
= (void*)predImpl
->valPtrForIndexField(def
.fldName_
);
43 AllDataType::copyVal(keyBuffer
, keyPtr
, def
.type_
, def
.length_
);
44 keyBuffer
= keyBuffer
+ AllDataType::size(def
.type_
, def
.length_
);
47 int bucketNo
= HashIndex::computeHashBucket(hIdxInfo
->type
,
48 keyStartBuffer
, hIdxInfo
->noOfBuckets
, hIdxInfo
->compLength
);
50 Bucket
*bucket
= &(hIdxInfo
->buckets
[bucketNo
]);
51 int ret
= bucket
->mutex_
.getLock(procSlot
);
54 printError(ErrLockTimeOut
,"Unable to acquire bucket Mutex for bucket %d",bucketNo
);
55 return ErrLockTimeOut
;
57 HashIndexNode
*head
= (HashIndexNode
*) bucket
->bucketList_
;
60 bucket
->mutex_
.releaseLock(procSlot
);
64 printDebug(DM_HashIndex
, "open:head for bucket %x is :%x", bucket
, head
);
65 bIter
= new BucketIter(head
);
66 bucket
->mutex_
.releaseLock(procSlot
);
73 void* TupleIterator::next()
75 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
78 if (fullTableScan
== scanType_
)
84 return cIter
->nextElement();
88 //evaluate till it succeeds
92 tuple
= cIter
->nextElement();
93 if(NULL
== tuple
) return NULL
;
94 //predImpl->setTuple(tuple);
95 printDebug(DM_Table
, "Evaluating the predicate from fullTableScan");
96 rv
= predImpl
->evaluate(result
);
97 if (rv
!= OK
) return NULL
;
100 }else if (hashIndexScan
== scanType_
)
104 //if there are no nodes in bucket bIter will be null
107 //evaluate till it succeeds
111 HashIndexNode
*node
= bIter
->next();
112 if (node
== NULL
) return NULL
;
113 printDebug(DM_HashIndex
, "next: returned HashIndexNode: %x", node
);
114 tuple
= node
->ptrToTuple_
;
116 printDebug(DM_HashIndex
, "next::tuple is null");
120 //if (!predImpl->isSingleTerm()) {
121 printDebug(DM_HashIndex
, "next: predicate has more than single term");
122 //predImpl->setTuple(tuple);
123 printDebug(DM_Table
, "Evaluating the predicate from hashIndexScan: has more than one term");
124 rv
= predImpl
->evaluate(result
);
125 if (rv
!= OK
) return NULL
;
135 DbRetVal
TupleIterator::close()
137 if (scanType_
== fullTableScan
)
141 } else if (scanType_
== hashIndexScan
)
146 scanType_
= unknownScan
;