allocator fixes
[csql.git] / include / Database.h
blob64309ef45f3654966f4d65d561ddff13573b3c90
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 #ifndef DATABASE_H
17 #define DATABASE_H
18 #include<os.h>
19 #include<Allocator.h>
20 #include<Debug.h>
21 #include<Util.h>
22 #include<Process.h>
24 class Bucket;
25 class Transaction;
27 class DatabaseMetaData
29 public:
30 int dbID_;
31 char dbName_[IDENTIFIER_LENGTH];
33 //TODO:: move this from here to system database
34 //as only sys db has the limit and user
35 //database does not have any limit for chunks
36 int noOfChunks_;
38 long maxSize_; //maximum size of database
39 long curSize_; //current size of database
41 //current page, this is used by the allocator to get new free page
42 Page *curPage_;
44 //first page, usually after this database meta data
45 //getFreePage function uses this to get the free page in this database
46 Page *firstPage_;
49 //This mutex is taken if pageInfo is accessed or modified for
50 //this database
51 Mutex dbAllocMutex_;
53 Mutex ckptMutex_;
55 Mutex dbTransTableMutex_;
57 Mutex dbProcTableMutex_;
59 // This mutex is taken for prepare in SqlStatement
60 Mutex dbPrepareStmtMutex_;
62 //To generate unique id
63 UniqueID chunkUniqueID_;
65 //This is where all hash index nodes are stored for all the
66 //indexes in this database
67 Chunk *hashIndexChunk_;
69 unsigned char reserved_[996];
73 class DatabaseManagerImpl;
74 class Table;
75 class ProcInfo;
76 class ThreadInfo;
77 class Transaction;
79 class Database
81 private:
82 //Only DatabaseManager creates this object
83 //initialization is done only in DatabaseManager during
84 //create, delete, open, close database methods
85 Database() { metaData_ = NULL; procSlot = -1; fdChkpt = -1; thrInfoOffset=0;}
86 DatabaseMetaData *metaData_;
87 int fdChkpt;
88 int thrInfoOffset;
91 public:
93 DbRetVal createSystemDatabaseChunk(AllocType type = FixedSizeAllocator,
94 size_t size = 0, int chunkID=-1);
95 DbRetVal deleteSystemDatabaseChunk(int id);
97 Chunk* getSystemDatabaseChunk(int id);
98 Transaction* getSystemDatabaseTrans(int slot);
99 inline void setThrInfoOffset() {
100 thrInfoOffset = os::alignLong(sizeof (DatabaseMetaData)) +
101 os::alignLong( MAX_CHUNKS * sizeof (Chunk)) +
102 os::alignLong( Conf::config.getMaxProcs()*sizeof(Transaction));
104 inline ThreadInfo* getThreadInfo(int slot){
105 if (!thrInfoOffset) setThrInfoOffset();
106 size_t off = thrInfoOffset + slot * sizeof (ThreadInfo);
107 return (ThreadInfo*)(((char*) metaData_) + off);
110 //ThreadInfo* getThreadInfo(int pidSlot, int thrSlot);
111 bool isLastThread();
113 void createAllCatalogTables();
114 void createSystemTables();
115 void createMetaDataTables();
117 void* allocLockHashBuckets();
118 Bucket* getLockHashBuckets();
120 void incrementChunk() { (metaData_->noOfChunks_)++;}
121 void decrementChunk() { (metaData_->noOfChunks_)--;}
123 int getUniqueIDForChunk();
125 const char* getName();
126 int getDatabaseID();
127 long getMaxSize();
128 long getCurrentSize();
129 Page* getCurrentPage();
130 int getNoOfChunks();
131 DatabaseMetaData* getMetaDataPtr() { return metaData_; }
132 Page* getFirstPage();
133 Chunk* getHashIndexChunk();
134 int getChkptfd() { return fdChkpt; }
136 void setDatabaseID(int id);
137 void setName(const char *name);
138 void setCurrentSize(long size);
139 void setCurrentPage(Page *page);
140 void setMaxSize(long size);
141 void setNoOfChunks(int maxChunks);
142 void setMetaDataPtr(DatabaseMetaData *ptr) {metaData_ = ptr; }
143 void setFirstPage(Page *ptr);
144 void setHashIndexChunk(Chunk* chunk);
145 void setUniqueChunkID(int id);
146 void setChkptfd(int fd) { fdChkpt = fd; }
148 // Gets the free page
149 // Each page is segmented by PAGE_SIZE, so it checks the pageInfo
150 // of each page to determine if the page is free
151 Page* getFreePage();
152 Page* getFreePage(size_t size);
154 void printStatistics();
156 int initAllocDatabaseMutex();
157 DbRetVal getAllocDatabaseMutex(bool procAccount = true);
158 DbRetVal releaseAllocDatabaseMutex(bool procAccount = true);
160 int initTransTableMutex();
161 DbRetVal getTransTableMutex();
162 DbRetVal releaseTransTableMutex();
164 int initCheckpointMutex();
165 DbRetVal getSCheckpointMutex(bool procAccount = true);
166 DbRetVal getXCheckpointMutex(bool procAccount = true);
167 DbRetVal releaseCheckpointMutex(bool procAccount = true);
169 int initProcessTableMutex();
170 DbRetVal getProcessTableMutex(bool procAccount = true);
171 DbRetVal releaseProcessTableMutex(bool procAccount = true);
173 int initPrepareStmtMutex();
174 DbRetVal getPrepareStmtMutex(bool procAccount = true);
175 DbRetVal releasePrepareStmtMutex(bool procAccount = true);
177 DbRetVal recoverMutex(Mutex *mut);
179 int procSlot;
180 void setProcSlot(int slot) { procSlot =slot;}
181 //checks whether the ptr falls in the range of the database file size
182 bool isValidAddress(void *ptr);
183 DbRetVal writeDirtyPages(char *chkptFile);
184 DbRetVal checkPoint();
185 DbRetVal recoverUserDB();
186 DbRetVal recoverSystemDB();
187 static int getCheckpointID();
188 static void setCheckpointID(int id);
189 friend class DatabaseManagerImpl;
190 friend class Table;
191 friend class TreeIndex;
192 friend class HashIndex;
193 friend class Transaction;
196 #endif