src_other/tuplebase/ZTSWatcherMUX.h

00001 /*  @(#) $Id: ZTSWatcherMUX.h,v 1.8 2007/07/15 20:31:02 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2006 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 __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__

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