1
/* Copyright (C) 2009 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
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.
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.
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
25
* PBMS transaction cache.
30
#ifndef __TRANSCACHE_MS_H__
31
#define __TRANSCACHE_MS_H__
33
#include "cslib/CSDefs.h"
34
#include "trans_log_ms.h"
36
#define TRANS_CACHE_NEW_REF ((TRef)-1)
37
#define TRANS_CACHE_UNKNOWN_REF ((TRef)-2)
39
class MSTransCache : public CSSharedRefObject
45
// Prepare to add another record to a tansaction
46
// This will preallocate anything required so that the call
47
// to tc_AddRec() cannot fail.
48
//void tc_PrepAddRec(TRef tref, uint32_t tr_id);
50
// Add a transaction record to an already existing transaction
51
// or possible creating a new one. Depending on the record added this may
52
// also commit or rollback the transaction.
53
// Returns false if the list is full.
54
//void tc_AddRec(uint64_t log_position, MSTransPtr rec);
55
void tc_AddRec(uint64_t log_position, MSTransPtr rec, TRef tref = TRANS_CACHE_UNKNOWN_REF);
56
void tc_LoadRec(uint64_t log_position, MSTransPtr rec) { tc_AddRec(log_position, rec);}
58
// Get the transaction ref of the first transaction in the list.
59
// Sets terminated to true or false depending on if the transaction is terminated.
60
// If there is no trsansaction then false is returned.
61
bool tc_GetTransaction(TRef *ref, bool *terminated);
63
uint32_t tc_GetTransactionID(TRef ref);
65
// Get the state of the terminated transaction.
66
MS_TxnState tc_TransactionState(TRef ref);
68
// Remove the transaction and all record associated with it.
69
void tc_FreeTransaction(TRef tref);
71
// Get the log position of the first transaction in the list.
72
// Returns false if there is no transaction.
73
bool tc_GetTransactionStartPosition(uint64_t *log_position);
75
// Get the nth record for the specified transaction. A pointer to the
76
// storage location is passed in. If there is no nth record 'false' is returned
77
// otherwise the passed in record buffer is filled out and the pointer to
79
bool tc_GetRecAt(TRef tref, size_t index, MSTransPtr rec, MS_TxnState *state);
81
uint32_t tc_GetCacheUsed() { return tc_Used;}
82
uint32_t tc_GetPercentCacheUsed() { return (tc_Used * 100)/(tc_Size-1);}
83
uint32_t tc_GetPercentCacheHit() { return (tc_TotalCacheCount * 100)/tc_TotalTransCount;}
85
// Notify the cache that recovery is in progress.
86
// In this case transaction records are not cached.
87
void tc_SetRecovering(bool recovering) {tc_Recovering = recovering;}
89
// Methods used to update the cache from disk.
90
bool tc_ShoulReloadCache(); // Test to see if the cache needs to be reloaded.
91
uint64_t tc_StartCacheReload(bool startup = false); // Initialize the cache for reload.
92
bool tc_ContinueCacheReload(); // Returns false when the cache is full.
93
void tc_CompleteCacheReload(); // Signal the end of the cache reload operation.
95
void tc_UpdateCacheVersion() {tc_CacheVersion++;}
97
void tc_SetSize(uint32_t cache_size); // Chang the size of the cache.
99
void tc_dropDatabase(uint32_t db_id); // clears records from ther cache for a dropped database.
101
static MSTransCache *newMSTransCache(uint32_t cache_size);
104
void tc_Initialize(uint32_t size);
105
TRef tc_NewTransaction(uint32_t tid);
106
void tc_FindTXNRef(uint32_t tid, TRef *tref);
108
struct TransList *tc_List; // The transaction list, One entry per transaction.
109
struct TransList *tc_OverFlow; // The first transaction to overflow.
110
uint32_t tc_Size; // The current size of the list
111
uint32_t tc_EOL; // The index of the first unused transaction list record
112
uint32_t tc_First; // The index of the first used transaction list record
113
uint32_t tc_Used; // The number of used transaction list records
115
uint64_t tc_TotalTransCount; // The number of transaction to be cached
116
uint64_t tc_TotalCacheCount; // The number of transaction cached
118
CSThread *tc_ReLoadingThread; // The thread performing a reload.
119
uint32_t tc_OverFlowTID; // The transaction id of the first transaction in th reload.
121
uint32_t tc_CacheVersion;
126
uint32_t tc_ReloadCnt;
132
#endif // __TRANSCACHE_MS_H__