00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __ZTBRep_TS__
00026 #define __ZTBRep_TS__ 1
00027 #include "zconfig.h"
00028
00029 #include "ZTBRep.h"
00030 #include "ZTS.h"
00031 #include "ZDList.h"
00032
00033 #include <map>
00034
00035 #ifndef ZCONFIG_TBRep_TS_Debug
00036 # define ZCONFIG_TBRep_TS_Debug 2
00037 #endif
00038
00039
00040 #pragma mark -
00041 #pragma mark * ZTBRep_TS
00042
00043 class ZTupleIndexFactory;
00044
00045 class ZTBRep_TS : public ZTBRep
00046 {
00047 public:
00048 enum { kDebug = ZCONFIG_TBRep_TS_Debug };
00049
00050 ZTBRep_TS(const std::vector<ZRef<ZTupleIndexFactory> >& iIndexFactories, ZRef<ZTS> iTS);
00051 virtual ~ZTBRep_TS();
00052
00053
00054 virtual void AllocateIDs(size_t iCount,
00055 Callback_AllocateIDs_t iCallback_AllocateIDs, void* iRefcon);
00056
00057 virtual ZTBRepTransaction* CreateTransaction();
00058
00059
00060 void AddIndex(ZRef<ZTupleIndexFactory> iFactory);
00061 void AddIndices(const std::vector<ZRef<ZTupleIndexFactory> >& iFactories);
00062
00063 ZRef<ZTS> GetTS();
00064
00065
00066
00067
00068 class TransTuple;
00069
00070 private:
00071 class Transaction;
00072 friend class Transaction;
00073
00074 class TransTupleUsing;
00075
00076 class TupleInUse;
00077 typedef std::map<uint64, TupleInUse> MapTupleInUse_t;
00078
00079
00080 ZTuple Trans_FetchTuple(Transaction* iTransaction, uint64 iID);
00081 void Trans_FetchTuples(Transaction* iTransaction,
00082 size_t iCount, const uint64* iIDs, ZTuple* oTuples);
00083
00084 void Trans_Search(Transaction* iTransaction, const ZTBSpec& iSpec, std::set<uint64>& ioIDs);
00085 void Trans_Search(Transaction* iTransaction, const ZTBSpec& iSpec, std::vector<uint64>& oIDs);
00086
00087 void Trans_SetTuple(Transaction* iTransaction, uint64 iID, const ZTuple& iTuple);
00088
00089 void Trans_AbortPreValidate(Transaction* iTransaction);
00090
00091 void Trans_Validate(Transaction* iTransaction,
00092 ZTBRepTransaction::Callback_Validate_t iCallback_Validate, void* iRefcon);
00093
00094 void Trans_AcceptFailure(Transaction* iTransaction);
00095
00096 void Trans_CancelPostValidate(Transaction* iTransaction);
00097
00098 void Trans_Commit(Transaction* iTransaction);
00099
00100
00101 void pFetchTuples(Transaction* iTransaction,
00102 size_t iCount, const uint64* iIDs, ZTuple* oTuples);
00103 void pFetchTuples(Transaction* iTransaction, const std::set<uint64>& iIDs);
00104
00105 void pTryValidation(Transaction* iTransaction);
00106 bool pAcquireLockOrQueue(Transaction* iTransaction, TupleInUse& iTIU, bool iWrite);
00107 void pReleaseLock(Transaction* iTransaction, TupleInUse& iTIU, bool iWrite);
00108
00109 TransTuple& pGetTransTuple(Transaction* iTransaction, uint64 iID);
00110 TransTuple& pAllocateTransTuple(const void* iPosition,
00111 Transaction* iTransaction, TupleInUse& iTIU,
00112 const ZTuple& iValue, uint64 iClockStart, bool iWritten);
00113 void pDisposeTransTuple(TransTuple& iTransTuple);
00114
00115 TupleInUse& pGetTupleInUse(uint64 iID);
00116 void pReleaseTupleInUse(TupleInUse& iTupleInUse);
00117
00118 ZRef<ZTS> fTS;
00119
00120 ZMutex fMutex_Structure;
00121 std::vector<ZRef<ZTupleIndexFactory> > fIndexFactories;
00122
00123 uint64 fClock;
00124
00125 MapTupleInUse_t fTuplesInUse;
00126
00127 std::set<Transaction*> fTransactions;
00128 };
00129
00130
00131 #pragma mark -
00132 #pragma mark * ZTBRep_TS::TupleInUse
00133
00134 class ZTBRep_TS::TupleInUse
00135 {
00136 public:
00137 uint64 fID;
00138
00139 ZooLib::DListHead<TransTupleUsing> fUsingTransTuples;
00140
00141 Transaction* fTransaction_Writer;
00142 size_t fTransaction_ReaderCount;
00143
00144 Transaction* fTransaction_Waiting;
00145
00146 ZTuple fValue;
00147
00148 uint64 fClock_LastWritten;
00149 };
00150
00151 #endif // __ZTBRep_TS__