1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
25 class DatabaseMetaData
29 char dbName_
[IDENTIFIER_LENGTH
];
31 //TODO:: move this from here to system database
32 //as only sys db has the limit and user
33 //database does not have any limit for chunks
36 long maxSize_
; //maximum size of database
37 long curSize_
; //current size of database
39 //current page, this is used by the allocator to get new free page
42 //first page, usually after this database meta data
43 //getFreePage function uses this to get the free page in this database
47 //This mutex is taken if pageInfo is accessed or modified for
53 Mutex dbTransTableMutex_
;
55 Mutex dbProcTableMutex_
;
56 //To generate unique id
57 UniqueID chunkUniqueID_
;
59 //This is where all hash index nodes are stored for all the
60 //indexes in this database
61 Chunk
*hashIndexChunk_
;
63 unsigned char reserved_
[996];
67 class DatabaseManagerImpl
;
75 //Only DatabaseManager creates this object
76 //initialization is done only in DatabaseManager during
77 //create, delete, open, close database methods
78 Database() { metaData_
= NULL
; procSlot
= -1; }
79 DatabaseMetaData
*metaData_
;
84 DbRetVal
createSystemDatabaseChunk(AllocType type
= FixedSizeAllocator
,
85 size_t size
= 0, int chunkID
=-1);
86 DbRetVal
deleteSystemDatabaseChunk(int id
);
88 Chunk
* getSystemDatabaseChunk(int id
);
89 Transaction
* getSystemDatabaseTrans(int slot
);
91 ThreadInfo
* getThreadInfo(int slot
);
92 //ThreadInfo* getThreadInfo(int pidSlot, int thrSlot);
95 void createAllCatalogTables();
96 void createSystemTables();
97 void createMetaDataTables();
99 void* allocLockHashBuckets();
100 Bucket
* getLockHashBuckets();
102 void incrementChunk() { (metaData_
->noOfChunks_
)++;}
103 void decrementChunk() { (metaData_
->noOfChunks_
)--;}
105 int getUniqueIDForChunk();
107 const char* getName();
110 long getCurrentSize();
111 Page
* getCurrentPage();
113 DatabaseMetaData
* getMetaDataPtr() { return metaData_
; }
114 Page
* getFirstPage();
115 Chunk
* getHashIndexChunk();
117 void setDatabaseID(int id
);
118 void setName(const char *name
);
119 void setCurrentSize(long size
);
120 void setCurrentPage(Page
*page
);
121 void setMaxSize(long size
);
122 void setNoOfChunks(int maxChunks
);
123 void setMetaDataPtr(DatabaseMetaData
*ptr
) {metaData_
= ptr
; }
124 void setFirstPage(Page
*ptr
);
125 void setHashIndexChunk(Chunk
* chunk
);
126 void setUniqueChunkID(int id
);
128 // Gets the free page
129 // Each page is segmented by PAGE_SIZE, so it checks the pageInfo
130 // of each page to determine if the page is free
132 Page
* getFreePage(size_t size
);
134 void printStatistics();
136 int initAllocDatabaseMutex();
137 DbRetVal
getAllocDatabaseMutex(bool procAccount
= true);
138 DbRetVal
releaseAllocDatabaseMutex(bool procAccount
= true);
140 int initTransTableMutex();
141 DbRetVal
getTransTableMutex();
142 DbRetVal
releaseTransTableMutex();
144 int initDatabaseMutex();
145 DbRetVal
getDatabaseMutex(bool procAccount
= true);
146 DbRetVal
releaseDatabaseMutex(bool procAccount
= true);
148 int initProcessTableMutex();
149 DbRetVal
getProcessTableMutex(bool procAccount
= true);
150 DbRetVal
releaseProcessTableMutex(bool procAccount
= true);
152 DbRetVal
recoverMutex(Mutex
*mut
);
155 void setProcSlot(int slot
) { procSlot
=slot
;}
156 //checks whether the ptr falls in the range of the database file size
157 bool isValidAddress(void *ptr
);
158 friend class DatabaseManagerImpl
;