using tree index
[csql.git] / include / Index.h
blob01c708f13449af75b5b7c727253f695ec21f576a
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);
126 class TreeIndex : public Index
129 TreeNode* locateNode(TreeNode *iter, void *tuple, IndexInfo *indInfo);
130 DbRetVal removeElement(TreeNode *iter, void *tuple, HashIndexInfo *info);
131 public:
132 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
133 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
134 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
137 class TreeIter
139 TreeNode *iter;
140 int fldOffset;
141 DataType type;
142 int length;
143 ComparisionOp op;
144 bool asc;
145 void *searchKey;
146 bool firstCall;
147 int nodeOffset;
148 bool recordsOver;
150 void* locateNode();
151 void* locateElement();
153 public:
154 TreeIter(){}
155 TreeIter(TreeNode *head) { iter = head; firstCall = true; recordsOver=false;}
156 void setSearchKey(void *key, ComparisionOp cop, bool ascending = true)
158 searchKey = key; op = cop; asc =ascending;
160 void setFldOffset(int off) { fldOffset = off; }
161 void setTypeLength(DataType t, int l) { type =t ; length =l; }
162 void* prev();
163 void* next();
166 enum IndexIntType
168 hashOneField = 1,
169 hash = 2,
170 tree = 3
173 class IndexInfo
175 public:
176 IndexType indType;
179 //Used by TableImpl to cache information related to hash indexes on that table
180 class HashIndexInfo :public IndexInfo
182 public:
183 FieldList idxFldList;
184 char *indexPtr;
185 int noOfBuckets;
186 Bucket* buckets;
187 int fldOffset;
188 bool isUnique;
189 DataType type;
190 int compLength;
191 void print()
193 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr, noOfBuckets, buckets);
196 #endif