/* 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