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 ***************************************************************************/
16 #include<CatalogTables.h>
22 DbRetVal
CatalogTableINDEX::insert(const char *name
, void *tptr
, int numFlds
, bool isUnique
,
23 void* chunk
, int bucketSize
, void *hChunk
, void *&tupleptr
)
25 Chunk
*tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
26 ChunkIterator iter
= tChunk
->getIterator();
28 //Checking for index having same name, proceed further only
29 //if no such indexes exists
31 while ((data
= iter
.nextElement())!= NULL
)
33 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
35 printError(ErrAlready
, "Index with name \'%s\' already exists "
36 "on the table \'%s\'.", name
, ((CTABLE
*)tptr
)->tblName_
);
42 tupleptr
= tChunk
->allocate(systemDatabase_
, &rv
);
46 "Could not allocate for INDEX catalog table");
49 CINDEX
*indexInfo
= (CINDEX
*)tupleptr
;
50 strcpy(indexInfo
->indName_
, name
);
51 indexInfo
->tblID_
= -1; //Not used currently
52 indexInfo
->tblPtr_
= tptr
;
53 indexInfo
->numFlds_
= numFlds
;
55 indexInfo
->indexType_
= treeIndex
;
57 indexInfo
->indexType_
= hashIndex
;
58 if (0 == bucketSize
) indexInfo
->indexType_
= trieIndex
;
59 indexInfo
->chunkPtr_
= chunk
;
60 indexInfo
->hashNodeChunk_
= hChunk
;
61 indexInfo
->noOfBuckets_
= bucketSize
;
62 indexInfo
->isUnique_
= isUnique
;
63 indexInfo
->fstIndFld_
=NULL
;
64 printDebug(DM_SystemDatabase
,"One Row inserted into INDEX %x %s",tupleptr
, name
);
68 DbRetVal
CatalogTableINDEX::remove(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
70 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
71 ChunkIterator iter
= fChunk
->getIterator();
74 while ((data
= iter
.nextElement())!= NULL
)
76 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
78 //remove this element and store the tuple ptr
79 //there will be only one row for this table(Primary key)
80 chunk
= (Chunk
*) ((CINDEX
*)data
)->chunkPtr_
;
81 hchunk
= (Chunk
*) ((CINDEX
*)data
)->hashNodeChunk_
;
88 fChunk
->free(systemDatabase_
, iptr
);
89 printDebug(DM_SystemDatabase
,"One Row deleted from INDEX %x %s",iptr
, name
);
93 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
99 DbRetVal
CatalogTableINDEX::get(const char *name
, void *&chunk
, void *&hchunk
, void *&iptr
)
101 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
102 ChunkIterator iter
= fChunk
->getIterator();
105 while ((data
= iter
.nextElement())!= NULL
)
107 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
109 //remove this element and store the tuple ptr
110 //there will be only one row for this table(Primary key)
111 chunk
= (Chunk
*) ((CINDEX
*)data
)->chunkPtr_
;
112 hchunk
= (Chunk
*) ((CINDEX
*)data
)->hashNodeChunk_
;
119 printError(ErrNotExists
,"Index %s not exists in INDEX catalog table", name
);
125 DbRetVal
CatalogTableINDEX::setChunkPtr(const char *name
, ObjectType type
, void *bChunk
, void *firstPage
, void *curPage
)
127 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
128 ChunkIterator iter
= fChunk
->getIterator();
131 while ((data
= iter
.nextElement())!= NULL
)
133 if (0 == strcmp(((CINDEX
*)data
)->indName_
, name
))
135 //remove this element and store the tuple ptr
136 //there will be only one row for this table(Primary key)
138 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setFirstPage(bChunk
);
139 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setCurPage(bChunk
);
140 ((Chunk
*)((CINDEX
*)data
)->hashNodeChunk_
)->setFirstPage(firstPage
);
141 ((Chunk
*)((CINDEX
*)data
)->hashNodeChunk_
)->setCurPage(curPage
);
142 } else if (type
== tIdx
) {
143 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setFirstPage(firstPage
);
144 ((Chunk
*) ((CINDEX
*)data
)->chunkPtr_
)->setCurPage(curPage
);
145 ((CINDEX
*)data
)->hashNodeChunk_
= bChunk
;
153 int CatalogTableINDEX::getNumIndexes(void *tptr
)
155 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
156 ChunkIterator iter
= fChunk
->getIterator();
159 while (NULL
!= (iptr
= iter
.nextElement()))
161 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
) numIndex
++;
166 ListIterator
CatalogTableINDEXFIELD::getIndexListIterater(char *name
)
169 Chunk
*chunk
=systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
170 ChunkIterator ifIter
= chunk
->getIterator();
172 while ((data
= ifIter
.nextElement())!= NULL
)
174 IndexInfoForDriver
*idxInfo
= new IndexInfoForDriver();
175 if(strcmp( name
,((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
) == 0)
177 strcpy(idxInfo
->indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
178 strcpy(idxInfo
->tableName
,((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
);
179 strcpy(idxInfo
->fieldName
,((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
180 idxInfo
->type
= ((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indexType_
;
181 idxInfo
->isUnique
= ((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->isUnique_
;
182 idxInfo
->isPrimary
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->isPrimary_
;
183 indexList
.append(idxInfo
);
186 return indexList
.getIterator();
189 char* CatalogTableINDEX::getIndexName(void *tptr
, int position
)
191 if (position
== 0) return NULL
;
192 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
193 ChunkIterator iter
= fChunk
->getIterator();
197 while (NULL
!= (iptr
= iter
.nextElement()))
199 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
) curPos
++;
200 if ( curPos
== position
) return ((CINDEX
*)iptr
)->indName_
;
206 void CatalogTableINDEX::getIndexPtrs(void *tptr
, char **&array
)
209 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
210 ChunkIterator iter
= fChunk
->getIterator();
212 while (NULL
!= (iptr
= iter
.nextElement()))
214 if (((CINDEX
*)iptr
)->tblPtr_
== tptr
)
216 array
[i
++] = (char*) iptr
;
222 ChunkIterator
CatalogTableINDEX::getIterator(void *iptr
)
224 CINDEX
*index
= (CINDEX
*)iptr
;
225 return ((Chunk
*)index
->chunkPtr_
)->getIterator();
229 int CatalogTableINDEX::getNoOfBuckets(void *iptr
)
231 CINDEX
*index
= (CINDEX
*)iptr
;
232 return index
->noOfBuckets_
;
235 int CatalogTableINDEX::getUnique(void *iptr
)
237 CINDEX
*index
= (CINDEX
*)iptr
;
238 return index
->isUnique_
;
241 IndexType
CatalogTableINDEX::getType(void *iptr
)
243 CINDEX
*index
= (CINDEX
*)iptr
;
244 return index
->indexType_
;
247 char* CatalogTableINDEX::getName(void *iptr
)
249 CINDEX
*index
= (CINDEX
*)iptr
;
250 return index
->indName_
;
253 int CatalogTableINDEX::getOffsetOfFirstField(void *iptr
)
255 CINDEX
*index
= (CINDEX
*)iptr
;
256 return ((CFIELD
*)(((CINDEXFIELD
*)(index
->fstIndFld_
))->fieldPtr
))->offset_
;
259 DbRetVal
CatalogTableINDEXFIELD::insert(FieldNameList
&fldList
, void *indexPtr
,
260 void *tblPtr
, char **&fptr
)
264 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexTableId
);
265 ChunkIterator iter
= tChunk
->getIterator();
266 CINDEXFIELD
*fInd
=NULL
;
270 while ((data
= iter
.nextElement())!= NULL
)
272 if ((((CINDEX
*)data
)->tblPtr_
==tblPtr
)
273 && (((CINDEX
*)indexPtr
)->numFlds_
== ((CINDEX
*)data
)->numFlds_
)
274 && (((CINDEX
*)indexPtr
)->indexType_
==((CINDEX
*)data
)->indexType_
)
275 && (data
!= indexPtr
) )
278 while (NULL
!= (fName
= fldList
.nextFieldName()))
281 fInd
=(CINDEXFIELD
*)((CINDEX
*)data
)->fstIndFld_
;
284 if (0 == strcmp(((CFIELD
*) fInd
->fieldPtr
)->fldName_
, fName
))
295 printError(ErrAlready
, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE
*)tblPtr
)->tblName_
, ((CINDEX
*)data
)->indName_
);
302 tChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
305 while (NULL
!= (fName
= fldList
.nextFieldName()))
308 fInd
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
311 if (0 == strcmp(((CFIELD
*) fInd
->fieldPtr
)->fldName_
, fName
))
313 printError(ErrAlready
,"Composite Index Can't be created with same Name");
314 fInd
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
320 tChunk
->free(systemDatabase_
,fldI
);
326 void *fieldptr
= tChunk
->allocate(systemDatabase_
, &rv
);
327 if (NULL
== fieldptr
)
329 printError(rv
, "Could not allocate for USER catalog table");
332 CINDEXFIELD
*fldInfo
= (CINDEXFIELD
*)fieldptr
;
333 fldInfo
->tablePtr
= tblPtr
;
334 fldInfo
->fieldPtr
= (CFIELD
*)fptr
[i
++];
335 fldInfo
->indexPtr
= indexPtr
;
336 fldInfo
->next
=(CINDEXFIELD
*)((CINDEX
*)indexPtr
)->fstIndFld_
;
337 ((CINDEX
*)indexPtr
)->fstIndFld_
=fldInfo
;
338 printDebug(DM_SystemDatabase
,"One Row inserted into INDEXFIELD %x", fldInfo
);
343 DbRetVal
CatalogTableINDEXFIELD::remove(void *iptr
)
346 fChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
347 ChunkIterator fIter
= fChunk
->getIterator();
349 while ((data
= fIter
.nextElement())!= NULL
)
351 if (((CINDEXFIELD
*)data
)->indexPtr
== iptr
)
353 //remove this element
354 if(((CFIELD
*)((CINDEXFIELD
*)data
)->fieldPtr
)->isUnique_
) ((CFIELD
*)((CINDEXFIELD
*)data
)->fieldPtr
)->isUnique_
= false;
355 fChunk
->free(systemDatabase_
, data
);
356 printDebug(DM_SystemDatabase
,"One Row deleted from INDEXFIELD %x", data
);
362 DbRetVal
CatalogTableINDEXFIELD::getFieldNameAndType(void *index
,
363 char *&name
, DataType
&type
)
366 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
367 ChunkIterator ifIter
= ifChunk
->getIterator();
369 while ((data
= ifIter
.nextElement())!= NULL
)
371 if (((CINDEXFIELD
*)data
)->indexPtr
== index
)
373 //store the field name
374 name
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
;
375 type
= ((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->type_
;
379 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
383 DbRetVal
CatalogTableINDEXFIELD::getFieldInfo(void *index
, FieldList
&list
)
386 ifChunk
= systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
387 ChunkIterator ifIter
= ifChunk
->getIterator();
390 while ((data
= ifIter
.nextElement())!= NULL
)
392 if (((CINDEXFIELD
*)data
)->indexPtr
== index
)
394 //add the information to the field list
395 CFIELD
*fTuple
= (CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
);
397 strcpy(fldDef
.fldName_
, fTuple
->fldName_
);
398 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
399 fldDef
.type_
= fTuple
->type_
;
400 fldDef
.length_
= fTuple
->length_
;
401 fldDef
.offset_
= fTuple
->offset_
;
402 fldDef
.isDefault_
= fTuple
->isDefault_
;
403 os::memcpy(fldDef
.defaultValueBuf_
, fTuple
->defaultValueBuf_
,
404 DEFAULT_VALUE_BUF_LENGTH
);
405 fldDef
.isNull_
= fTuple
->isNull_
;
406 fldDef
.isUnique_
= fTuple
->isUnique_
;
407 fldDef
.isPrimary_
= fTuple
->isPrimary_
;
413 printError(ErrNotExists
,"Index %x not exists in catalog table", index
);
419 void CatalogTableINDEXFIELD::printAllIndex()
421 Chunk
*chunk
=systemDatabase_
->getSystemDatabaseChunk(IndexFieldTableId
);
422 ChunkIterator ifIter
= chunk
->getIterator();
424 char indexName
[IDENTIFIER_LENGTH
] = {'\0'};
425 while ((data
= ifIter
.nextElement())!= NULL
)
427 if(strcmp(indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
)!=0)
429 printf(" <Index Name> %s </Index Name> \n",((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);
430 if(0==((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indexType_
)
431 printf(" <Index Type> Hash Index </Index Type> \n");
433 printf(" <Index Type> Tree Index </Index Type> \n");
434 printf(" <Table Name> %s </Table Name> \n",((CTABLE
*)(((CINDEXFIELD
*)data
)->tablePtr
))->tblName_
);
435 printf(" <Field Name> %s </Field Name> \n",((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
439 printf(" <Field Name> %s </Field Name> \n",((CFIELD
*)(((CINDEXFIELD
*)data
)->fieldPtr
))->fldName_
);
441 strcpy(indexName
,((CINDEX
*)(((CINDEXFIELD
*)data
)->indexPtr
))->indName_
);