Drizzled Public API Documentation

open_table_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-04
00023  *
00024  * H&G2JCtL
00025  *
00026  * Media Stream Tables.
00027  *
00028  */
00029 
00030 #pragma once
00031 #ifndef __OPENTABLE_MS_H__
00032 #define __OPENTABLE_MS_H__
00033 
00034 #include "cslib/CSDefs.h"
00035 #include "cslib/CSFile.h"
00036 
00037 #include "engine_ms.h"
00038 
00039 #include "database_ms.h"
00040 
00041 class MSOpenTablePool;
00042 
00043 // Keep the value of MS_OT_BUFFER_SIZE less than 0XFFFF to avoid compiler warnings.
00044 //
00045 // From looking at transmition speed using tcpdump it looks like 16K is a goor buffer size.
00046 // It is very important that it be larger than the TCP frame size of about 1.5K on the 
00047 // machine I was testing on. There did not eam to be much to gain by having a buffer size
00048 // larger tham 16K.
00049 //
00050 // I wonder if performance could be gained by having 2 buffers and 2 threads per send
00051 // so that one buffer could be being sent while the other was being filled.
00052 #ifdef DEBUG
00053 //#define MS_OT_BUFFER_SIZE   ((uint16_t)(512))
00054 #define MS_OT_BUFFER_SIZE   ((uint16_t)(16384))
00055 #else
00056 //#define MS_OT_BUFFER_SIZE   ((uint16_t)(0XFFF0)) 
00057 #define MS_OT_BUFFER_SIZE   ((uint16_t)(16384))  
00058 #endif
00059 
00060 #define MS_UB_SAME_TAB      0
00061 #define MS_UB_NEW_HANDLE    1
00062 #define MS_UB_NEW_BLOB      2
00063 #define MS_UB_RETAINED      3
00064 #define MS_UB_NEW_RETAINED    4
00065 
00066 /*
00067 typedef struct MSUsedBlob {
00068   int         ub_state;
00069   uint16_t        ub_col_index;
00070   uint32_t        ub_repo_id;
00071   uint64_t        ub_repo_offset;
00072 
00073   uint16_t        ub_head_size;
00074   uint64_t        ub_blob_size;
00075   uint64_t        ub_blob_ref_id;
00076   uint32_t        ub_blob_id;
00077   uint32_t        ub_auth_code;
00078   char        ub_blob_url[PBMS_BLOB_URL_SIZE];
00079 } MSUsedBlobRec, *MSUsedBlobPtr;
00080 */
00081 
00082 class MSOpenTable : public CSRefObject, public CSPooled {
00083 public:
00084   bool        inUse;
00085   bool        isNotATable;
00086   MSOpenTable     *nextTable;
00087   MSOpenTablePool   *myPool;
00088 
00089   CSFile        *myTableFile;
00090 
00091   MSRepository    *myWriteRepo;
00092   MSRepoFile      *myWriteRepoFile;
00093 
00094   MSTempLogFile   *myTempLogFile;
00095 
00096   char        myOTBuffer[MS_OT_BUFFER_SIZE];
00097 
00098   MSOpenTable();
00099   virtual ~MSOpenTable();
00100 
00101   virtual void returnToPool();
00102 
00103   void close();
00104 
00105   void createBlob(PBMSBlobURLPtr bh, uint64_t size, char *metadata, uint16_t metadata_size, CSInputStream *stream, CloudKeyPtr cloud_key = NULL, Md5Digest *checksum = NULL);
00106   void createBlob(PBMSBlobIDPtr blob_id, uint64_t blob_size, char *metadata, uint16_t metadata_size);
00107   void sendRepoBlob(uint64_t blob_id, uint64_t req_offset, uint64_t req_size, uint32_t auth_code, bool info_only, CSHTTPOutputStream *stream);
00108   void useBlob(int type, uint32_t db_id, uint32_t tab_id, uint64_t blob_id, uint32_t auth_code, uint16_t col_index, uint64_t blob_size, uint64_t blob_ref_id, PBMSBlobURLPtr ret_blob_url);
00109 
00110   void releaseReference(uint64_t blob_id, uint64_t blob_ref_id);
00111   void freeReference(uint64_t blob_id, uint64_t blob_ref_id);
00112   void commitReference(uint64_t blob_id, uint64_t blob_ref_id);
00113   void checkBlob(CSStringBuffer *buffer, uint64_t blob_id, uint32_t auth_code, uint32_t temp_log_id, uint32_t temp_log_offset);
00114   bool deleteReferences(uint32_t temp_log_id, uint32_t temp_log_offset, bool *must_quit);
00115 
00116   void openForReading();
00117   void closeForWriting();
00118 
00119   bool haveTable() { return (myPool != NULL); }
00120   uint32_t getTableID();
00121   MSTable *getDBTable();
00122   MSDatabase *getDB();
00123 
00124   void formatBlobURL(PBMSBlobURLPtr blob_url, uint64_t blob_id, uint32_t auth_code, uint64_t blob_size, uint32_t tab_id, uint64_t blob_ref_id);
00125   void formatBlobURL(PBMSBlobURLPtr blob_url, uint64_t blob_id, uint32_t auth_code, uint64_t blob_size, uint64_t blob_ref_id);
00126   void formatRepoURL(PBMSBlobURLPtr blob_url, uint32_t log_id, uint64_t log_offset, uint32_t auth_code, uint64_t blob_size);
00127 
00128   /* Make this object linkable: */
00129   virtual CSObject *getNextLink() { return iNextLink; }
00130   virtual CSObject *getPrevLink() { return iPrevLink; }
00131   virtual void setNextLink(CSObject *link) { iNextLink = link; }
00132   virtual void setPrevLink(CSObject *link) { iPrevLink = link; }
00133 
00134 private:
00135   CSObject      *iNextLink;
00136   CSObject      *iPrevLink;
00137 
00138 //  uint32_t        iUseSize;
00139 //  uint32_t        iUseCount;
00140 //  MSUsedBlobPtr   iUsedBlobs;
00141   
00142 
00143   void openForWriting();
00144 
00145 public:
00146   static MSOpenTable *newOpenTable(MSOpenTablePool *pool);
00147 };
00148 
00149 class MSOpenTablePool : public CSRefObject, public CSPooled {
00150 public:
00151   uint32_t      myPoolTableID;            /* Non-zero if in the ID list. */
00152   bool      isRemovingTP;           /* Set to true if the table pool is being removed. */
00153   MSTable     *myPoolTable;
00154   MSDatabase    *myPoolDB;
00155 
00156   MSOpenTablePool();
00157   virtual ~MSOpenTablePool();
00158 
00159   MSOpenTable *getPoolTable();            /* Returns NULL if there is no table in the pool. */
00160   void returnOpenTable(MSOpenTable *otab);
00161 
00162   void addOpenTable(MSOpenTable *otab);
00163   void removeOpenTable(MSOpenTable *otab);
00164 
00165   void removeOpenTablesNotInUse();
00166   
00167   virtual void returnToPool();
00168 
00169 #ifdef DEBUG
00170   void check();
00171 #endif
00172   
00173   uint32_t getSize() { return iPoolTables.getSize(); }
00174 
00175 private:
00176   MSOpenTable   *iTablePool;            /* A list of tables currently not in use. */
00177   CSLinkedList  iPoolTables;            /* A list of all tables in this pool */
00178 
00179 public:
00180   static MSOpenTablePool *newPool(uint32_t db_id, uint32_t tab_id);
00181 };
00182 
00183 class MSTableList : public CSObject {
00184 public:
00185   MSTableList();
00186   ~MSTableList();
00187 
00188   static void startUp();
00189   static void shutDown();
00190 
00191   static void debug(MSOpenTable *otab);
00192 
00193   static MSOpenTable *getOpenTableByID(uint32_t db_id, uint32_t tab_id);
00194   static MSOpenTable *getOpenTableForDB(uint32_t db_id);
00195   static void releaseTable(MSOpenTable *otab);
00196 
00197   static bool removeTablePoolIfEmpty(MSOpenTablePool *pool);
00198   static void removeTablePool(MSOpenTablePool *pool);
00199   static void removeTablePool(MSOpenTable *otab);
00200   static void removeDatabaseTables(MSDatabase *database);
00201 
00202   static MSOpenTablePool *lockTablePoolForDeletion(uint32_t db_id, uint32_t tab_id, CSString *db_name, CSString *tab_name);
00203   static MSOpenTablePool *lockTablePoolForDeletion(MSTable *table);
00204   static MSOpenTablePool *lockTablePoolForDeletion(CSString *table_url);
00205   static MSOpenTablePool *lockTablePoolForDeletion(MSOpenTable *otab);
00206   static void unlockTablePool(MSOpenTablePool *pool);
00207 
00208 private:
00209   static CSSyncOrderedList  *gPoolListByID;
00210 
00211 };
00212 
00213 #endif