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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* PBMS transaction cache.
29
#ifndef __TRANSCACHE_MS_H__
30
#define __TRANSCACHE_MS_H__
32
#include "cslib/CSDefs.h"
33
#include "trans_log_ms.h"
35
#define TRANS_CACHE_NEW_REF ((TRef)-1)
36
#define TRANS_CACHE_UNKNOWN_REF ((TRef)-2)
38
class MSTransCache : public CSSharedRefObject
44
// Prepare to add another record to a tansaction
45
// This will preallocate anything required so that the call
46
// to tc_AddRec() cannot fail.
47
//void tc_PrepAddRec(TRef tref, uint32_t tr_id);
49
// Add a transaction record to an already existing transaction
50
// or possible creating a new one. Depending on the record added this may
51
// also commit or rollback the transaction.
52
// Returns false if the list is full.
53
//void tc_AddRec(uint64_t log_position, MSTransPtr rec);
54
void tc_AddRec(uint64_t log_position, MSTransPtr rec, TRef tref = TRANS_CACHE_UNKNOWN_REF);
55
void tc_LoadRec(uint64_t log_position, MSTransPtr rec) { tc_AddRec(log_position, rec);}
57
// Get the transaction ref of the first transaction in the list.
58
// Sets terminated to true or false depending on if the transaction is terminated.
59
// If there is no trsansaction then false is returned.
60
bool tc_GetTransaction(TRef *ref, bool *terminated);
62
uint32_t tc_GetTransactionID(TRef ref);
64
// Get the state of the terminated transaction.
65
MS_TxnState tc_TransactionState(TRef ref);
67
// Remove the transaction and all record associated with it.
68
void tc_FreeTransaction(TRef tref);
70
// Get the log position of the first transaction in the list.
71
// Returns false if there is no transaction.
72
bool tc_GetTransactionStartPosition(uint64_t *log_position);
74
// Get the nth record for the specified transaction. A pointer to the
75
// storage location is passed in. If there is no nth record 'false' is returned
76
// otherwise the passed in record buffer is filled out and the pointer to
78
bool tc_GetRecAt(TRef tref, size_t index, MSTransPtr rec, MS_TxnState *state);
80
uint32_t tc_GetCacheUsed() { return tc_Used;}
81
uint32_t tc_GetPercentCacheUsed() { return (tc_Used * 100)/(tc_Size-1);}
82
uint32_t tc_GetPercentCacheHit() { return (tc_TotalCacheCount * 100)/tc_TotalTransCount;}
84
// Notify the cache that recovery is in progress.
85
// In this case transaction records are not cached.
86
void tc_SetRecovering(bool recovering) {tc_Recovering = recovering;}
88
// Methods used to update the cache from disk.
89
bool tc_ShoulReloadCache(); // Test to see if the cache needs to be reloaded.
90
uint64_t tc_StartCacheReload(bool startup = false); // Initialize the cache for reload.
91
bool tc_ContinueCacheReload(); // Returns false when the cache is full.
92
void tc_CompleteCacheReload(); // Signal the end of the cache reload operation.
94
void tc_UpdateCacheVersion() {tc_CacheVersion++;}
96
void tc_SetSize(uint32_t cache_size); // Chang the size of the cache.
98
void tc_dropDatabase(uint32_t db_id); // clears records from ther cache for a dropped database.
100
static MSTransCache *newMSTransCache(uint32_t cache_size);
103
void tc_Initialize(uint32_t size);
104
TRef tc_NewTransaction(uint32_t tid);
105
void tc_FindTXNRef(uint32_t tid, TRef *tref);
107
struct TransList *tc_List; // The transaction list, One entry per transaction.
108
struct TransList *tc_OverFlow; // The first transaction to overflow.
109
uint32_t tc_Size; // The current size of the list
110
uint32_t tc_EOL; // The index of the first unused transaction list record
111
uint32_t tc_First; // The index of the first used transaction list record
112
uint32_t tc_Used; // The number of used transaction list records
114
uint64_t tc_TotalTransCount; // The number of transaction to be cached
115
uint64_t tc_TotalCacheCount; // The number of transaction cached
117
CSThread *tc_ReLoadingThread; // The thread performing a reload.
118
uint32_t tc_OverFlowTID; // The transaction id of the first transaction in th reload.
120
uint32_t tc_CacheVersion;
125
uint32_t tc_ReloadCnt;
131
#endif // __TRANSCACHE_MS_H__