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 ***************************************************************************/
22 #define TRIE_MAX_LENGTH 64
47 IndexNode
*head_
[TRIE_SIZE
];
48 TrieNode
*next_
[TRIE_SIZE
];
62 //Note::after this array of pointer to tuples are stored
63 long long getTotalElements();
64 TreeNode
* locateNode(Database
*db
,TreeNode
*iter
, void *tuple
, IndexInfo
*indInfo
,DbRetVal
&rv
);
65 TreeNode
*locateNodeFromFirstLevel(TreeNode
*ftnode
,IndexInfo
*indInfo
,void *tuple
,int *nodepos
);
66 DbRetVal
insertNodeIntoFirstLevel(Database
* db
, IndexInfo
* indInfo
, void* indexPtr
, TreeNode
* newNode
,int nodepos
);
67 DbRetVal
insert(Database
*db
, IndexInfo
*info
, void *indexPtr
, void *tuple
);
68 DbRetVal
insertRecordIntoNodeAndArrangeFirstLevel(Database
* db
, IndexInfo
* indInfo
, void* iptr
, void * tuple
, TreeNode
* fstLevel
,int nodepos
);
69 DbRetVal
remove(Database
*db
, IndexInfo
*info
, void *indexPtr
, void *tuple
);
70 DbRetVal
update(Database
*db
, IndexInfo
*info
, void *indexPtr
, void *tuple
);
72 void displayAll(int offset
);
82 BucketIter(){iter
= head
= NULL
;
83 isUnique
=false; recordsOver
= false;}
84 void setHead(IndexNode
*hd
) { iter
= head
= hd
; recordsOver
=false;}
85 BucketIter(IndexNode
*head
) { iter
= head
= head
;
86 isUnique
=false; recordsOver
= false;}
87 void setUnique() { isUnique
= true; }
88 bool getUnique() { return isUnique
; }
90 void reset() { iter
= head
; recordsOver
=false;}
91 friend class BucketList
;
97 BucketList(){ head
= NULL
;}
98 BucketList(IndexNode
*h
){ head
= h
; }
99 void *getBucketListHead(){ return head
;}
100 DbRetVal
insert(Chunk
*chunk
, Database
*db
, void *key
, void *tuple
);
101 DbRetVal
remove(Chunk
*chunk
, Database
*db
, void *key
);
103 BucketIter
getIterator()
117 // create (one) object for each indexing mechanisms here
118 // Also need to make changes to getIndex() and destroy() methods
119 // accordingly for new index machanism.
120 static HashIndex
*hIdx
;
121 static TreeIndex
*tIdx
;
122 static TrieIndex
*iIdx
;
123 static long usageCount
;
125 static Index
* getIndex(IndexType type
);
126 static void init() { usageCount
++; }
127 static void destroy();
128 virtual DbRetVal
insert(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
)=0;
129 virtual DbRetVal
remove(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
)=0;
130 virtual DbRetVal
update(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
)=0;
132 class HashIndex
: public Index
134 //No members as it will be called by multiple threads
136 DbRetVal
insert(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
137 DbRetVal
remove(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
138 DbRetVal
update(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
139 bool checkForUniqueKey(IndexNode
*head
,HashIndexInfo
*info
, void *tuple
);
140 static unsigned int computeHashBucket(DataType type
, void *key
, int noOfBuckets
, int length
=0);
141 static DbRetVal
insertLogicalUndoLog(Database
*sysdb
, void *info
);
142 static DbRetVal
deleteLogicalUndoLog(Database
*sysdb
, void *info
);
145 class TreeIndex
: public Index
147 //No members as it will be called by multiple threads
148 DbRetVal
removeElement(Database
*db
, TreeNode
*iter
, void *tuple
, HashIndexInfo
*info
);
149 void removeNode(Database
*db
,void *indexPtr
,TreeNode
*fltnode
, TreeNode
*node
,int pos
);
151 DbRetVal
insert(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
152 DbRetVal
remove(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
153 DbRetVal
update(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
154 static DbRetVal
insertLogicalUndoLog(Database
*sysdb
, void *info
);
155 static DbRetVal
deleteLogicalUndoLog(Database
*sysdb
, void *info
);
156 static DbRetVal
getTreeNodeMutex(TreeNode
*, int procSlot
, bool isX
=false);
157 static DbRetVal
upgradeTreeNodeMutex(TreeNode
*, int procSlot
);
161 class TrieIndex
: public Index
164 DbRetVal
insert(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
165 DbRetVal
remove(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
166 DbRetVal
update(TableImpl
*tbl
, Transaction
*tr
, void *indexPtr
, IndexInfo
*info
, void *tuple
, bool undoFlag
);
167 bool checkForUniqueKey(IndexNode
*head
, IndexInfo
*info
, void *tuple
);
168 static void computeHashValues(DataType type
, void *key
, char *in
, int length
=0);
169 static DbRetVal
insertLogicalUndoLog(Database
*sysdb
, void *info
);
170 static DbRetVal
deleteLogicalUndoLog(Database
*sysdb
, void *info
);
171 void displayAll(TrieNode
*node
, int level
=1);
172 void printTrieNode(TrieNode
*node
, int level
);
174 DbRetVal
addToValueList(Database
*, void**, Chunk
*, IndexInfo
*, void*, void*);
175 DbRetVal
removeFromValueList(Database
*, void**, Chunk
*, void*, void*);
193 void* locateElement();
198 TreeIter(){ iter
=head
=NULL
; searchKey
=fstLTnode
=NULL
;isUnique
= false;}
199 TreeIter(TreeNode
*hd
,void *fTnode
, int slot
) { fstLTnode
= fTnode
; iter
= head
= hd
; firstCall
= true; recordsOver
=false; procSlot
=slot
; isUnique
=false;}
200 void set(TreeNode
*hd
,void *fTnode
, int slot
) { fstLTnode
= fTnode
; iter
= head
= hd
; firstCall
= true; recordsOver
=false; procSlot
=slot
; isUnique
=false;}
201 void setSearchKey(void *key
, ComparisionOp cop
, bool ascending
= true)
203 searchKey
= key
; op
= cop
; asc
=ascending
;
205 void setUnique() { isUnique
= true; }
206 bool getUnique() { return isUnique
; }
207 void setFldOffset(int off
) { fldOffset
= off
; }
208 void setTypeLength(DataType t
, int l
) { type
=t
; length
=l
; }
212 void* getFirstElement();
213 void* getLastElement();
214 void reset();// { iter = head; firstCall = true; recordsOver=false; }
233 virtual ~IndexInfo() {}
236 //Used by TableImpl to cache information related to hash indexes on that table
237 class HashIndexInfo
:public IndexInfo
240 FieldList idxFldList
;
246 printf("HashIndexInfo indexPtr:%x noOfBuckets:%d buckets:%x \n",indexPtr
, noOfBuckets
, buckets
);
248 ~HashIndexInfo() { idxFldList
.removeAll(); }