src_other/tuplebase/ZTS_Watchable.h

00001 /*  @(#) $Id: ZTS_Watchable.h,v 1.16 2007/06/04 22:53:20 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2005 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 __ZTS_Watchable__
00026 #define __ZTS_Watchable__
00027 #include "zconfig.h"
00028 
00029 #include "ZTS.h"
00030 #include "ZTSWatcher.h"
00031 
00032 #ifndef ZCONFIG_TS_Watchable_Debug
00033 #       define ZCONFIG_TS_Watchable_Debug 2
00034 #endif
00035 
00036 // =================================================================================================
00037 #pragma mark -
00038 #pragma mark * ZTS_Watchable
00039 
00040 class ZMemoryBlock;
00041 
00042 class ZTS_Watchable : public ZTS
00043         {
00044 public:
00045         enum { kDebug = ZCONFIG_TS_Watchable_Debug };
00046 
00047         ZTS_Watchable(ZRef<ZTS> iTS);
00048         virtual ~ZTS_Watchable();
00049 
00050 // From ZTS
00051         virtual void AllocateIDs(size_t iCount, uint64& oBaseID, size_t& oCount);
00052         virtual void SetTuples(size_t iCount, const uint64* iIDs, const ZTuple* iTuples);
00053         virtual void GetTuples(size_t iCount, const uint64* iIDs, ZTuple* oTuples);
00054         virtual void Search(const ZTBSpec& iTBSpec,
00055                 const std::set<uint64>& iSkipIDs, std::set<uint64>& oIDs);
00056         virtual ZMutexBase& GetReadLock();
00057         virtual ZMutexBase& GetWriteLock();
00058 
00059 // Our protocol
00060         ZRef<ZTSWatcher> NewWatcher();
00061 
00062 private:
00063         class PQuery;
00064         class PSpec;
00065         class PTuple;
00066         class WatcherQuery;
00067         class WatcherQueryTripped;
00068         class WatcherQueryUsing;
00069 
00070         class Watcher;
00071         friend class Watcher;
00072 
00073         void Watcher_Finalize(Watcher* iWatcher);
00074 
00075         void Watcher_AllocateIDs(Watcher* iWatcher,
00076                 size_t iCount, uint64& oBaseID, size_t& oCountIssued);
00077 
00078         void Watcher_Sync(Watcher* iWatcher,
00079                 const uint64* iRemovedIDs, size_t iRemovedIDsCount,
00080                 const uint64* iAddedIDs, size_t iAddedIDsCount,
00081                 const int64* iRemovedQueries, size_t iRemovedQueriesCount,
00082                 const ZTSWatcher::AddedQueryCombo* iAddedQueries, size_t iAddedQueriesCount,
00083                 vector<uint64>& oAddedIDs,
00084                 vector<uint64>& oChangedTupleIDs, vector<ZTuple>& oChangedTuples,
00085                 const uint64* iWrittenTupleIDs, const ZTuple* iWrittenTuples, size_t iWrittenTuplesCount,
00086                 std::map<int64, std::vector<uint64> >& oChangedQueries);
00087 
00088         void Watcher_SetCallback(Watcher* iWatcher, ZTSWatcher::Callback_t iCallback, void* iRefcon);
00089 
00090         class Quisitioner;
00091         friend class Quisitioner;
00092 
00093         void Quisitioner_Search(PQuery* iPQuery, const ZTBSpec& iTBSpec, std::set<uint64>& ioIDs);
00094         void Quisitioner_Search(PQuery* iPQuery, const ZTBSpec& iTBSpec, std::vector<uint64>& oIDs);
00095 
00096         void Quisitioner_FetchTuples(size_t iCount, const uint64* iIDs, ZTuple* oTuples);
00097         ZTuple Quisitioner_FetchTuple(uint64 iID);
00098 
00099 
00100         void pSetTuples(size_t iCount, const uint64* iIDs, const ZTuple* iTuples);
00101 
00102         void pSetTuples(size_t iCount, const uint64* iIDs, const ZTuple* iTuples,
00103                 std::set<Watcher*>& ioTouchedWatchers);
00104 
00105         void pInvalidatePSpecs(const ZTuple& iOld, const ZTuple& iNew,
00106                 std::set<Watcher*>& ioTouchedWatchers);
00107 
00108         void pInvalidateTuple(uint64 iID, std::set<Watcher*>& ioTouchedWatchers);
00109 
00110         PTuple* pGetPTupleExtant(uint64 iID);
00111         PTuple* pGetPTuple(uint64 iID);
00112         void pReleasePTuple(PTuple* iPTuple);
00113 
00114         PSpec* pGetPSpec(const ZTBSpec& iTBSpec);
00115         void pReleasePSpec(PSpec* iPSpec);
00116 
00117         void pReleasePQuery(PQuery* iPQuery);
00118 
00119         void pUpdateQueryResults(PQuery* iPQuery);
00120 
00121         ZRef<ZTS> fTS;
00122         ZMutex fMutex_Structure;
00123 
00124         std::map<ZMemoryBlock, PQuery*> fMB_To_PQuery;
00125 
00126         std::map<ZTuplePropName, std::set<PSpec*> > fPropName_To_PSpec;
00127         std::map<ZTBSpec, PSpec*> fTBSpec_To_PSpec;
00128 
00129         std::map<uint64, PTuple*> fID_To_PTuple;
00130 
00131         std::set<Watcher*> fWatchers;
00132         };
00133 
00134 #endif // __ZTS_Watchable__

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