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 ***************************************************************************/
17 #include<DatabaseManager.h>
18 #include<DatabaseManagerImpl.h>
22 #include<Transaction.h>
23 #include<CatalogTables.h>
31 DatabaseManagerImpl::~DatabaseManagerImpl()
33 //Note:Databases are closed by the session interface
38 void DatabaseManagerImpl::createLockManager()
40 lMgr_
= new LockManager(systemDatabase_
);
44 void DatabaseManagerImpl::createTransactionManager()
47 tMgr_
= new TransactionManager();
48 tMgr_
->setFirstTrans(systemDatabase_
->getSystemDatabaseTrans(0));
52 DbRetVal
DatabaseManagerImpl::openSystemDatabase()
54 DbRetVal rv
= openDatabase(SYSTEMDB
);
55 if (rv
!= OK
) return rv
;
56 systemDatabase_
= db_
;
58 if (NULL
== systemDatabase_
)
60 printError(ErrAlready
, "Database is already opened");
63 printDebug(DM_Database
, "Opened system database");
64 logFinest(logger
, "Opened system database");
68 DbRetVal
DatabaseManagerImpl::closeSystemDatabase()
71 //make them to point to system database file descriptor
72 //and database pointer
73 db_
= systemDatabase_
;
76 printDebug(DM_Database
, "Closed system database");
77 logFinest(logger
, "Closed System database");
81 DbRetVal
DatabaseManagerImpl::createDatabase(const char *name
, size_t size
)
85 printError(ErrAlready
, "Database is already created");
88 caddr_t rtnAddr
= (caddr_t
) NULL
;
89 shared_memory_id shm_id
= 0;
91 char *startaddr
= (char*)config
.getMapAddress();
92 shared_memory_key key
= 0;
93 if (0 == strcmp(name
, SYSTEMDB
))
96 key
= config
.getSysDbKey();
100 startaddr
= startaddr
+ config
.getMaxSysDbSize();
101 key
= config
.getUserDbKey();
103 shm_id
= os::shm_create(key
, size
, 0666);
106 printError(ErrOS
, "Shared memory create failed");
110 void *shm_ptr
= os::shm_attach(shm_id
, startaddr
, SHM_RND
);
111 rtnAddr
= (caddr_t
) shm_ptr
;
112 if (rtnAddr
< 0 || shm_ptr
== (char*)0xffffffff)
114 printError(ErrOS
, "Shared memory attach returned -ve value %d", rtnAddr
);
117 memset(shm_ptr
, 0, size
);
118 db_
= new Database();
119 printDebug(DM_Database
, "Creating database:%s",name
);
121 //TODO:for user database do not have transtable and processtable mutex
122 db_
->setMetaDataPtr((DatabaseMetaData
*)rtnAddr
);
123 db_
->setDatabaseID(1);
125 db_
->setMaxSize(size
);
126 db_
->setMaxChunks(MAX_CHUNKS
);
127 db_
->initAllocDatabaseMutex();
128 db_
->initTransTableMutex();
129 db_
->initDatabaseMutex();
130 db_
->initProcessTableMutex();
132 //compute the first page after book keeping information
133 size_t offset
= os::alignLong(sizeof (DatabaseMetaData
));
134 //Only for system db chunk array, trans array and proc array will be there
135 if (0 == strcmp(name
, SYSTEMDB
))
137 offset
= offset
+ os::alignLong( MAX_CHUNKS
* sizeof (Chunk
));
138 offset
= offset
+ os::alignLong( config
.getMaxTrans() * sizeof(Transaction
));
139 offset
= offset
+ os::alignLong( config
.getMaxProcs() * sizeof(ProcInfo
));
140 offset
= offset
+ os::alignLong( config
.getMaxProcs() *
141 config
.getMaxThreads() * sizeof(ThreadInfo
));
143 int multiple
= os::floor(offset
/ PAGE_SIZE
);
144 char *curPage
= (((char*)rtnAddr
) + ((multiple
+ 1) * PAGE_SIZE
));
146 db_
->setCurrentPage(curPage
);
147 db_
->setFirstPage(curPage
);
149 if (0 == strcmp(name
, SYSTEMDB
)) return OK
;
151 //Allocate new chunk to store hash index nodes
152 Chunk
*chunkInfo
= createUserChunk(sizeof(HashIndexNode
));
153 if (NULL
== chunkInfo
)
155 printError(ErrSysInternal
, "Failed to allocate hash index nodes chunk");
156 return ErrSysInternal
;
158 printDebug(DM_Database
, "Creating Chunk for storing Hash index nodes %x",
161 db_
->setHashIndexChunk(chunkInfo
);
162 logFinest(logger
, "Created database %s" , name
);
167 DbRetVal
DatabaseManagerImpl::deleteDatabase(const char *name
)
169 shared_memory_id shm_id
= 0;
170 if (0 == strcmp(name
, SYSTEMDB
))
172 shm_id
= os::shm_open(config
.getSysDbKey(), 100, 0666);
173 os::shmctl(shm_id
, IPC_RMID
);
175 shm_id
= os::shm_open(config
.getUserDbKey(), 100, 0666);
176 os::shmctl(shm_id
, IPC_RMID
);
178 logFinest(logger
, "Deleted database %s" , name
);
183 DbRetVal
DatabaseManagerImpl::openDatabase(const char *name
)
185 long size
= config
.getMaxSysDbSize();
186 char *startaddr
= (char*)config
.getMapAddress();
187 if (0 == strcmp(name
, SYSTEMDB
))
189 if (NULL
!=systemDatabase_
)
191 printError(ErrAlready
, "System Database already open");
197 if (NULL
==systemDatabase_
)
199 printError(ErrNotOpen
, "System Database not open");
202 size
= config
.getMaxDbSize();
203 startaddr
= startaddr
+ config
.getMaxSysDbSize();
207 printError(ErrAlready
, "User Database already open");
211 if (0 == strcmp(name
, SYSTEMDB
))
212 attAddr
= ProcessManager::sysAddr
;
214 attAddr
= ProcessManager::usrAddr
;
216 if (ProcessManager::noThreads
== 0)
218 //system db should be opened before user database files
219 caddr_t rtnAddr
= (caddr_t
) NULL
;
221 shared_memory_id shm_id
= 0;
222 shared_memory_key key
= 0;
224 if (0 == strcmp(name
, SYSTEMDB
))
225 key
= config
.getSysDbKey();
227 key
= config
.getUserDbKey();
228 shm_id
= os::shm_open(key
, size
, 0666);
231 printError(ErrOS
, "Shared memory open failed");
235 void *shm_ptr
= os::shm_attach(shm_id
, startaddr
, SHM_RND
);
237 rtnAddr
= (caddr_t
) shm_ptr
;
239 if (rtnAddr
< 0 || shm_ptr
== (char*)0xffffffff)
241 printError(ErrOS
, "Shared memory attach returned -ve value %x %d", shm_ptr
, errno
);
246 db_
= new Database();
247 db_
->setMetaDataPtr((DatabaseMetaData
*)rtnAddr
);
248 if (0 == strcmp(name
, SYSTEMDB
))
249 ProcessManager::sysAddr
= rtnAddr
;
251 ProcessManager::usrAddr
= rtnAddr
;
253 printDebug(DM_Database
, "Opening database: %s", name
);
254 logFinest(logger
, "Opened database %s" , name
);
259 db_
= new Database();
260 printDebug(DM_Database
, "Opening database: %s", name
);
261 db_
->setMetaDataPtr((DatabaseMetaData
*)attAddr
);
262 logFinest(logger
, "Opened database %s" , name
);
266 DbRetVal
DatabaseManagerImpl::closeDatabase()
268 printDebug(DM_Database
, "Closing database: %s",(char*)db_
->getName());
269 logFinest(logger
, "Closed database");
272 printError(ErrAlready
, "Database is already closed\n");
275 //check if this is the last thread to be deregistered.
276 if (ProcessManager::noThreads
== 1)
278 os::shm_detach((char*)db_
->getMetaDataPtr());
283 //Assumes that system database mutex is taken before calling this.
284 Chunk
* DatabaseManagerImpl::createUserChunk(size_t size
)
286 //Allocate new node in system database to store
287 Chunk
*chunk
= getSystemTableChunk(UserChunkTableId
);
288 void *ptr
= chunk
->allocate(systemDatabase_
);
291 printError(ErrNoMemory
, "Allocation failed for User chunk catalog table");
294 Chunk
*chunkInfo
= (Chunk
*)ptr
;
295 chunkInfo
->initMutex();
296 if (0 != size
) chunkInfo
->setSize(size
);
297 if (chunkInfo
->allocSize_
> PAGE_SIZE
)
298 chunkInfo
->curPage_
= db_
->getFreePage(chunkInfo
->allocSize_
);
300 chunkInfo
->curPage_
= db_
->getFreePage();
301 if ( NULL
== chunkInfo
->curPage_
)
303 chunkInfo
->destroyMutex();
304 chunk
->free(db_
, ptr
);
305 printError(ErrNoMemory
, "Database full: No space to allocate from database");
308 PageInfo
* firstPageInfo
= ((PageInfo
*)chunkInfo
->curPage_
);
309 if (chunkInfo
->allocSize_
> PAGE_SIZE
)
311 int multiple
= os::floor(chunkInfo
->allocSize_
/ PAGE_SIZE
);
312 int offset
= ((multiple
+ 1) * PAGE_SIZE
);
313 firstPageInfo
->setPageAsUsed(offset
);
316 firstPageInfo
->setPageAsUsed(chunkInfo
->allocSize_
);
319 VarSizeInfo
*varInfo
= (VarSizeInfo
*)(((char*)firstPageInfo
) + sizeof(PageInfo
));
320 varInfo
->isUsed_
= 0;
321 varInfo
->size_
= PAGE_SIZE
- sizeof(PageInfo
) - sizeof(VarSizeInfo
);
324 chunkInfo
->firstPage_
= chunkInfo
->curPage_
;
327 chunkInfo
->setAllocType(VariableSizeAllocator
);
329 chunkInfo
->setAllocType(FixedSizeAllocator
);
331 //TODO::Generate chunkid::use tableid
332 chunkInfo
->setChunkID(-1);
333 printDebug(DM_Database
, "Creating new User chunk chunkID:%d size: %d firstPage:%x",
334 -1, chunkInfo
->allocSize_
, firstPageInfo
);
340 //Assumes that system database mutex is taken before calling this.
341 DbRetVal
DatabaseManagerImpl::deleteUserChunk(Chunk
*chunk
)
343 //Go to the pages and set them to notUsed
344 Page
*page
= chunk
->firstPage_
;
345 PageInfo
* pageInfo
= ((PageInfo
*)page
);
346 //Here...sure that atleast one page will be there even no tuples
347 //are inserted.so not checking if pageInfo == NULL
348 while( pageInfo
->nextPage_
!= NULL
)
350 PageInfo
*prev
= pageInfo
;
351 pageInfo
= (PageInfo
*)(pageInfo
->nextPage_
);
352 //sets pageInfo->isUsed_ = 0 and pageInfo->hasFreeSpace_ = 0
353 //and initializes the page content to zero
354 if(NULL
== pageInfo
->nextPageAfterMerge_
)
355 os::memset(prev
, 0, PAGE_SIZE
);
358 int size
= (char*) pageInfo
->nextPageAfterMerge_
- (char*) pageInfo
;
359 os::memset(prev
, 0, size
);
361 printDebug(DM_Database
,"deleting user chunk:%x clearing page %x",chunk
, prev
);
363 //The above loop wont execute for the last page
364 //and for the case where table has only one page
365 if(NULL
== pageInfo
->nextPageAfterMerge_
)
366 os::memset(pageInfo
, 0, PAGE_SIZE
);
369 int size
= (char*) pageInfo
->nextPageAfterMerge_
- (char*) pageInfo
;
370 os::memset(pageInfo
, 0, size
);
372 printDebug(DM_Database
,"deleting user chunk:%x clearing page %x",chunk
, pageInfo
);
373 chunk
->chunkID_
= -1;
374 chunk
->allocSize_
= 0;
375 chunk
->curPage_
= NULL
;
376 chunk
->firstPage_
= NULL
;
377 chunk
->destroyMutex();
378 printDebug(DM_Database
,"deleting user chunk:%x",chunk
);
382 //-1 -> Unable to create chunk. No memory
383 //-2 -> Unable to update the catalog tables
384 DbRetVal
DatabaseManagerImpl::createTable(const char *name
, TableDef
&def
)
387 size_t sizeofTuple
= def
.getTupleSize();
388 rv
= systemDatabase_
->getDatabaseMutex();
390 printError(rv
, "Unable to get Database mutex");
393 //create a chunk to store the tuples
394 Chunk
*ptr
= createUserChunk(sizeofTuple
);
397 systemDatabase_
->releaseDatabaseMutex();
398 printError(ErrNoResource
, "Unable to create user chunk");
399 return ErrNoResource
;
401 printDebug(DM_Database
,"Created UserChunk:%x", ptr
);
403 CatalogTableTABLE
cTable(systemDatabase_
);
404 int tblID
= ((Chunk
*)ptr
)->getChunkID();
406 rv
= cTable
.insert(name
, tblID
, sizeofTuple
,
407 def
.getFieldCount(), ptr
, tptr
);
410 deleteUserChunk(ptr
);
411 systemDatabase_
->releaseDatabaseMutex();
412 printError(ErrSysInternal
, "Unable to update catalog table TABLE");
413 return ErrSysInternal
;
415 printDebug(DM_Database
,"Inserted into TABLE:%s",name
);
417 FieldIterator iter
= def
.getFieldIterator();
418 CatalogTableFIELD
cField(systemDatabase_
);
419 rv
= cField
.insert(iter
, tblID
,tptr
);
422 deleteUserChunk(ptr
);
423 void *cptr
, *ttptr
;//Dummy as remove below needs both these OUT params
424 cTable
.remove(name
, cptr
, ttptr
);
425 systemDatabase_
->releaseDatabaseMutex();
426 printError(ErrSysInternal
, "Unable to update catalog table FIELD");
427 return ErrSysInternal
;
429 printDebug(DM_Database
,"Inserted into FIELD:%s",name
);
430 systemDatabase_
->releaseDatabaseMutex();
431 printDebug(DM_Database
,"Table Created:%s",name
);
432 logFinest(logger
, "Table Created %s" , name
);
436 //TODO::If any operation fails in between, then we may have some
437 //dangling tuples, say we have have rows in INDEX table
438 //which will not have any corresponding entries in TABLE
439 //CHANGE the sequence so that it deletes from the bottom as
440 //opposed to start from top as is written now
441 DbRetVal
DatabaseManagerImpl::dropTable(const char *name
)
445 DbRetVal rv
= systemDatabase_
->getDatabaseMutex();
447 printError(ErrSysInternal
, "Unable to get database mutex");
448 return ErrSysInternal
;
451 //remove the entry in TABLE
452 CatalogTableTABLE
cTable(systemDatabase_
);
453 rv
= cTable
.remove(name
, chunk
, tptr
);
455 systemDatabase_
->releaseDatabaseMutex();
456 printError(ErrSysInternal
, "Unable to update catalog table TABLE");
457 return ErrSysInternal
;
459 printDebug(DM_Database
,"Deleted from TABLE:%s",name
);
461 //remove the entries in the FIELD table
462 CatalogTableFIELD
cField(systemDatabase_
);
463 rv
= cField
.remove(tptr
);
465 systemDatabase_
->releaseDatabaseMutex();
466 printError(ErrSysInternal
, "Unable to update catalog table FIELD");
467 return ErrSysInternal
;
469 printDebug(DM_Database
,"Deleted from FIELD:%s",name
);
471 rv
= deleteUserChunk((Chunk
*)chunk
);
473 systemDatabase_
->releaseDatabaseMutex();
474 printError(rv
, "Unable to delete the chunk");
477 printDebug(DM_Database
,"Deleted UserChunk:%x", chunk
);
479 //TODO::check whether indexes are available and drop that also.
480 CatalogTableINDEX
cIndex(systemDatabase_
);
481 int noIndexes
= cIndex
.getNumIndexes(tptr
);
483 for (int i
=1 ; i
<= noIndexes
; i
++) {
484 idxName
= cIndex
.getIndexName(tptr
, i
);
485 dropIndexInt(idxName
, false);
487 Chunk
*chunkNode
= systemDatabase_
->getSystemDatabaseChunk(UserChunkTableId
);
488 chunkNode
->free(systemDatabase_
, (Chunk
*) chunk
);
489 systemDatabase_
->releaseDatabaseMutex();
490 printDebug(DM_Database
, "Deleted Table %s" , name
);
491 logFinest(logger
, "Deleted Table %s" , name
);
495 //Return values: NULL for table not found
496 Table
* DatabaseManagerImpl::openTable(const char *name
)
499 //TODO::store table handles in list so that if it is
500 //not closed by the application. destructor shall close it.
501 TableImpl
*table
= new TableImpl();
503 table
->setSystemDB(systemDatabase_
);
504 table
->setLockManager(lMgr_
);
505 table
->setTrans(&(tMgr_
->trans
));
507 //to store the chunk pointer of table
510 //to store the tuple pointer of the table
513 //TODO::need to take shared lock on the table so that
514 //all ddl operation will be denied on that table
515 //which includes index creation, alter table
517 DbRetVal rv
= systemDatabase_
->getDatabaseMutex();
519 printError(ErrSysInternal
, "Unable to get database mutex");
522 CatalogTableTABLE
cTable(systemDatabase_
);
523 ret
= cTable
.getChunkAndTblPtr(name
, chunk
, tptr
);
526 printError(ErrNotExists
, "Table not exists %s", name
);
529 TABLE
*tTuple
= (TABLE
*)tptr
;
530 table
->setTableInfo(tTuple
->tblName_
, tTuple
->tblID_
, tTuple
->length_
,
531 tTuple
->numFlds_
, tTuple
->numIndexes_
, tTuple
->chunkPtr_
);
533 //get field information from FIELD table
534 CatalogTableFIELD
cField(systemDatabase_
);
535 cField
.getFieldInfo(tptr
, table
->fldList_
);
537 //get the number of indexes on this table
538 //and populate the indexPtr array
539 CatalogTableINDEX
cIndex(systemDatabase_
);
540 table
->numIndexes_
= cIndex
.getNumIndexes(tptr
);
541 if (table
->numIndexes_
)
542 table
->indexPtr_
= new char*[table
->numIndexes_
];
544 table
->indexPtr_
= NULL
;
545 cIndex
.getIndexPtrs(tptr
, table
->indexPtr_
);
547 //HACK:Below works only for one hash index on table
548 if (table
->numIndexes_
== 1)
550 SingleFieldHashIndexInfo
*hIdxInfo
= new SingleFieldHashIndexInfo();
551 CatalogTableINDEXFIELD
cIndexField(systemDatabase_
);
552 cIndexField
.getFieldNameAndType(table
->indexPtr_
[0], hIdxInfo
->fldName
,
554 ChunkIterator citer
= CatalogTableINDEX::getIterator(table
->indexPtr_
[0]);
555 hIdxInfo
->noOfBuckets
= CatalogTableINDEX::getNoOfBuckets(table
->indexPtr_
[0]);
556 hIdxInfo
->buckets
= (Bucket
*)citer
.nextElement();
558 table
->idxInfo
= (IndexInfo
*) hIdxInfo
;
560 systemDatabase_
->releaseDatabaseMutex();
561 printDebug(DM_Database
,"Opening table handle name:%s chunk:%x numIndex:%d",
562 name
, chunk
, table
->numIndexes_
);
563 logFinest(logger
, "Opening Table %s" , name
);
568 //Return values: -1 for table not found
569 void DatabaseManagerImpl::closeTable(Table
*table
)
571 printDebug(DM_Database
,"Closing table handle: %x", table
);
572 if (NULL
== table
) return;
574 logFinest(logger
, "Closing Table");
577 DbRetVal
DatabaseManagerImpl::createIndex(const char *indName
, IndexInitInfo
*info
)
580 if (info
->indType
== hashIndex
)
582 //Assumes info is of type HashIndexInitInfo
583 HashIndexInitInfo
*hInfo
= (HashIndexInitInfo
*) info
;
584 rv
= createHashIndex(indName
, info
->tableName
, info
->list
, hInfo
->bucketSize
);
594 //-1 -> Table does not exists
595 //-2 -> Field does not exists
596 //-3 -> bucketSize is not valid
597 DbRetVal
DatabaseManagerImpl::createHashIndex(const char *indName
, const char *tblName
,
598 FieldNameList
&fldList
, int bucketSize
)
600 //validate the bucket size
601 if (bucketSize
< 100 || bucketSize
> 200000)
603 printError(ErrBadRange
, "Index Bucket size %d not in range 100-200000",
609 DbRetVal rv
= systemDatabase_
->getDatabaseMutex();
612 printError(ErrSysInternal
, "Unable to get database mutex");
613 return ErrSysInternal
;
616 //check whether table exists
617 CatalogTableTABLE
cTable(systemDatabase_
);
618 cTable
.getChunkAndTblPtr(tblName
, chunk
, tptr
);
621 systemDatabase_
->releaseDatabaseMutex();
622 printError(ErrNotExists
, "Table does not exist %s", tblName
);
625 int totFlds
= fldList
.size();
627 //check whether field exists
628 char **fptr
= new char* [totFlds
];
629 CatalogTableFIELD
cField(systemDatabase_
);
630 rv
= cField
.getFieldPtrs(fldList
, tptr
, fptr
);
634 systemDatabase_
->releaseDatabaseMutex();
635 printError(ErrNotExists
, "Field does not exist");
639 //create chunk to store the meta data of the index created
640 //for latches and bucket pointers
641 Chunk
* chunkInfo
= createUserChunk(bucketSize
* sizeof(Bucket
));
642 if (NULL
== chunkInfo
)
645 systemDatabase_
->releaseDatabaseMutex();
646 printError(ErrSysInternal
, "Unable to create chunk");
647 return ErrSysInternal
;
649 //create memory for holding the bucket pointers
650 void *buckets
= chunkInfo
->allocate(db_
);
654 deleteUserChunk(chunkInfo
);
655 systemDatabase_
->releaseDatabaseMutex();
656 printError(ErrNoMemory
, "Unable to allocate memory for bucket");
659 Bucket
*buck
= (Bucket
*) buckets
;
660 initHashBuckets(buck
, bucketSize
);
662 //create chunk to store the hash index nodes
663 Chunk
* hChunk
= createUserChunk(sizeof(HashIndexNode
));
667 deleteUserChunk(chunkInfo
);
668 systemDatabase_
->releaseDatabaseMutex();
669 printError(ErrSysInternal
, "Unable to create chunk for storing hash index nodes");
670 return ErrSysInternal
;
675 void *tupleptr
= NULL
;
676 CatalogTableINDEX
cIndex(systemDatabase_
);
677 DbRetVal ret
= cIndex
.insert(indName
, tptr
, fldList
.size(),
678 chunkInfo
, bucketSize
, hChunk
, tupleptr
);
682 deleteUserChunk(hChunk
);
683 deleteUserChunk(chunkInfo
);
684 systemDatabase_
->releaseDatabaseMutex();
685 printError(ErrSysInternal
, "Catalog table updation failed in INDEX table");
686 return ErrSysInternal
;
688 //add rows to INDEXFIELD
689 CatalogTableINDEXFIELD
cIndexField(systemDatabase_
);
690 ret
= cIndexField
.insert(fldList
, tupleptr
, tptr
, fptr
);
695 deleteUserChunk(hChunk
);
696 deleteUserChunk(chunkInfo
);
697 systemDatabase_
->releaseDatabaseMutex();
698 printError(ErrSysInternal
, "Catalog table updation failed in INDEXFIELD table");
699 return ErrSysInternal
;
702 //TODO::If tuples present in this table, then
703 //create hash index nodes and store it
705 systemDatabase_
->releaseDatabaseMutex();
706 printDebug(DM_Database
, "Creating Hash Index Name:%s tblname:%s buckets:%x",
707 indName
, tblName
, buckets
);
708 logFinest(logger
, "Creating HashIndex %s on %s with bucket size %d",
709 indName
, tblName
, buckets
);
713 void DatabaseManagerImpl::initHashBuckets(Bucket
*buck
, int bucketSize
)
715 os::memset((void*)buck
, 0, bucketSize
* sizeof(Bucket
));
717 for (int i
=0; i
< bucketSize
; i
++)
719 buck
[i
].mutex_
.init();
724 DbRetVal
DatabaseManagerImpl::dropIndex(const char *name
)
726 return dropIndexInt(name
, true);
729 DbRetVal
DatabaseManagerImpl::dropIndexInt(const char *name
, bool takeLock
)
732 void *chunk
= NULL
, *hchunk
= NULL
;
736 rv
= systemDatabase_
->getDatabaseMutex();
739 printError(ErrSysInternal
, "Unable to get database mutex");
740 return ErrSysInternal
;
744 //remove the entry in INDEX
745 CatalogTableINDEX
cIndex(systemDatabase_
);
746 rv
= cIndex
.remove(name
, chunk
, hchunk
, tptr
);
749 if (takeLock
) systemDatabase_
->releaseDatabaseMutex();
750 printError(ErrSysInternal
, "Catalog table updation failed for INDEX table");
751 return ErrSysInternal
;
753 printDebug(DM_Database
, "Removing from INDEX %s",name
);
754 //remove the entries in the INDEXFIELD table
755 CatalogTableINDEXFIELD
cIndexField(systemDatabase_
);
756 rv
= cIndexField
.remove(tptr
);
759 if (takeLock
) systemDatabase_
->releaseDatabaseMutex();
760 printError(ErrSysInternal
, "Catalog table updation failed for INDEX table");
761 return ErrSysInternal
;
763 printDebug(DM_Database
, "Removing from INDEXFIELD %s",name
);
765 //delete the index chunk
766 rv
= deleteUserChunk((Chunk
*)chunk
);
769 if (takeLock
) systemDatabase_
->releaseDatabaseMutex();
770 printError(ErrSysInternal
, "Unable to delete the index chunk");
771 return ErrSysInternal
;
773 //delete the index hash node chunk
774 rv
= deleteUserChunk((Chunk
*)hchunk
);
777 if (takeLock
) systemDatabase_
->releaseDatabaseMutex();
778 printError(ErrSysInternal
, "Unable to delete the index hash node chunk");
779 return ErrSysInternal
;
781 if (takeLock
) systemDatabase_
->releaseDatabaseMutex();
782 Chunk
*chunkNode
= systemDatabase_
->getSystemDatabaseChunk(UserChunkTableId
);
783 chunkNode
->free(systemDatabase_
, (Chunk
*) chunk
);
784 chunkNode
->free(systemDatabase_
, (Chunk
*) hchunk
);
786 //TODO::If tuples present in this table, then
787 //free all hash index nodes for this table.
788 //free all nodes in list of all buckets
791 printDebug(DM_Database
, "Dropped hash index %s",name
);
792 logFinest(logger
, "Deleted Index %s", name
);
796 DbRetVal
DatabaseManagerImpl::registerThread()
801 printError(ErrAlready
, "Process already registered\n");
804 pMgr_
= new ProcessManager(sysDb());
805 return pMgr_
->registerThread();
808 DbRetVal
DatabaseManagerImpl::deregisterThread()
812 printError(ErrBadCall
, "Process already deregistered or never registered\n");
815 DbRetVal rv
= pMgr_
->deregisterThread();
821 ChunkIterator
DatabaseManagerImpl::getSystemTableIterator(CatalogTableID id
)
823 Chunk
*fChunk
= systemDatabase_
->getSystemDatabaseChunk(id
);
824 return fChunk
->getIterator();
827 Chunk
* DatabaseManagerImpl::getSystemTableChunk(CatalogTableID id
)
829 return systemDatabase_
->getSystemDatabaseChunk(id
);