1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
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
);
63 void displayAll(int offset
);
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
;
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()
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
;
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
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
);
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
);
163 void* locateElement();
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
; }
182 void* getFirstElement();
183 void* getLastElement();
184 void reset();// { iter = head; firstCall = true; recordsOver=false; }
198 virtual ~IndexInfo() {}
201 //Used by TableImpl to cache information related to hash indexes on that table
202 class HashIndexInfo
:public IndexInfo
205 FieldList idxFldList
;
215 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr
, noOfBuckets
, buckets
);
217 ~HashIndexInfo() { idxFldList
.removeAll(); }