checkpoint server changes
[csql.git] / src / storage / SessionImpl.cxx
blob87b512f1fccf88d141475fc46a97ebadcde59693
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>
27 //Before calling this method, application is required to call readConfigValues
28 DbRetVal SessionImpl::initSystemDatabase()
31 long value=0;
33 DbRetVal rv = OK;
34 rv = readConfigFile();
35 if (rv != OK)
37 printError(ErrSysInit, "Configuration file read failed\n");
38 return ErrSysInit;
41 // Conf::config.print();
43 dbMgr = new DatabaseManagerImpl();
44 rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
45 if (OK != rv) return rv;
47 dbMgr->setSysDb(dbMgr->db());
48 dbMgr->setDb(NULL);
50 Database *db = dbMgr->sysDb();
52 rv = db->getXCheckpointMutex();
53 if (OK != rv)
55 printError(ErrLockTimeOut, "Unable to get Database Mutex");
56 return rv;
60 db->createAllCatalogTables();
62 //create the default dba user
63 CatalogTableUSER cUser(db);
64 rv = cUser.insert(I_USER, I_PASS);
65 if (OK != rv)
67 db->releaseCheckpointMutex();
68 return rv;
71 rv = cUser.insert(DBAUSER, DBAPASS);
72 if (OK != rv)
74 db->releaseCheckpointMutex();
75 return rv;
77 void *ret = NULL;
78 //Allocate space for the lock hash bucket
79 ret = db->allocLockHashBuckets();
80 if (NULL == ret)
82 db->releaseCheckpointMutex();
83 printError(ErrSysInit, "Allocation of Lock buckets failed");
84 return ErrSysInit;
86 db->releaseCheckpointMutex();
88 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config.getMaxSysDbSize()/1048576, Conf::config.getMaxDbSize()/1048576);
89 //create user database
90 rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
91 if (OK != rv) return rv;
92 return OK;
95 DbRetVal SessionImpl::destroySystemDatabase()
97 DbRetVal rv = OK;
98 rv = dbMgr->deleteDatabase(SYSTEMDB);
99 if (OK != rv) return rv;
100 rv = dbMgr->deleteDatabase("userdb");
101 if (OK != rv) return rv;
102 delete dbMgr;
103 dbMgr = NULL;
104 return OK;
107 DbRetVal SessionImpl::open(const char *username, const char *password)
109 DbRetVal rv = OK;
110 rv = readConfigFile();
111 if (rv != OK)
113 printError(ErrSysFatal, "Configuration file read failed\n");
114 return ErrSysFatal;
117 if ( NULL == dbMgr)
119 dbMgr = new DatabaseManagerImpl();
121 int ret = ProcessManager::mutex.tryLock(10, 100);
122 //If you are not getting lock ret !=0, it means somebody else is there.
123 if (ret != 0)
125 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
126 return ErrSysInternal;
128 rv = dbMgr->openSystemDatabase();
129 if (OK != rv)
131 printError(rv,"Unable to open the system database");
132 ProcessManager::mutex.releaseLock(-1, false);
133 return rv;
136 rv = authenticate(username, password);
137 if (OK != rv)
139 dbMgr->closeSystemDatabase();
140 delete dbMgr; dbMgr = NULL;
141 ProcessManager::mutex.releaseLock(-1, false);
142 return rv;
145 dbMgr->createTransactionManager();
146 dbMgr->createLockManager();
147 rv = dbMgr->registerThread();
148 if (OK != rv)
150 printError(rv,"Unable to register to csql server");
151 dbMgr->closeSystemDatabase();
152 ProcessManager::mutex.releaseLock(-1, false);
153 delete dbMgr; dbMgr = NULL;
154 return rv;
156 rv = dbMgr->openDatabase("userdb");
157 if (OK != rv) {
158 dbMgr->closeSystemDatabase();
159 ProcessManager::mutex.releaseLock(-1, false);
160 delete dbMgr; dbMgr = NULL;
161 return rv;
163 ProcessManager::mutex.releaseLock(-1, false);
164 ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
165 //ProcessManager::systemDatabase = dbMgr->sysDb();
166 isXTaken = false;
167 return OK;
169 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
171 CatalogTableUSER cUser(dbMgr->sysDb());
172 cUser.authenticate(username, password, isAuthenticated, isDba);
173 strcpy(userName, username);
174 if (!isAuthenticated)
176 printError(ErrNoPrivilege,"User Authentication failed");
177 return ErrNoPrivilege;
179 return OK;
181 DbRetVal SessionImpl::getExclusiveLock()
183 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
184 if (OK != rv) {
185 printError(ErrLockTimeOut, "Unable to acquire proc table mutex");
186 return rv;
188 if (dbMgr->isAnyOneRegistered()) {
189 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
190 dbMgr->sysDb()->releaseProcessTableMutex(true);
191 return ErrLockTimeOut;
193 isXTaken = true;
194 return rv;
196 DbRetVal SessionImpl::close()
198 DbRetVal rv = OK;
199 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
200 if (dbMgr)
202 int ret = ProcessManager::mutex.tryLock(10,100);
203 //If you are not getting lock ret !=0, it means somebody else is there.
204 if (ret != 0)
206 printError(ErrSysInternal, "Another thread calling open:Wait and then Retry\n");
207 return ErrSysInternal;
210 rv = dbMgr->closeDatabase();
211 if (rv != OK) {
212 ProcessManager::mutex.releaseLock(-1, false);
213 return ErrBadCall;
215 rv = dbMgr->deregisterThread();
216 if (rv != OK) {
217 ProcessManager::mutex.releaseLock(-1, false);
218 return ErrBadCall;
220 rv = dbMgr->closeSystemDatabase();
221 if (rv != OK) {
222 ProcessManager::mutex.releaseLock(-1, false);
223 return ErrBadCall;
225 ProcessManager::mutex.releaseLock(-1, false);
226 delete dbMgr;
227 dbMgr = NULL;
229 if (uMgr)
231 delete uMgr;
232 uMgr = NULL;
234 isXTaken = false;
235 return OK;
238 DatabaseManager* SessionImpl::getDatabaseManager()
240 return dbMgr;
243 UserManager* SessionImpl::getUserManager()
245 if (!isAuthenticated)
247 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
248 return NULL;
250 if (uMgr != NULL) return uMgr;
251 UserManagerImpl *userMgr = new UserManagerImpl();
252 if(0 == strcmp(userName, DBAUSER))
253 userMgr->setDba(true);
254 else
255 userMgr->setDba(false);
257 userMgr->setSysDb(dbMgr->sysDb());
259 userMgr->setUserName(userName);
260 uMgr = userMgr;
261 return userMgr;
264 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
266 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
268 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
269 return ErrSysFatal;
271 DbRetVal rv = OK;
273 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
274 return rv;
278 DbRetVal SessionImpl::commit()
280 DbRetVal rv = OK;
281 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
283 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
284 return ErrSysFatal;
286 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
287 if (OK != rv)
289 printError(rv,"Transaction commit failed\n");
290 return rv;
292 return OK;
296 DbRetVal SessionImpl::rollback()
298 DbRetVal rv = OK;
299 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
301 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
302 return ErrSysFatal;
304 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
305 if (OK != rv)
307 printError(rv, "Transaction rollback failed\n");
308 return rv;
310 return OK;
313 DbRetVal SessionImpl::readConfigFile()
315 // Check if env variable is set or not
316 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
317 if (confFilename == NULL)
319 if (os::fileExists(DEFAULT_CONFIG_FILE)) {
320 confFilename = DEFAULT_CONFIG_FILE;
322 else {
323 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable "
324 "should be set.");
325 return ErrSysInit;
329 int rv = Conf::config.readAllValues(confFilename);
330 if (rv != 0) return ErrSysInit;
331 return OK;
333 Database* SessionImpl::getSystemDatabase()
335 return dbMgr->sysDb();