~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/Database_ms.h

Added the PBMS daemon plugin.

(Augen zu und durch!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
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
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-06-26
 
23
 *
 
24
 * H&G2JCtL
 
25
 *
 
26
 * Contains all the information about an open database.
 
27
 *
 
28
 */
 
29
 
 
30
#ifndef __DATABASE_MS_H__
 
31
#define __DATABASE_MS_H__
 
32
 
 
33
#include "CSDefs.h"
 
34
#include "CSStorage.h"
 
35
#include "CSStrUtil.h"
 
36
 
 
37
#include "Table_ms.h"
 
38
#include "Repository_ms.h"
 
39
#include "alias_ms.h"
 
40
#include "TempLog_ms.h"
 
41
#include "Compactor_ms.h"
 
42
#include "cloud.h"
 
43
 
 
44
class MSOpenTable;
 
45
class MSBackup;
 
46
 
 
47
class MSDatabase : public CSSharedRefObject {
 
48
public:
 
49
        bool                            myIsPBMS;
 
50
        uint32_t                                myDatabaseID;
 
51
        CSString                        *myDatabaseName;        // The database name may not be related to the database path,
 
52
        CSString                        *myDatabasePath;        // do not make any assumptions here!
 
53
        CSSyncSparseArray       *myTempLogArray;
 
54
        MSCompactorThread       *myCompactorThread;
 
55
        MSTempLogThread         *myTempLogThread;
 
56
        CSSyncVector            *myRepostoryList;
 
57
        CloudDB                         *myBlobCloud;
 
58
        uint8_t                         myBlobType;                     // Cloud or repository
 
59
 
 
60
 
 
61
 
 
62
        MSDatabase();
 
63
        virtual ~MSDatabase();
 
64
 
 
65
        const char *getDatabaseNameCString();
 
66
        
 
67
        MSTable *getTable(CSString *tab_name, bool create);
 
68
        MSTable *getTable(const char *tab_name, bool create);
 
69
        MSTable *getTable(uint32_t tab_id, bool missing_ok);
 
70
        MSTable *getNextTable(uint32_t *pos);
 
71
 
 
72
        void addTable(uint32_t tab_id, const char *tab_name, off_t file_size, bool to_delete);
 
73
        void addTableFromFile(CSDirectory *dir, const char *file_name, bool to_delete);
 
74
        void removeTable(MSTable *tab);
 
75
        void dropTable(MSTable *tab);
 
76
        void renameTable(MSTable *tab, const char *to_name);
 
77
        CSString *getATableName();
 
78
        u_int getTableCount();
 
79
 
 
80
        void openWriteRepo(MSOpenTable *otab);
 
81
 
 
82
        MSRepository *getRepoFullOfTrash(time_t *wait_time);
 
83
        MSRepository *lockRepo(off_t size);
 
84
        void removeRepo(uint32_t repo_id, bool *mustQuit);
 
85
 
 
86
        MSRepoFile *getRepoFileFromPool(uint32_t repo_id, bool missing_ok);
 
87
        void returnRepoFileToPool(MSRepoFile *file);
 
88
        
 
89
        uint64_t newBlobRefId() // Returns a unique blob referfence Id.
 
90
        {
 
91
                uint64_t id;
 
92
                enter_();
 
93
                lock_(&iBlobRefIdLock);
 
94
                id = iNextBlobRefId++;
 
95
                unlock_(&iBlobRefIdLock);
 
96
                return_(COMMIT_MASK(id));
 
97
        }
 
98
        
 
99
 
 
100
private:
 
101
        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);
 
102
public:
 
103
#ifdef HAVE_ALIAS_SUPPORT
 
104
        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);
 
105
#else
 
106
        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)
 
107
        {
 
108
                queueTempLogEvent(otab, type, tab_id, blob_id, auth_code, log_id, log_offset, q_time);
 
109
        }
 
110
#endif
 
111
        MSTempLogFile *openTempLogFile(uint32_t log_id, size_t *log_rec_size, size_t *log_head_size);
 
112
        u_int getTempLogCount();
 
113
        void removeTempLog(uint32_t log_id);
 
114
 
 
115
        /* Make this object sortable: */
 
116
        virtual CSObject *getKey();
 
117
        virtual int compareKey(CSObject *);
 
118
 
 
119
        MSCompactorThread *getCompactorThread();
 
120
        CSSyncVector *getRepositoryList();
 
121
 
 
122
#ifdef HAVE_ALIAS_SUPPORT
 
123
        bool findBlobWithAlias(const char *alias, uint32_t *repo_id = NULL, uint64_t *repo_offset = NULL)
 
124
        {
 
125
                bool found;
 
126
                uint32_t x_repo_id;
 
127
                uint64_t x_repo_offset;
 
128
                bool referenced; // The BLOB can be referenced or non referenced.
 
129
                
 
130
                enter_();
 
131
                if (!repo_id) repo_id = &x_repo_id;
 
132
                if (!repo_offset) repo_offset = &x_repo_offset;
 
133
                
 
134
                lock_(&iBlobAliaseLock);
 
135
                found = iBlobAliases->findBlobByAlias(alias, &referenced, repo_id, repo_offset);
 
136
                unlock_(&iBlobAliaseLock);
 
137
                return_(found);
 
138
        }
 
139
        uint32_t registerBlobAlias(uint32_t repo_id, uint64_t repo_offset, const char *alias);
 
140
        uint32_t updateBlobAlias(uint32_t repo_id, uint64_t repo_offset, uint32_t old_alias_hash, const char *alias);
 
141
        void deleteBlobAlias(MSDiskAliasPtr diskRec);
 
142
        void deleteBlobAlias(uint32_t repo_id, uint64_t repo_offset, uint32_t alias_hash);
 
143
        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);
 
144
#endif
 
145
 
 
146
        bool isValidHeaderField(const char *name);
 
147
        
 
148
        bool isRecovering() { return iRecovering;} // Indicates the database is being recovered from a dump.
 
149
        void setRecovering(bool recovering) { // Indicate if the database is being recovered from a dump.
 
150
                if (iRecovering == recovering)
 
151
                        return;
 
152
                iRecovering = recovering;
 
153
                if (iRecovering) {
 
154
                        myCompactorThread->suspend();
 
155
                        myTempLogThread->suspend();
 
156
                } else {
 
157
                        myCompactorThread->resume();
 
158
                        myTempLogThread->resume();
 
159
                }
 
160
        }
 
161
        
 
162
        bool isBackup;
 
163
        void setBackupDatabase(); // Signals the database that it is the destination for a backup process.
 
164
        void releaseBackupDatabase(); 
 
165
        
 
166
        void startBackup(MSBackupInfo *backup_info);
 
167
        void terminateBackup();
 
168
        bool backupStatus(uint64_t *total, uint64_t *completed, bool *completed_ok);
 
169
        uint32_t backupID();
 
170
        
 
171
private:
 
172
        MSBackup                        *iBackupThread;
 
173
        uint32_t                                iBackupTime; // The time at which the backup was started.
 
174
        bool                            iRecovering;
 
175
#ifdef HAVE_ALIAS_SUPPORT
 
176
        MSAlias                         *iBlobAliases;
 
177
        CSLock                          iBlobAliaseLock; // Write lock for the BLOB aliases index. This is required because of the .
 
178
#endif
 
179
        bool                            iClosing;
 
180
 
 
181
        CSSyncSortedList        *iTableList;
 
182
        CSSparseArray           *iTableArray;
 
183
        uint32_t                                iMaxTableID;
 
184
 
 
185
        MSTempLog                       *iWriteTempLog;
 
186
        bool                            iDropping;
 
187
        void                            dropDatabase();
 
188
        void                            startThreads();
 
189
 
 
190
        CSLock                          iBlobRefIdLock; // Lock for the BLOB ref counter.
 
191
        uint64_t                                iNextBlobRefId;
 
192
        
 
193
public:
 
194
        static uint64_t                 gBackupDatabaseID;
 
195
        static uint64_t                 gRepoThreshold;
 
196
        static uint64_t                 gTempLogThreshold;
 
197
 
 
198
        CSSyncSortedList        iHTTPMetaDataHeaders;
 
199
        static void startUp(const char *default_http_headers);
 
200
        static void stopThreads();
 
201
        static void shutDown();
 
202
 
 
203
        static MSDatabase *getBackupDatabase(CSString *db_location, CSString *db_name, uint32_t db_id, bool create);
 
204
        
 
205
        static MSDatabase *getDatabase(CSString *db_name, bool create);
 
206
        static MSDatabase *getDatabase(const char *db_name, bool create);
 
207
        static MSDatabase *getDatabase(uint32_t db_id);
 
208
        static uint32_t getDatabaseID(CSString *db_name, bool create);
 
209
        static uint32_t getDatabaseID(const char *db_name, bool create);
 
210
 
 
211
        static void wakeTempLogThreads();
 
212
        static void dropDatabase(MSDatabase *doomedDatabase, const char *db_name = NULL);
 
213
        static void dropDatabase(const char *db_name);
 
214
        
 
215
        static bool convertTablePathToIDs(const char *path, uint32_t *db_id, uint32_t *tab_id, bool create);
 
216
        static bool convertTableAndDatabaseToIDs(const char *db_name, const char *tab_name, uint32_t *db_id, uint32_t *tab_id, bool create);
 
217
 
 
218
 
 
219
private:
 
220
        static CSSyncSortedList *gDatabaseList;
 
221
        static CSSparseArray    *gDatabaseArray;
 
222
        
 
223
        
 
224
        static void removeDatabasePath(CSString *doomedDatabasePath);
 
225
 
 
226
        static uint32_t getDBID(CSPath *path, CSString *db_name);
 
227
        static CSPath *createDatabasePath(const char *location, CSString *db_name, uint32_t *db_id_ptr, bool *create, bool is_pbms = false);
 
228
        static MSDatabase *newDatabase(const char *db_location, CSString *db_name, uint32_t     db_id, bool create);
 
229
        static MSDatabase *loadDatabase(CSString *db_name,  bool create);
 
230
};
 
231
 
 
232
#endif