~stewart/drizzle/bug911643

« back to all changes in this revision

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

  • Committer: Mark Atwood
  • Date: 2011-12-20 02:32:53 UTC
  • mfrom: (2469.1.1 drizzle-build)
  • Revision ID: me@mark.atwood.name-20111220023253-bvu0kr14kwsdvz7g
mergeĀ lp:~brianaker/drizzle/deprecate-pbms

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