Renamed server directory to storage directory in src
[csql.git] / src / storage / DatabaseManagerImpl.cxx
blobb82890f6c43dec8aef2e30f02bc70eaf1c1b1eb9
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>
31 DatabaseManagerImpl::~DatabaseManagerImpl()
33 //Note:Databases are closed by the session interface
34 Table *tbl = NULL;
35 ListIterator iter = tableHandleList.getIterator();
36 while ((tbl = (Table *)iter.nextElement()) != NULL) delete tbl;
37 tableHandleList.reset();
38 delete tMgr_;
39 delete lMgr_;
42 void DatabaseManagerImpl::createLockManager()
44 lMgr_ = new LockManager(systemDatabase_);
45 return;
48 void DatabaseManagerImpl::createTransactionManager()
51 tMgr_ = new TransactionManager();
52 tMgr_->setFirstTrans(systemDatabase_->getSystemDatabaseTrans(0));
53 return;
55 void DatabaseManagerImpl::setProcSlot()
57 systemDatabase_->setProcSlot(procSlot);
58 db_->setProcSlot(procSlot);
60 DbRetVal DatabaseManagerImpl::openSystemDatabase()
62 DbRetVal rv = openDatabase(SYSTEMDB);
63 if (rv != OK) return rv;
64 systemDatabase_ = db_;
65 db_ = NULL;
66 printDebug(DM_Database, "Opened system database");
67 logFinest(logger, "Opened system database");
68 return OK;
71 DbRetVal DatabaseManagerImpl::closeSystemDatabase()
73 Database *db = db_;
74 //make them to point to system database file descriptor
75 //and database pointer
76 db_ = systemDatabase_;
77 closeDatabase();
78 db_ = db;
79 printDebug(DM_Database, "Closed system database");
80 logFinest(logger, "Closed System database");
81 return OK;
84 DbRetVal DatabaseManagerImpl::createDatabase(const char *name, size_t size)
86 if (NULL != db_ )
88 printError(ErrAlready, "Database is already created");
89 return ErrAlready;
91 caddr_t rtnAddr = (caddr_t) NULL;
92 shared_memory_id shm_id = 0;
94 char *startaddr = (char*)Conf::config.getMapAddress();
95 shared_memory_key key = 0;
96 if (0 == strcmp(name, SYSTEMDB))
99 key = Conf::config.getSysDbKey();
101 else
103 startaddr = startaddr + Conf::config.getMaxSysDbSize();
104 key = Conf::config.getUserDbKey();
106 shm_id = os::shm_create(key, size, 0666);
107 if (-1 == shm_id)
109 if (errno == EEXIST) {
110 printError(ErrOS, "Shared Memory already exists");
112 printError(ErrOS, "Shared memory create failed");
113 return ErrOS;
116 void *shm_ptr = os::shm_attach(shm_id, startaddr, SHM_RND);
117 rtnAddr = (caddr_t) shm_ptr;
118 if (rtnAddr < 0 || shm_ptr == (char*)0xffffffff)
120 printError(ErrOS, "Shared memory attach returned -ve value %d", rtnAddr);
121 return ErrOS;
123 memset(shm_ptr, 0, size );
124 db_ = new Database();
125 printDebug(DM_Database, "Creating database:%s",name);
127 //TODO:for user database do not have transtable and processtable mutex
128 db_->setMetaDataPtr((DatabaseMetaData*)rtnAddr);
129 db_->setDatabaseID(1);
130 db_->setName(name);
131 db_->setMaxSize(size);
132 db_->setNoOfChunks(0);
133 db_->initAllocDatabaseMutex();
134 db_->initTransTableMutex();
135 db_->initDatabaseMutex();
136 db_->initProcessTableMutex();
138 //compute the first page after book keeping information
139 size_t offset = os::alignLong(sizeof (DatabaseMetaData));
140 //Only for system db chunk array, trans array and proc array will be there
141 if (0 == strcmp(name, SYSTEMDB))
143 offset = offset + os::alignLong( MAX_CHUNKS * sizeof (Chunk));
144 offset = offset + os::alignLong( Conf::config.getMaxProcs() * sizeof(Transaction));
145 offset = offset + os::alignLong( Conf::config.getMaxProcs() * sizeof(ThreadInfo));
147 int multiple = os::floor(offset / PAGE_SIZE);
148 char *curPage = (((char*)rtnAddr) + ((multiple + 1) * PAGE_SIZE));
150 db_->setCurrentPage(curPage);
151 db_->setFirstPage(curPage);
153 if (0 == strcmp(name, SYSTEMDB)) return OK;
155 /*Allocate new chunk to store hash index nodes
156 Chunk *chunkInfo = createUserChunk(sizeof(HashIndexNode));
157 if (NULL == chunkInfo)
159 printError(ErrSysInternal, "Failed to allocate hash index nodes chunk");
160 return ErrSysInternal;
162 printDebug(DM_Database, "Creating Chunk for storing Hash index nodes %x",
163 chunkInfo);
165 db_->setHashIndexChunk(chunkInfo);*/
166 logFinest(logger, "Created database %s" , name);
168 return OK;
171 DbRetVal DatabaseManagerImpl::deleteDatabase(const char *name)
173 shared_memory_id shm_id = 0;
174 if (0 == strcmp(name, SYSTEMDB))
176 shm_id = os::shm_open(Conf::config.getSysDbKey(), 100, 0666);
177 os::shmctl(shm_id, IPC_RMID);
178 delete systemDatabase_;
179 systemDatabase_ = NULL;
180 } else {
181 shm_id = os::shm_open(Conf::config.getUserDbKey(), 100, 0666);
182 os::shmctl(shm_id, IPC_RMID);
183 delete db_;
184 db_ = NULL;
186 logFinest(logger, "Deleted database %s" , name);
187 return OK;
190 DbRetVal DatabaseManagerImpl::openDatabase(const char *name)
192 long size = Conf::config.getMaxSysDbSize();
193 char *startaddr = (char*)Conf::config.getMapAddress();
194 if (0 == strcmp(name , SYSTEMDB))
196 if (NULL !=systemDatabase_)
198 printError(ErrAlready, "System Database already open");
199 return ErrAlready;
202 else
204 if (NULL ==systemDatabase_)
206 printError(ErrNotOpen, "System Database not open");
207 return ErrNotOpen;
209 size = Conf::config.getMaxDbSize();
210 startaddr = startaddr + Conf::config.getMaxSysDbSize();
212 if (NULL != db_)
214 printError(ErrAlready, "User Database already open");
215 return ErrAlready;
217 //system db should be opened before user database files
218 caddr_t rtnAddr = (caddr_t) NULL;
220 shared_memory_id shm_id = 0;
221 shared_memory_key key = 0;
223 if (0 == strcmp(name, SYSTEMDB))
224 key = Conf::config.getSysDbKey();
225 else
226 key = Conf::config.getUserDbKey();
229 int ret = ProcessManager::mutex.getLock(-1, false);
230 //If you are not getting lock ret !=0, it means somebody else is there.
231 //he will close the database.
232 if (ret != 0)
234 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
235 return ErrSysInternal;
237 void *shm_ptr = NULL;
238 bool firstThread = false;
239 //printf("PRABA::DEBUG:: opendb %d %s\n", ProcessManager::noThreads, name);
240 if (ProcessManager::noThreads == 0 && 0 == strcmp(name, SYSTEMDB)
241 || ProcessManager::noThreads == 1 && 0 != strcmp(name, SYSTEMDB) ) {
242 shm_id = os::shm_open(key, size, 0666);
243 if (shm_id == -1 )
245 printError(ErrOS, "Shared memory open failed");
246 ProcessManager::mutex.releaseLock(-1, false);
247 return ErrOS;
249 shm_ptr = os::shm_attach(shm_id, startaddr, SHM_RND);
250 if (0 == strcmp(name, SYSTEMDB))
252 firstThread = true;
253 ProcessManager::sysAddr = (char*) shm_ptr;
255 else
257 ProcessManager::usrAddr = (char*) shm_ptr;
259 } else {
260 if (0 == strcmp(name, SYSTEMDB))
261 shm_ptr = ProcessManager::sysAddr;
262 else
263 shm_ptr = ProcessManager::usrAddr;
265 ProcessManager::mutex.releaseLock(-1, false);
268 rtnAddr = (caddr_t) shm_ptr;
270 if (rtnAddr < 0 || shm_ptr == (char*)0xffffffff)
272 printError(ErrOS, "Shared memory attach returned -ve value %x %d", shm_ptr, errno);
273 return ErrOS;
275 db_ = new Database();
276 db_->setMetaDataPtr((DatabaseMetaData*)rtnAddr);
278 if (firstThread) ProcessManager::systemDatabase = db_;
280 printDebug(DM_Database, "Opening database: %s", name);
281 logFinest(logger, "Opened database %s" , name);
282 return OK;
285 DbRetVal DatabaseManagerImpl::closeDatabase()
288 if (NULL == db_)
290 //Database is already closed
291 return OK;
293 printDebug(DM_Database, "Closing database: %s",(char*)db_->getName());
294 //check if this is the last thread to be deregistered
295 int ret = ProcessManager::mutex.getLock(-1, false);
296 //If you are not getting lock ret !=0, it means somebody else is there.
297 //he will close the database.
298 if (ret == 0) {
299 //printf("PRABA::FOR DEBUG closedb %d %s\n", ProcessManager::noThreads, (char*)db_->getName());
300 if (ProcessManager::noThreads == 0 && 0 == strcmp((char*)db_->getName(), SYSTEMDB)
301 || ProcessManager::noThreads == 1 && 0 != strcmp((char*)db_->getName(), SYSTEMDB) ) {
302 os::shm_detach((char*)db_->getMetaDataPtr());
305 ProcessManager::mutex.releaseLock(-1, false);
306 logFinest(logger, "Closed database");
307 delete db_;
308 db_ = NULL;
309 return OK;
311 //Assumes that system database mutex is taken before calling this.
312 Chunk* DatabaseManagerImpl::createUserChunk(size_t size)
314 //Allocate new node in system database to store
315 Chunk *chunk = getSystemTableChunk(UserChunkTableId);
316 DbRetVal rv = OK;
317 void *ptr = chunk->allocate(systemDatabase_, &rv);
318 if (NULL == ptr)
320 printError(rv, "Allocation failed for User chunk catalog table");
321 return NULL;
323 Chunk *chunkInfo = (Chunk*)ptr;
324 chunkInfo->initMutex();
325 if (0 != size) chunkInfo->setSize(size);
326 if (chunkInfo->allocSize_ > PAGE_SIZE)
327 chunkInfo->curPage_ = db_->getFreePage(chunkInfo->allocSize_);
328 else
329 chunkInfo->curPage_ = db_->getFreePage();
330 if ( NULL == chunkInfo->curPage_)
332 chunkInfo->destroyMutex();
333 chunk->free(db_, ptr);
334 printError(ErrNoMemory, "Database full: No space to allocate from database");
335 return NULL;
337 PageInfo* firstPageInfo = ((PageInfo*)chunkInfo->curPage_);
338 if (chunkInfo->allocSize_ > PAGE_SIZE)
340 int multiple = os::floor(chunkInfo->allocSize_ / PAGE_SIZE);
341 int offset = ((multiple + 1) * PAGE_SIZE);
342 firstPageInfo->setPageAsUsed(offset);
344 else
346 firstPageInfo->setPageAsUsed(chunkInfo->allocSize_);
347 char *data = ((char*)firstPageInfo) + sizeof(PageInfo);
348 *(int*)data =0;
350 if (0 == size)
352 VarSizeInfo *varInfo = (VarSizeInfo*)(((char*)firstPageInfo) + sizeof(PageInfo));
353 varInfo->isUsed_ = 0;
354 varInfo->size_ = PAGE_SIZE - sizeof(PageInfo) - sizeof(VarSizeInfo);
357 chunkInfo->firstPage_ = chunkInfo->curPage_;
359 if (0 == size)
360 chunkInfo->setAllocType(VariableSizeAllocator);
361 else
362 chunkInfo->setAllocType(FixedSizeAllocator);
364 //TODO::Generate chunkid::use tableid
365 chunkInfo->setChunkID(-1);
366 db_->incrementChunk();
367 printDebug(DM_Database, "Creating new User chunk chunkID:%d size: %d firstPage:%x",
368 -1, chunkInfo->allocSize_, firstPageInfo);
370 return chunkInfo;
373 //Assumes that system database mutex is taken before calling this.
374 DbRetVal DatabaseManagerImpl::deleteUserChunk(Chunk *chunk)
376 //Go to the pages and set them to notUsed
377 Page *page = chunk->firstPage_;
378 PageInfo* pageInfo = ((PageInfo*)page);
379 //Here...sure that atleast one page will be there even no tuples
380 //are inserted.so not checking if pageInfo == NULL
381 while( pageInfo->nextPage_ != NULL)
383 PageInfo *prev = pageInfo;
384 pageInfo = (PageInfo*)(pageInfo->nextPage_);
385 //sets pageInfo->isUsed_ = 0 and pageInfo->hasFreeSpace_ = 0
386 //and initializes the page content to zero
387 if(NULL == pageInfo->nextPageAfterMerge_)
388 os::memset(prev, 0, PAGE_SIZE);
389 else
391 int size = (char*) pageInfo->nextPageAfterMerge_ - (char*) pageInfo;
392 os::memset(prev, 0, size);
394 printDebug(DM_Database,"deleting user chunk:%x clearing page %x",chunk, prev);
396 //The above loop wont execute for the last page
397 //and for the case where table has only one page
398 if(NULL == pageInfo->nextPageAfterMerge_)
399 os::memset(pageInfo, 0, PAGE_SIZE);
400 else
402 int size = (char*) pageInfo->nextPageAfterMerge_ - (char*) pageInfo;
403 os::memset(pageInfo, 0, size);
405 printDebug(DM_Database,"deleting user chunk:%x clearing page %x",chunk, pageInfo);
406 chunk->chunkID_ = -1;
407 chunk->allocSize_ = 0;
408 chunk->curPage_ = NULL;
409 chunk->firstPage_ = NULL;
410 chunk->destroyMutex();
411 db_->decrementChunk();
412 printDebug(DM_Database,"deleting user chunk:%x",chunk);
413 return OK;
416 //-1 -> Unable to create chunk. No memory
417 //-2 -> Unable to update the catalog tables
418 DbRetVal DatabaseManagerImpl::createTable(const char *name, TableDef &def)
420 DbRetVal rv = OK;
421 int fldCount = def.getFieldCount();
422 //If total field count is less than 32, then 1 integer is used to store all null
423 //information, if it is more then 1 char is used to store null information
424 //of each field
425 //This is to done to reduce cpu cycles for small tables
426 int addSize = 0;
427 if (fldCount < 31) addSize = 4; else addSize = os::align(fldCount);
428 size_t sizeofTuple = os::align(def.getTupleSize())+addSize;
430 rv = systemDatabase_->getDatabaseMutex();
431 if (OK != rv ) {
432 printError(rv, "Unable to get Database mutex");
433 return rv;
436 void *tptr =NULL;
437 void *chunk = NULL;
439 //check whether table already exists
440 CatalogTableTABLE cTable(systemDatabase_);
441 cTable.getChunkAndTblPtr(name, chunk, tptr);
442 if (NULL != tptr)
444 systemDatabase_->releaseDatabaseMutex();
445 printError(ErrAlready, "Table %s already exists", name);
446 return ErrAlready;
449 //create a chunk to store the tuples
450 Chunk *ptr = createUserChunk(sizeofTuple);
451 if (NULL == ptr)
453 systemDatabase_->releaseDatabaseMutex();
454 printError(ErrNoResource, "Unable to create user chunk");
455 return ErrNoResource;
457 printDebug(DM_Database,"Created UserChunk:%x", ptr);
459 //add row to TABLE
460 int tblID = ((Chunk*)ptr)->getChunkID();
461 rv = cTable.insert(name, tblID, sizeofTuple,
462 def.getFieldCount(), ptr, tptr);
463 if (OK != rv)
465 deleteUserChunk(ptr);
466 systemDatabase_->releaseDatabaseMutex();
467 printError(ErrSysInternal, "Unable to update catalog table TABLE");
468 return ErrSysInternal;
470 printDebug(DM_Database,"Inserted into TABLE:%s",name);
471 //add rows to FIELD
472 FieldIterator iter = def.getFieldIterator();
473 CatalogTableFIELD cField(systemDatabase_);
474 rv = cField.insert(iter, tblID ,tptr);
475 if (OK != rv)
477 deleteUserChunk(ptr);
478 void *cptr, *ttptr;//Dummy as remove below needs both these OUT params
479 cTable.remove(name, cptr, ttptr);
480 systemDatabase_->releaseDatabaseMutex();
481 printError(ErrSysInternal, "Unable to update catalog table FIELD");
482 return ErrSysInternal;
484 printDebug(DM_Database,"Inserted into FIELD:%s",name);
485 systemDatabase_->releaseDatabaseMutex();
486 printDebug(DM_Database,"Table Created:%s",name);
487 logFinest(logger, "Table Created %s" , name);
488 return OK;
491 //TODO::If any operation fails in between, then we may have some
492 //dangling tuples, say we have have rows in INDEX table
493 //which will not have any corresponding entries in TABLE
494 //CHANGE the sequence so that it deletes from the bottom as
495 //opposed to start from top as is written now
496 DbRetVal DatabaseManagerImpl::dropTable(const char *name)
498 void *chunk = NULL;
499 void *tptr =NULL;
500 DbRetVal rv = systemDatabase_->getDatabaseMutex();
501 if (OK != rv) {
502 printError(ErrSysInternal, "Unable to get database mutex");
503 return ErrSysInternal;
506 //remove the entry in TABLE
507 CatalogTableTABLE cTable(systemDatabase_);
508 rv = cTable.getChunkAndTblPtr(name, chunk, tptr);
509 if (OK != rv) {
510 systemDatabase_->releaseDatabaseMutex();
511 printError(ErrSysInternal, "Table %s does not exist", name);
512 return ErrSysInternal;
514 rv = lMgr_->getExclusiveLock(chunk, NULL);
515 if (rv !=OK)
517 systemDatabase_->releaseDatabaseMutex();
518 printError(ErrLockTimeOut, "Unable to acquire exclusive lock on the table\n");
519 return rv;
522 rv = cTable.remove(name, chunk, tptr);
523 if (OK != rv) {
524 systemDatabase_->releaseDatabaseMutex();
525 printError(ErrSysInternal, "Unable to update catalog table TABLE");
526 return ErrSysInternal;
528 printDebug(DM_Database,"Deleted from TABLE:%s",name);
530 //remove the entries in the FIELD table
531 CatalogTableFIELD cField(systemDatabase_);
532 rv = cField.remove(tptr);
533 if (OK != rv) {
534 systemDatabase_->releaseDatabaseMutex();
535 printError(ErrSysInternal, "Unable to update catalog table FIELD");
536 return ErrSysInternal;
538 printDebug(DM_Database,"Deleted from FIELD:%s",name);
540 rv = deleteUserChunk((Chunk*)chunk);
541 if (OK != rv) {
542 systemDatabase_->releaseDatabaseMutex();
543 printError(rv, "Unable to delete the chunk");
544 return rv;
546 printDebug(DM_Database,"Deleted UserChunk:%x", chunk);
548 //TODO::check whether indexes are available and drop that also.
549 CatalogTableINDEX cIndex(systemDatabase_);
550 int noIndexes = cIndex.getNumIndexes(tptr);
551 for (int i =1 ; i<= noIndexes; i++) {
552 char *idxName = cIndex.getIndexName(tptr, 1);
553 dropIndexInt(idxName, false);
555 Chunk *chunkNode = systemDatabase_->getSystemDatabaseChunk(UserChunkTableId);
556 chunkNode->free(systemDatabase_, (Chunk *) chunk);
557 systemDatabase_->releaseDatabaseMutex();
558 printDebug(DM_Database, "Deleted Table %s" , name);
559 logFinest(logger, "Deleted Table %s" , name);
560 rv = lMgr_->releaseLock(chunk);
561 if (rv !=OK)
563 printError(ErrLockTimeOut, "Unable to release exclusive lock on the table\n");
564 return rv;
566 return OK;
569 //Return values: NULL for table not found
570 Table* DatabaseManagerImpl::openTable(const char *name)
572 DbRetVal ret = OK;
573 //TODO::store table handles in list so that if it is
574 //not closed by the application. destructor shall close it.
575 TableImpl *table = new TableImpl();
576 table->setDB(db_);
577 table->setSystemDB(systemDatabase_);
578 table->setLockManager(lMgr_);
579 table->setTrans(ProcessManager::getThreadTransAddr(systemDatabase_->procSlot));
581 //to store the chunk pointer of table
582 void *chunk = NULL;
584 //to store the tuple pointer of the table
585 void *tptr =NULL;
587 //TODO::need to take shared lock on the table so that
588 //all ddl operation will be denied on that table
589 //which includes index creation, alter table
591 DbRetVal rv = systemDatabase_->getDatabaseMutex();
592 if (OK != rv) {
593 printError(ErrSysInternal, "Unable to get database mutex");
594 delete table;
595 return NULL;
597 CatalogTableTABLE cTable(systemDatabase_);
598 ret = cTable.getChunkAndTblPtr(name, chunk, tptr);
599 if ( OK != ret)
601 systemDatabase_->releaseDatabaseMutex();
602 delete table;
603 printError(ErrNotExists, "Table not exists %s", name);
604 return NULL;
606 TABLE *tTuple = (TABLE*)tptr;
607 table->setTableInfo(tTuple->tblName_, tTuple->tblID_, tTuple->length_,
608 tTuple->numFlds_, tTuple->numIndexes_, tTuple->chunkPtr_);
609 /*rv = table->lock(true); //take shared lock
610 if (rv !=OK)
612 printError(ErrLockTimeOut, "Unable to acquire shared lock on the table\n");
613 systemDatabase_->releaseDatabaseMutex();
614 delete table;
615 return NULL;
619 if (tTuple->numFlds_ < 31)
621 table->isIntUsedForNULL = true;
622 table->iNullInfo = 0;
623 table->iNotNullInfo =0;
625 else
627 table->isIntUsedForNULL = false;
628 int noFields = os::align(tTuple->numFlds_);
629 table->cNullInfo = (char*) malloc(noFields);
630 table->cNotNullInfo = (char*) malloc(noFields);
631 for (int i =0 ; i < noFields; i++) table->cNullInfo[i] =0;
632 for (int i =0 ; i < noFields; i++) table->cNotNullInfo[i] =0;
636 //get field information from FIELD table
637 CatalogTableFIELD cField(systemDatabase_);
638 cField.getFieldInfo(tptr, table->fldList_);
640 //populate the notnull info
641 FieldIterator fIter = table->fldList_.getIterator();
642 int fldpos=1;
643 while (fIter.hasElement())
645 FieldDef def = fIter.nextElement();
646 if (table->isIntUsedForNULL) {
647 if (def.isNull_) SETBIT(table->iNotNullInfo, fldpos);
649 else {
650 if (def.isNull_) table->cNotNullInfo[fldpos-1] = 1;
652 fldpos++;
655 //get the number of indexes on this table
656 //and populate the indexPtr array
657 CatalogTableINDEX cIndex(systemDatabase_);
658 table->numIndexes_ = cIndex.getNumIndexes(tptr);
659 if (table->numIndexes_) {
660 table->indexPtr_ = new char*[table->numIndexes_];
661 table->idxInfo = new IndexInfo*[table->numIndexes_];
663 else
665 table->indexPtr_ = NULL;
667 cIndex.getIndexPtrs(tptr, table->indexPtr_);
668 for (int i =0 ; i < table->numIndexes_; i++ )
670 HashIndexInfo *hIdxInfo = new HashIndexInfo();
671 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
672 cIndexField.getFieldInfo(table->indexPtr_[i], hIdxInfo->idxFldList);
673 ChunkIterator citer = CatalogTableINDEX::getIterator(table->indexPtr_[i]);
674 hIdxInfo->indexPtr = table->indexPtr_[i];
675 hIdxInfo->noOfBuckets = CatalogTableINDEX::getNoOfBuckets(table->indexPtr_[i]);
676 FieldIterator fIter = hIdxInfo->idxFldList.getIterator();
677 bool firstFld = true;
678 while (fIter.hasElement())
680 FieldDef def = fIter.nextElement();
681 if (firstFld)
683 hIdxInfo->fldOffset = table->fldList_.getFieldOffset(def.fldName_);
684 hIdxInfo->type = table->fldList_.getFieldType(def.fldName_);
685 hIdxInfo->compLength = table->fldList_.getFieldLength(def.fldName_);
686 firstFld = false;
687 }else {
688 hIdxInfo->type = typeComposite;
689 hIdxInfo->compLength = hIdxInfo->compLength +
690 table->fldList_.getFieldLength(def.fldName_);
694 hIdxInfo->isUnique = CatalogTableINDEX::getUnique(table->indexPtr_[i]);
695 hIdxInfo->buckets = (Bucket*)citer.nextElement();
696 table->idxInfo[i] = (IndexInfo*) hIdxInfo;
698 systemDatabase_->releaseDatabaseMutex();
699 // lMgr-> tTuple->chunkPtr_
700 printDebug(DM_Database,"Opening table handle name:%s chunk:%x numIndex:%d",
701 name, chunk, table->numIndexes_);
702 logFinest(logger, "Opening Table %s" , name);
704 tableHandleList.append(table);
706 return table;
711 List DatabaseManagerImpl::getAllTableNames()
713 DbRetVal ret = OK;
714 //to store the tuple pointer of the table
715 void *tptr =NULL;
717 DbRetVal rv = systemDatabase_->getDatabaseMutex();
718 if (OK != rv) {
719 printError(ErrSysInternal, "Unable to get database mutex");
720 List tableList;
721 return tableList;
723 CatalogTableTABLE cTable(systemDatabase_);
724 List tableList = cTable.getTableList();
725 systemDatabase_->releaseDatabaseMutex();
726 return tableList;
732 //Return values: -1 for table not found
733 void DatabaseManagerImpl::closeTable(Table *table)
735 printDebug(DM_Database,"Closing table handle: %x", table);
736 if (NULL == table) return;
737 //table->unlock();
738 tableHandleList.remove(table, false);
739 logFinest(logger, "Closing Table");
742 DbRetVal DatabaseManagerImpl::createIndex(const char *indName, IndexInitInfo *info)
744 DbRetVal rv = OK;
745 if (!info->isUnique && info->isPrimary)
747 printError(ErrBadCall, "Primary key cannot be non unique\n");
748 return ErrBadCall;
750 if (info->indType == hashIndex)
752 //Assumes info is of type HashIndexInitInfo
753 HashIndexInitInfo *hInfo = (HashIndexInitInfo*) info;
754 rv = createHashIndex(indName, info->tableName, info->list, hInfo->bucketSize,
755 info->isUnique, info->isPrimary);
757 else if (info->indType == treeIndex)
759 //TODO::tree index
760 printError(ErrNotYet, "Tree Index not supported\n");
761 return ErrNotYet;
762 }else {
763 printError(ErrBadCall, "Index type not supported\n");
764 return ErrBadCall;
766 return rv;
770 //-1 -> Table does not exists
771 //-2 -> Field does not exists
772 //-3 -> bucketSize is not valid
773 DbRetVal DatabaseManagerImpl::createHashIndex(const char *indName, const char *tblName,
774 FieldNameList &fldList, int bucketSize, bool isUnique, bool isPrimary)
776 //validate the bucket size
777 if (bucketSize < 100 || bucketSize > 200000)
779 printError(ErrBadRange, "Index Bucket size %d not in range 100-200000",
780 bucketSize);
781 return ErrBadRange;
783 int totFlds = fldList.size();
784 if (totFlds == 0)
786 printError(ErrBadCall, "No Field name specified");
787 return ErrBadCall;
789 void *tptr =NULL;
790 void *chunk = NULL;
791 DbRetVal rv = systemDatabase_->getDatabaseMutex();
792 if (OK != rv)
794 printError(ErrSysInternal, "Unable to get database mutex");
795 return ErrSysInternal;
798 //check whether table exists
799 CatalogTableTABLE cTable(systemDatabase_);
800 cTable.getChunkAndTblPtr(tblName, chunk, tptr);
801 if (NULL == tptr)
803 systemDatabase_->releaseDatabaseMutex();
804 printError(ErrNotExists, "Table does not exist %s", tblName);
805 return ErrNotExists;
808 //check whether field exists
809 char **fptr = new char* [totFlds];
810 CatalogTableFIELD cField(systemDatabase_);
811 rv = cField.getFieldPtrs(fldList, tptr, fptr);
812 if (OK != rv)
814 delete[] fptr;
815 systemDatabase_->releaseDatabaseMutex();
816 //TODO::check test cases of dbapi/Index, they give wrong results
817 //if (rv == ErrBadCall) {
818 //// if (isPrimary) printError(ErrBadCall, "Field can have NULL values");
819 //} else {
820 //printError(ErrNotExists, "Field does not exist");
821 //}
822 //return ErrBadCall;
823 if (rv != ErrBadCall) {
824 printError(ErrNotExists, "Field does not exist");
825 return ErrNotExists;
828 for (int i=0; i <totFlds; i++)
830 FIELD* fInfo = (FIELD*)fptr[i];
831 if (fInfo->type_ == typeFloat || fInfo->type_ == typeDouble || fInfo->type_ == typeTimeStamp)
833 printError(ErrBadArg, "HashIndex cannot be created for float or double or timestamp type");
834 delete[] fptr;
835 systemDatabase_->releaseDatabaseMutex();
836 return ErrBadArg;
838 if (!fInfo->isNull_ && isPrimary )
840 printError(ErrBadArg, "Primary Index cannot be created on field without NOTNULL constraint");
841 delete[] fptr;
842 systemDatabase_->releaseDatabaseMutex();
843 return ErrBadArg;
846 //create chunk to store the meta data of the index created
847 //for latches and bucket pointers
848 printDebug(DM_HashIndex, "Creating chunk for storing hash buckets of size %d\n",
849 bucketSize * sizeof(Bucket));
850 Chunk* chunkInfo = createUserChunk(bucketSize * sizeof(Bucket));
851 if (NULL == chunkInfo)
853 delete[] fptr;
854 systemDatabase_->releaseDatabaseMutex();
855 printError(ErrSysInternal, "Unable to create chunk");
856 return ErrSysInternal;
858 //create memory for holding the bucket pointers
859 void *buckets = chunkInfo->allocate(db_, &rv);
860 if (NULL == buckets)
862 delete[] fptr;
863 deleteUserChunk(chunkInfo);
864 systemDatabase_->releaseDatabaseMutex();
865 printError(rv, "Unable to allocate memory for bucket");
866 return rv;
868 Bucket *buck = (Bucket*) buckets;
869 initHashBuckets(buck, bucketSize);
871 //create chunk to store the hash index nodes
872 Chunk* hChunk = createUserChunk(sizeof(HashIndexNode));
873 if (NULL == hChunk)
875 delete[] fptr;
876 deleteUserChunk(chunkInfo);
877 systemDatabase_->releaseDatabaseMutex();
878 printError(ErrSysInternal, "Unable to create chunk for storing hash index nodes");
879 return ErrSysInternal;
882 //add row to INDEX
883 void *tupleptr = NULL;
884 CatalogTableINDEX cIndex(systemDatabase_);
885 rv = cIndex.insert(indName, tptr, fldList.size(), isUnique,
886 chunkInfo, bucketSize, hChunk, tupleptr);
887 if (OK != rv)
889 delete[] fptr;
890 deleteUserChunk(hChunk);
891 deleteUserChunk(chunkInfo);
892 systemDatabase_->releaseDatabaseMutex();
893 printError(ErrSysInternal, "Catalog table updation failed in INDEX table");
894 return ErrSysInternal;
896 //add rows to INDEXFIELD
897 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
898 rv = cIndexField.insert(fldList, tupleptr, tptr, fptr);
900 if (OK != rv)
902 delete[] fptr;
903 cIndex.remove(indName, (void *&)chunkInfo, (void *&)hChunk, (void *&)tupleptr);
904 deleteUserChunk(hChunk);
905 deleteUserChunk(chunkInfo);
906 systemDatabase_->releaseDatabaseMutex();
907 printError(ErrSysInternal, "Catalog table updation failed in INDEXFIELD table");
908 return ErrSysInternal;
910 delete[] fptr;
911 systemDatabase_->releaseDatabaseMutex();
913 //TODO:: Take table lock
915 // Following code is written by Kishor Amballi
916 TableImpl *tbl = (TableImpl *) openTable(tblName);
917 if (! tbl->numTuples()) {
918 printDebug(DM_Database, "Creating Hash Index Name:%s tblname:%s buckets:%x", indName, tblName, buckets);
919 logFinest(logger, "Creating HashIndex %s on %s with bucket size %d", indName, tblName, buckets);
920 return OK;
922 HashIndexInfo *indxInfo = NULL;
923 int i = 0;
924 for (i = 0; i < tbl->numIndexes_; i++) {
925 if(((HashIndexInfo *)tbl->idxInfo[i])->indexPtr == tupleptr) {
926 indxInfo = (HashIndexInfo *) tbl->idxInfo[i];
927 break;
930 void *recPtr = NULL;
931 ChunkIterator chIter = ((Chunk *)chunk)->getIterator();
932 while ((recPtr = chIter.nextElement()) != NULL) {
933 rv = tbl->insertIndexNode(*tbl->trans, tupleptr, indxInfo, recPtr);
934 if (rv == ErrUnique) {
935 closeTable(tbl);
936 dropIndex(indName);
937 return rv;
940 closeTable(tbl);
941 printDebug(DM_Database, "Creating Hash Index Name:%s tblname:%s buckets:%x", indName, tblName, buckets);
942 logFinest(logger, "Creating HashIndex %s on %s with bucket size %d", indName, tblName, buckets);
943 return OK;
946 void DatabaseManagerImpl::initHashBuckets(Bucket *buck, int bucketSize)
948 os::memset((void*)buck, 0, bucketSize * sizeof(Bucket));
950 for (int i=0; i < bucketSize ; i++)
952 buck[i].mutex_.init("Bucket");
954 return;
957 DbRetVal DatabaseManagerImpl::dropIndex(const char *name)
959 return dropIndexInt(name, true);
962 DbRetVal DatabaseManagerImpl::dropIndexInt(const char *name, bool takeLock)
964 DbRetVal rv = OK;
965 void *chunk = NULL, *hchunk = NULL;
966 void *tptr =NULL;
967 int ret = 0;
968 if (takeLock) {
969 rv = systemDatabase_->getDatabaseMutex();
970 if (OK != rv)
972 printError(ErrSysInternal, "Unable to get database mutex");
973 return ErrSysInternal;
977 //remove the entry in INDEX
978 CatalogTableINDEX cIndex(systemDatabase_);
979 rv = cIndex.remove(name, chunk, hchunk, tptr);
980 if (OK != rv)
982 if (takeLock) systemDatabase_->releaseDatabaseMutex();
983 printError(ErrSysInternal, "Catalog table updation failed for INDEX table");
984 return ErrSysInternal;
986 printDebug(DM_Database, "Removing from INDEX %s",name);
987 //remove the entries in the INDEXFIELD table
988 CatalogTableINDEXFIELD cIndexField(systemDatabase_);
989 rv = cIndexField.remove(tptr);
990 if (OK != rv)
992 if (takeLock) systemDatabase_->releaseDatabaseMutex();
993 printError(ErrSysInternal, "Catalog table updation failed for INDEX table");
994 return ErrSysInternal;
996 printDebug(DM_Database, "Removing from INDEXFIELD %s",name);
998 //delete the index chunk
999 rv = deleteUserChunk((Chunk*)chunk);
1000 if (OK != rv)
1002 if (takeLock) systemDatabase_->releaseDatabaseMutex();
1003 printError(ErrSysInternal, "Unable to delete the index chunk");
1004 return ErrSysInternal;
1006 //delete the index hash node chunk
1007 rv = deleteUserChunk((Chunk*)hchunk);
1008 if (OK != rv)
1010 if (takeLock) systemDatabase_->releaseDatabaseMutex();
1011 printError(ErrSysInternal, "Unable to delete the index hash node chunk");
1012 return ErrSysInternal;
1014 if (takeLock) systemDatabase_->releaseDatabaseMutex();
1015 Chunk *chunkNode = systemDatabase_->getSystemDatabaseChunk(UserChunkTableId);
1016 chunkNode->free(systemDatabase_, (Chunk *) chunk);
1017 chunkNode->free(systemDatabase_, (Chunk *) hchunk);
1019 //TODO::If tuples present in this table, then
1020 //free all hash index nodes for this table.
1021 //free all nodes in list of all buckets
1022 //Take table lock
1024 printDebug(DM_Database, "Dropped hash index %s",name);
1025 logFinest(logger, "Deleted Index %s", name);
1026 return OK;
1028 DbRetVal DatabaseManagerImpl::printIndexInfo(char *name)
1030 CatalogTableINDEX cIndex(systemDatabase_);
1031 DbRetVal rv = OK;
1032 void *chunk = NULL, *hchunk = NULL;
1033 void *tptr =NULL;
1034 rv = cIndex.get(name, chunk, hchunk, tptr);
1035 if (OK != rv) return rv;
1036 printf("<IndexName> %s </IndexName>\n", name);
1037 printf("<Unique> %d </Unique>\n", CatalogTableINDEX::getUnique(tptr));
1038 Chunk *ch = (Chunk*) chunk;
1039 printf("<HashBucket>\n");
1040 printf(" <TotalPages> %d </TotalPages>\n", ch->totalPages());
1041 printf(" <TotalBuckets> %d </TotalBuckets> \n", CatalogTableINDEX::getNoOfBuckets(tptr));
1042 printf("</HashBucket>\n");
1044 ch = (Chunk*) hchunk;
1045 printf("<IndexNodes>\n");
1046 printf(" <TotalPages> %d </TotalPages>\n", ch->totalPages());
1047 printf(" <TotalNodes> %d </TotalNodes>\n", ch->getTotalDataNodes());
1048 printf("<IndexNodes>\n");
1049 return OK;
1052 DbRetVal DatabaseManagerImpl::registerThread()
1054 DbRetVal rv = OK;
1055 if (pMgr_ != NULL)
1057 printError(ErrAlready, "Process already registered\n");
1058 return ErrAlready;
1060 pMgr_ = new ProcessManager();
1061 rv = pMgr_->registerThread();
1062 if (rv ==OK) { procSlot = pMgr_->getProcSlot();
1063 printDebug(DM_Process, "Process registed with slot %d\n", procSlot);
1065 return rv;
1068 DbRetVal DatabaseManagerImpl::deregisterThread()
1070 DbRetVal rv = OK;
1071 if (pMgr_ != NULL)
1073 rv = pMgr_->deregisterThread(procSlot);
1074 delete pMgr_;
1075 pMgr_ = NULL;
1077 return rv;
1080 bool DatabaseManagerImpl::isAnyOneRegistered()
1082 if (pMgr_ != NULL) return pMgr_->isAnyOneRegistered();
1083 return true;
1087 void DatabaseManagerImpl::printUsageStatistics()
1089 pMgr_->printUsageStatistics();
1090 tMgr_->printUsageStatistics();
1091 lMgr_->printUsageStatistics();
1094 void DatabaseManagerImpl::printDebugLockInfo()
1096 lMgr_->printDebugInfo();
1099 void DatabaseManagerImpl::printDebugTransInfo()
1101 tMgr_->printDebugInfo(systemDatabase_);
1103 void DatabaseManagerImpl::printDebugProcInfo()
1105 pMgr_->printDebugInfo();
1107 void DatabaseManagerImpl::printDebugChunkInfo()
1109 printf("<NotYetImplemented> </NotYetImplemented>\n");
1111 ChunkIterator DatabaseManagerImpl::getSystemTableIterator(CatalogTableID id)
1113 Chunk *fChunk = systemDatabase_->getSystemDatabaseChunk(id);
1114 return fChunk->getIterator();
1117 Chunk* DatabaseManagerImpl::getSystemTableChunk(CatalogTableID id)
1119 return systemDatabase_->getSystemDatabaseChunk(id);