Drizzled Public API Documentation

database_ms.h

00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-06-26
00023  *
00024  * H&G2JCtL
00025  *
00026  * Contains all the information about an open database.
00027  *
00028  */
00029 
00030 #pragma once
00031 #ifndef __DATABASE_MS_H__
00032 #define __DATABASE_MS_H__
00033 
00034 #include "cslib/CSDefs.h"
00035 #include "cslib/CSStorage.h"
00036 #include "cslib/CSStrUtil.h"
00037 
00038 #include "table_ms.h"
00039 #include "repository_ms.h"
00040 #ifdef HAVE_ALIAS_SUPPORT
00041 #include "alias_ms.h"
00042 #endif
00043 #include "temp_log_ms.h"
00044 #include "compactor_ms.h"
00045 #include "cloud_ms.h"
00046 
00047 class MSOpenTable;
00048 class MSBackup;
00049 
00050 class MSDatabase : public CSSharedRefObject {
00051 public:
00052   bool        myIsPBMS;
00053   uint32_t        myDatabaseID;
00054   CSString      *myDatabaseName;  // The database name may not be related to the database path,
00055   CSString      *myDatabasePath;  // do not make any assumptions here!
00056   CSSyncSparseArray *myTempLogArray;
00057   MSCompactorThread *myCompactorThread;
00058   MSTempLogThread   *myTempLogThread;
00059   CSSyncVector    *myRepostoryList;
00060   CloudDB       *myBlobCloud;
00061   uint8_t       myBlobType;     // Cloud or repository
00062 
00063 
00064 
00065   MSDatabase();
00066   virtual ~MSDatabase();
00067 
00068   const char *getDatabaseNameCString();
00069   
00070   MSTable *getTable(CSString *tab_name, bool create);
00071   MSTable *getTable(const char *tab_name, bool create);
00072   MSTable *getTable(uint32_t tab_id, bool missing_ok);
00073   MSTable *getNextTable(uint32_t *pos);
00074 
00075   void addTable(uint32_t tab_id, const char *tab_name, off64_t file_size, bool to_delete);
00076   void addTableFromFile(CSDirectory *dir, const char *file_name, bool to_delete);
00077   void removeTable(MSTable *tab);
00078   void dropTable(MSTable *tab);
00079   void renameTable(MSTable *tab, const char *to_name);
00080   CSString *getATableName();
00081   uint32_t getTableCount();
00082 
00083   void openWriteRepo(MSOpenTable *otab);
00084 
00085   MSRepository *getRepoFullOfTrash(time_t *wait_time);
00086   MSRepository *lockRepo(off64_t size);
00087   void removeRepo(uint32_t repo_id, bool *mustQuit);
00088 
00089   MSRepoFile *getRepoFileFromPool(uint32_t repo_id, bool missing_ok);
00090   void returnRepoFileToPool(MSRepoFile *file);
00091   
00092   uint64_t newBlobRefId() // Returns a unique blob referfence Id.
00093   {
00094     uint64_t id;
00095     enter_();
00096     lock_(&iBlobRefIdLock);
00097     id = iNextBlobRefId++;
00098     unlock_(&iBlobRefIdLock);
00099     return_(COMMIT_MASK(id));
00100   }
00101   
00102 
00103 private:
00104   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);
00105 public:
00106 #ifdef HAVE_ALIAS_SUPPORT
00107   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);
00108 #else
00109   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)
00110   {
00111     queueTempLogEvent(otab, type, tab_id, blob_id, auth_code, log_id, log_offset, q_time);
00112   }
00113 #endif
00114   MSTempLogFile *openTempLogFile(uint32_t log_id, size_t *log_rec_size, size_t *log_head_size);
00115   uint32_t getTempLogCount();
00116   void removeTempLog(uint32_t log_id);
00117 
00118   /* Make this object sortable: */
00119   virtual CSObject *getKey();
00120   virtual int compareKey(CSObject *);
00121 
00122   MSCompactorThread *getCompactorThread();
00123   CSSyncVector *getRepositoryList();
00124 
00125 #ifdef HAVE_ALIAS_SUPPORT
00126   bool findBlobWithAlias(const char *alias, uint32_t *repo_id = NULL, uint64_t *repo_offset = NULL)
00127   {
00128     bool found;
00129     uint32_t x_repo_id;
00130     uint64_t x_repo_offset;
00131     bool referenced; // The BLOB can be referenced or non referenced.
00132     
00133     enter_();
00134     if (!repo_id) repo_id = &x_repo_id;
00135     if (!repo_offset) repo_offset = &x_repo_offset;
00136     
00137     lock_(&iBlobAliaseLock);
00138     found = iBlobAliases->findBlobByAlias(alias, &referenced, repo_id, repo_offset);
00139     unlock_(&iBlobAliaseLock);
00140     return_(found);
00141   }
00142   uint32_t registerBlobAlias(uint32_t repo_id, uint64_t repo_offset, const char *alias);
00143   uint32_t updateBlobAlias(uint32_t repo_id, uint64_t repo_offset, uint32_t old_alias_hash, const char *alias);
00144   void deleteBlobAlias(MSDiskAliasPtr diskRec);
00145   void deleteBlobAlias(uint32_t repo_id, uint64_t repo_offset, uint32_t alias_hash);
00146   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);
00147 #endif
00148 
00149   bool isValidHeaderField(const char *name);
00150   
00151   bool isRecovering() { return iRecovering;} // Indicates the database is being recovered from a dump.
00152   void setRecovering(bool recovering) { // Indicate if the database is being recovered from a dump.
00153     if (iRecovering == recovering)
00154       return;
00155     iRecovering = recovering;
00156     if (iRecovering) {
00157       myCompactorThread->suspend();
00158       myTempLogThread->suspend();
00159     } else {
00160       myCompactorThread->resume();
00161       myTempLogThread->resume();
00162     }
00163   }
00164   
00165   bool isBackup;
00166   void setBackupDatabase(); // Signals the database that it is the destination for a backup process.
00167   void releaseBackupDatabase(); 
00168   
00169   void startBackup(MSBackupInfo *backup_info);
00170   void terminateBackup();
00171   bool backupStatus(uint64_t *total, uint64_t *completed, bool *completed_ok);
00172   uint32_t backupID();
00173   
00174 private:
00175   MSBackup      *iBackupThread;
00176   uint32_t        iBackupTime; // The time at which the backup was started.
00177   bool        iRecovering;
00178 #ifdef HAVE_ALIAS_SUPPORT
00179   MSAlias       *iBlobAliases;
00180   CSLock        iBlobAliaseLock; // Write lock for the BLOB aliases index. This is required because of the .
00181 #endif
00182   bool        iClosing;
00183 
00184   CSSyncSortedList  *iTableList;
00185   CSSparseArray   *iTableArray;
00186   uint32_t        iMaxTableID;
00187 
00188   MSTempLog     *iWriteTempLog;
00189   bool        iDropping;
00190   void        dropDatabase();
00191   void        startThreads();
00192 
00193   CSLock        iBlobRefIdLock; // Lock for the BLOB ref counter.
00194   uint64_t        iNextBlobRefId;
00195   
00196 public:
00197 
00198   CSSyncSortedList  iHTTPMetaDataHeaders;
00199   static void startUp(const char *default_http_headers);
00200   static void stopThreads();
00201   static void shutDown();
00202 
00203   static MSDatabase *getBackupDatabase(CSString *db_location, CSString *db_name, uint32_t db_id, bool create);
00204   
00205   static MSDatabase *getDatabase(CSString *db_name, bool create);
00206   static MSDatabase *getDatabase(const char *db_name, bool create);
00207   static MSDatabase *getDatabase(uint32_t db_id, bool missing_ok = false);
00208   static uint32_t getDatabaseID(CSString *db_name, bool create);
00209   static uint32_t getDatabaseID(const char *db_name, bool create);
00210 
00211   static void wakeTempLogThreads();
00212   static void dropDatabase(MSDatabase *doomedDatabase, const char *db_name = NULL);
00213   static void dropDatabase(const char *db_name);
00214   
00215   static bool convertTablePathToIDs(const char *path, uint32_t *db_id, uint32_t *tab_id, bool create);
00216   static bool convertTableAndDatabaseToIDs(const char *db_name, const char *tab_name, uint32_t *db_id, uint32_t *tab_id, bool create);
00217 
00218 
00219 private:
00220   static CSSyncSortedList *gDatabaseList;
00221   static CSSparseArray  *gDatabaseArray;
00222   
00223   
00224   static void removeDatabasePath(CSString *doomedDatabasePath);
00225 
00226   static uint32_t getDBID(CSPath *path, CSString *db_name);
00227   static CSPath *createDatabasePath(const char *location, CSString *db_name, uint32_t *db_id_ptr, bool *create, bool is_pbms = false);
00228   static MSDatabase *newDatabase(const char *db_location, CSString *db_name, uint32_t db_id, bool create);
00229   static MSDatabase *loadDatabase(CSString *db_name,  bool create);
00230   static uint32_t fileToTableId(const char *file_name, const char *name_part = NULL);
00231   const char *fileToTableName(size_t size, char *tab_name, const char *file_name);
00232 
00233 };
00234 
00235 #endif