code reorg
[csql.git] / include / Database.h
blobb424b5a910a20830a3c28ff3ed128d1476cd9056
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;
26 class Database;
28 class DllExport DatabaseMetaData
30 public:
31 int dbID_;
32 char dbName_[IDENTIFIER_LENGTH];
34 //TODO:: move this from here to system database
35 //as only sys db has the limit and user
36 //database does not have any limit for chunks
37 int noOfChunks_;
39 long maxSize_; //maximum size of database
40 long curSize_; //current size of database
42 //current page, this is used by the allocator to get new free page
43 Page *curPage_;
45 //first page, usually after this database meta data
46 //getFreePage function uses this to get the free page in this database
47 Page *firstPage_;
50 //This mutex is taken if pageInfo is accessed or modified for
51 //this database
52 Mutex dbAllocMutex_;
54 Mutex ckptMutex_;
56 Mutex dbTransTableMutex_;
58 Mutex dbProcTableMutex_;
60 // This mutex is taken for prepare in SqlStatement
61 Mutex dbPrepareStmtMutex_;
63 //To generate unique id
64 UniqueID chunkUniqueID_;
66 //This is where all hash index nodes are stored for all the
67 //indexes in this database
68 Chunk *hashIndexChunk_;
70 bool canTakeCheckPoint_;
72 unsigned char reserved_[780];
76 class DatabaseManagerImpl;
77 class Table;
78 class ProcInfo;
79 class ThreadInfo;
80 class Transaction;
82 class DllExport Database
84 private:
85 //Only DatabaseManager creates this object
86 //initialization is done only in DatabaseManager during
87 //create, delete, open, close database methods
88 Database() { metaData_ = NULL; procSlot = -1; fdChkpt = (file_desc)-1; thrInfoOffset=0;}
89 DatabaseMetaData *metaData_;
90 file_desc fdChkpt;
91 int thrInfoOffset;
94 public:
96 DbRetVal createSystemDatabaseChunk(AllocType type = FixedSizeAllocator,
97 size_t size = 0, int chunkID=-1);
98 DbRetVal deleteSystemDatabaseChunk(int id);
100 Chunk* getSystemDatabaseChunk(int id);
101 Transaction* getSystemDatabaseTrans(int slot);
102 inline void setThrInfoOffset() {
103 thrInfoOffset = os::alignLong(sizeof (DatabaseMetaData)) +
104 os::alignLong( MAX_CHUNKS * sizeof (Chunk)) +
105 os::alignLong( Conf::config.getMaxProcs()*sizeof(Transaction));
107 inline ThreadInfo* getThreadInfo(int slot){
108 if (!thrInfoOffset) setThrInfoOffset();
109 size_t off = thrInfoOffset + slot * sizeof (ThreadInfo);
110 return (ThreadInfo*)(((char*) metaData_) + off);
113 //ThreadInfo* getThreadInfo(int pidSlot, int thrSlot);
114 bool isLastThread();
116 void createAllCatalogTables();
117 void createSystemTables();
118 void createMetaDataTables();
120 void* allocLockHashBuckets();
121 Bucket* getLockHashBuckets();
123 void incrementChunk() { (metaData_->noOfChunks_)++;}
124 void decrementChunk() { (metaData_->noOfChunks_)--;}
126 int getUniqueIDForChunk();
128 const char* getName();
129 int getDatabaseID();
130 long getMaxSize();
131 long getCurrentSize();
132 Page* getCurrentPage();
133 int getNoOfChunks();
134 DatabaseMetaData* getMetaDataPtr() { return metaData_; }
135 Page* getFirstPage();
136 Chunk* getHashIndexChunk();
137 file_desc getChkptfd() { return fdChkpt; }
138 bool getCanTakeCheckPoint() { return metaData_->canTakeCheckPoint_; }
140 void setDatabaseID(int id);
141 void setName(const char *name);
142 void setCurrentSize(long size);
143 void setCurrentPage(Page *page);
144 void setMaxSize(long size);
145 void setNoOfChunks(int maxChunks);
146 void setMetaDataPtr(DatabaseMetaData *ptr) {metaData_ = ptr; }
147 void setFirstPage(Page *ptr);
148 void setHashIndexChunk(Chunk* chunk);
149 void setUniqueChunkID(int id);
150 void setChkptfd(file_desc fd) { fdChkpt = fd; }
151 void setCanTakeCheckPoint(bool ctcp)
152 { metaData_->canTakeCheckPoint_ = ctcp; }
153 // Gets the free page
154 // Each page is segmented by PAGE_SIZE, so it checks the pageInfo
155 // of each page to determine if the page is free
156 Page* getFreePage();
157 Page* getFreePage(size_t size);
159 void printStatistics();
160 void printDebugMutexInfo();
162 int initAllocDatabaseMutex();
163 DbRetVal getAllocDatabaseMutex(bool procAccount = true);
164 DbRetVal releaseAllocDatabaseMutex(bool procAccount = true);
166 int initTransTableMutex();
167 DbRetVal getTransTableMutex();
168 DbRetVal releaseTransTableMutex();
170 int initCheckpointMutex();
171 DbRetVal getSCheckpointMutex(bool procAccount = true);
172 DbRetVal getXCheckpointMutex(bool procAccount = true);
173 DbRetVal releaseCheckpointMutex(bool procAccount = true);
175 int initProcessTableMutex();
176 DbRetVal getProcessTableMutex(bool procAccount = true);
177 DbRetVal releaseProcessTableMutex(bool procAccount = true);
179 int initPrepareStmtMutex();
180 DbRetVal getPrepareStmtMutex(bool procAccount = true);
181 DbRetVal releasePrepareStmtMutex(bool procAccount = true);
183 DbRetVal recoverMutex(Mutex *mut);
185 int procSlot;
186 void setProcSlot(int slot) { procSlot =slot;}
187 //checks whether the ptr falls in the range of the database file size
188 bool isValidAddress(void *ptr);
190 DbRetVal writeDirtyPages(char *chkptFile);
191 DbRetVal checkPoint();
192 DbRetVal filterAndRemoveStmtLogs();
193 DbRetVal recoverUserDB();
194 DbRetVal recoverSystemDB();
195 static int getCheckpointID();
196 static void setCheckpointID(int id);
198 friend class DatabaseManagerImpl;
199 friend class Table;
200 friend class TreeIndex;
201 friend class HashIndex;
202 friend class TrieIndex;
203 friend class Transaction;
207 #endif