DatabaseManagerImpl Class Reference

#include <DatabaseManagerImpl.h>

Inheritance diagram for DatabaseManagerImpl:

Inheritance graph
[legend]
Collaboration diagram for DatabaseManagerImpl:

Collaboration graph
[legend]

Public Member Functions

Databasedb ()
DatabasesysDb ()
void setSysDb (Database *db)
void setDb (Database *db)
void setProcSlot ()
TransactionManagertxnMgr ()
LockManagerlockMgr ()
DbRetVal createDatabase (const char *name, size_t size)
DbRetVal deleteDatabase (const char *name)
DbRetVal openDatabase (const char *name)
DbRetVal closeDatabase ()
DbRetVal createTable (const char *name, TableDef &def)
 creates a table in the database
DbRetVal dropTable (const char *name)
 deletes a table from the database
TableopenTable (const char *name)
 opens a table for processing
void closeTable (Table *table)
 closes the table handle passed
DbRetVal createIndex (const char *indName, IndexInitInfo *info)
 creates an index on the specified table.
DbRetVal dropIndex (const char *name)
 deletes the index object
List getAllTableNames ()
 Returns all the tables as list.
DbRetVal registerThread ()
DbRetVal deregisterThread ()
bool isAnyOneRegistered ()
void printUsageStatistics ()
void printDebugLockInfo ()
void printDebugTransInfo ()
void printDebugChunkInfo ()
void printDebugProcInfo ()
DbRetVal printIndexInfo (char *name)

Friends

class SessionImpl

Detailed Description

Definition at line 45 of file DatabaseManagerImpl.h.


Member Function Documentation

DbRetVal DatabaseManagerImpl::closeDatabase (  ) 

Definition at line 293 of file DatabaseManagerImpl.cxx.

References DM_Database, Mutex::getLock(), Database::getMetaDataPtr(), Database::getName(), logFinest, logger, ProcessManager::mutex, ProcessManager::noThreads, OK, printDebug, Mutex::releaseLock(), os::shm_detach(), and SYSTEMDB.

Referenced by SessionImpl::close().

00294 {
00295 
00296     if (NULL == db_) 
00297     {
00298         //Database is already closed
00299         return OK;
00300     }
00301     printDebug(DM_Database, "Closing database: %s",(char*)db_->getName());
00302     //check if this is the last thread to be deregistered
00303     int ret = ProcessManager::mutex.getLock(-1, false);
00304     //If you are not getting lock ret !=0, it means somebody else is there.
00305     //he will close the database.
00306     if (ret == 0) {
00307     //printf("PRABA::FOR DEBUG closedb %d %s\n", ProcessManager::noThreads, (char*)db_->getName());
00308     if (ProcessManager::noThreads == 0 && 0 == strcmp((char*)db_->getName(), SYSTEMDB)
00309        || ProcessManager::noThreads == 1 && 0 != strcmp((char*)db_->getName(),  SYSTEMDB) )  {
00310                 os::shm_detach((char*)db_->getMetaDataPtr());
00311             }
00312     }
00313     ProcessManager::mutex.releaseLock(-1, false);
00314     logFinest(logger, "Closed database");
00315     delete db_;
00316     db_ = NULL;
00317     return OK;
00318 }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::closeTable ( Table table  )  [virtual]

closes the table handle passed

Parameters:
table handle to the table

Implements DatabaseManager.

Definition at line 724 of file DatabaseManagerImpl.cxx.

References DM_Database, logFinest, logger, and printDebug.

Referenced by main().

00725 {
00726     printDebug(DM_Database,"Closing table handle: %x", table);
00727     if (NULL == table) return;
00728     //table->unlock();
00729     delete table;
00730     logFinest(logger, "Closing Table");
00731 }

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::createDatabase ( const char *  name,
size_t  size 
)

Definition at line 84 of file DatabaseManagerImpl.cxx.

References os::alignLong(), Conf::config, DM_Database, ErrAlready, ErrOS, os::floor(), Config::getMaxSysDbSize(), Config::getSysDbKey(), Config::getUserDbKey(), Database::initAllocDatabaseMutex(), Database::initDatabaseMutex(), Database::initProcessTableMutex(), Database::initTransTableMutex(), logFinest, logger, MAX_CHUNKS, OK, PAGE_SIZE, printDebug, printError, Database::setCurrentPage(), Database::setDatabaseID(), Database::setFirstPage(), Database::setMaxSize(), Database::setMetaDataPtr(), Database::setName(), Database::setNoOfChunks(), os::shm_attach(), os::shm_create(), and SYSTEMDB.

Referenced by SessionImpl::initSystemDatabase().

00085 {
00086     if (NULL != db_ )
00087     {
00088         printError(ErrAlready, "Database is already created");
00089         return ErrAlready;
00090     }
00091     caddr_t rtnAddr = (caddr_t) NULL;
00092     shared_memory_id shm_id = 0;
00093 
00094     char *startaddr = (char*)Conf::config.getMapAddress();
00095     shared_memory_key key = 0;
00096     if (0 == strcmp(name, SYSTEMDB))
00097     {
00098         
00099         key = Conf::config.getSysDbKey();
00100     }
00101     else
00102     {
00103         startaddr = startaddr + Conf::config.getMaxSysDbSize();
00104         key = Conf::config.getUserDbKey();
00105     }
00106     shm_id = os::shm_create(key, size, 0666);
00107     if (-1 == shm_id)
00108     {
00109                 if (errno == EEXIST) {
00110                         printError(ErrOS, "Shared Memory already exists");
00111                 }
00112         printError(ErrOS, "Shared memory create failed");
00113         return ErrOS;
00114     }
00115 
00116     void *shm_ptr = os::shm_attach(shm_id, startaddr, SHM_RND);
00117     rtnAddr  = (caddr_t) shm_ptr;
00118     if (rtnAddr < 0 || shm_ptr == (char*)0xffffffff)
00119     {
00120         printError(ErrOS, "Shared memory attach returned -ve value %d", rtnAddr);
00121         return ErrOS;
00122     }
00123     memset(shm_ptr, 0, size );
00124     db_ = new Database();
00125     printDebug(DM_Database, "Creating database:%s",name);
00126 
00127     //TODO:for user database do not have transtable and processtable mutex
00128     db_->setMetaDataPtr((DatabaseMetaData*)rtnAddr);
00129     db_->setDatabaseID(1);
00130     db_->setName(name);
00131     db_->setMaxSize(size);
00132     db_->setNoOfChunks(0);
00133     db_->initAllocDatabaseMutex();
00134     db_->initTransTableMutex();
00135     db_->initDatabaseMutex();
00136     db_->initProcessTableMutex();
00137 
00138     //compute the first page after book keeping information
00139     size_t offset = os::alignLong(sizeof (DatabaseMetaData));
00140     //Only for system db chunk array, trans array and proc array will be there
00141     if (0 == strcmp(name, SYSTEMDB))
00142     {
00143         offset = offset + os::alignLong( MAX_CHUNKS  * sizeof (Chunk));
00144         offset = offset + os::alignLong( Conf::config.getMaxProcs()   * sizeof(Transaction));
00145         offset = offset + os::alignLong( Conf::config.getMaxProcs() * sizeof(ThreadInfo));
00146     }
00147     int multiple = os::floor(offset / PAGE_SIZE);
00148     char *curPage = (((char*)rtnAddr) + ((multiple + 1) * PAGE_SIZE));
00149 
00150     db_->setCurrentPage(curPage);
00151     db_->setFirstPage(curPage);
00152 
00153     if (0 == strcmp(name, SYSTEMDB)) return OK;
00154 
00155     /*Allocate new chunk to store hash index nodes
00156     Chunk *chunkInfo = createUserChunk(sizeof(HashIndexNode));
00157     if (NULL == chunkInfo)
00158     {
00159         printError(ErrSysInternal, "Failed to allocate hash index nodes chunk");
00160         return ErrSysInternal;
00161     }
00162     printDebug(DM_Database, "Creating Chunk for storing Hash index nodes %x",
00163                                                                   chunkInfo);
00164 
00165     db_->setHashIndexChunk(chunkInfo);*/
00166     logFinest(logger, "Created database %s" , name);
00167 
00168     return OK;
00169 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::createIndex ( const char *  indName,
IndexInitInfo info 
) [virtual]

creates an index on the specified table.


Create appropriate derived class object of IndexInitInfo based on the type of
the index created and pass it to this method.

Parameters:
indName index name
info IndexInitInfo

Implements DatabaseManager.

Definition at line 733 of file DatabaseManagerImpl.cxx.

References HashIndexInitInfo::bucketSize, ErrBadCall, ErrNotYet, hashIndex, IndexInitInfo::indType, IndexInitInfo::isPrimary, IndexInitInfo::isUnique, IndexInitInfo::list, OK, printError, IndexInitInfo::tableName, and treeIndex.

00734 {
00735     DbRetVal rv = OK;
00736     if (!info->isUnique && info->isPrimary) 
00737     {
00738         printError(ErrBadCall, "Primary key cannot be non unique\n");
00739         return ErrBadCall;
00740     }
00741     if (info->indType == hashIndex)
00742     {
00743         //Assumes info is of type HashIndexInitInfo
00744         HashIndexInitInfo *hInfo = (HashIndexInitInfo*) info;
00745         rv = createHashIndex(indName, info->tableName, info->list, hInfo->bucketSize,
00746                              info->isUnique, info->isPrimary);
00747     }
00748     else if (info->indType == treeIndex)
00749     {
00750         //TODO::tree index
00751         printError(ErrNotYet, "Tree Index not supported\n");
00752         return ErrNotYet;
00753     }else {
00754         printError(ErrBadCall, "Index type not supported\n");
00755         return ErrBadCall;
00756     }
00757     return rv;
00758 }

DbRetVal DatabaseManagerImpl::createTable ( const char *  name,
TableDef def 
) [virtual]

creates a table in the database

Parameters:
name name of the table
def table definition
Returns:
DbRetVal

Implements DatabaseManager.

Definition at line 426 of file DatabaseManagerImpl.cxx.

References os::align(), DM_Database, ErrAlready, ErrNoResource, ErrSysInternal, CatalogTableTABLE::getChunkAndTblPtr(), Database::getDatabaseMutex(), TableDef::getFieldCount(), TableDef::getFieldIterator(), TableDef::getTupleSize(), CatalogTableFIELD::insert(), CatalogTableTABLE::insert(), logFinest, logger, OK, printDebug, printError, Database::releaseDatabaseMutex(), and CatalogTableTABLE::remove().

00427 {
00428     DbRetVal rv = OK;
00429     int fldCount = def.getFieldCount();
00430     //If total field count is less than 32, then 1 integer is used to store all null
00431     //information, if it is more then 1 char is used to store null information 
00432     //of each field
00433     //This is to done to reduce cpu cycles for small tables
00434     int addSize = 0;
00435     if (fldCount < 31) addSize = 4; else addSize = os::align(fldCount);
00436     size_t sizeofTuple = os::align(def.getTupleSize())+addSize;
00437 
00438     rv = systemDatabase_->getDatabaseMutex();
00439     if (OK != rv ) {
00440         printError(rv, "Unable to get Database mutex");
00441         return rv;
00442     }
00443 
00444     void *tptr =NULL;
00445     void *chunk = NULL;
00446 
00447     //check whether table already exists
00448     CatalogTableTABLE cTable(systemDatabase_);
00449     cTable.getChunkAndTblPtr(name, chunk, tptr);
00450     if (NULL != tptr)
00451     {
00452         systemDatabase_->releaseDatabaseMutex();
00453         printError(ErrAlready, "Table %s already exists", name);
00454         return ErrAlready;
00455     }
00456 
00457     //create a chunk to store the tuples
00458     Chunk *ptr = createUserChunk(sizeofTuple);
00459     if (NULL  == ptr)
00460     {
00461         systemDatabase_->releaseDatabaseMutex();
00462         printError(ErrNoResource, "Unable to create user chunk");
00463         return ErrNoResource;
00464     }
00465     printDebug(DM_Database,"Created UserChunk:%x", ptr);
00466 
00467     //add row to TABLE
00468     int tblID = ((Chunk*)ptr)->getChunkID();
00469     rv = cTable.insert(name, tblID, sizeofTuple,
00470                                    def.getFieldCount(), ptr, tptr);
00471     if (OK != rv)
00472     {
00473         deleteUserChunk(ptr);
00474         systemDatabase_->releaseDatabaseMutex();
00475         printError(ErrSysInternal, "Unable to update catalog table TABLE");
00476         return ErrSysInternal;
00477     }
00478     printDebug(DM_Database,"Inserted into TABLE:%s",name);
00479     //add rows to FIELD
00480     FieldIterator iter = def.getFieldIterator();
00481     CatalogTableFIELD cField(systemDatabase_);
00482     rv = cField.insert(iter, tblID ,tptr);
00483     if (OK != rv)
00484     {
00485         deleteUserChunk(ptr);
00486         void *cptr, *ttptr;//Dummy as remove below needs both these OUT params
00487         cTable.remove(name, cptr, ttptr);
00488         systemDatabase_->releaseDatabaseMutex();
00489         printError(ErrSysInternal, "Unable to update catalog table FIELD");
00490         return ErrSysInternal;
00491     }
00492     printDebug(DM_Database,"Inserted into FIELD:%s",name);
00493     systemDatabase_->releaseDatabaseMutex();
00494     printDebug(DM_Database,"Table Created:%s",name);
00495     logFinest(logger, "Table Created %s" , name);
00496     return OK;
00497 }

Here is the call graph for this function:

Database* DatabaseManagerImpl::db (  )  [inline]

Definition at line 87 of file DatabaseManagerImpl.h.

Referenced by SessionImpl::initSystemDatabase(), main(), setDb(), and setSysDb().

00087 { return db_; }

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::deleteDatabase ( const char *  name  ) 

Definition at line 175 of file DatabaseManagerImpl.cxx.

References Conf::config, logFinest, logger, OK, os::shm_open(), os::shmctl(), and SYSTEMDB.

Referenced by SessionImpl::destroySystemDatabase().

00176 {
00177     shared_memory_id shm_id = 0;
00178     if (0 == strcmp(name, SYSTEMDB))
00179     {
00180         shm_id = os::shm_open(Conf::config.getSysDbKey(), 100, 0666);
00181         os::shmctl(shm_id, IPC_RMID);
00182                 delete systemDatabase_;
00183                 systemDatabase_ = NULL;
00184     } else {
00185         shm_id = os::shm_open(Conf::config.getUserDbKey(), 100, 0666);
00186         os::shmctl(shm_id, IPC_RMID);
00187                 delete db_;
00188                 db_ = NULL;
00189     }
00190     logFinest(logger, "Deleted database %s" , name);
00191     return OK;
00192 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::deregisterThread (  ) 

Definition at line 1039 of file DatabaseManagerImpl.cxx.

References ProcessManager::deregisterThread(), and OK.

Referenced by SessionImpl::close().

01040 {
01041     DbRetVal rv = OK;
01042     if (pMgr_ != NULL) 
01043     {
01044         rv = pMgr_->deregisterThread(procSlot);
01045         delete pMgr_;
01046         pMgr_ = NULL;
01047     }
01048     return rv;
01049 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::dropIndex ( const char *  name  )  [virtual]

deletes the index object

Parameters:
name index name

Implements DatabaseManager.

Definition at line 928 of file DatabaseManagerImpl.cxx.

00929 {
00930     return dropIndexInt(name, true);
00931 }

DbRetVal DatabaseManagerImpl::dropTable ( const char *  name  )  [virtual]

deletes a table from the database

Parameters:
name name of the table
Returns:
DbRetVal

Implements DatabaseManager.

Definition at line 504 of file DatabaseManagerImpl.cxx.

References DM_Database, ErrLockTimeOut, ErrSysInternal, Chunk::free(), CatalogTableTABLE::getChunkAndTblPtr(), Database::getDatabaseMutex(), LockManager::getExclusiveLock(), CatalogTableINDEX::getIndexName(), CatalogTableINDEX::getNumIndexes(), Database::getSystemDatabaseChunk(), logFinest, logger, OK, printDebug, printError, Database::releaseDatabaseMutex(), LockManager::releaseLock(), CatalogTableFIELD::remove(), CatalogTableTABLE::remove(), and UserChunkTableId.

Referenced by main().

00505 {
00506     void *chunk = NULL;
00507     void *tptr =NULL;
00508     DbRetVal rv = systemDatabase_->getDatabaseMutex();
00509     if (OK != rv) {
00510         printError(ErrSysInternal, "Unable to get database mutex");
00511         return ErrSysInternal;
00512     }
00513 
00514     //remove the entry in TABLE
00515     CatalogTableTABLE cTable(systemDatabase_);
00516     rv = cTable.getChunkAndTblPtr(name, chunk, tptr);
00517     if (OK != rv) {
00518         systemDatabase_->releaseDatabaseMutex();
00519         printError(ErrSysInternal, "Table %s does not exist", name);
00520         return ErrSysInternal;
00521     }
00522     rv = lMgr_->getExclusiveLock(chunk, NULL);
00523     if (rv !=OK)
00524     {
00525         systemDatabase_->releaseDatabaseMutex();
00526         printError(ErrLockTimeOut, "Unable to acquire exclusive lock on the table\n");
00527         return rv;
00528     }
00529 
00530     rv = cTable.remove(name, chunk, tptr);
00531     if (OK != rv) {
00532         systemDatabase_->releaseDatabaseMutex();
00533         printError(ErrSysInternal, "Unable to update catalog table TABLE");
00534         return ErrSysInternal;
00535     }
00536     printDebug(DM_Database,"Deleted from TABLE:%s",name);
00537 
00538     //remove the entries in the FIELD table
00539     CatalogTableFIELD cField(systemDatabase_);
00540     rv = cField.remove(tptr);
00541     if (OK != rv) {
00542         systemDatabase_->releaseDatabaseMutex();
00543         printError(ErrSysInternal, "Unable to update catalog table FIELD");
00544         return ErrSysInternal;
00545     }
00546     printDebug(DM_Database,"Deleted from FIELD:%s",name);
00547 
00548     rv = deleteUserChunk((Chunk*)chunk);
00549     if (OK != rv) {
00550         systemDatabase_->releaseDatabaseMutex();
00551         printError(rv, "Unable to delete the chunk");
00552         return rv;
00553     }
00554     printDebug(DM_Database,"Deleted UserChunk:%x", chunk);
00555 
00556     //TODO::check whether indexes are available and drop that also.
00557     CatalogTableINDEX cIndex(systemDatabase_);
00558     int noIndexes = cIndex.getNumIndexes(tptr);
00559     for (int i =1 ; i<= noIndexes; i++) {
00560         char *idxName = cIndex.getIndexName(tptr, 1);
00561         dropIndexInt(idxName, false);
00562     }
00563     Chunk *chunkNode = systemDatabase_->getSystemDatabaseChunk(UserChunkTableId);
00564     chunkNode->free(systemDatabase_, (Chunk *) chunk);
00565     systemDatabase_->releaseDatabaseMutex();
00566     printDebug(DM_Database, "Deleted Table %s" , name);
00567     logFinest(logger, "Deleted Table %s" , name);
00568     rv = lMgr_->releaseLock(chunk);
00569     if (rv !=OK)
00570     {
00571         printError(ErrLockTimeOut, "Unable to release exclusive lock on the table\n");
00572         return rv;
00573     }
00574     return OK;
00575 }

Here is the call graph for this function:

Here is the caller graph for this function:

List DatabaseManagerImpl::getAllTableNames (  )  [virtual]

Returns all the tables as list.

Returns:
List of table names

Implements DatabaseManager.

Definition at line 702 of file DatabaseManagerImpl.cxx.

References ErrSysInternal, Database::getDatabaseMutex(), CatalogTableTABLE::getTableList(), OK, printError, and Database::releaseDatabaseMutex().

Referenced by handleEchoAndComment(), and main().

00703 {
00704     DbRetVal ret = OK;
00705     //to store the tuple pointer of the table
00706     void *tptr =NULL;
00707 
00708     DbRetVal rv = systemDatabase_->getDatabaseMutex();
00709     if (OK != rv) {
00710         printError(ErrSysInternal, "Unable to get database mutex");
00711         List tableList;
00712         return tableList;
00713     }
00714     CatalogTableTABLE cTable(systemDatabase_);
00715     List tableList = cTable.getTableList();
00716     systemDatabase_->releaseDatabaseMutex();
00717     return tableList;
00718 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool DatabaseManagerImpl::isAnyOneRegistered (  ) 

Definition at line 1051 of file DatabaseManagerImpl.cxx.

References ProcessManager::isAnyOneRegistered().

Referenced by main().

01052 {
01053     if (pMgr_ != NULL) return pMgr_->isAnyOneRegistered();
01054     return true;
01055 }

Here is the call graph for this function:

Here is the caller graph for this function:

LockManager* DatabaseManagerImpl::lockMgr (  )  [inline]

Definition at line 94 of file DatabaseManagerImpl.h.

Referenced by SessionImpl::commit(), SessionImpl::rollback(), and SessionImpl::startTransaction().

00094 { return lMgr_; }

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::openDatabase ( const char *  name  ) 

Definition at line 198 of file DatabaseManagerImpl.cxx.

References Conf::config, DM_Database, ErrAlready, ErrNotOpen, ErrOS, ErrSysInternal, Mutex::getLock(), Config::getMaxDbSize(), Config::getMaxSysDbSize(), Config::getSysDbKey(), Config::getUserDbKey(), logFinest, logger, ProcessManager::mutex, ProcessManager::noThreads, OK, printDebug, printError, Mutex::releaseLock(), Database::setMetaDataPtr(), os::shm_attach(), os::shm_open(), size, ProcessManager::sysAddr, ProcessManager::systemDatabase, SYSTEMDB, and ProcessManager::usrAddr.

Referenced by SessionImpl::open().

00199 {
00200     long size = Conf::config.getMaxSysDbSize();
00201     char *startaddr = (char*)Conf::config.getMapAddress();
00202     if (0 == strcmp(name , SYSTEMDB))
00203     {
00204         if (NULL !=systemDatabase_)
00205         {
00206             printError(ErrAlready, "System Database already open");
00207             return ErrAlready;
00208         }
00209     }
00210     else
00211     {
00212         if (NULL ==systemDatabase_)
00213         {
00214             printError(ErrNotOpen, "System Database not open");
00215             return ErrNotOpen;
00216         }
00217         size = Conf::config.getMaxDbSize();
00218         startaddr = startaddr +  Conf::config.getMaxSysDbSize();
00219     }
00220     if (NULL != db_)
00221     {
00222         printError(ErrAlready, "User Database already open");
00223         return ErrAlready;
00224     }
00225     //system db should be opened before user database files 
00226     caddr_t rtnAddr = (caddr_t) NULL;
00227 
00228     shared_memory_id shm_id = 0;
00229     shared_memory_key key = 0;
00230 
00231     if (0 == strcmp(name, SYSTEMDB))
00232         key = Conf::config.getSysDbKey();
00233     else
00234        key = Conf::config.getUserDbKey();
00235 
00236 
00237     int ret = ProcessManager::mutex.getLock(-1, false);
00238     //If you are not getting lock ret !=0, it means somebody else is there.
00239     //he will close the database.
00240     if (ret != 0)
00241     {
00242         printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
00243         return ErrSysInternal;
00244     }
00245     void *shm_ptr = NULL;
00246     bool firstThread = false;
00247     //printf("PRABA::DEBUG:: opendb %d %s\n", ProcessManager::noThreads, name);
00248     if (ProcessManager::noThreads == 0 && 0 == strcmp(name, SYSTEMDB)
00249        || ProcessManager::noThreads == 1 && 0 != strcmp(name, SYSTEMDB) ) {
00250        shm_id = os::shm_open(key, size, 0666);
00251        if (shm_id == -1 )
00252        {
00253            printError(ErrOS, "Shared memory open failed");
00254            ProcessManager::mutex.releaseLock(-1, false);
00255            return ErrOS;
00256        }
00257        shm_ptr = os::shm_attach(shm_id, startaddr, SHM_RND);
00258        if (0 == strcmp(name, SYSTEMDB))
00259        {
00260              firstThread = true;
00261              ProcessManager::sysAddr = (char*) shm_ptr;
00262        }
00263        else
00264        {
00265               ProcessManager::usrAddr = (char*) shm_ptr;
00266        }
00267     } else {
00268         if (0 == strcmp(name, SYSTEMDB))
00269               shm_ptr = ProcessManager::sysAddr;
00270         else
00271               shm_ptr = ProcessManager::usrAddr;
00272     }
00273     ProcessManager::mutex.releaseLock(-1, false);
00274 
00275 
00276     rtnAddr  = (caddr_t) shm_ptr;
00277 
00278     if (rtnAddr < 0 || shm_ptr == (char*)0xffffffff)
00279     {
00280         printError(ErrOS, "Shared memory attach returned -ve value %x %d", shm_ptr, errno);
00281         return ErrOS;
00282     } 
00283     db_ = new Database();
00284     db_->setMetaDataPtr((DatabaseMetaData*)rtnAddr);
00285 
00286     if (firstThread) ProcessManager::systemDatabase = db_;
00287 
00288     printDebug(DM_Database, "Opening database: %s", name);
00289     logFinest(logger, "Opened database %s" , name);
00290     return OK;
00291 }

Here is the call graph for this function:

Here is the caller graph for this function:

Table * DatabaseManagerImpl::openTable ( const char *  name  )  [virtual]

opens a table for processing

Parameters:
name name of the table
Returns:
DbRetVal

Implements DatabaseManager.

Definition at line 578 of file DatabaseManagerImpl.cxx.

References os::align(), TABLE::chunkPtr_, TableImpl::cNotNullInfo, TableImpl::cNullInfo, DM_Database, ErrNotExists, ErrSysInternal, TableImpl::fldList_, CatalogTableTABLE::getChunkAndTblPtr(), Database::getDatabaseMutex(), CatalogTableFIELD::getFieldInfo(), FieldList::getFieldLength(), CatalogTableINDEXFIELD::getFieldNameAndType(), FieldList::getFieldOffset(), CatalogTableINDEX::getIndexPtrs(), CatalogTableINDEX::getIterator(), FieldList::getIterator(), CatalogTableINDEX::getNoOfBuckets(), CatalogTableINDEX::getNumIndexes(), ProcessManager::getThreadTransAddr(), CatalogTableINDEX::getUnique(), FieldIterator::hasElement(), TableImpl::idxInfo, TableImpl::indexPtr_, TableImpl::iNotNullInfo, TableImpl::iNullInfo, TableImpl::isIntUsedForNULL, TABLE::length_, logFinest, logger, ChunkIterator::nextElement(), FieldIterator::nextElement(), TABLE::numFlds_, TableImpl::numIndexes_, TABLE::numIndexes_, OK, printDebug, printError, Database::procSlot, Database::releaseDatabaseMutex(), SETBIT, TableImpl::setDB(), TableImpl::setLockManager(), TableImpl::setSystemDB(), TableImpl::setTableInfo(), TableImpl::setTrans(), TABLE::tblID_, and TABLE::tblName_.

Referenced by main().

00579 {
00580     DbRetVal ret = OK;
00581     //TODO::store table handles in list so that if it is
00582     //not closed by the application. destructor shall close it.
00583     TableImpl *table = new TableImpl();
00584     table->setDB(db_);
00585     table->setSystemDB(systemDatabase_);
00586     table->setLockManager(lMgr_);
00587     table->setTrans(ProcessManager::getThreadTransAddr(systemDatabase_->procSlot));
00588 
00589     //to store the chunk pointer of table
00590     void *chunk = NULL;
00591 
00592     //to store the tuple pointer of the table
00593     void *tptr =NULL;
00594 
00595     //TODO::need to take shared lock on the table so that
00596     //all ddl operation will be denied on that table
00597     //which includes index creation, alter table
00598 
00599     DbRetVal rv = systemDatabase_->getDatabaseMutex();
00600     if (OK != rv) {
00601         printError(ErrSysInternal, "Unable to get database mutex");
00602         delete table;
00603         return NULL;
00604     }
00605     CatalogTableTABLE cTable(systemDatabase_);
00606     ret = cTable.getChunkAndTblPtr(name, chunk, tptr);
00607     if ( OK != ret)
00608     {
00609         systemDatabase_->releaseDatabaseMutex();
00610         delete table;
00611         printError(ErrNotExists, "Table not exists %s", name);
00612         return NULL;
00613     }
00614     TABLE *tTuple = (TABLE*)tptr;
00615     table->setTableInfo(tTuple->tblName_, tTuple->tblID_, tTuple->length_,
00616                         tTuple->numFlds_, tTuple->numIndexes_, tTuple->chunkPtr_);
00617     /*rv = table->lock(true); //take shared lock
00618     if (rv !=OK)
00619     {
00620         printError(ErrLockTimeOut, "Unable to acquire shared lock on the table\n");
00621         systemDatabase_->releaseDatabaseMutex();
00622         delete table;
00623         return NULL;
00624     }*/
00625 
00626 
00627     if (tTuple->numFlds_ < 31) 
00628     { 
00629         table->isIntUsedForNULL = true;
00630         table->iNullInfo = 0;
00631         table->iNotNullInfo =0;
00632     }
00633     else
00634     {
00635         table->isIntUsedForNULL = false;
00636         int noFields = os::align(tTuple->numFlds_);
00637         table->cNullInfo = (char*) malloc(noFields);
00638         table->cNotNullInfo = (char*) malloc(noFields);
00639         for (int i =0 ; i < noFields; i++) table->cNullInfo[i] =0;
00640         for (int i =0 ; i < noFields; i++) table->cNotNullInfo[i] =0;
00641 
00642     }
00643 
00644     //get field information from FIELD table
00645     CatalogTableFIELD cField(systemDatabase_);
00646     cField.getFieldInfo(tptr, table->fldList_);
00647 
00648     //populate the notnull info
00649     FieldIterator fIter = table->fldList_.getIterator();
00650     int fldpos=1;
00651     while (fIter.hasElement())
00652     {
00653         FieldDef def = fIter.nextElement();
00654         if (table->isIntUsedForNULL) {
00655             if (def.isNull_) SETBIT(table->iNotNullInfo, fldpos);
00656         }
00657         else {
00658             if (def.isNull_) table->cNotNullInfo[fldpos-1] = 1;
00659         }
00660         fldpos++;
00661    } 
00662 
00663     //get the number of indexes on this table
00664     //and populate the indexPtr array
00665     CatalogTableINDEX cIndex(systemDatabase_);
00666     table->numIndexes_ = cIndex.getNumIndexes(tptr);
00667     if (table->numIndexes_) {
00668         table->indexPtr_ = new char*[table->numIndexes_];
00669         table->idxInfo = new IndexInfo*[table->numIndexes_];
00670     }
00671     else
00672     {
00673         table->indexPtr_ = NULL;
00674     }
00675     cIndex.getIndexPtrs(tptr, table->indexPtr_);
00676     for (int i =0 ; i < table->numIndexes_; i++ )
00677     {
00678         SingleFieldHashIndexInfo *hIdxInfo = new SingleFieldHashIndexInfo();
00679         CatalogTableINDEXFIELD cIndexField(systemDatabase_);
00680         cIndexField.getFieldNameAndType(table->indexPtr_[i], hIdxInfo->fldName,
00681                                                       hIdxInfo->type);
00682         ChunkIterator citer = CatalogTableINDEX::getIterator(table->indexPtr_[i]);
00683         hIdxInfo->noOfBuckets = CatalogTableINDEX::getNoOfBuckets(table->indexPtr_[i]);
00684         hIdxInfo->isUnique = CatalogTableINDEX::getUnique(table->indexPtr_[i]);
00685         hIdxInfo->buckets = (Bucket*)citer.nextElement();
00686         hIdxInfo->offset = table->fldList_.getFieldOffset(hIdxInfo->fldName);
00687         hIdxInfo->length = table->fldList_.getFieldLength(hIdxInfo->fldName);
00688         table->idxInfo[i] = (IndexInfo*) hIdxInfo;
00689     }
00690     systemDatabase_->releaseDatabaseMutex();
00691    // lMgr->  tTuple->chunkPtr_
00692     printDebug(DM_Database,"Opening table handle name:%s chunk:%x numIndex:%d",
00693                                          name, chunk, table->numIndexes_);
00694     logFinest(logger, "Opening Table %s" , name);
00695 
00696         
00697     return table;
00698 }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::printDebugChunkInfo (  ) 

Definition at line 1078 of file DatabaseManagerImpl.cxx.

01079 {
01080     printf("<NotYetImplemented> </NotYetImplemented>\n");
01081 }

void DatabaseManagerImpl::printDebugLockInfo (  ) 

Definition at line 1065 of file DatabaseManagerImpl.cxx.

References LockManager::printDebugInfo().

Referenced by main().

01066 {
01067     lMgr_->printDebugInfo();
01068 }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::printDebugProcInfo (  ) 

Definition at line 1074 of file DatabaseManagerImpl.cxx.

References ProcessManager::printDebugInfo().

Referenced by main().

01075 {
01076     pMgr_->printDebugInfo();
01077 }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::printDebugTransInfo (  ) 

Definition at line 1070 of file DatabaseManagerImpl.cxx.

References TransactionManager::printDebugInfo().

Referenced by main().

01071 {
01072     tMgr_->printDebugInfo(systemDatabase_);
01073 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::printIndexInfo ( char *  name  ) 

Definition at line 999 of file DatabaseManagerImpl.cxx.

References CatalogTableINDEX::get(), CatalogTableINDEX::getNoOfBuckets(), Chunk::getTotalDataNodes(), CatalogTableINDEX::getUnique(), OK, and Chunk::totalPages().

Referenced by main().

01000 {
01001     CatalogTableINDEX cIndex(systemDatabase_);
01002     DbRetVal rv = OK;
01003     void *chunk = NULL, *hchunk = NULL;
01004     void *tptr =NULL;
01005     rv = cIndex.get(name, chunk, hchunk, tptr);
01006     if (OK != rv) return rv;
01007     printf("<IndexName> %s </IndexName>\n", name);
01008     printf("<Unique> %d </Unique>\n", CatalogTableINDEX::getUnique(tptr));
01009     Chunk *ch = (Chunk*) chunk;
01010     printf("<HashBucket>\n");
01011     printf("  <TotalPages> %d </TotalPages>\n", ch->totalPages());
01012     printf("  <TotalBuckets> %d </TotalBuckets> \n", CatalogTableINDEX::getNoOfBuckets(tptr));
01013     printf("</HashBucket>\n");
01014 
01015     ch = (Chunk*) hchunk;
01016     printf("<IndexNodes>\n");
01017     printf("  <TotalPages> %d </TotalPages>\n", ch->totalPages());
01018     printf("  <TotalNodes> %d </TotalNodes>\n", ch->getTotalDataNodes());
01019     printf("<IndexNodes>\n");
01020     return OK;
01021 }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::printUsageStatistics (  ) 

Definition at line 1058 of file DatabaseManagerImpl.cxx.

References LockManager::printUsageStatistics(), TransactionManager::printUsageStatistics(), and ProcessManager::printUsageStatistics().

Referenced by main().

01059 {
01060     pMgr_->printUsageStatistics();
01061     tMgr_->printUsageStatistics();
01062     lMgr_->printUsageStatistics();
01063 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal DatabaseManagerImpl::registerThread (  ) 

Definition at line 1023 of file DatabaseManagerImpl.cxx.

References DM_Process, ErrAlready, ProcessManager::getProcSlot(), OK, printDebug, printError, and ProcessManager::registerThread().

Referenced by SessionImpl::open().

01024 {
01025     DbRetVal rv = OK;
01026     if (pMgr_ != NULL) 
01027     {
01028         printError(ErrAlready, "Process already registered\n");
01029         return ErrAlready;
01030     }
01031     pMgr_ = new ProcessManager();
01032     rv = pMgr_->registerThread();
01033     if (rv ==OK) { procSlot = pMgr_->getProcSlot();
01034         printDebug(DM_Process, "Process registed with slot %d\n", procSlot);
01035     }
01036     return rv;
01037 }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::setDb ( Database db  )  [inline]

Definition at line 90 of file DatabaseManagerImpl.h.

References db().

Referenced by SessionImpl::initSystemDatabase().

00090 { db_ = db; }

Here is the call graph for this function:

Here is the caller graph for this function:

void DatabaseManagerImpl::setProcSlot (  ) 

Definition at line 51 of file DatabaseManagerImpl.cxx.

References Database::setProcSlot().

00052 {
00053 systemDatabase_->setProcSlot(procSlot);
00054 db_->setProcSlot(procSlot);
00055 }

Here is the call graph for this function:

void DatabaseManagerImpl::setSysDb ( Database db  )  [inline]

Definition at line 89 of file DatabaseManagerImpl.h.

References db().

Referenced by SessionImpl::initSystemDatabase().

00089 { systemDatabase_ = db; }

Here is the call graph for this function:

Here is the caller graph for this function:

Database* DatabaseManagerImpl::sysDb (  )  [inline]

Definition at line 88 of file DatabaseManagerImpl.h.

Referenced by SessionImpl::getSystemDatabase(), SessionImpl::getUserManager(), SessionImpl::initSystemDatabase(), and main().

00088 { return systemDatabase_; }

Here is the caller graph for this function:

TransactionManager* DatabaseManagerImpl::txnMgr (  )  [inline]

Definition at line 93 of file DatabaseManagerImpl.h.

Referenced by SessionImpl::commit(), SessionImpl::rollback(), and SessionImpl::startTransaction().

00093 { return tMgr_; }

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class SessionImpl [friend]

Definition at line 123 of file DatabaseManagerImpl.h.


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 22:46:58 2008 for csql by  doxygen 1.4.7