src_other/tuplebase/ZTBRep_TS.h

00001 /*  @(#) $Id: ZTBRep_TS.h,v 1.12 2007/07/15 20:08:20 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2003 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // From ZTBRep
00054         virtual void AllocateIDs(size_t iCount,
00055                         Callback_AllocateIDs_t iCallback_AllocateIDs, void* iRefcon);
00056 
00057         virtual ZTBRepTransaction* CreateTransaction();
00058 
00059 // Our protocol
00060         void AddIndex(ZRef<ZTupleIndexFactory> iFactory);
00061         void AddIndices(const std::vector<ZRef<ZTupleIndexFactory> >& iFactories);
00062 
00063         ZRef<ZTS> GetTS();
00064 
00065 
00066         // The TransTuple name has to be public, so we can typedef
00067         // MapTransTuple_t outside this class declaration.
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 // Trans
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 // Private
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__

Generated on Thu Jul 26 11:21:57 2007 for ZooLib by  doxygen 1.4.7