windows porting
[csql.git] / include / Index.h
blob1a8190d58787f2d9bee525fea7c6b2f185a89f14
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 bool checkForUniqueKey(HashIndexNode *head,HashIndexInfo *info, void *tuple);
128 static unsigned int computeHashBucket(DataType type, void *key, int noOfBuckets, int length=0);
129 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
130 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
133 class TreeIndex : public Index
135 //No members as it will be called by multiple threads
136 DbRetVal removeElement(Database *db, TreeNode *iter, void *tuple, HashIndexInfo *info);
137 void removeNode(Database *db,void *indexPtr,TreeNode *fltnode, TreeNode *node,int pos);
138 public:
139 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
140 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
141 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
142 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
143 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
144 static DbRetVal getTreeNodeMutex(TreeNode*, int procSlot, bool isX=false);
145 static DbRetVal upgradeTreeNodeMutex(TreeNode*, int procSlot);
148 class TreeIter
150 TreeNode *iter;
151 TreeNode *head;
152 int fldOffset;
153 DataType type;
154 int length;
155 ComparisionOp op;
156 bool asc;
157 void *searchKey;
158 bool firstCall;
159 int nodeOffset;
160 bool recordsOver;
161 void *fstLTnode;
162 void* locateNode();
163 void* locateElement();
164 int procSlot;
165 bool isUnique;
167 public:
168 TreeIter(){ iter=head=NULL; searchKey=fstLTnode=NULL;isUnique = false;}
169 TreeIter(TreeNode *hd,void *fTnode, int slot ) { fstLTnode = fTnode; iter = head = hd; firstCall = true; recordsOver=false; procSlot=slot; isUnique=false;}
170 void set(TreeNode *hd,void *fTnode, int slot ) { fstLTnode = fTnode; iter = head = hd; firstCall = true; recordsOver=false; procSlot=slot; isUnique=false;}
171 void setSearchKey(void *key, ComparisionOp cop, bool ascending = true)
173 searchKey = key; op = cop; asc =ascending;
175 void setUnique() { isUnique = true; }
176 bool getUnique() { return isUnique; }
177 void setFldOffset(int off) { fldOffset = off; }
178 void setTypeLength(DataType t, int l) { type =t ; length =l; }
179 void* prev();
180 void* next();
181 void nextNode();
182 void* getFirstElement();
183 void* getLastElement();
184 void reset();// { iter = head; firstCall = true; recordsOver=false; }
187 enum IndexIntType
189 hashOneField = 1,
190 hash = 2,
191 tree = 3
194 class IndexInfo
196 public:
197 IndexType indType;
198 virtual ~IndexInfo() {}
201 //Used by TableImpl to cache information related to hash indexes on that table
202 class HashIndexInfo :public IndexInfo
204 public:
205 FieldList idxFldList;
206 char *indexPtr;
207 int noOfBuckets;
208 Bucket* buckets;
209 int fldOffset;
210 bool isUnique;
211 DataType type;
212 int compLength;
213 void print()
215 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr, noOfBuckets, buckets);
217 ~HashIndexInfo() { idxFldList.removeAll(); }
219 #endif