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 __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
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
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__