moving destructor to cxx file
[csql.git] / include / Index.h
blob9ac9a0723865560fe207d991afd89fc38cb9b7e9
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 virtual DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
109 virtual DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
110 virtual DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
112 class HashIndex : public Index
115 public:
116 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
117 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
118 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
119 static unsigned int computeHashBucket(DataType type, void *key, int noOfBuckets, int length=0);
120 static DbRetVal insertLogicalUndoLog(Database *sysdb, void *info);
121 static DbRetVal deleteLogicalUndoLog(Database *sysdb, void *info);
124 class TreeIndex : public Index
127 TreeNode* locateNode(TreeNode *iter, void *tuple, IndexInfo *indInfo);
128 DbRetVal removeElement(Database *db, TreeNode *iter, void *tuple, HashIndexInfo *info);
129 public:
130 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
131 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
132 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
135 class TreeIter
137 TreeNode *iter;
138 TreeNode *head;
139 int fldOffset;
140 DataType type;
141 int length;
142 ComparisionOp op;
143 bool asc;
144 void *searchKey;
145 bool firstCall;
146 int nodeOffset;
147 bool recordsOver;
149 void* locateNode();
150 void* locateElement();
152 public:
153 TreeIter(){}
154 TreeIter(TreeNode *hd) { iter = head = hd; firstCall = true; recordsOver=false;}
155 void setSearchKey(void *key, ComparisionOp cop, bool ascending = true)
157 searchKey = key; op = cop; asc =ascending;
159 void setFldOffset(int off) { fldOffset = off; }
160 void setTypeLength(DataType t, int l) { type =t ; length =l; }
161 void* prev();
162 void* next();
163 void nextNode();
164 void* getFirstElement();
165 void* getLastElement();
166 void reset() { iter = head; firstCall = true; recordsOver=false; }
169 enum IndexIntType
171 hashOneField = 1,
172 hash = 2,
173 tree = 3
176 class IndexInfo
178 public:
179 IndexType indType;
180 virtual ~IndexInfo() {}
183 //Used by TableImpl to cache information related to hash indexes on that table
184 class HashIndexInfo :public IndexInfo
186 public:
187 FieldList idxFldList;
188 char *indexPtr;
189 int noOfBuckets;
190 Bucket* buckets;
191 int fldOffset;
192 bool isUnique;
193 DataType type;
194 int compLength;
195 void print()
197 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr, noOfBuckets, buckets);
199 ~HashIndexInfo() { idxFldList.removeAll(); }
201 #endif