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 __ZTSWatcherMUX__
00026 #define __ZTSWatcherMUX__
00027 #include "zconfig.h"
00028
00029 #include "ZDList.h"
00030 #include "ZTSWatcher.h"
00031
00032 #ifndef ZCONFIG_TSWatcherMUX_Debug
00033 # define ZCONFIG_TSWatcherMUX_Debug 1
00034 #endif
00035
00036
00037 #pragma mark -
00038 #pragma mark * ZTSWatcherMUX
00039
00040 class ZTSWatcherMUX
00041 {
00042 public:
00043 enum { kDebug = ZCONFIG_TSWatcherMUX_Debug };
00044
00045 ZTSWatcherMUX(ZRef<ZTSWatcher> iTSWatcher, size_t iCacheSize_Queries, size_t iCacheSize_Tuples);
00046 ~ZTSWatcherMUX();
00047
00048 ZRef<ZTSWatcher> NewWatcher();
00049 ZRef<ZTSWatcher> NewWatcher(bool iAlwaysForceSync);
00050
00051 private:
00052 class Watcher;
00053
00054 class PQuery;
00055 class DLink_PQuery_Sync;
00056 class DLink_PQuery_Cached;
00057
00058 class WQuery;
00059 class DLink_WQuery_PQuery;
00060 class DLink_WQuery_Tripped;
00061
00062 class PTuple;
00063 class DLink_PTuple_Sync;
00064 class DLink_PTuple_Cached;
00065
00066 class WTuple;
00067 class DLink_WTuple_PTuple;
00068 class DLink_WTuple_Tripped;
00069
00070 void Watcher_Finalize(Watcher* iWatcher);
00071
00072 bool Watcher_AllocateIDs(Watcher* iWatcher,
00073 size_t iCount, uint64& oBaseID, size_t& oCountIssued);
00074
00075 bool Watcher_Sync(Watcher* iWatcher,
00076 const uint64* iRemovedIDs, size_t iRemovedIDsCount,
00077 const uint64* iAddedIDs, size_t iAddedIDsCount,
00078 const int64* iRemovedQueries, size_t iRemovedQueriesCount,
00079 const ZTSWatcher::AddedQueryCombo* iAddedQueries, size_t iAddedQueriesCount,
00080 std::vector<uint64>& oAddedIDs,
00081 std::vector<uint64>& oChangedTupleIDs, std::vector<ZTuple>& oChangedTuples,
00082 const uint64* iWrittenTupleIDs, const ZTuple* iWrittenTuples, size_t iWrittenTuplesCount,
00083 std::map<int64, std::vector<uint64> >& oChangedQueries);
00084
00085 void Watcher_SetCallback(Watcher* iWatcher, ZTSWatcher::Callback_t iCallback, void* iRefcon);
00086
00087 bool pSyncAll(bool iWaitForSync, Watcher* iWatcher);
00088
00089 PQuery* pGetPQuery(const ZTSWatcher::AddedQueryCombo& iAQC);
00090 PQuery* pGetPQueryIfExtant(int64 iRefcon);
00091
00092 PTuple* pGetPTuple(uint64 iID);
00093 PTuple* pGetPTupleMustExist(uint64 iID);
00094 PTuple* pGetPTupleIfExtant(uint64 iID);
00095
00096 void Callback();
00097 static void sCallback(void* iRefcon);
00098
00099 ZMutex fMutex_Structure;
00100 ZCondition fCondition;
00101
00102 int64 fCurrentSyncNumber;
00103 int64 fNextSyncNumber;
00104 bool fForceNextSync;
00105
00106 ZRef<ZTSWatcher> fTSWatcher;
00107
00108 std::set<Watcher*> fWatchers;
00109
00110 int64 fNextQueryRefcon;
00111 std::map<ZMemoryBlock, PQuery> fMB_To_PQuery;
00112 std::map<int64, PQuery*> fRefcon_To_PQuery;
00113 ZooLib::DListHead<DLink_PQuery_Sync> fPQueries_Sync;
00114 ZooLib::DListHead<DLink_PQuery_Cached> fPQueries_Cached;
00115 size_t fCacheSize_Queries;
00116
00117 std::map<uint64, PTuple> fPTuples;
00118 ZooLib::DListHead<DLink_PTuple_Sync> fPTuples_Sync;
00119 ZooLib::DListHead<DLink_PTuple_Cached> fPTuples_Cached;
00120 size_t fCacheSize_Tuples;
00121
00122 friend class Watcher;
00123 };
00124
00125 #endif // __ZTSWatcherMUX__