isFldNull() method call from predicateImpl:evaluate dumps core for JoinTableImpl...
[csql.git] / include / Index.h
blobdb18d9202985b8cfb0555fb4b82916880aa1ae04
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 #ifndef INDEX_H
17 #define INDEX_H
18 #include<DataType.h>
19 #include<Debug.h>
20 #include<Info.h>
23 class Chunk;
24 class Database;
25 class Transaction;
26 class TableImpl;
27 class CINDEX;
29 class Bucket
31 public:
32 Mutex mutex_;
33 void *bucketList_;
35 class HashIndexNode
37 public:
38 void *ptrToKey_;
39 void *ptrToTuple_;
40 HashIndexNode *next_;
43 class IndexInfo;
44 class TreeNode
46 public:
47 Mutex mutex_;
48 void *min_;
49 void *max_;
50 int noElements_;
51 int balance_;
52 TreeNode *next_;
53 TreeNode *prev_;
54 //Note::after this array of pointer to tuples are stored
56 DbRetVal insert(Database *db, IndexInfo *info, void *indexPtr, void *tuple);
57 DbRetVal insert(int position, Database *db, IndexInfo *indInfo, CINDEX *iptr, void *tuple, TreeNode *iter);
58 DbRetVal remove(Database *db, IndexInfo *info, void *indexPtr, void *tuple);
59 DbRetVal update(Database *db, IndexInfo *info, void *indexPtr, void *tuple);
60 void displayAll(IndexInfo *indInfo, void *indexPtr);
61 void displayAll(int offset);
64 class BucketIter
66 HashIndexNode *iter;
67 public:
68 BucketIter(){}
69 BucketIter(HashIndexNode *head) { iter = head;}
70 HashIndexNode* next();
71 friend class BucketList;
73 class BucketList
75 HashIndexNode *head;
76 public:
77 BucketList(){ head = NULL;}
78 BucketList(HashIndexNode *h){ head = h; }
79 void *getBucketListHead(){ return head;}
80 DbRetVal insert(Chunk *chunk, Database *db, void *key, void *tuple);
81 DbRetVal remove(Chunk *chunk, Database *db, void *key);
82 BucketIter getIterator()
84 BucketIter it;
85 it.iter = head;
86 return it;
90 class HashIndex;
91 class IndexInfo;
92 class HashIndexInfo;
93 class TreeIndex;
94 class Index
96 // create (one) object for each indexing mechanisms here
97 // Also need to make changes to getIndex() and destroy() methods
98 // accordingly for new index machanism.
99 static HashIndex *hIdx;
100 static TreeIndex *tIdx;
101 static long usageCount;
102 public:
103 static Index* getIndex(IndexType type);
104 static void init() { usageCount++; }
105 static void destroy() {
106 usageCount--;
107 if(!usageCount) {
108 if(!hIdx) { delete hIdx; hIdx=NULL; }
111 virtual DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
112 virtual DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
113 virtual DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
115 class HashIndex : public Index
118 public:
119 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
120 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
121 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
122 static unsigned int computeHashBucket(DataType type, void *key, int noOfBuckets, int length=0);
123 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
124 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
127 class TreeIndex : public Index
130 TreeNode* locateNode(TreeNode *iter, void *tuple, IndexInfo *indInfo);
131 DbRetVal removeElement(Database *db, TreeNode *iter, void *tuple, HashIndexInfo *info);
132 public:
133 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
134 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
135 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
138 class TreeIter
140 TreeNode *iter;
141 int fldOffset;
142 DataType type;
143 int length;
144 ComparisionOp op;
145 bool asc;
146 void *searchKey;
147 bool firstCall;
148 int nodeOffset;
149 bool recordsOver;
151 void* locateNode();
152 void* locateElement();
154 public:
155 TreeIter(){}
156 TreeIter(TreeNode *head) { iter = head; firstCall = true; recordsOver=false;}
157 void setSearchKey(void *key, ComparisionOp cop, bool ascending = true)
159 searchKey = key; op = cop; asc =ascending;
161 void setFldOffset(int off) { fldOffset = off; }
162 void setTypeLength(DataType t, int l) { type =t ; length =l; }
163 void* prev();
164 void* next();
165 void nextNode();
168 enum IndexIntType
170 hashOneField = 1,
171 hash = 2,
172 tree = 3
175 class IndexInfo
177 public:
178 IndexType indType;
181 //Used by TableImpl to cache information related to hash indexes on that table
182 class HashIndexInfo :public IndexInfo
184 public:
185 FieldList idxFldList;
186 char *indexPtr;
187 int noOfBuckets;
188 Bucket* buckets;
189 int fldOffset;
190 bool isUnique;
191 DataType type;
192 int compLength;
193 void print()
195 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr, noOfBuckets, buckets);
198 #endif