Adding new tool cacheverify to the build system
[csql.git] / src / server / Transaction.cxx
blob4901f7e9c6fe9422c06037c492dcedf0e6386d4e
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<Transaction.h>
17 #include<Lock.h>
18 #include<Database.h>
19 #include<Allocator.h>
20 #include<CatalogTables.h>
21 #include<Debug.h>
23 DbRetVal Transaction::insertIntoHasList(Database *sysdb, LockHashNode *node)
25 //allocate lock node
26 Chunk *chunk = sysdb->getSystemDatabaseChunk(TransHasTableId);
27 TransHasNode *hasNode = (TransHasNode*)chunk->allocate(sysdb);
28 if (NULL == hasNode)
30 printError(ErrNoMemory, "No memory to allocate Lock node");
31 return ErrNoMemory;
33 printDebug(DM_Transaction, "insertIntoHasList new TransHasNode created:%x",
34 hasNode);
35 hasNode->node_ = node;
36 hasNode->next_ = NULL;
37 if (NULL == hasLockList_)
39 printDebug(DM_Transaction, "hasLockList is null:It is now %x",hasNode);
40 hasLockList_ = hasNode;
41 return OK;
44 TransHasNode *it = hasLockList_;
45 while (NULL != it->next_) { it = it->next_; }
46 it->next_ = hasNode;
47 printDebug(DM_Transaction, "Added to hasLockList at end:%x",it);
48 return OK;
51 DbRetVal Transaction::removeFromHasList(Database *sysdb, void *tuple)
53 Chunk *chunk = sysdb->getSystemDatabaseChunk(TransHasTableId);
54 TransHasNode *iter = hasLockList_, *prev = hasLockList_;
55 if (NULL == iter)
57 printError(ErrNotFound, "There are no tuple lock in has list.");
58 return ErrNotFound;
60 while (iter != NULL)
62 if (tuple == iter->node_->ptrToTuple_)
64 prev->next_ = iter->next_;
65 chunk->free(sysdb, iter);
66 if (iter == hasLockList_) hasLockList_ = NULL;
67 return OK;
69 prev = iter;
70 iter = iter->next_;
72 printError(ErrNotFound, "There are no tuple lock in has list.");
73 return ErrNotFound;
77 DbRetVal Transaction::releaseAllLocks(LockManager *lockManager_)
79 Database *sysdb =lockManager_->systemDatabase_;
80 Chunk *chunk = sysdb->getSystemDatabaseChunk(TransHasTableId);
81 TransHasNode *iter = hasLockList_, *prev;
82 while (NULL != iter)
84 prev = iter;
85 iter = iter->next_;
86 printDebug(DM_Transaction, "Releasing lock %x",prev->node_->ptrToTuple_);
87 lockManager_->releaseLock(prev->node_->ptrToTuple_);
88 chunk->free(sysdb, prev);
90 hasLockList_ = NULL;
91 return OK;
93 bool Transaction::findInHasList(Database *sysdb, LockHashNode *node)
95 TransHasNode *iter = hasLockList_;
96 while (NULL != iter)
98 if (iter->node_ == node) return true;
99 iter = iter->next_;
101 return false;
104 DbRetVal Transaction::appendUndoLog(Database *sysdb, OperationType type,
105 void *data, size_t size)
107 UndoLogInfo *logInfo = createUndoLog(sysdb, type, data, size);
108 if (logInfo == NULL) return ErrNoMemory;
109 os::memcpy((char*)logInfo + sizeof(UndoLogInfo), data, size);
110 addAtBegin(logInfo);
111 printDebug(DM_Transaction, "creating undo log and append %x optype:%d",
112 logInfo, type);
113 return OK;
118 DbRetVal Transaction::appendLogicalUndoLog(Database *sysdb, OperationType type, void *data,
119 size_t size, void* indexPtr)
121 UndoLogInfo *logInfo = createUndoLog(sysdb, type, data, size);
122 if (logInfo == NULL) return ErrNoMemory;
123 char **indPtr = (char**)((char*)logInfo + sizeof(UndoLogInfo));
124 *indPtr = (char*) indexPtr;
125 addAtBegin(logInfo);
126 printDebug(DM_Transaction, "creating logical undo log and append %x optype:%d",
127 logInfo, type);
128 return OK;
131 UndoLogInfo* Transaction::createUndoLog(Database *sysdb, OperationType type, void *data,
132 size_t size)
134 Chunk *chunk = sysdb->getSystemDatabaseChunk(UndoLogTableID);
135 UndoLogInfo *logInfo = (UndoLogInfo*)chunk->allocate(sysdb,
136 size + sizeof(UndoLogInfo));
137 if (logInfo == NULL) return NULL;
138 logInfo->opType_ = type;
139 logInfo->ptrToTuple_ = data;
140 logInfo->size_ = size;
141 logInfo->next_ = NULL;
142 return logInfo;
145 void Transaction::addAtBegin(UndoLogInfo* logInfo)
147 //add it to the begin of the log list
148 logInfo->next_ = firstUndoLog_;
149 firstUndoLog_ = logInfo;
150 return;
153 UndoLogInfo* Transaction::popUndoLog()
155 UndoLogInfo *iter = firstUndoLog_, *prev = firstUndoLog_;
156 if(NULL != iter)
158 prev = iter;
159 iter = iter->next_;
161 firstUndoLog_ = iter;
162 return prev;
166 int Transaction::noOfUndoLogs()
168 UndoLogInfo *iter = firstUndoLog_;
169 int count =0;
170 while(NULL != iter)
172 count++;
173 iter = iter->next_;
175 return count;
177 void Transaction::printDebugInfo(Database *sysdb)
179 printf("<TransactionInfo>\n");
180 if (waitLock_ != NULL)
182 printf("<WaitLock>");
183 waitLock_->print();
184 printf("</WaitLock>");
187 printf("<UndoLogs>\n");
188 Chunk *chunk = sysdb->getSystemDatabaseChunk(UndoLogTableID);
189 printf(" <TotalPages> %d </TotalPages>\n", chunk->totalPages());
190 UndoLogInfo *iter = firstUndoLog_;
191 int count =0;
192 while(NULL != iter)
194 iter->print();
195 iter = iter->next_;
196 count++;
198 printf("</TotalNodes> %d </TotalNodes>\n", count);
199 printf("</UndoLogs>\n");
201 printf("<TransHasList>\n");
202 chunk = sysdb->getSystemDatabaseChunk(TransHasTableId);
203 printf(" <TotalPages> %d </TotalPages>\n", chunk->totalPages());
204 TransHasNode *hasIter = hasLockList_;
205 count =0;
206 while (NULL != hasIter)
208 hasIter->print();
209 hasIter = hasIter->next_;
210 count++;
212 printf("</TotalNodes> %d </TotalNodes>\n", count);
213 printf("</TransHasList>\n");
215 printf("</TransactionInfo>\n");
216 return ;
218 DbRetVal Transaction::removeUndoLogs(Database *sysdb)
220 Chunk *chunk = sysdb->getSystemDatabaseChunk(UndoLogTableID);
221 UndoLogInfo *logInfo = NULL;
222 while(NULL != (logInfo = popUndoLog()))
224 chunk->free(sysdb, logInfo);
226 return OK;
230 DbRetVal Transaction::applyUndoLogs(Database *sysdb)
232 Chunk *chunk = sysdb->getSystemDatabaseChunk(UndoLogTableID);
233 UndoLogInfo *logInfo = NULL;
234 while(NULL != (logInfo = popUndoLog()))
236 switch(logInfo->opType_)
238 case InsertOperation:
239 *((int*)(logInfo->ptrToTuple_) - 1) = 0;
240 //May memcpy is not needed as no one will update this
241 //as lock is taken on this tuple
242 os::memcpy(logInfo->ptrToTuple_, (char*) logInfo +
243 sizeof(UndoLogInfo), logInfo->size_);
244 break;
245 case DeleteOperation:
246 *((int*)(logInfo->ptrToTuple_) - 1) = 1;
247 os::memcpy(logInfo->ptrToTuple_, (char*) logInfo +
248 sizeof(UndoLogInfo), logInfo->size_);
249 break;
250 case UpdateOperation:
251 os::memcpy(logInfo->ptrToTuple_, (char*) logInfo +
252 sizeof(UndoLogInfo), logInfo->size_);
253 break;
255 case InsertHashIndexOperation:
256 //TODO
257 break;
258 case UpdateHashIndexOperation:
259 //TODO
260 break;
261 case DeleteHashIndexOperation:
262 //TODO
263 break;
265 chunk->free(sysdb, logInfo);
267 return OK;