~pbms-core/pbms/5.11-beta

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
/* 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;
	csWord4				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;
	csWord1				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(csWord4 tab_id, bool missing_ok);
	MSTable *getNextTable(csWord4 *pos);

	void addTable(csWord4 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(csWord4 repo_id, bool *mustQuit);

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

private:
	void queueTempLogEvent(MSOpenTable *otab, int type, csWord4 tab_id, csWord8 blob_id, csWord4 auth_code, csWord4 *log_id, csWord4 *log_offset, csWord4 *q_time);
public:
	void queueForDeletion(MSOpenTable *otab, int type, csWord4 tab_id, csWord8 blob_id, csWord4 auth_code, csWord4 *log_id, csWord4 *log_offset, csWord4 *q_time, MSDiskAliasPtr aliasDiskRec);
	MSTempLogFile *openTempLogFile(csWord4 log_id, size_t *log_rec_size, size_t *log_head_size);
	u_int getTempLogCount();
	void removeTempLog(csWord4 log_id);

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

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

	bool findBlobWithAlias(const char *alias, csWord4 *repo_id = NULL, csWord8 *repo_offset = NULL)
	{
		bool found;
		csWord4 x_repo_id;
		csWord8 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);
	}
	csWord4 registerBlobAlias(csWord4 repo_id, csWord8 repo_offset, const char *alias);
	csWord4 updateBlobAlias(csWord4 repo_id, csWord8 repo_offset, csWord4 old_alias_hash, const char *alias);
	void deleteBlobAlias(MSDiskAliasPtr diskRec);
	void deleteBlobAlias(csWord4 repo_id, csWord8 repo_offset, csWord4 alias_hash);
	void moveBlobAlias(csWord4 old_repo_id, csWord8 old_repo_offset, csWord4 alias_hash, csWord4 new_repo_id, csWord8 new_repo_offset);

	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(csWord8 *total, csWord8 *completed, bool *completed_ok);
	csWord4 backupID();
	
private:
	MSBackup			*iBackupThread;
	csWord4				iBackupTime; // The time at which the backup was started.
	bool				iRecovering;
	MSAlias				*iBlobAliases;
	CSLock				iBlobAliaseLock; // Write lock for the BLOB aliases index. This is required because of the .
	bool				iClosing;

	CSSyncSortedList	*iTableList;
	CSSparseArray		*iTableArray;
	csWord4				iMaxTableID;

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

	CSLock				iBlobRefIdLock; // Lock for the BLOB ref counter.
	csWord8				iNextBlobRefId;
	
public:
	static csWord8			gBackupDatabaseID;
	static csWord8			gRepoThreshold;
	static csWord8			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, csWord4 db_id, bool create);
	
	static MSDatabase *getDatabase(CSString *db_name, bool create);
	static MSDatabase *getDatabase(const char *db_name, bool create);
	static MSDatabase *getDatabase(csWord4 db_id);
	static csWord4 getDatabaseID(CSString *db_name, bool create);
	static csWord4 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, csWord4 *db_id, csWord4 *tab_id, bool create);
	static bool convertTableAndDatabaseToIDs(const char *db_name, const char *tab_name, csWord4 *db_id, csWord4 *tab_id, bool create);


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

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

#endif