TableImpl class is added with 3 more fields
[csql.git] / include / Index.h
blob30d41416d1aaae17ffb16c932ef87d15338e4446
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 HashIndexNode *head;
68 public:
69 BucketIter(){}
70 BucketIter(HashIndexNode *head) { iter = head = head;}
71 HashIndexNode* next();
72 void reset() { iter = head; }
73 friend class BucketList;
75 class BucketList
77 HashIndexNode *head;
78 public:
79 BucketList(){ head = NULL;}
80 BucketList(HashIndexNode *h){ head = h; }
81 void *getBucketListHead(){ return head;}
82 DbRetVal insert(Chunk *chunk, Database *db, void *key, void *tuple);
83 DbRetVal remove(Chunk *chunk, Database *db, void *key);
84 BucketIter getIterator()
86 BucketIter it;
87 it.iter = head;
88 return it;
92 class HashIndex;
93 class IndexInfo;
94 class HashIndexInfo;
95 class TreeIndex;
96 class Index
98 // create (one) object for each indexing mechanisms here
99 // Also need to make changes to getIndex() and destroy() methods
100 // accordingly for new index machanism.
101 static HashIndex *hIdx;
102 static TreeIndex *tIdx;
103 static long usageCount;
104 public:
105 static Index* getIndex(IndexType type);
106 static void init() { usageCount++; }
107 static void destroy() {
108 usageCount--;
109 if(!usageCount) {
110 if(!hIdx) { delete hIdx; hIdx=NULL; }
113 virtual DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
114 virtual DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
115 virtual DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
117 class HashIndex : public Index
120 public:
121 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
122 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
123 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
124 static unsigned int computeHashBucket(DataType type, void *key, int noOfBuckets, int length=0);
125 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
126 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
129 class TreeIndex : public Index
132 TreeNode* locateNode(TreeNode *iter, void *tuple, IndexInfo *indInfo);
133 DbRetVal removeElement(Database *db, TreeNode *iter, void *tuple, HashIndexInfo *info);
134 public:
135 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
136 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
137 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
140 class TreeIter
142 TreeNode *iter;
143 TreeNode *head;
144 int fldOffset;
145 DataType type;
146 int length;
147 ComparisionOp op;
148 bool asc;
149 void *searchKey;
150 bool firstCall;
151 int nodeOffset;
152 bool recordsOver;
154 void* locateNode();
155 void* locateElement();
157 public:
158 TreeIter(){}
159 TreeIter(TreeNode *head) { iter = head = head; firstCall = true; recordsOver=false;}
160 void setSearchKey(void *key, ComparisionOp cop, bool ascending = true)
162 searchKey = key; op = cop; asc =ascending;
164 void setFldOffset(int off) { fldOffset = off; }
165 void setTypeLength(DataType t, int l) { type =t ; length =l; }
166 void* prev();
167 void* next();
168 void nextNode();
169 void reset() { iter = head; firstCall = true; recordsOver=false; }
172 enum IndexIntType
174 hashOneField = 1,
175 hash = 2,
176 tree = 3
179 class IndexInfo
181 public:
182 IndexType indType;
185 //Used by TableImpl to cache information related to hash indexes on that table
186 class HashIndexInfo :public IndexInfo
188 public:
189 FieldList idxFldList;
190 char *indexPtr;
191 int noOfBuckets;
192 Bucket* buckets;
193 int fldOffset;
194 bool isUnique;
195 DataType type;
196 int compLength;
197 void print()
199 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr, noOfBuckets, buckets);
202 #endif