removing old folder content which had current storage source files
[csql.git] / src / storage / SessionImpl.cxx
blobc67163b21d498c174df560d14b7217624630b087
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;
65 db->createAllCatalogTables();
67 //create the default dba user
68 CatalogTableUSER cUser(db);
69 rv = cUser.insert(I_USER, I_PASS);
70 if (OK != rv)
72 db->releaseCheckpointMutex();
73 return rv;
76 rv = cUser.insert(DBAUSER, DBAPASS);
77 if (OK != rv)
79 db->releaseCheckpointMutex();
80 return rv;
82 void *ret = NULL;
83 //Allocate space for the lock hash bucket
84 ret = db->allocLockHashBuckets();
85 if (NULL == ret)
87 db->releaseCheckpointMutex();
88 printError(ErrSysInit, "Allocation of Lock buckets failed");
89 return ErrSysInit;
91 Bucket* buckets = dbMgr->sysDb()->getLockHashBuckets();
92 Bucket* bucket;
93 char mutName[IDENTIFIER_LENGTH];
94 for (int i =0; i< LOCK_BUCKET_SIZE; i++)
96 bucket = &(buckets[i]);
97 sprintf(mutName, "LOCKBKT:%d", i);
98 bucket->mutex_.init(mutName);
101 db->releaseCheckpointMutex();
102 #if !(defined MMDB && defined EMBED)
103 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config.getMaxSysDbSize()/1048576, Conf::config.getMaxDbSize()/1048576);
104 #endif
105 //create user database
106 rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
107 if (OK != rv) return rv;
108 return OK;
111 DbRetVal SessionImpl::destroySystemDatabase()
113 DbRetVal rv = OK;
114 rv = dbMgr->deleteDatabase(SYSTEMDB);
115 if (OK != rv) return rv;
116 rv = dbMgr->deleteDatabase("userdb");
117 if (OK != rv) return rv;
118 delete dbMgr;
119 dbMgr = NULL;
120 return OK;
123 DbRetVal SessionImpl::open(const char *username, const char *password)
125 #if (defined MMDB) && (defined EMBED)
126 openEmbeddedConnection(username, password);
127 # else
128 DbRetVal rv = OK;
129 rv = readConfigFile();
130 if (rv != OK)
132 printError(ErrSysFatal, "Configuration file read failed\n");
133 return ErrSysFatal;
136 if ( NULL == dbMgr)
138 dbMgr = new DatabaseManagerImpl();
140 int ret = ProcessManager::mutex.tryLock(10, 100);
141 //If you are not getting lock ret !=0, it means somebody else is there.
142 if (ret != 0)
144 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
145 return ErrSysInternal;
147 rv = dbMgr->openSystemDatabase();
148 if (OK != rv)
150 printError(rv,"Unable to open the system database");
151 ProcessManager::mutex.releaseLock(-1, false);
152 return rv;
155 rv = authenticate(username, password);
156 if (OK != rv)
158 dbMgr->closeSystemDatabase();
159 delete dbMgr; dbMgr = NULL;
160 ProcessManager::mutex.releaseLock(-1, false);
161 return rv;
164 dbMgr->createTransactionManager();
165 dbMgr->createLockManager();
166 rv = dbMgr->registerThread();
167 if (OK != rv)
169 printError(rv,"Unable to register to csql server");
170 dbMgr->closeSystemDatabase();
171 ProcessManager::mutex.releaseLock(-1, false);
172 delete dbMgr; dbMgr = NULL;
173 return rv;
175 rv = dbMgr->openDatabase("userdb");
176 if (OK != rv) {
177 dbMgr->closeSystemDatabase();
178 ProcessManager::mutex.releaseLock(-1, false);
179 delete dbMgr; dbMgr = NULL;
180 return rv;
182 ProcessManager::mutex.releaseLock(-1, false);
183 ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
184 //ProcessManager::systemDatabase = dbMgr->sysDb();
185 isXTaken = false;
186 return OK;
187 #endif
189 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
191 CatalogTableUSER cUser(dbMgr->sysDb());
192 cUser.authenticate(username, password, isAuthenticated, isDba);
193 strcpy(userName, username);
194 if (!isAuthenticated)
196 printError(ErrNoPrivilege,"User Authentication failed");
197 return ErrNoPrivilege;
199 return OK;
201 DbRetVal SessionImpl::getExclusiveLock()
203 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
204 if (OK != rv) {
205 printError(ErrLockTimeOut, "Unable to acquire proc table mutex");
206 return rv;
208 if (dbMgr->isAnyOneRegistered()) {
209 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
210 dbMgr->sysDb()->releaseProcessTableMutex(true);
211 return ErrLockTimeOut;
213 isXTaken = true;
214 return rv;
216 DbRetVal SessionImpl::close()
218 # if (defined MMDB) && (defined EMBED)
219 closeEmbeddedConnection();
220 # else
221 DbRetVal rv = OK;
222 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
223 if (dbMgr)
225 int ret = ProcessManager::mutex.tryLock(10,100);
226 //If you are not getting lock ret !=0, it means somebody else is there.
227 if (ret != 0)
229 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
230 return ErrSysInternal;
233 rv = dbMgr->closeDatabase();
234 if (rv != OK) {
235 ProcessManager::mutex.releaseLock(-1, false);
236 return ErrBadCall;
238 rv = dbMgr->deregisterThread();
239 if (rv != OK) {
240 ProcessManager::mutex.releaseLock(-1, false);
241 return ErrBadCall;
243 rv = dbMgr->closeSystemDatabase();
244 if (rv != OK) {
245 ProcessManager::mutex.releaseLock(-1, false);
246 return ErrBadCall;
248 ProcessManager::mutex.releaseLock(-1, false);
249 delete dbMgr;
250 dbMgr = NULL;
252 if (uMgr)
254 delete uMgr;
255 uMgr = NULL;
257 isXTaken = false;
258 return OK;
259 #endif
262 DatabaseManager* SessionImpl::getDatabaseManager()
264 return dbMgr;
267 UserManager* SessionImpl::getUserManager()
269 if (!isAuthenticated)
271 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
272 return NULL;
274 if (uMgr != NULL) return uMgr;
275 UserManagerImpl *userMgr = new UserManagerImpl();
276 if(0 == strcmp(userName, DBAUSER))
277 userMgr->setDba(true);
278 else
279 userMgr->setDba(false);
281 userMgr->setSysDb(dbMgr->sysDb());
283 userMgr->setUserName(userName);
284 uMgr = userMgr;
285 return userMgr;
288 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
290 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
292 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
293 return ErrSysFatal;
295 DbRetVal rv = OK;
297 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
298 return rv;
302 DbRetVal SessionImpl::commit()
304 DbRetVal rv = OK;
305 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
307 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
308 return ErrSysFatal;
310 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
311 if (OK != rv)
313 printError(rv,"Transaction commit failed\n");
314 return rv;
316 return OK;
320 DbRetVal SessionImpl::rollback()
322 DbRetVal rv = OK;
323 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
325 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
326 return ErrSysFatal;
328 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
329 if (OK != rv)
331 printError(rv, "Transaction rollback failed\n");
332 return rv;
334 return OK;
337 DbRetVal SessionImpl::readConfigFile()
339 // Check if env variable is set or not
340 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
341 if (confFilename == NULL)
343 if (os::fileExists(DEFAULT_CONFIG_FILE)) {
344 confFilename = DEFAULT_CONFIG_FILE;
346 else {
347 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable "
348 "should be set.");
349 return ErrSysInit;
353 int rv = Conf::config.readAllValues(confFilename);
354 if (rv != 0) return ErrSysInit;
355 return OK;
357 Database* SessionImpl::getSystemDatabase()
359 return dbMgr->sysDb();
362 #if (defined MMDB && defined EMBED)
363 DbRetVal SessionImpl::openEmbeddedConnection(const char *username, const char *password)
365 DbRetVal rv = OK;
366 rv = readConfigFile();
367 if (rv != OK)
369 printError(ErrSysFatal, "Configuration file read failed\n");
370 return ErrSysFatal;
373 if ( NULL == dbMgr)
375 dbMgr = new DatabaseManagerImpl();
377 int ret = ProcessManager::mutex.tryLock(10, 100);
378 //If you are not getting lock ret !=0, it means somebody else is there.
379 if (ret != 0)
381 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
382 return ErrSysInternal;
385 if (!noOfThreads) {
386 ret = initSystemDatabase();
387 if (0 != ret) {
388 printError(ErrSysInternal, "Unable to initialize the Database");
389 ProcessManager::mutex.releaseLock(-1, false);
390 return ErrSysInternal;
392 ProcessManager::systemDatabase = dbMgr->sysDb();
393 } else {
394 rv = dbMgr->openSystemDatabase();
395 if (OK != rv)
397 printError(rv,"Unable to open the system database");
398 ProcessManager::mutex.releaseLock(-1, false);
399 return rv;
403 rv = authenticate(username, password);
404 if (OK != rv)
406 delete dbMgr; dbMgr = NULL;
407 ProcessManager::mutex.releaseLock(-1, false);
408 return rv;
411 dbMgr->createTransactionManager();
412 dbMgr->createLockManager();
413 if (noOfThreads) {
414 rv = dbMgr->openDatabase("userdb");
415 if (OK != rv) {
416 dbMgr->closeSystemDatabase();
417 ProcessManager::mutex.releaseLock(-1, false);
418 delete dbMgr; dbMgr = NULL;
419 return rv;
423 rv = dbMgr->registerThread();
424 if (OK != rv)
426 printError(rv,"Unable to register to csql server");
427 ProcessManager::mutex.releaseLock(-1, false);
428 delete dbMgr; dbMgr = NULL;
429 return rv;
432 ProcessManager::mutex.releaseLock(-1, false);
433 noOfThreads++;
434 return OK;
437 DbRetVal SessionImpl::closeEmbeddedConnection()
439 DbRetVal rv = OK;
440 if (dbMgr)
442 int ret = ProcessManager::mutex.tryLock(10,100);
443 //If you are not getting lock ret !=0, it means somebody else is there.
444 if (ret != 0)
446 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
447 return ErrSysInternal;
450 rv = dbMgr->deregisterThread();
451 if (rv != OK) {
452 ProcessManager::mutex.releaseLock(-1, false);
453 return ErrBadCall;
455 ProcessManager::mutex.releaseLock(-1, false);
457 if (uMgr)
459 delete uMgr;
460 uMgr = NULL;
462 if(noOfThreads == 1) {
463 destroySystemDatabase();
464 Conf::logger.stopLogger();
466 noOfThreads--;
467 return OK;
469 #endif