~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Original author: Paul McCullagh
 * Continued development: Barry Leslie
 *
 * 2007-06-26
 *
 * H&G2JCtL
 *
 * Contains all the information about an open database.
 *
 */

#ifndef __DATABASE_MS_H__
#define __DATABASE_MS_H__

#include "CSDefs.h"
#include "CSStorage.h"
#include "CSStrUtil.h"

#include "Table_ms.h"
#include "Repository_ms.h"
#include "alias_ms.h"
#include "TempLog_ms.h"
#include "Compactor_ms.h"
#include "cloud.h"

class MSOpenTable;
class MSBackup;

class MSDatabase : public CSSharedRefObject {
public:
	bool				myIsPBMS;
	uint32_t				myDatabaseID;
	CSString			*myDatabaseName;	// The database name may not be related to the database path,
	CSString			*myDatabasePath;	// do not make any assumptions here!
	CSSyncSparseArray	*myTempLogArray;
	MSCompactorThread	*myCompactorThread;
	MSTempLogThread		*myTempLogThread;
	CSSyncVector		*myRepostoryList;
	CloudDB				*myBlobCloud;
	uint8_t				myBlobType;			// Cloud or repository



	MSDatabase();
	virtual ~MSDatabase();

	const char *getDatabaseNameCString();
	
	MSTable *getTable(CSString *tab_name, bool create);
	MSTable *getTable(const char *tab_name, bool create);
	MSTable *getTable(uint32_t tab_id, bool missing_ok);
	MSTable *getNextTable(uint32_t *pos);

	void addTable(uint32_t tab_id, const char *tab_name, off_t file_size, bool to_delete);
	void addTableFromFile(CSDirectory *dir, const char *file_name, bool to_delete);
	void removeTable(MSTable *tab);
	void dropTable(MSTable *tab);
	void renameTable(MSTable *tab, const char *to_name);
	CSString *getATableName();
	u_int getTableCount();

	void openWriteRepo(MSOpenTable *otab);

	MSRepository *getRepoFullOfTrash(time_t *wait_time);
	MSRepository *lockRepo(off_t size);
	void removeRepo(uint32_t repo_id, bool *mustQuit);

	MSRepoFile *getRepoFileFromPool(uint32_t repo_id, bool missing_ok);
	void returnRepoFileToPool(MSRepoFile *file);
	
	uint64_t newBlobRefId() // Returns a unique blob referfence Id.
	{
		uint64_t id;
		enter_();
		lock_(&iBlobRefIdLock);
		id = iNextBlobRefId++;
		unlock_(&iBlobRefIdLock);
		return_(COMMIT_MASK(id));
	}
	

private:
	void queueTempLogEvent(MSOpenTable *otab, int type, uint32_t tab_id, uint64_t blob_id, uint32_t auth_code, uint32_t *log_id, uint32_t *log_offset, uint32_t *q_time);
public:
#ifdef HAVE_ALIAS_SUPPORT
	void queueForDeletion(MSOpenTable *otab, int type, uint32_t tab_id, uint64_t blob_id, uint32_t auth_code, uint32_t *log_id, uint32_t *log_offset, uint32_t *q_time, MSDiskAliasPtr aliasDiskRec);
#else
	void queueForDeletion(MSOpenTable *otab, int type, uint32_t tab_id, uint64_t blob_id, uint32_t auth_code, uint32_t *log_id, uint32_t *log_offset, uint32_t *q_time)
	{
		queueTempLogEvent(otab, type, tab_id, blob_id, auth_code, log_id, log_offset, q_time);
	}
#endif
	MSTempLogFile *openTempLogFile(uint32_t log_id, size_t *log_rec_size, size_t *log_head_size);
	u_int getTempLogCount();
	void removeTempLog(uint32_t log_id);

	/* Make this object sortable: */
	virtual CSObject *getKey();
	virtual int compareKey(CSObject *);

	MSCompactorThread *getCompactorThread();
	CSSyncVector *getRepositoryList();

#ifdef HAVE_ALIAS_SUPPORT
	bool findBlobWithAlias(const char *alias, uint32_t *repo_id = NULL, uint64_t *repo_offset = NULL)
	{
		bool found;
		uint32_t x_repo_id;
		uint64_t x_repo_offset;
		bool referenced; // The BLOB can be referenced or non referenced.
		
		enter_();
		if (!repo_id) repo_id = &x_repo_id;
		if (!repo_offset) repo_offset = &x_repo_offset;
		
		lock_(&iBlobAliaseLock);
		found = iBlobAliases->findBlobByAlias(alias, &referenced, repo_id, repo_offset);
		unlock_(&iBlobAliaseLock);
		return_(found);
	}
	uint32_t registerBlobAlias(uint32_t repo_id, uint64_t repo_offset, const char *alias);
	uint32_t updateBlobAlias(uint32_t repo_id, uint64_t repo_offset, uint32_t old_alias_hash, const char *alias);
	void deleteBlobAlias(MSDiskAliasPtr diskRec);
	void deleteBlobAlias(uint32_t repo_id, uint64_t repo_offset, uint32_t alias_hash);
	void moveBlobAlias(uint32_t old_repo_id, uint64_t old_repo_offset, uint32_t alias_hash, uint32_t new_repo_id, uint64_t new_repo_offset);
#endif

	bool isValidHeaderField(const char *name);
	
	bool isRecovering() { return iRecovering;} // Indicates the database is being recovered from a dump.
	void setRecovering(bool recovering) { // Indicate if the database is being recovered from a dump.
		if (iRecovering == recovering)
			return;
		iRecovering = recovering;
		if (iRecovering) {
			myCompactorThread->suspend();
			myTempLogThread->suspend();
		} else {
			myCompactorThread->resume();
			myTempLogThread->resume();
		}
	}
	
	bool isBackup;
	void setBackupDatabase(); // Signals the database that it is the destination for a backup process.
	void releaseBackupDatabase(); 
	
	void startBackup(MSBackupInfo *backup_info);
	void terminateBackup();
	bool backupStatus(uint64_t *total, uint64_t *completed, bool *completed_ok);
	uint32_t backupID();
	
private:
	MSBackup			*iBackupThread;
	uint32_t				iBackupTime; // The time at which the backup was started.
	bool				iRecovering;
#ifdef HAVE_ALIAS_SUPPORT
	MSAlias				*iBlobAliases;
	CSLock				iBlobAliaseLock; // Write lock for the BLOB aliases index. This is required because of the .
#endif
	bool				iClosing;

	CSSyncSortedList	*iTableList;
	CSSparseArray		*iTableArray;
	uint32_t				iMaxTableID;

	MSTempLog			*iWriteTempLog;
	bool				iDropping;
	void				dropDatabase();
	void				startThreads();

	CSLock				iBlobRefIdLock; // Lock for the BLOB ref counter.
	uint64_t				iNextBlobRefId;
	
public:
	static uint64_t			gBackupDatabaseID;
	static uint64_t			gRepoThreshold;
	static uint64_t			gTempLogThreshold;

	CSSyncSortedList	iHTTPMetaDataHeaders;
	static void startUp(const char *default_http_headers);
	static void stopThreads();
	static void shutDown();

	static MSDatabase *getBackupDatabase(CSString *db_location, CSString *db_name, uint32_t db_id, bool create);
	
	static MSDatabase *getDatabase(CSString *db_name, bool create);
	static MSDatabase *getDatabase(const char *db_name, bool create);
	static MSDatabase *getDatabase(uint32_t db_id);
	static uint32_t getDatabaseID(CSString *db_name, bool create);
	static uint32_t getDatabaseID(const char *db_name, bool create);

	static void wakeTempLogThreads();
	static void dropDatabase(MSDatabase *doomedDatabase, const char *db_name = NULL);
	static void dropDatabase(const char *db_name);
	
	static bool convertTablePathToIDs(const char *path, uint32_t *db_id, uint32_t *tab_id, bool create);
	static bool convertTableAndDatabaseToIDs(const char *db_name, const char *tab_name, uint32_t *db_id, uint32_t *tab_id, bool create);


private:
	static CSSyncSortedList	*gDatabaseList;
	static CSSparseArray	*gDatabaseArray;
	
	
	static void removeDatabasePath(CSString *doomedDatabasePath);

	static uint32_t getDBID(CSPath *path, CSString *db_name);
	static CSPath *createDatabasePath(const char *location, CSString *db_name, uint32_t *db_id_ptr, bool *create, bool is_pbms = false);
	static MSDatabase *newDatabase(const char *db_location, CSString *db_name, uint32_t	db_id, bool create);
	static MSDatabase *loadDatabase(CSString *db_name,  bool create);
};

#endif