00001 /* Copyright (C) 2009 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 * Barry Leslie 00020 * 00021 * 2009-06-10 00022 * 00023 * H&G2JCtL 00024 * 00025 * PBMS transaction cache. 00026 * 00027 */ 00028 00029 #pragma once 00030 #ifndef __TRANSCACHE_MS_H__ 00031 #define __TRANSCACHE_MS_H__ 00032 00033 #include "cslib/CSDefs.h" 00034 #include "trans_log_ms.h" 00035 00036 #define TRANS_CACHE_NEW_REF ((TRef)-1) 00037 #define TRANS_CACHE_UNKNOWN_REF ((TRef)-2) 00038 00039 class MSTransCache : public CSSharedRefObject 00040 { 00041 public: 00042 MSTransCache(); 00043 ~MSTransCache(); 00044 00045 // Prepare to add another record to a tansaction 00046 // This will preallocate anything required so that the call 00047 // to tc_AddRec() cannot fail. 00048 //void tc_PrepAddRec(TRef tref, uint32_t tr_id); 00049 00050 // Add a transaction record to an already existing transaction 00051 // or possible creating a new one. Depending on the record added this may 00052 // also commit or rollback the transaction. 00053 // Returns false if the list is full. 00054 //void tc_AddRec(uint64_t log_position, MSTransPtr rec); 00055 void tc_AddRec(uint64_t log_position, MSTransPtr rec, TRef tref = TRANS_CACHE_UNKNOWN_REF); 00056 void tc_LoadRec(uint64_t log_position, MSTransPtr rec) { tc_AddRec(log_position, rec);} 00057 00058 // Get the transaction ref of the first transaction in the list. 00059 // Sets terminated to true or false depending on if the transaction is terminated. 00060 // If there is no trsansaction then false is returned. 00061 bool tc_GetTransaction(TRef *ref, bool *terminated); 00062 00063 uint32_t tc_GetTransactionID(TRef ref); 00064 00065 // Get the state of the terminated transaction. 00066 MS_TxnState tc_TransactionState(TRef ref); 00067 00068 // Remove the transaction and all record associated with it. 00069 void tc_FreeTransaction(TRef tref); 00070 00071 // Get the log position of the first transaction in the list. 00072 // Returns false if there is no transaction. 00073 bool tc_GetTransactionStartPosition(uint64_t *log_position); 00074 00075 // Get the nth record for the specified transaction. A pointer to the 00076 // storage location is passed in. If there is no nth record 'false' is returned 00077 // otherwise the passed in record buffer is filled out and the pointer to 00078 // it is returned. 00079 bool tc_GetRecAt(TRef tref, size_t index, MSTransPtr rec, MS_TxnState *state); 00080 00081 uint32_t tc_GetCacheUsed() { return tc_Used;} 00082 uint32_t tc_GetPercentCacheUsed() { return (tc_Used * 100)/(tc_Size-1);} 00083 uint32_t tc_GetPercentCacheHit() { return (tc_TotalCacheCount * 100)/tc_TotalTransCount;} 00084 00085 // Notify the cache that recovery is in progress. 00086 // In this case transaction records are not cached. 00087 void tc_SetRecovering(bool recovering) {tc_Recovering = recovering;} 00088 00089 // Methods used to update the cache from disk. 00090 bool tc_ShoulReloadCache(); // Test to see if the cache needs to be reloaded. 00091 uint64_t tc_StartCacheReload(bool startup = false); // Initialize the cache for reload. 00092 bool tc_ContinueCacheReload(); // Returns false when the cache is full. 00093 void tc_CompleteCacheReload(); // Signal the end of the cache reload operation. 00094 00095 void tc_UpdateCacheVersion() {tc_CacheVersion++;} 00096 00097 void tc_SetSize(uint32_t cache_size); // Chang the size of the cache. 00098 00099 void tc_dropDatabase(uint32_t db_id); // clears records from ther cache for a dropped database. 00100 00101 static MSTransCache *newMSTransCache(uint32_t cache_size); 00102 00103 private: 00104 void tc_Initialize(uint32_t size); 00105 TRef tc_NewTransaction(uint32_t tid); 00106 void tc_FindTXNRef(uint32_t tid, TRef *tref); 00107 00108 struct TransList *tc_List; // The transaction list, One entry per transaction. 00109 struct TransList *tc_OverFlow; // The first transaction to overflow. 00110 uint32_t tc_Size; // The current size of the list 00111 uint32_t tc_EOL; // The index of the first unused transaction list record 00112 uint32_t tc_First; // The index of the first used transaction list record 00113 uint32_t tc_Used; // The number of used transaction list records 00114 00115 uint64_t tc_TotalTransCount; // The number of transaction to be cached 00116 uint64_t tc_TotalCacheCount; // The number of transaction cached 00117 00118 CSThread *tc_ReLoadingThread; // The thread performing a reload. 00119 uint32_t tc_OverFlowTID; // The transaction id of the first transaction in th reload. 00120 bool tc_Full; 00121 uint32_t tc_CacheVersion; 00122 00123 bool tc_Recovering; 00124 00125 #ifdef DEBUG 00126 uint32_t tc_ReloadCnt; 00127 #endif 00128 00129 }; 00130 00131 00132 #endif // __TRANSCACHE_MS_H__