code reorg
[csql.git] / src / storage / DbMgrIndexImpl.cxx
blobb1ca357167f1782063d4bc7f57285dcd36f3dc22
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<Database.h>
17 #include<DatabaseManager.h>
18 #include<DatabaseManagerImpl.h>
19 #include<os.h>
20 #include<Table.h>
21 #include<TableImpl.h>
22 #include<Transaction.h>
23 #include<CatalogTables.h>
24 #include<Index.h>
25 #include<Lock.h>
26 #include<Debug.h>
27 #include<Config.h>
28 #include<Process.h>
30 DbRetVal DatabaseManagerImpl::renameIndex(const char *oldName,const char *newName)
32 void *chunk = NULL;
33 DbRetVal rv = systemDatabase_->getXCheckpointMutex();
34 if (OK != rv) {
35 printError(ErrSysInternal, "Unable to get database mutex for rename table");
36 return ErrSysInternal;
38 CatalogTableTABLE cTable(systemDatabase_);
39 rv = cTable.renameIndex(oldName,newName);
40 if (OK != rv) {
41 printError(ErrSysInternal, "Unable to rename table");
42 systemDatabase_->releaseCheckpointMutex();
43 return ErrSysInternal;
45 systemDatabase_->releaseCheckpointMutex();
46 return OK;
49 DbRetVal DatabaseManagerImpl::createIndex(const char *indName, IndexInitInfo *info)
51 DbRetVal rv = OK;
52 if (!info->isUnique && info->isPrimary)
54 printError(ErrBadCall, "Primary key cannot be non unique\n");
55 return ErrBadCall;
57 if (!Util::isIdentifier((char*)indName)) {
58 printError(ErrBadArg, "Invalid character for index name");
59 return ErrBadArg;
62 if (info->indType == hashIndex)
64 //Assumes info is of type HashIndexInitInfo
65 HashIndexInitInfo *hInfo = (HashIndexInitInfo*) info;
66 rv = createHashIndex(indName, info->tableName, info->list, hInfo->bucketSize,
67 info->isUnique, info->isPrimary);
69 else if (info->indType == treeIndex)
71 HashIndexInitInfo *hInfo = (HashIndexInitInfo*) info;
72 rv = createTreeIndex(indName, info->tableName, info->list,
73 hInfo->bucketSize, info->isUnique, info->isPrimary);
75 else if (info->indType == trieIndex)
77 HashIndexInitInfo *hInfo = (HashIndexInitInfo*) info;
78 rv = createTrieIndex(indName, info->tableName, info->list,
79 info->isUnique, info->isPrimary);
81 }else {
82 printError(ErrBadCall, "Index type not supported\n");
83 return ErrBadCall;
85 return rv;
89 //-1 -> Table does not exists
90 //-2 -> Field does not exists
91 //-3 -> bucketSize is not valid
92 DbRetVal DatabaseManagerImpl::createHashIndex(const char *indName, const char *tblName,
93 FieldNameList &fldList, int bucketSize, bool isUnique, bool isPrimary)
95 //validate the bucket size
96 if (bucketSize < 100 || bucketSize > 200000)
98 printError(ErrBadRange, "Index Bucket size %d not in range 100-200000",
99 bucketSize);
100 return ErrBadRange;
102 int totFlds = fldList.size();
103 if (totFlds == 0)
105 printError(ErrBadCall, "No Field name specified");
106 return ErrBadCall;
108 void *tptr =NULL;
109 void *chunk = NULL;
110 void *vcchunk = NULL;
111 DbRetVal rv = systemDatabase_->getXCheckpointMutex();
112 if (OK != rv)
114 printError(ErrSysInternal, "Unable to get database mutex");
115 return ErrSysInternal;
118 //check whether table exists
119 CatalogTableTABLE cTable(systemDatabase_);
120 cTable.getChunkAndTblPtr(tblName, chunk, tptr, vcchunk);
121 if (NULL == tptr)
123 systemDatabase_->releaseCheckpointMutex();
124 printError(ErrNotExists, "Table does not exist %s", tblName);
125 return ErrNotExists;
128 //check whether field exists
129 char **fptr = new char* [totFlds];
130 CatalogTableFIELD cField(systemDatabase_);
131 rv = cField.getFieldPtrs(fldList, tptr, fptr);
132 if (OK != rv)
134 delete[] fptr;
135 systemDatabase_->releaseCheckpointMutex();
136 //TODO::check test cases of dbapi/Index, they give wrong results
137 //if (rv == ErrBadCall) {
138 //// if (isPrimary) printError(ErrBadCall, "Field can have NULL values");
139 //} else {
140 //printError(ErrNotExists, "Field does not exist");
141 //}
142 //return ErrBadCall;
143 if (rv != ErrBadCall) {
144 printError(ErrNotExists, "Field does not exist");
145 return ErrNotExists;
148 for (int i=0; i <totFlds; i++)
150 CFIELD* fInfo = (CFIELD*)fptr[i];
151 if (fInfo->type_ == typeFloat || fInfo->type_ == typeDouble || fInfo->type_ == typeTimeStamp)
153 printError(ErrBadArg, "HashIndex cannot be created for float or double or timestamp type");
154 delete[] fptr;
155 systemDatabase_->releaseCheckpointMutex();
156 return ErrBadArg;
158 if (!fInfo->isNull_ && isPrimary )
160 printError(ErrBadArg, "Primary Index cannot be created on field without NOTNULL constraint");
161 delete[] fptr;
162 systemDatabase_->releaseCheckpointMutex();
163 return ErrBadArg;
165 if(isPrimary){fInfo->isPrimary_=true;fInfo->isUnique_=true;}
166 if(isUnique){fInfo->isUnique_=true;}
168 //create chunk to store the meta data of the index created
169 //for latches and bucket pointers
170 printDebug(DM_HashIndex, "Creating chunk for storing hash buckets of size %d\n",
171 bucketSize * sizeof(Bucket));
172 Chunk* chunkInfo = createUserChunk(bucketSize * sizeof(Bucket));
173 if (NULL == chunkInfo)
175 delete[] fptr;
176 systemDatabase_->releaseCheckpointMutex();
177 printError(ErrSysInternal, "Unable to create chunk");
178 return ErrSysInternal;
180 chunkInfo->setChunkName(indName);
181 //create memory for holding the bucket pointers
182 void *buckets = chunkInfo->allocate(db_, &rv);
183 if (NULL == buckets)
185 delete[] fptr;
186 deleteUserChunk(chunkInfo);
187 systemDatabase_->releaseCheckpointMutex();
188 printError(rv, "Unable to allocate memory for bucket");
189 return rv;
191 Bucket *buck = (Bucket*) buckets;
192 initHashBuckets(buck, bucketSize);
194 //create chunk to store the hash index nodes
195 Chunk* hChunk = createUserChunk(sizeof(IndexNode));
196 if (NULL == hChunk)
198 delete[] fptr;
199 deleteUserChunk(chunkInfo);
200 systemDatabase_->releaseCheckpointMutex();
201 printError(ErrSysInternal, "Unable to create chunk for storing hash index nodes");
202 return ErrSysInternal;
205 hChunk->setChunkName(indName);
206 //add row to INDEX
207 void *tupleptr = NULL;
208 CatalogTableINDEX cIndex(systemDatabase_);
209 rv = cIndex.insert(indName, tptr, fldList.size(), isUnique,
210 chunkInfo, bucketSize, hChunk, tupleptr);
211 if (OK != rv)
213 delete[] fptr;
214 deleteUserChunk(hChunk);
215 deleteUserChunk(chunkInfo);
216 systemDatabase_->releaseCheckpointMutex();
217 printError(ErrSysInternal, "Catalog table updation failed in INDEX table");
218 return ErrSysInternal;
220 //add rows to INDEXFIELD
221 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
222 rv = cIndexField.insert(fldList, tupleptr, tptr, fptr);
224 if (OK != rv)
226 delete[] fptr;
227 cIndex.remove(indName, (void *&)chunkInfo, (void *&)hChunk, (void *&)tupleptr);
228 deleteUserChunk(hChunk);
229 deleteUserChunk(chunkInfo);
230 systemDatabase_->releaseCheckpointMutex();
231 printError(ErrSysInternal, "Catalog table updation failed in INDEXFIELD table");
232 return ErrSysInternal;
234 delete[] fptr;
235 systemDatabase_->releaseCheckpointMutex();
237 //TODO:: Take table lock
239 // Following code is written by Kishor Amballi
240 TableImpl *tbl = (TableImpl *) openTable(tblName);
241 if (NULL == tbl) {
242 printError(ErrSysInternal, "Unable to open table %s", tblName);
243 return ErrSysInternal;
245 if (! tbl->numTuples()) {
246 printDebug(DM_Database, "Creating Hash Index Name:%s tblname:%s buckets:%x", indName, tblName, buckets);
247 logFinest(Conf::logger, "Creating HashIndex %s on %s with bucket size %d", indName, tblName, buckets);
248 closeTable(tbl);
249 return OK;
251 HashIndexInfo *indxInfo = NULL;
252 int i = 0;
253 for (i = 0; i < tbl->numIndexes_; i++) {
254 if(((HashIndexInfo *)tbl->idxInfo[i])->indexPtr == tupleptr) {
255 indxInfo = (HashIndexInfo *) tbl->idxInfo[i];
256 break;
259 void *recPtr = NULL;
260 ChunkIterator chIter = ((Chunk *)chunk)->getIterator();
261 tbl->setLoading(true);
262 while ((recPtr = chIter.nextElement()) != NULL) {
263 rv = tbl->insertIndexNode(*tbl->trans, tupleptr, indxInfo, recPtr);
264 if (rv == ErrUnique) {
265 closeTable(tbl);
266 dropIndex(indName);
267 return rv;
270 closeTable(tbl);
271 printDebug(DM_Database, "Creating Hash Index Name:%s tblname:%s buckets:%x", indName, tblName, buckets);
272 logFinest(Conf::logger, "Creating HashIndex %s on %s with bucket size %d", indName, tblName, buckets);
273 return OK;
277 DbRetVal DatabaseManagerImpl::createTreeIndex(const char *indName, const char *tblName,
278 FieldNameList &fldList, int nodeSize, bool isUnique, bool isPrimary)
280 if (nodeSize < 20 || nodeSize > 20000)
282 printError(ErrBadRange,"Tree Index Node size %d not in range 20-20000",
283 nodeSize);
284 return ErrBadRange;
286 int totFlds = fldList.size();
287 if (totFlds == 0) {
288 printError(ErrBadCall, "No Field name specified");
289 return ErrBadCall;
290 }else if (totFlds != 1) {
291 printError(ErrBadCall, "Composite index not supported for Tree");
292 return ErrBadCall;
294 void *tptr =NULL;
295 void *chunk = NULL;
296 void *vcchunk = NULL;
297 DbRetVal rv = systemDatabase_->getXCheckpointMutex();
298 if (OK != rv)
300 printError(ErrSysInternal, "Unable to get database mutex");
301 return ErrSysInternal;
303 //check whether table exists
305 CatalogTableTABLE cTable(systemDatabase_);
306 cTable.getChunkAndTblPtr(tblName, chunk, tptr, vcchunk);
307 if (NULL == tptr)
309 systemDatabase_->releaseCheckpointMutex();
310 printError(ErrNotExists, "Table does not exist %s", tblName);
311 return ErrNotExists;
313 char **fptr = new char* [totFlds];
314 CatalogTableFIELD cField(systemDatabase_);
315 rv = cField.getFieldPtrs(fldList, tptr, fptr);
316 if (OK != rv)
318 delete[] fptr;
319 systemDatabase_->releaseCheckpointMutex();
320 if (rv != ErrBadCall) {
321 printError(ErrNotExists, "Field does not exist");
322 return ErrNotExists;
325 for (int i=0; i <totFlds; i++)
327 CFIELD* fInfo = (CFIELD*)fptr[i];
328 if (!fInfo->isNull_ && isPrimary )
330 printError(ErrBadArg, "Primary Index cannot be created on field without NOTNULL constraint");
331 delete[] fptr;
332 systemDatabase_->releaseCheckpointMutex();
333 return ErrBadArg;
335 if (fInfo->type_ == typeVarchar)
337 printError(ErrBadArg, "Tree Index not supported for varchar type. Use char data type instead.");
338 delete[] fptr;
339 systemDatabase_->releaseCheckpointMutex();
340 return ErrBadArg;
344 int chunkSize = sizeof(TreeNode)+(nodeSize * sizeof(void*));
345 printDebug(DM_HashIndex, "Creating chunk for storing tree nodes of size %d\n", chunkSize);
347 Chunk* chunkInfo = createUserChunk(chunkSize);
348 if (NULL == chunkInfo)
350 delete[] fptr;
351 systemDatabase_->releaseCheckpointMutex();
352 printError(ErrSysInternal, "Unable to create chunk");
353 return ErrSysInternal;
357 void *tupleptr = NULL;
359 CatalogTableINDEX cIndex(systemDatabase_);
360 rv = cIndex.insert(indName, tptr, fldList.size(), isUnique,
361 chunkInfo, nodeSize, NULL, tupleptr);
362 if (OK != rv)
364 delete[] fptr;
365 deleteUserChunk(chunkInfo);
366 systemDatabase_->releaseCheckpointMutex();
367 printError(ErrSysInternal, "Catalog table updation failed in INDEX table");
368 return ErrSysInternal;
370 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
371 rv = cIndexField.insert(fldList, tupleptr, tptr, fptr);
373 if (OK != rv)
375 delete[] fptr;
376 void *hChunk = NULL;
377 cIndex.remove(indName, (void *&)chunkInfo, (void *&)hChunk, (void *&)tupleptr);
378 deleteUserChunk(chunkInfo);
379 systemDatabase_->releaseCheckpointMutex();
380 printError(ErrSysInternal, "Catalog table updation failed in INDEXFIELD table");
381 return ErrSysInternal;
383 delete[] fptr;
384 rv = createIndexNodeForRecords(tblName, tupleptr, chunk);
385 if (rv != OK)
387 dropIndex(indName);
388 systemDatabase_->releaseCheckpointMutex();
389 return rv;
391 systemDatabase_->releaseCheckpointMutex();
392 logFinest(Conf::logger, "Creating TreeIndex %s on %s rv:%d",
393 indName, tblName, rv);
394 return OK;
396 DbRetVal DatabaseManagerImpl::createIndexNodeForRecords(const char* tblName,
397 void *tupleptr, void *chunk)
399 //TODO::if tuples already present in this table, then create tree index '
400 //nodes
401 TableImpl *tbl = (TableImpl *) openTable(tblName);
402 if (NULL == tbl) {
403 printError(ErrSysInternal, "Unable to open table %s", tblName);
404 return ErrSysInternal;
406 if (! tbl->numTuples()) {
407 closeTable(tbl);
408 return OK;
410 HashIndexInfo *indxInfo = NULL;
411 int i = 0;
412 for (i = 0; i < tbl->numIndexes_; i++) {
413 if(((HashIndexInfo *)tbl->idxInfo[i])->indexPtr == tupleptr) {
414 indxInfo = (HashIndexInfo *) tbl->idxInfo[i];
415 break;
418 void *recPtr = NULL;
419 ChunkIterator chIter = ((Chunk *)chunk)->getIterator();
420 tbl->setLoading(true);
421 DbRetVal rv = OK;
422 while ((recPtr = chIter.nextElement()) != NULL) {
423 rv = tbl->insertIndexNode(*tbl->trans, tupleptr, indxInfo, recPtr);
424 if (rv == ErrUnique) {
425 closeTable(tbl);
426 return rv;
429 closeTable(tbl);
430 return OK;
433 DbRetVal DatabaseManagerImpl::createTrieIndex(const char *indName, const char *tblName,
434 FieldNameList &fldList, bool isUnique, bool isPrimary)
436 int totFlds = fldList.size();
437 void *tptr =NULL;
438 char **fptr = new char* [totFlds];
439 DbRetVal rv = validateIndex(tblName, fldList, &tptr, &fptr, isPrimary);
440 if (OK != rv)
442 delete[] fptr;
443 return rv;
445 rv = systemDatabase_->getXCheckpointMutex();
446 if (OK != rv)
448 printError(ErrSysInternal, "Unable to get database mutex");
449 return ErrSysInternal;
452 //below statements are actually setting values in the catalog table
453 //thats why mutex is taken before this stmt. Do not change the order
454 CFIELD* fInfo = (CFIELD*)fptr[0];
455 if(isPrimary){fInfo->isPrimary_=true;fInfo->isUnique_=true;}
456 if(isUnique){fInfo->isUnique_=true;}
458 printDebug(DM_TrieIndex, "Creating chunk for storing trie nodes\n" );
459 Chunk* chunkInfo = createUserChunk(sizeof(TrieNode));
461 //chunk to store the linked list of trie values
462 Chunk* hChunk = createUserChunk(sizeof(IndexNode));
463 if (NULL == chunkInfo || NULL == hChunk)
465 delete[] fptr;
466 if (chunkInfo) deleteUserChunk(chunkInfo);
467 systemDatabase_->releaseCheckpointMutex();
468 printError(ErrSysInternal, "Unable to create trie node chunk");
469 return ErrSysInternal;
471 chunkInfo->setChunkName(indName);
472 hChunk->setChunkName(indName);
473 void *tupleptr = NULL;
474 rv = updateIndexCatalogTables(indName,tptr, fptr, fldList, isUnique,
475 chunkInfo, hChunk , tupleptr);
476 delete[] fptr;
477 if (OK != rv) {
478 printError(ErrSysInternal, "Catalog table updation failed");
479 deleteUserChunk(chunkInfo);
480 deleteUserChunk(hChunk);
481 systemDatabase_->releaseCheckpointMutex();
482 return rv;
485 void *chunk = NULL;
486 void *vcchunk = NULL;
487 CatalogTableTABLE cTable(systemDatabase_);
488 cTable.getChunkAndTblPtr(tblName, chunk, tptr, vcchunk);
489 //create index nodes if records already exist in the table
490 rv = createIndexNodeForRecords(tblName, tupleptr, chunk);
491 if (rv != OK)
493 dropIndex(indName);
494 systemDatabase_->releaseCheckpointMutex();
495 return rv;
497 systemDatabase_->releaseCheckpointMutex();
498 logFinest(Conf::logger, "Creating TrieIndex %s on %s rv:%d",
499 indName, tblName, rv);
500 return OK;
502 DbRetVal DatabaseManagerImpl::validateIndex(const char *tblName,
503 FieldNameList &fldList, void **tptr, char ***fptr,
504 bool isPrimary)
506 int totFlds = fldList.size();
507 if (totFlds != 1)
509 printError(ErrBadCall, "No Field name specified or composite fields specified");
510 return ErrBadCall;
512 void *chunk = NULL;
513 void *vcchunk = NULL;
514 //check whether table exists
515 CatalogTableTABLE cTable(systemDatabase_);
516 cTable.getChunkAndTblPtr(tblName, chunk, *tptr, vcchunk);
517 if (NULL == tptr)
519 printError(ErrNotExists, "Table does not exist %s", tblName);
520 return ErrNotExists;
523 //check whether field exists
524 CatalogTableFIELD cField(systemDatabase_);
525 DbRetVal rv = cField.getFieldPtrs(fldList, *tptr, *fptr);
526 if (OK != rv)
528 if (rv != ErrBadCall) {
529 printError(ErrNotExists, "Field does not exist");
530 return ErrNotExists;
533 CFIELD* fInfo = (CFIELD*)*fptr[0];
534 if (!(fInfo->type_ == typeInt || fInfo->type_ == typeLongLong || fInfo->type_ == typeString || fInfo->type_ == typeVarchar))
536 printError(ErrBadArg, "Trie Index cannot be created for float or double or timestamp type");
537 return ErrBadArg;
539 if (!fInfo->isNull_ && isPrimary )
541 printError(ErrBadArg, "Primary Index cannot be created on field without NOTNULL constraint");
542 return ErrBadArg;
545 return OK;
548 DbRetVal DatabaseManagerImpl::updateIndexCatalogTables(const char *indName,
549 void *tptr, char **fptr, FieldNameList &fldList,
550 bool isUnique, Chunk* chunkInfo, Chunk* hChunk,
551 void *&tupleptr )
553 //void *tupleptr = NULL;
554 CatalogTableINDEX cIndex(systemDatabase_);
555 DbRetVal rv = cIndex.insert(indName, tptr, fldList.size(), isUnique,
556 chunkInfo, 0, hChunk, tupleptr);
557 if (OK != rv)
559 printError(ErrSysInternal, "Catalog table updation failed in INDEX table");
560 return ErrSysInternal;
562 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
563 rv = cIndexField.insert(fldList, tupleptr, tptr, fptr);
564 if (OK != rv)
566 //rollback the previous operation
567 cIndex.remove(indName, (void *&)chunkInfo, (void *&)hChunk, (void *&)tupleptr);
568 printError(ErrSysInternal, "Catalog table updation failed in INDEXFIELD table");
569 return ErrSysInternal;
571 return rv;
574 DbRetVal DatabaseManagerImpl::removeIndexCatalogTables(const char *name, void *&chunk, void *&hchunk, void *&tptr)
576 //remove the entry in INDEX
577 CatalogTableINDEX cIndex(systemDatabase_);
578 DbRetVal rv = cIndex.remove(name, chunk, hchunk, tptr);
579 if (OK != rv)
581 printError(ErrSysInternal, "Catalog table updation failed for INDEX table");
582 return ErrSysInternal;
584 printDebug(DM_Database, "Removing from INDEX %s",name);
585 //remove the entries in the INDEXFIELD table
586 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
587 rv = cIndexField.remove(tptr);
588 if (OK != rv)
590 printError(ErrSysInternal, "Catalog table updation failed for INDEX table");
591 return ErrSysInternal;
593 printDebug(DM_Database, "Removing from INDEXFIELD %s",name);
594 return OK;
596 DbRetVal DatabaseManagerImpl::removeIndexChunks(void* chunk, void* hchunk, IndexType iType)
598 DbRetVal rv = deleteUserChunk((Chunk*)chunk);
599 if (OK != rv)
601 printError(ErrSysInternal, "Unable to delete the index chunk");
602 return ErrSysInternal;
604 //delete the index hash node chunk
605 if (iType == hashIndex || iType == trieIndex) {
606 rv = deleteUserChunk((Chunk*)hchunk);
607 if (OK != rv)
609 printError(ErrSysInternal, "Unable to delete the index hash node chunk");
610 return ErrSysInternal;
613 return OK;
616 void DatabaseManagerImpl::initHashBuckets(Bucket *buck, int bucketSize)
618 os::memset((void*)buck, 0, bucketSize * sizeof(Bucket));
619 char mutName[IDENTIFIER_LENGTH];
620 for (int i=0; i < bucketSize ; i++)
622 sprintf(mutName, "BKT:%d",i);
623 buck[i].mutex_.init(mutName);
625 return;
628 DbRetVal DatabaseManagerImpl::dropIndex(const char *name)
630 return dropIndexInt(name, true);
633 DbRetVal DatabaseManagerImpl::dropIndexInt(const char *name, bool takeLock)
635 DbRetVal rv = OK;
636 void *chunk = NULL, *hchunk = NULL;
637 void *tptr =NULL;
638 int ret = 0;
639 if (takeLock) {
640 rv = systemDatabase_->getXCheckpointMutex();
641 if (OK != rv)
643 printError(ErrSysInternal, "Unable to get database mutex");
644 return ErrSysInternal;
647 rv = removeIndexCatalogTables(name, chunk, hchunk, tptr);
648 if (OK != rv)
650 if (takeLock) systemDatabase_->releaseCheckpointMutex();
651 return rv;
654 CINDEX *iptr = (CINDEX*)tptr;
655 rv = removeIndexChunks(chunk, hchunk, iptr->indexType_);
656 if (OK != rv)
658 if (takeLock) systemDatabase_->releaseCheckpointMutex();
659 return rv;
661 if (takeLock) systemDatabase_->releaseCheckpointMutex();
663 printDebug(DM_Database, "Dropped index %s",name);
664 logFinest(Conf::logger, "Deleted Index %s", name);
665 return OK;
668 void DatabaseManagerImpl::printTreeIndexNodeInfo(char *name, bool flag)
670 CatalogTableINDEX cIndex(systemDatabase_);
671 DbRetVal rv = OK;
672 void *chunk = NULL, *hchunk = NULL;
673 void *tptr =NULL;
674 rv = cIndex.get(name, chunk, hchunk, tptr);
675 if (OK != rv) return;
676 IndexType iType = CatalogTableINDEX::getType(tptr);
677 if (treeIndex != iType)
679 printf("%s is not a tree index\n ");
680 return;
682 Chunk *ch = (Chunk*) chunk;
683 if(flag){ if(hchunk)((TreeNode*) hchunk)->displayAll(); }
684 else {
685 int offset = CatalogTableINDEX::getOffsetOfFirstField(tptr);
686 //if(typeInt != offset) { printf("%s is not on Integer Type Field. To see info Index should be on integer type field. \n "); return;}
687 if(hchunk) ((TreeNode*) hchunk)->displayAll(offset);
690 DbRetVal DatabaseManagerImpl::printIndexDebugInfo(char *name)
692 CatalogTableINDEX cIndex(systemDatabase_);
693 DbRetVal rv = OK;
694 void *chunk = NULL, *hchunk = NULL;
695 void *tptr =NULL;
696 rv = cIndex.get(name, chunk, hchunk, tptr);
697 if (OK != rv) return rv;
698 IndexType iType = CatalogTableINDEX::getType(tptr);
699 if(hashIndex == iType) {
701 else if (treeIndex == iType) {
702 CINDEX *iptr = (CINDEX*)tptr;
703 TreeNode *start = (TreeNode*) iptr->hashNodeChunk_;
704 start->displayAll(0);
706 else if (trieIndex == iType) {
707 ChunkIterator citer = CatalogTableINDEX::getIterator(tptr);
708 TrieNode* start = (TrieNode*)citer.nextElement();
709 if(start) TrieIndex::displayAll(start);
711 else
712 printf("Unknown Index\n");
716 DbRetVal DatabaseManagerImpl::printIndexInfo(char *name)
718 CatalogTableINDEX cIndex(systemDatabase_);
719 DbRetVal rv = OK;
720 void *chunk = NULL, *hchunk = NULL;
721 void *tptr =NULL;
722 rv = cIndex.get(name, chunk, hchunk, tptr);
723 if (OK != rv) return rv;
724 printf("<IndexName> %s </IndexName>\n", name);
725 printf("<Unique> %d </Unique>\n", CatalogTableINDEX::getUnique(tptr));
726 IndexType iType = CatalogTableINDEX::getType(tptr);
727 if(hashIndex == iType)
728 printf("<Type> Hash Index </Type>\n");
729 else if (treeIndex == iType)
730 printf("<Type> Tree Index </Type>\n");
731 else if (trieIndex == iType)
732 printf("<Type> Trie Index </Type>\n");
733 else
734 printf("<Type> Unknown Index </Type>\n");
736 Chunk *ch = (Chunk*) chunk;
737 printf("<HashBucket>\n");
738 printf(" <TotalPages> %d </TotalPages>\n", ch->totalPages());
739 printf(" <TotalBuckets> %d </TotalBuckets> \n", CatalogTableINDEX::getNoOfBuckets(tptr));
740 printf("</HashBucket>\n");
741 printf("<IndexNodes>\n");
742 if(hashIndex == iType){
743 ch = (Chunk*) hchunk;
744 printf(" <TotalPages> %d </TotalPages>\n", ch->totalPages());
745 printf(" <TotalNodes> %d </TotalNodes>\n", ch->getTotalDataNodes());
746 } else if (treeIndex == iType) {
747 printf(" <TotalNodes> %d </TotalNodes>\n", ch->getTotalDataNodes());
748 if(hchunk)
749 printf(" <TotalElements> %lld </TotalElements>\n",((TreeNode*) hchunk)->getTotalElements());
750 else
751 printf(" <TotalElements> 0 </TotalElements>\n");
752 } else if (trieIndex == iType)
754 printf(" <TrieNodes> \n");
755 printf(" <TotalPages> %d </TotalPages>\n", ch->totalPages());
756 printf(" <TotalNodes> %d </TotalNodes>\n", ch->getTotalDataNodes());
757 printf(" </TrieNodes> \n <TrieValues>\n");
758 ch = (Chunk*) hchunk;
759 printf(" <TotalPages> %d </TotalPages>\n", ch->totalPages());
760 printf(" <TotalNodes> %d </TotalNodes>\n", ch->getTotalDataNodes());
761 printf(" </TrieValues>\n");
762 } else
764 printf("Unknown Index type\n");
766 printf("<IndexNodes>\n");
767 return OK;