code reorg
[csql.git] / src / storage / CatalogTablesIdx.cxx
blob02b6302f398fe9d875bbe7b9a146867893efd300
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 #include<CatalogTables.h>
17 #include<Database.h>
18 #include<Allocator.h>
19 #include<Field.h>
20 #include<Debug.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
30 void *data = NULL;
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_);
37 return ErrAlready;
41 DbRetVal rv =OK;
42 tupleptr = tChunk->allocate(systemDatabase_, &rv);
43 if (NULL == tupleptr)
45 printError(rv,
46 "Could not allocate for INDEX catalog table");
47 return rv;
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;
54 if (NULL == hChunk)
55 indexInfo->indexType_ = treeIndex;
56 else
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);
65 return OK;
68 DbRetVal CatalogTableINDEX::remove(const char *name, void *&chunk, void *&hchunk, void *&iptr)
70 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
71 ChunkIterator iter = fChunk->getIterator();
73 void *data = NULL;
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_;
82 iptr = (void*) data;
83 break;
86 if (NULL != iptr)
88 fChunk->free(systemDatabase_, iptr);
89 printDebug(DM_SystemDatabase,"One Row deleted from INDEX %x %s",iptr, name);
91 else
93 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
94 return ErrNotExists;
96 return OK;
99 DbRetVal CatalogTableINDEX::get(const char *name, void *&chunk, void *&hchunk, void *&iptr)
101 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
102 ChunkIterator iter = fChunk->getIterator();
104 void *data = NULL;
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_;
113 iptr = (void*) data;
114 break;
117 if (NULL == iptr)
119 printError(ErrNotExists,"Index %s not exists in INDEX catalog table", name);
120 return ErrNotExists;
122 return OK;
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();
130 void *data = NULL;
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)
137 if (type == hIdx) {
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;
147 break;
150 return OK;
153 int CatalogTableINDEX::getNumIndexes(void *tptr)
155 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
156 ChunkIterator iter = fChunk->getIterator();
157 void *iptr = NULL;
158 int numIndex =0;
159 while (NULL != (iptr = iter.nextElement()))
161 if (((CINDEX*)iptr)->tblPtr_ == tptr) numIndex++;
163 return numIndex;
166 ListIterator CatalogTableINDEXFIELD::getIndexListIterater(char *name)
168 List indexList;
169 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
170 ChunkIterator ifIter = chunk->getIterator();
171 void *data = NULL;
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();
194 void *iptr = NULL;
195 int numIndex =0;
196 int curPos =0;
197 while (NULL != (iptr = iter.nextElement()))
199 if (((CINDEX*)iptr)->tblPtr_ == tptr) curPos++;
200 if ( curPos == position ) return ((CINDEX*)iptr)->indName_;
202 return NULL;
206 void CatalogTableINDEX::getIndexPtrs(void *tptr, char **&array)
208 void *iptr = NULL;
209 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
210 ChunkIterator iter = fChunk->getIterator();
211 int i=0;
212 while (NULL != (iptr = iter.nextElement()))
214 if (((CINDEX*)iptr)->tblPtr_ == tptr)
216 array[i++] = (char*) iptr;
219 return;
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)
263 Chunk *tChunk;
264 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexTableId);
265 ChunkIterator iter = tChunk->getIterator();
266 CINDEXFIELD *fInd=NULL;
267 char *fName =NULL;
268 void *data = NULL;
269 bool isFldInd=false;
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) )
277 fldList.resetIter();
278 while (NULL != (fName = fldList.nextFieldName()))
280 isFldInd=false;
281 fInd=(CINDEXFIELD*)((CINDEX*)data)->fstIndFld_ ;
282 while (fInd)
284 if (0 == strcmp(((CFIELD *) fInd->fieldPtr)->fldName_, fName))
286 isFldInd=true;
287 break;
289 fInd=fInd->next;
291 if(!isFldInd) break;
293 if(isFldInd)
295 printError(ErrAlready, "Index on this field already exists on table \'%s\' by name \'%s\'", ((CTABLE *)tblPtr)->tblName_, ((CINDEX *)data)->indName_);
296 return ErrAlready;
302 tChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
303 fldList.resetIter();
304 int i =0;
305 while (NULL != (fName = fldList.nextFieldName()))
307 DbRetVal rv = OK;
308 fInd=(CINDEXFIELD*)((CINDEX*)indexPtr)->fstIndFld_;
309 while(fInd)
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_;
315 CINDEXFIELD *fldI;
316 while(fInd)
318 fldI=fInd;
319 fInd=fInd->next;
320 tChunk->free(systemDatabase_,fldI);
322 return ErrAlready;
324 fInd=fInd->next;
326 void *fieldptr = tChunk->allocate(systemDatabase_, &rv);
327 if (NULL == fieldptr)
329 printError(rv, "Could not allocate for USER catalog table");
330 return rv;
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);
340 return OK;
343 DbRetVal CatalogTableINDEXFIELD::remove(void *iptr)
345 Chunk *fChunk;
346 fChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
347 ChunkIterator fIter = fChunk->getIterator();
348 void *data = NULL;
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);
359 return OK;
362 DbRetVal CatalogTableINDEXFIELD::getFieldNameAndType(void *index,
363 char *&name, DataType &type)
365 Chunk *ifChunk;
366 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
367 ChunkIterator ifIter = ifChunk->getIterator();
368 void *data = NULL;
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_;
376 return OK;
379 printError(ErrNotExists,"Index %x not exists in catalog table", index);
380 return ErrNotExists;
383 DbRetVal CatalogTableINDEXFIELD::getFieldInfo(void *index, FieldList &list)
385 Chunk *ifChunk;
386 ifChunk = systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
387 ChunkIterator ifIter = ifChunk->getIterator();
388 void *data = NULL;
389 int rowCount =0;
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);
396 FieldDef fldDef;
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_;
408 list.append(fldDef);
410 rowCount++;
412 if (!rowCount) {
413 printError(ErrNotExists,"Index %x not exists in catalog table", index);
414 return ErrNotExists;
416 return OK;
419 void CatalogTableINDEXFIELD::printAllIndex()
421 Chunk *chunk=systemDatabase_->getSystemDatabaseChunk(IndexFieldTableId);
422 ChunkIterator ifIter = chunk->getIterator();
423 void *data = NULL;
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");
432 else
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_);
437 else
439 printf(" <Field Name> %s </Field Name> \n",((CFIELD*)(((CINDEXFIELD*)data)->fieldPtr))->fldName_);
441 strcpy(indexName,((CINDEX*)(((CINDEXFIELD*)data)->indexPtr))->indName_);