code reorg and doxygen documentation
[csql.git] / src / storage / SessionImpl.cxx
blobff40df420f248d36a80887507b28fc4d553bce60
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<DatabaseManagerImpl.h>
17 #include<DatabaseManager.h>
18 #include<CatalogTables.h>
19 #include<Database.h>
20 #include<SessionImpl.h>
21 #include<UserManager.h>
22 #include<UserManagerImpl.h>
23 #include<Transaction.h>
24 #include<Debug.h>
25 #include<Config.h>
26 #include<Process.h>
28 #if (defined MMDB && defined EMBED)
29 int SessionImpl::noOfThreads = 0;
30 #endif
32 //Before calling this method, application is required to call readConfigValues
33 DbRetVal SessionImpl::initSystemDatabase()
36 long value=0;
38 DbRetVal rv = OK;
39 rv = readConfigFile();
40 if (rv != OK)
42 printError(ErrSysInit, "Configuration file read failed\n");
43 return ErrSysInit;
46 // Conf::config.print();
48 if (dbMgr == NULL) dbMgr = new DatabaseManagerImpl();
49 rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
50 if (OK != rv) return rv;
52 dbMgr->setSysDb(dbMgr->db());
53 dbMgr->setDb(NULL);
55 Database *db = dbMgr->sysDb();
57 rv = db->getXCheckpointMutex();
58 if (OK != rv)
60 printError(ErrLockTimeOut, "Unable to get Database Mutex");
61 return rv;
64 db->createAllCatalogTables();
66 //create the default dba user
67 CatalogTableUSER cUser(db);
68 rv = cUser.insert(I_USER, I_PASS);
69 if (OK != rv)
71 db->releaseCheckpointMutex();
72 return rv;
74 rv = cUser.insert(DBAUSER, DBAPASS);
75 if (OK != rv)
77 db->releaseCheckpointMutex();
78 return rv;
80 void *ret = NULL;
81 //Allocate space for the lock hash bucket
82 ret = db->allocLockHashBuckets();
83 if (NULL == ret)
85 db->releaseCheckpointMutex();
86 printError(ErrSysInit, "Allocation of Lock buckets failed");
87 return ErrSysInit;
89 Bucket* buckets = dbMgr->sysDb()->getLockHashBuckets();
90 Bucket* bucket;
91 char mutName[IDENTIFIER_LENGTH];
92 for (int i =0; i< LOCK_BUCKET_SIZE; i++)
94 bucket = &(buckets[i]);
95 sprintf(mutName, "LOCKBKT:%d", i);
96 bucket->mutex_.init(mutName);
99 db->releaseCheckpointMutex();
100 #if !(defined MMDB && defined EMBED)
101 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config.getMaxSysDbSize()/1048576, Conf::config.getMaxDbSize()/1048576);
102 #endif
103 //create user database
104 rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
105 if (OK != rv) return rv;
106 return OK;
109 DbRetVal SessionImpl::destroySystemDatabase()
111 DbRetVal rv = OK;
112 rv = dbMgr->deleteDatabase(SYSTEMDB);
113 if (OK != rv) return rv;
114 rv = dbMgr->deleteDatabase("userdb");
115 if (OK != rv) return rv;
116 delete dbMgr;
117 dbMgr = NULL;
118 return OK;
121 DbRetVal SessionImpl::open(const char *username, const char *password)
123 #if (defined MMDB) && (defined EMBED)
124 openEmbeddedConnection(username, password);
125 #else
126 DbRetVal rv = OK;
127 rv = readConfigFile();
128 if (rv != OK)
130 printError(ErrSysFatal, "Configuration file read failed\n");
131 return ErrSysFatal;
134 if ( NULL == dbMgr)
136 dbMgr = new DatabaseManagerImpl();
138 int ret = ProcessManager::mutex.tryLock(10, 100);
139 //If you are not getting lock ret !=0, it means somebody else is there.
140 if (ret != 0)
142 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
143 return ErrSysInternal;
145 rv = dbMgr->openSystemDatabase();
146 if (OK != rv)
148 printError(rv,"Unable to open the system database");
149 ProcessManager::mutex.releaseLock(-1, false);
150 return rv;
153 rv = authenticate(username, password);
154 if (OK != rv)
156 dbMgr->closeSystemDatabase();
157 delete dbMgr; dbMgr = NULL;
158 ProcessManager::mutex.releaseLock(-1, false);
159 return rv;
162 dbMgr->createTransactionManager();
163 dbMgr->createLockManager();
164 rv = dbMgr->registerThread();
165 if (OK != rv)
167 printError(rv,"Unable to register to csql server");
168 dbMgr->closeSystemDatabase();
169 ProcessManager::mutex.releaseLock(-1, false);
170 delete dbMgr; dbMgr = NULL;
171 return rv;
173 rv = dbMgr->openDatabase("userdb");
174 if (OK != rv) {
175 dbMgr->closeSystemDatabase();
176 ProcessManager::mutex.releaseLock(-1, false);
177 delete dbMgr; dbMgr = NULL;
178 return rv;
180 ProcessManager::mutex.releaseLock(-1, false);
181 ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
182 //ProcessManager::systemDatabase = dbMgr->sysDb();
183 isXTaken = false;
184 return OK;
185 #endif
187 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
189 CatalogTableUSER cUser(dbMgr->sysDb());
190 cUser.authenticate(username, password, isAuthenticated, isDba);
191 strcpy(userName, username);
192 if (!isAuthenticated)
194 printError(ErrNoPrivilege,"User Authentication failed");
195 return ErrNoPrivilege;
197 return OK;
199 DbRetVal SessionImpl::getExclusiveLock()
201 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
202 if (OK != rv) {
203 printError(ErrLockTimeOut, "Unable to acquire proc table mutex");
204 return rv;
206 if (dbMgr->isAnyOneRegistered()) {
207 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
208 dbMgr->sysDb()->releaseProcessTableMutex(true);
209 return ErrLockTimeOut;
211 isXTaken = true;
212 return rv;
215 DbRetVal SessionImpl::close()
217 # if (defined MMDB) && (defined EMBED)
218 closeEmbeddedConnection();
219 # else
220 DbRetVal rv = OK;
221 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
222 if (dbMgr)
224 int ret = ProcessManager::mutex.tryLock(10,100);
225 //If you are not getting lock ret !=0, it means somebody else is there.
226 if (ret != 0)
228 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
229 return ErrSysInternal;
232 rv = dbMgr->closeDatabase();
233 if (rv != OK) {
234 ProcessManager::mutex.releaseLock(-1, false);
235 return ErrBadCall;
237 rv = dbMgr->deregisterThread();
238 if (rv != OK) {
239 ProcessManager::mutex.releaseLock(-1, false);
240 return ErrBadCall;
242 rv = dbMgr->closeSystemDatabase();
243 if (rv != OK) {
244 ProcessManager::mutex.releaseLock(-1, false);
245 return ErrBadCall;
247 ProcessManager::mutex.releaseLock(-1, false);
248 delete dbMgr;
249 dbMgr = NULL;
251 if (uMgr)
253 delete uMgr;
254 uMgr = NULL;
256 isXTaken = false;
257 return OK;
258 #endif
261 DatabaseManager* SessionImpl::getDatabaseManager()
263 return dbMgr;
266 UserManager* SessionImpl::getUserManager()
268 if (!isAuthenticated)
270 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
271 return NULL;
273 if (uMgr != NULL) return uMgr;
274 UserManagerImpl *userMgr = new UserManagerImpl();
275 if(0 == strcmp(userName, DBAUSER))
276 userMgr->setDba(true);
277 else
278 userMgr->setDba(false);
280 userMgr->setSysDb(dbMgr->sysDb());
282 userMgr->setUserName(userName);
283 uMgr = userMgr;
284 return userMgr;
287 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
289 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
291 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
292 return ErrSysFatal;
294 DbRetVal rv = OK;
296 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
297 return rv;
301 DbRetVal SessionImpl::commit()
303 DbRetVal rv = OK;
304 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
306 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
307 return ErrSysFatal;
309 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
310 if (OK != rv)
312 printError(rv,"Transaction commit failed\n");
313 return rv;
315 return OK;
318 DbRetVal SessionImpl::rollback()
320 DbRetVal rv = OK;
321 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
323 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
324 return ErrSysFatal;
326 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
327 if (OK != rv)
329 printError(rv, "Transaction rollback failed\n");
330 return rv;
332 return OK;
335 DbRetVal SessionImpl::readConfigFile()
337 // Check if env variable is set or not
338 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
339 if (confFilename == NULL)
341 if (os::fileExists(DEFAULT_CONFIG_FILE)) {
342 confFilename = DEFAULT_CONFIG_FILE;
344 else {
345 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable "
346 "should be set.");
347 return ErrSysInit;
351 int rv = Conf::config.readAllValues(confFilename);
352 if (rv != 0) return ErrSysInit;
353 return OK;
356 Database* SessionImpl::getSystemDatabase()
358 return dbMgr->sysDb();
361 #if (defined MMDB && defined EMBED)
362 DbRetVal SessionImpl::openEmbeddedConnection(const char *username, const char *password)
364 DbRetVal rv = OK;
365 rv = readConfigFile();
366 if (rv != OK)
368 printError(ErrSysFatal, "Configuration file read failed\n");
369 return ErrSysFatal;
372 if ( NULL == dbMgr)
374 dbMgr = new DatabaseManagerImpl();
376 int ret = ProcessManager::mutex.tryLock(10, 100);
377 //If you are not getting lock ret !=0, it means somebody else is there.
378 if (ret != 0)
380 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
381 return ErrSysInternal;
384 if (!noOfThreads) {
385 ret = initSystemDatabase();
386 if (0 != ret) {
387 printError(ErrSysInternal, "Unable to initialize the Database");
388 ProcessManager::mutex.releaseLock(-1, false);
389 return ErrSysInternal;
391 ProcessManager::systemDatabase = dbMgr->sysDb();
392 } else {
393 rv = dbMgr->openSystemDatabase();
394 if (OK != rv)
396 printError(rv,"Unable to open the system database");
397 ProcessManager::mutex.releaseLock(-1, false);
398 return rv;
402 rv = authenticate(username, password);
403 if (OK != rv)
405 delete dbMgr; dbMgr = NULL;
406 ProcessManager::mutex.releaseLock(-1, false);
407 return rv;
410 dbMgr->createTransactionManager();
411 dbMgr->createLockManager();
412 if (noOfThreads) {
413 rv = dbMgr->openDatabase("userdb");
414 if (OK != rv) {
415 dbMgr->closeSystemDatabase();
416 ProcessManager::mutex.releaseLock(-1, false);
417 delete dbMgr; dbMgr = NULL;
418 return rv;
422 rv = dbMgr->registerThread();
423 if (OK != rv)
425 printError(rv,"Unable to register to csql server");
426 ProcessManager::mutex.releaseLock(-1, false);
427 delete dbMgr; dbMgr = NULL;
428 return rv;
431 ProcessManager::mutex.releaseLock(-1, false);
432 noOfThreads++;
433 return OK;
436 DbRetVal SessionImpl::closeEmbeddedConnection()
438 DbRetVal rv = OK;
439 if (dbMgr)
441 int ret = ProcessManager::mutex.tryLock(10,100);
442 //If you are not getting lock ret !=0, it means somebody else is there.
443 if (ret != 0)
445 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
446 return ErrSysInternal;
449 rv = dbMgr->deregisterThread();
450 if (rv != OK) {
451 ProcessManager::mutex.releaseLock(-1, false);
452 return ErrBadCall;
454 ProcessManager::mutex.releaseLock(-1, false);
456 if (uMgr)
458 delete uMgr;
459 uMgr = NULL;
461 if(noOfThreads == 1) {
462 destroySystemDatabase();
463 Conf::logger.stopLogger();
465 noOfThreads--;
466 return OK;
468 #endif