~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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