00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef INDEX_H
00017 #define INDEX_H
00018 #include<DataType.h>
00019 #include<Debug.h>
00020 #include<Info.h>
00021
00022
00023 class Chunk;
00024 class Database;
00025 class Transaction;
00026 class TableImpl;
00027
00028 class Bucket
00029 {
00030 public:
00031 Mutex mutex_;
00032 void *bucketList_;
00033 };
00034 class HashIndexNode
00035 {
00036 public:
00037 void *ptrToKey_;
00038 void *ptrToTuple_;
00039 HashIndexNode *next_;
00040 };
00041 class BucketIter
00042 {
00043 HashIndexNode *iter;
00044 public:
00045 BucketIter(){}
00046 BucketIter(HashIndexNode *head) { iter = head;}
00047 HashIndexNode* next();
00048 friend class BucketList;
00049 };
00050 class BucketList
00051 {
00052 HashIndexNode *head;
00053 public:
00054 BucketList(){ head = NULL;}
00055 BucketList(HashIndexNode *h){ head = h; }
00056 DbRetVal insert(Chunk *chunk, Database *db, void *key, void *tuple);
00057 DbRetVal remove(Chunk *chunk, Database *db, void *key);
00058 BucketIter getIterator()
00059 {
00060 BucketIter it;
00061 it.iter = head;
00062 return it;
00063 }
00064
00065 };
00066 class HashIndex;
00067 class IndexInfo;
00068 class Index
00069 {
00070
00071
00072
00073 static HashIndex *hIdx;
00074 static long usageCount;
00075 public:
00076 static Index* getIndex(IndexType type);
00077 static void init() { usageCount++; }
00078 static void destroy() {
00079 usageCount--;
00080 if(!usageCount) {
00081 if(!hIdx) { delete hIdx; hIdx=NULL; }
00082 }
00083 }
00084 virtual DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
00085 virtual DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
00086 virtual DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag)=0;
00087 };
00088 class HashIndex : public Index
00089 {
00090
00091 public:
00092 DbRetVal insert(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
00093 DbRetVal remove(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
00094 DbRetVal update(TableImpl *tbl, Transaction *tr, void *indexPtr, IndexInfo *info, void *tuple, bool undoFlag);
00095 static unsigned int computeHashBucket(DataType type, void *key, int noOfBuckets);
00096
00097 };
00098
00099 enum IndexIntType
00100 {
00101 hashOneField = 1,
00102 hash = 2,
00103 tree = 3
00104
00105 };
00106 class IndexInfo
00107 {
00108 public:
00109 IndexType type;
00110 };
00111
00112
00113 class SingleFieldHashIndexInfo :public IndexInfo
00114 {
00115 public:
00116 char *fldName;
00117 DataType type ;
00118 char *indexPtr;
00119 int noOfBuckets;
00120 Bucket* buckets;
00121 int fldPos;
00122 bool isUnique;
00123 int offset;
00124 int length;
00125 void print()
00126 {
00127 printf("SingleFieldHashIndexInfo fldname:%s type:%d indexPtr:%x noOfBuckets:%d buckets:%x isUnique:%d\n", fldName, type, indexPtr, noOfBuckets, buckets, isUnique);
00128 }
00129 };
00130 #endif
00131