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();
51 DbRetVal
TupleIterator::open()
53 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
54 if (fullTableScan
== scanType_
)
56 *cIter
= ((Chunk
*)chunkPtr_
)->getIterator();
57 }else if (hashIndexScan
== scanType_
)
59 HashIndexInfo
*hIdxInfo
= (HashIndexInfo
*)info
;
60 FieldIterator iter
= hIdxInfo
->idxFldList
.getIterator();
61 int offset
= hIdxInfo
->fldOffset
;
62 if(!keyBuffer
) keyBuffer
= (char*) malloc(hIdxInfo
->compLength
);
64 char *keyBufferIter
= keyBuffer
;
65 while(iter
.hasElement())
67 FieldDef
*def
= iter
.nextElement();
68 //keyPtr = (void*)predImpl->valPtrForIndexField(def->fldName_,hIdxInfo->isUnique);
69 //TODO::PRABA::the below opt should be done for hash also
70 keyPtr
= (void*)predImpl
->valPtrForIndexField(def
->fldName_
,false);
72 printError(ErrSysFatal
, "Fatal: Should not come here");
75 AllDataType::copyVal(keyBufferIter
, keyPtr
, def
->type_
, def
->length_
);
76 keyBufferIter
= keyBufferIter
+ def
->length_
;
78 int bucketNo
= HashIndex::computeHashBucket(hIdxInfo
->type
,
79 keyBuffer
, hIdxInfo
->noOfBuckets
, hIdxInfo
->compLength
);
80 Bucket
*bucket
= &(hIdxInfo
->buckets
[bucketNo
]);
81 IndexNode
*head
= (IndexNode
*) bucket
->bucketList_
;
87 printDebug(DM_HashIndex
, "open:head for bucket %x is :%x", bucket
, head
);
89 }else if (trieIndexScan
== scanType_
)
91 HashIndexInfo
*indInfo
= (HashIndexInfo
*)info
;
92 char hashValue
[TRIE_MAX_LENGTH
];
93 FieldIterator iter
= indInfo
->idxFldList
.getIterator();
94 FieldDef
*def
= iter
.nextElement();
95 void* keyPtr
= (void*)predImpl
->valPtrForIndexField(def
->fldName_
,false);
97 printError(ErrSysFatal
, "Fatal: Should not come here");
99 TrieIndex::computeHashValues(indInfo
->type
, keyPtr
, hashValue
, indInfo
->compLength
);
100 TrieNode
* start
= (TrieNode
*)indInfo
->buckets
;
103 bIter
->setHead(NULL
);
108 while(-1 != hashValue
[cnt
+1]) {
109 next
= (char**)&(start
->next_
[hashValue
[cnt
]]);
112 printError(ErrNotFound
, "No trie node found \n");
115 //traverse till the end
116 start
= (TrieNode
*) *next
;
119 void **ptr
= (void**)&(start
->head_
[hashValue
[cnt
]]);
120 IndexNode
*head
= (IndexNode
*) *ptr
;
123 bIter
->setHead(head
);
126 bIter
->setHead(head
);
128 }else if (treeIndexScan
== scanType_
)
130 HashIndexInfo
*hIdxInfo
= (HashIndexInfo
*)info
;
131 CINDEX
*iptr
= (CINDEX
*) hIdxInfo
->indexPtr
;
132 TreeNode
*fstNode
=(TreeNode
*)iptr
->hashNodeChunk_
;
134 TreeNode
*start
= (TreeNode
*)*((char**)((char*)fstNode
+ sizeof(TreeNode
)));
135 tIter
->set(start
,(TreeNode
*)iptr
->hashNodeChunk_
,procSlot
);
137 tIter
->set(NULL
,(TreeNode
*)iptr
->hashNodeChunk_
,procSlot
);
139 if (hIdxInfo
->isUnique
) tIter
->setUnique();
145 //not returing previous tuple for all iterators and for tree iterator.
146 //it just decrements the nodeOffset for tree iterator.
147 void* TupleIterator::prev()
149 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
151 if (treeIndexScan
== scanType_
)
153 if (NULL
== tIter
) return NULL
;
154 tuple
= tIter
->prev();
155 predImpl
->setTuple(tuple
);
157 printDebug(DM_HashIndex
, "prev::tuple is null");
159 //TODO::evaluate as it is done in next() before returning
164 void* TupleIterator::next()
166 PredicateImpl
*predImpl
= (PredicateImpl
*) pred_
;
169 if (fullTableScan
== scanType_
)
174 return cIter
->nextElement();
179 bool isLargeSizeAllocator
= cIter
->isLargeSize();
180 void *val
= predImpl
->getValIfPointLookupOnInt(offset
);
183 int value
= *(int*)val
;
184 if (isLargeSizeAllocator
) {
187 tup
= (char*)cIter
->nextElement();
188 if(NULL
== tup
) return NULL
;
189 if (value
== *((int*)(tup
+offset
))) break;
193 tup
= (char*)cIter
->nextElementIntMatch(value
, offset
);
197 val
= predImpl
->getVal1IfBetweenOnInt(offset
);
199 void *val2
= predImpl
->getVal2IfBetweenOnInt(offset
);
200 int value1
= *(int*)val
;
201 int value2
= *(int*)val2
;
204 if(isLargeSizeAllocator
)
205 tup
= (char*)cIter
->nextElement();
207 tup
= (char*)cIter
->nextElementInt();
208 if(NULL
== tup
) return NULL
;
209 if (*((int*)(tup
+offset
)) >= value1
&&
210 *((int*)(tup
+offset
)) <= value2
) break;
215 //evaluate till it succeeds
219 if(isLargeSizeAllocator
)
220 tuple
= cIter
->nextElement();
222 tuple
= cIter
->nextElementInt();
223 if(NULL
== tuple
) return NULL
;
224 //predImpl->setTuple(tuple);
225 printDebug(DM_Table
, "Evaluating the predicate from fullTableScan");
226 predImpl
->evaluateForTable(result
, (char*)tuple
);
229 }else if (hashIndexScan
== scanType_
|| trieIndexScan
== scanType_
)
231 //evaluate till it succeeds
235 IndexNode
*node
= bIter
->next();
236 if (node
== NULL
) return NULL
;
237 printDebug(DM_HashIndex
, "next: returned IndexNode: %x", node
);
238 tuple
= node
->ptrToTuple_
;
240 printDebug(DM_HashIndex
, "next::tuple is null");
244 //if (!predImpl->isSingleTerm()) {
245 printDebug(DM_HashIndex
, "next: predicate has more than single term");
246 //predImpl->setTuple(tuple);
247 printDebug(DM_Table
, "Evaluating the predicate from hashIndexScan: has more than one term");
248 predImpl
->evaluateForTable(result
, (char*)tuple
);
254 }else if (treeIndexScan
== scanType_
)
256 if (NULL
== tIter
) return NULL
;
260 tuple
= tIter
->next();
262 printDebug(DM_HashIndex
, "next::tuple is null");
265 //predImpl->setTuple(tuple);
266 predImpl
->evaluateForTable(result
, (char*)tuple
);
267 if(!result
&& (isBetween
|| isPointLook
)) tIter
->nextNode();
273 DbRetVal
TupleIterator::close()
275 if (isClosed
) return OK
;
281 void TupleIterator::reset()
284 if (scanType_
== fullTableScan
) {
285 if (cIter
) *cIter
= ((Chunk
*)chunkPtr_
)->getIterator();
287 else if (scanType_
== hashIndexScan
) {
288 if(bIter
) bIter
->reset();
290 else if (scanType_
== treeIndexScan
) { if(tIter
) tIter
->reset(); }