submitting patch from enterprise version
[csql.git] / include / Util.h
blob47119e9278e6a4586f96eb8c13beb6c8ffa9cbc4
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifndef UTIL_H
21 #define UTIL_H
22 #include<ErrorType.h>
23 #include<Debug.h>
24 enum UniqueIDType {
25 STMT_ID=0,
26 TXN_ID
29 class ListNode
31 public:
32 void *element;
33 ListNode *next;
37 class ListIterator
39 ListNode *iter;
40 ListNode *start;
41 public:
42 ListIterator(){}
44 ListIterator(ListNode *head) { iter = head; start = head; }
46 bool hasElement()
48 if (iter == NULL) return false; else return true;
51 void reset()
53 iter = start;
55 //isRemove ->the node needs to deleted after returning
56 void* nextElement(bool isRemove = false)
58 if (iter == NULL) return NULL;
59 ListNode *node = iter;
60 iter = iter ->next;
61 return node->element;
63 //index start with one, such that 1->first element in list
64 void* getElement(int index)
66 ListNode *localIter = start;
67 if (localIter == NULL) return NULL;
68 for (int i=0; i <index; i++) {
69 localIter = localIter->next;
70 if (localIter == NULL) break;
72 return localIter->element;
76 class Identifier
78 public:
79 char name[IDENTIFIER_LENGTH];
82 class List
84 ListNode *head;
85 int totalElements;
86 public:
87 List() { head = NULL; totalElements = 0;}
89 DbRetVal append(void *elem)
91 ListNode *newNode = new ListNode();
92 newNode->element = elem;
93 newNode->next = NULL;
94 totalElements++;
95 //If this is the first node, set it as head
96 if (NULL == head) { head = newNode; return OK; }
98 ListNode *iter = head;
99 while (NULL != iter->next) iter = iter->next;
100 iter->next = newNode;
101 return OK;
103 //Warning:Try to avoid using this method while using the iterator.The behavior
104 //is undefined. Instead set flag isRemove to yes and call nextElement of iterator.
105 DbRetVal remove(void *elem, bool err=true)
107 if (NULL == head)
109 if (err)
110 printError(ErrNotExists, "There are no elements in the list. Empty list");
111 return ErrNotExists;
113 ListNode *iter = head, *prev = head;
114 while (iter != NULL)
116 if (elem == iter->element)
118 if (iter == head) { head = iter->next; delete iter; return OK;}
119 prev->next = iter->next;
120 delete iter;
121 totalElements--;
122 return OK;
124 prev = iter;
125 iter = iter->next;
127 if (err)
128 printError(ErrNotFound, "There are no elements in the list");
129 return ErrNotFound;
132 //index start with one, such that 1->first element in list
133 void* get(int index)
135 ListNode *localIter = head;
136 if (localIter == NULL) return NULL;
137 for (int i=0; i <index -1; i++) {
138 localIter = localIter->next;
139 if (localIter == NULL) break;
141 return localIter->element;
145 bool exists(void *elem)
147 ListNode *iter = head;
148 while (iter != NULL)
150 if (elem == iter->element)
152 return true;
154 iter = iter->next;
156 return false;
159 ListIterator getIterator()
161 ListIterator iter(head);
162 return iter;
164 void reset()
166 if (NULL == head) return;
167 ListNode *iter = head, *prevIter = head;
168 while (iter->next != NULL)
170 prevIter = iter;
171 iter = iter->next;
172 delete prevIter;
175 delete iter;
176 head = NULL;
177 totalElements = 0;
178 return;
180 int size()
182 return totalElements;
186 #define MAX_UNIQUE_ID 10
187 class GlobalUniqueID
189 void *ptr;
190 public:
191 GlobalUniqueID() { ptr = NULL; }
192 DbRetVal create();
193 DbRetVal open();
194 DbRetVal close() { os::shm_detach(ptr); return OK; }
195 DbRetVal destroy();
196 int getID(UniqueIDType type);
199 class UniqueID
201 int startID;
202 Mutex mutex;
203 public:
204 UniqueID() { startID = 1; mutex.init(); }
207 void setID(int id)
209 startID = id;mutex.init();
213 int getID()
215 //TODO::change mutex to atomic increment instruction
216 if (mutex.getLock(-1, false) != 0) return 0;
217 startID++;
218 mutex.releaseLock(-1, false);
219 return startID;
223 #endif