allocator fixes
[csql.git] / include / Index.h
blob6a042cc36c09205233324166bcf3f42061a16c88
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>
22 class Database;
23 class Chunk;
24 class Transaction;
25 class TableImpl;
26 class CINDEX;
28 class Bucket
30 public:
31 Mutex mutex_;
32 void *bucketList_;
34 class HashIndexNode
36 public:
37 void *ptrToKey_;
38 void *ptrToTuple_;
39 HashIndexNode *next_;
42 class IndexInfo;
43 class TreeNode
45 public:
46 Mutex mutex_;
47 void *min_;
48 void *max_;
49 int noElements_;
50 TreeNode *next_;
51 TreeNode *prev_;
52 int balance_;
53 //Note::after this array of pointer to tuples are stored
54 long long getTotalElements();
55 TreeNode* locateNode(Database *db,TreeNode *iter, void *tuple, IndexInfo *indInfo,DbRetVal &rv);
56 TreeNode *locateNodeFromFirstLevel(TreeNode *ftnode,IndexInfo *indInfo,void *tuple,int *nodepos);
57 DbRetVal insertNodeIntoFirstLevel(Database * db, IndexInfo * indInfo, void* indexPtr, TreeNode * newNode,int nodepos);
58 DbRetVal insert(Database *db, IndexInfo *info, void *indexPtr, void *tuple);
59 DbRetVal insertRecordIntoNodeAndArrangeFirstLevel(Database * db, IndexInfo * indInfo, void* iptr, void * tuple, TreeNode * fstLevel,int nodepos);
60 DbRetVal remove(Database *db, IndexInfo *info, void *indexPtr, void *tuple);
61 DbRetVal update(Database *db, IndexInfo *info, void *indexPtr, void *tuple);
62 void displayAll();
63 void displayAll(int offset);
66 class BucketIter
68 HashIndexNode *iter;
69 HashIndexNode *head;
70 bool isUnique;
71 bool recordsOver;
72 public:
73 BucketIter(){iter = head = NULL;
74 isUnique=false; recordsOver = false;}
75 void setHead(HashIndexNode *hd) { iter = head = hd; recordsOver=false;}
76 BucketIter(HashIndexNode *head) { iter = head = head;
77 isUnique=false; recordsOver = false;}
78 void setUnique() { isUnique = true; }
79 bool getUnique() { return isUnique; }
80 HashIndexNode* next();
81 void reset() { iter = head; recordsOver=false;}
82 friend class BucketList;
84 class BucketList
86 HashIndexNode *head;
87 public:
88 BucketList(){ head = NULL;}
89 BucketList(HashIndexNode *h){ head = h; }
90 void *getBucketListHead(){ return head;}
91 DbRetVal insert(Chunk *chunk, Database *db, void *key, void *tuple);
92 DbRetVal remove(Chunk *chunk, Database *db, void *key);
93 BucketIter getIterator()
95 BucketIter it;
96 it.iter = head;
97 return it;
100 class HashIndex;
101 class IndexInfo;
102 class HashIndexInfo;
103 class TreeIndex;
104 class Index
106 // create (one) object for each indexing mechanisms here
107 // Also need to make changes to getIndex() and destroy() methods
108 // accordingly for new index machanism.
109 static HashIndex *hIdx;
110 static TreeIndex *tIdx;
111 static long usageCount;
112 public:
113 static Index* getIndex(IndexType type);
114 static void init() { usageCount++; }
115 static void destroy();
116 virtual DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
117 virtual DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
118 virtual DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
120 class HashIndex : public Index
122 //No members as it will be called by multiple threads
123 public:
124 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
125 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
126 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
127 static unsigned int computeHashBucket(DataType type, void *key, int noOfBuckets, int length=0);
128 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
129 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
132 class TreeIndex : public Index
134 //No members as it will be called by multiple threads
135 DbRetVal removeElement(Database *db, TreeNode *iter, void *tuple, HashIndexInfo *info);
136 void removeNode(Database *db,void *indexPtr,TreeNode *fltnode, TreeNode *node,int pos);
137 public:
138 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
139 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
140 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
141 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
142 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
144 class TreeIter
146 TreeNode *iter;
147 TreeNode *head;
148 int fldOffset;
149 DataType type;
150 int length;
151 ComparisionOp op;
152 bool asc;
153 void *searchKey;
154 bool firstCall;
155 int nodeOffset;
156 bool recordsOver;
157 void *fstLTnode;
158 void* locateNode();
159 void* locateElement();
160 int procSlot;
161 bool isUnique;
163 public:
164 TreeIter(){ iter=head=NULL; searchKey=fstLTnode=NULL;isUnique = false;}
165 TreeIter(TreeNode *hd,void *fTnode, int slot ) { fstLTnode = fTnode; iter = head = hd; firstCall = true; recordsOver=false; procSlot=slot; isUnique=false;}
166 void set(TreeNode *hd,void *fTnode, int slot ) { fstLTnode = fTnode; iter = head = hd; firstCall = true; recordsOver=false; procSlot=slot; isUnique=false;}
167 void setSearchKey(void *key, ComparisionOp cop, bool ascending = true)
169 searchKey = key; op = cop; asc =ascending;
171 void setUnique() { isUnique = true; }
172 bool getUnique() { return isUnique; }
173 void setFldOffset(int off) { fldOffset = off; }
174 void setTypeLength(DataType t, int l) { type =t ; length =l; }
175 void* prev();
176 void* next();
177 void nextNode();
178 void* getFirstElement();
179 void* getLastElement();
180 void reset();// { iter = head; firstCall = true; recordsOver=false; }
183 enum IndexIntType
185 hashOneField = 1,
186 hash = 2,
187 tree = 3
190 class IndexInfo
192 public:
193 IndexType indType;
194 virtual ~IndexInfo() {}
197 //Used by TableImpl to cache information related to hash indexes on that table
198 class HashIndexInfo :public IndexInfo
200 public:
201 FieldList idxFldList;
202 char *indexPtr;
203 int noOfBuckets;
204 Bucket* buckets;
205 int fldOffset;
206 bool isUnique;
207 DataType type;
208 int compLength;
209 void print()
211 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr, noOfBuckets, buckets);
213 ~HashIndexInfo() { idxFldList.removeAll(); }
215 #endif