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::setPlan()
25 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
26 if (treeIndexScan
== scanType_
)
28 HashIndexInfo
*hIdxInfo
= (HashIndexInfo
*)info
;
29 FieldIterator iter
= hIdxInfo
->idxFldList
.getIterator();
32 FieldDef
*def
= iter
.nextElement();
33 keyPtr
= (char*)predImpl
->opAndValPtrForIndexField(def
->fldName_
, hIdxInfo
->isUnique
,op
);
35 CINDEX
*iptr
= (CINDEX
*) hIdxInfo
->indexPtr
;
36 TreeNode
*fstNode
=(TreeNode
*)iptr
->hashNodeChunk_
;
38 TreeNode
*start
= (TreeNode
*)*((char**)((char*)fstNode
+ sizeof(TreeNode
)));
39 tIter
->set(start
,(TreeNode
*)iptr
->hashNodeChunk_
,procSlot
);
41 tIter
->set(NULL
,(TreeNode
*)iptr
->hashNodeChunk_
,procSlot
);
43 tIter
->setSearchKey(keyPtr
, op
);
44 if (hIdxInfo
->isUnique
) tIter
->setUnique();
45 tIter
->setFldOffset(hIdxInfo
->fldOffset
);
46 tIter
->setTypeLength(hIdxInfo
->type
, hIdxInfo
->compLength
);
48 if(predImpl
) predImpl
->setIfNoLeftRight();
52 DbRetVal
TupleIterator::open()
54 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
55 if (fullTableScan
== scanType_
)
57 *cIter
= ((Chunk
*)chunkPtr_
)->getIterator();
58 }else if (hashIndexScan
== scanType_
)
60 HashIndexInfo
*hIdxInfo
= (HashIndexInfo
*)info
;
61 FieldIterator iter
= hIdxInfo
->idxFldList
.getIterator();
62 int offset
= hIdxInfo
->fldOffset
;
63 if(!keyBuffer
) keyBuffer
= (char*) malloc(hIdxInfo
->compLength
);
65 char *keyBufferIter
= keyBuffer
;
66 while(iter
.hasElement())
68 FieldDef
*def
= iter
.nextElement();
69 //keyPtr = (void*)predImpl->valPtrForIndexField(def->fldName_,hIdxInfo->isUnique);
70 //TODO::PRABA::the below opt should be done for hash also
71 keyPtr
= (void*)predImpl
->valPtrForIndexField(def
->fldName_
,false);
73 printError(ErrSysFatal
, "Fatal: Should not come here");
76 AllDataType::copyVal(keyBufferIter
, keyPtr
, def
->type_
, def
->length_
);
77 keyBufferIter
= keyBufferIter
+ def
->length_
;
79 int bucketNo
= HashIndex::computeHashBucket(hIdxInfo
->type
,
80 keyBuffer
, hIdxInfo
->noOfBuckets
, hIdxInfo
->compLength
);
81 Bucket
*bucket
= &(hIdxInfo
->buckets
[bucketNo
]);
82 IndexNode
*head
= (IndexNode
*) bucket
->bucketList_
;
88 printDebug(DM_HashIndex
, "open:head for bucket %x is :%x", bucket
, head
);
90 }else if (trieIndexScan
== scanType_
)
92 HashIndexInfo
*indInfo
= (HashIndexInfo
*)info
;
93 char hashValue
[TRIE_MAX_LENGTH
];
94 FieldIterator iter
= indInfo
->idxFldList
.getIterator();
95 FieldDef
*def
= iter
.nextElement();
96 void* keyPtr
= (void*)predImpl
->valPtrForIndexField(def
->fldName_
,false);
98 printError(ErrSysFatal
, "Fatal: Should not come here");
100 TrieIndex::computeHashValues(indInfo
->type
, keyPtr
, hashValue
, indInfo
->compLength
);
101 TrieNode
* start
= (TrieNode
*)indInfo
->buckets
;
104 bIter
->setHead(NULL
);
109 while(-1 != hashValue
[cnt
+1]) {
110 next
= (char**)&(start
->next_
[hashValue
[cnt
]]);
113 printError(ErrNotFound
, "No trie node found \n");
116 //traverse till the end
117 start
= (TrieNode
*) *next
;
120 void **ptr
= (void**)&(start
->head_
[hashValue
[cnt
]]);
121 IndexNode
*head
= (IndexNode
*) *ptr
;
124 bIter
->setHead(head
);
127 bIter
->setHead(head
);
129 }else if (treeIndexScan
== scanType_
)
131 HashIndexInfo
*hIdxInfo
= (HashIndexInfo
*)info
;
132 CINDEX
*iptr
= (CINDEX
*) hIdxInfo
->indexPtr
;
133 TreeNode
*fstNode
=(TreeNode
*)iptr
->hashNodeChunk_
;
135 TreeNode
*start
= (TreeNode
*)*((char**)((char*)fstNode
+ sizeof(TreeNode
)));
136 tIter
->set(start
,(TreeNode
*)iptr
->hashNodeChunk_
,procSlot
);
138 tIter
->set(NULL
,(TreeNode
*)iptr
->hashNodeChunk_
,procSlot
);
140 if (hIdxInfo
->isUnique
) tIter
->setUnique();
146 //not returing previous tuple for all iterators and for tree iterator.
147 //it just decrements the nodeOffset for tree iterator.
148 void* TupleIterator::prev()
150 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
152 if (treeIndexScan
== scanType_
)
154 if (NULL
== tIter
) return NULL
;
155 tuple
= tIter
->prev();
156 predImpl
->setTuple(tuple
);
158 printDebug(DM_HashIndex
, "prev::tuple is null");
160 //TODO::evaluate as it is done in next() before returning
165 void* TupleIterator::next()
167 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
170 if (fullTableScan
== scanType_
)
175 return cIter
->nextElement();
180 bool isLargeSizeAllocator
= cIter
->isLargeSize();
181 void *val
= predImpl
->getValIfPointLookupOnInt(offset
);
184 int value
= *(int*)val
;
185 if (isLargeSizeAllocator
) {
188 tup
= (char*)cIter
->nextElement();
189 if(NULL
== tup
) return NULL
;
190 if (value
== *((int*)(tup
+offset
))) break;
194 tup
= (char*)cIter
->nextElementIntMatch(value
, offset
);
198 val
= predImpl
->getVal1IfBetweenOnInt(offset
);
200 void *val2
= predImpl
->getVal2IfBetweenOnInt(offset
);
201 int value1
= *(int*)val
;
202 int value2
= *(int*)val2
;
205 if(isLargeSizeAllocator
)
206 tup
= (char*)cIter
->nextElement();
208 tup
= (char*)cIter
->nextElementInt();
209 if(NULL
== tup
) return NULL
;
210 if (*((int*)(tup
+offset
)) >= value1
&&
211 *((int*)(tup
+offset
)) <= value2
) break;
216 //evaluate till it succeeds
220 if(isLargeSizeAllocator
)
221 tuple
= cIter
->nextElement();
223 tuple
= cIter
->nextElementInt();
224 if(NULL
== tuple
) return NULL
;
225 //predImpl->setTuple(tuple);
226 printDebug(DM_Table
, "Evaluating the predicate from fullTableScan");
227 predImpl
->evaluateForTable(result
, (char*)tuple
);
230 }else if (hashIndexScan
== scanType_
|| trieIndexScan
== scanType_
)
232 //evaluate till it succeeds
236 IndexNode
*node
= bIter
->next();
237 if (node
== NULL
) return NULL
;
238 printDebug(DM_HashIndex
, "next: returned IndexNode: %x", node
);
239 tuple
= node
->ptrToTuple_
;
241 printDebug(DM_HashIndex
, "next::tuple is null");
245 //if (!predImpl->isSingleTerm()) {
246 printDebug(DM_HashIndex
, "next: predicate has more than single term");
247 //predImpl->setTuple(tuple);
248 printDebug(DM_Table
, "Evaluating the predicate from hashIndexScan: has more than one term");
249 predImpl
->evaluateForTable(result
, (char*)tuple
);
255 }else if (treeIndexScan
== scanType_
)
257 if (NULL
== tIter
) return NULL
;
261 tuple
= tIter
->next();
263 printDebug(DM_HashIndex
, "next::tuple is null");
266 //predImpl->setTuple(tuple);
267 predImpl
->evaluateForTable(result
, (char*)tuple
);
268 if(!result
&& (isBetween
|| isPointLook
)) tIter
->nextNode();
274 DbRetVal
TupleIterator::close()
276 if (isClosed
) return OK
;
282 void TupleIterator::reset()
285 if (scanType_
== fullTableScan
) {
286 if (cIter
) *cIter
= ((Chunk
*)chunkPtr_
)->getIterator();
288 else if (scanType_
== hashIndexScan
) {
289 if(bIter
) bIter
->reset();
291 else if (scanType_
== treeIndexScan
) { if(tIter
) tIter
->reset(); }