Repository: ignite Updated Branches: refs/heads/master abb9d9428 -> 41dcce7b8
IGNITE-5154: Added RemoteFilter to C++ ContinuosQuery example. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/41dcce7b Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/41dcce7b Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/41dcce7b Branch: refs/heads/master Commit: 41dcce7b8adb86e5a053f62c658e36a9c67c35f5 Parents: abb9d94 Author: Igor Sapego <[email protected]> Authored: Wed May 31 18:34:21 2017 +0300 Committer: Igor Sapego <[email protected]> Committed: Wed May 31 18:34:21 2017 +0300 ---------------------------------------------------------------------- .../src/continuous_query_example.cpp | 125 ++++++++++++++++++- 1 file changed, 119 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/41dcce7b/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp b/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp index b08d4b8..2ca3163 100644 --- a/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp +++ b/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp @@ -33,14 +33,14 @@ using namespace examples; /** Cache name. */ const char* CACHE_NAME = "cpp_cache_continuous_query"; -/* +/** * Listener class. */ template<typename K, typename V> class Listener : public event::CacheEntryEventListener<K, V> { public: - /* + /** * Default constructor. */ Listener() @@ -65,6 +65,110 @@ public: } }; +/** + * Range Filter. Only lets through keys from the specified range. + */ +template<typename K, typename V> +struct RangeFilter : event::CacheEntryEventFilter<int32_t, std::string> +{ + /** + * Default constructor. + */ + RangeFilter() : + rangeBegin(0), + rangeEnd(0) + { + // No-op. + } + + /** + * Constructor. + * + * @param from Range beginning. Inclusive. + * @param to Range end. Not inclusive. + */ + RangeFilter(const K& from, const K& to) : + rangeBegin(from), + rangeEnd(to) + { + // No-op. + } + + /** + * Destructor. + */ + virtual ~RangeFilter() + { + // No-op. + } + + /** + * Event callback. + * + * @param event Event. + * @return True if the event passes filter. + */ + virtual bool Process(const CacheEntryEvent<K, V>& event) + { + return event.GetKey() >= rangeBegin && event.GetKey() < rangeEnd; + } + + /** Beginning of the range. */ + K rangeBegin; + + /** End of the range. */ + K rangeEnd; +}; + +namespace ignite +{ + namespace binary + { + template<> + struct BinaryType< RangeFilter<int32_t, std::string> > + { + static int32_t GetTypeId() + { + return GetBinaryStringHashCode("RangeFilter<int32_t,std::string>"); + } + + static void GetTypeName(std::string& dst) + { + dst = "RangeFilter<int32_t,std::string>"; + + } + + static int32_t GetFieldId(const char* name) + { + return GetBinaryStringHashCode(name); + } + + static bool IsNull(const RangeFilter<int32_t, std::string>&) + { + return false; + } + + static void GetNull(RangeFilter<int32_t, std::string>& dst) + { + dst = RangeFilter<int32_t, std::string>(); + } + + static void Write(BinaryWriter& writer, const RangeFilter<int32_t, std::string>& obj) + { + writer.WriteInt32("rangeBegin", obj.rangeBegin); + writer.WriteInt32("rangeEnd", obj.rangeEnd); + } + + static void Read(BinaryReader& reader, RangeFilter<int32_t, std::string>& dst) + { + dst.rangeBegin = reader.ReadInt32("rangeBegin"); + dst.rangeEnd = reader.ReadInt32("rangeEnd"); + } + }; + } +} + + int main() { IgniteConfiguration cfg; @@ -80,6 +184,12 @@ int main() std::cout << ">>> Cache continuous query example started." << std::endl; std::cout << std::endl; + // Get binding. + IgniteBinding binding = ignite.GetBinding(); + + // Registering remote filter. + binding.RegisterCacheEntryEventFilter< RangeFilter<int32_t, std::string> >(); + // Get cache instance. Cache<int32_t, std::string> cache = ignite.GetOrCreateCache<int32_t, std::string>(CACHE_NAME); @@ -97,17 +207,20 @@ int main() } // Declaring listener. - Listener<int, std::string> listener; + Listener<int32_t, std::string> listener; + + // Declaring filter. + RangeFilter<int32_t, std::string> filter(keyCnt, keyCnt + 5); // Declaring continuous query. - continuous::ContinuousQuery<int, std::string> qry(MakeReference(listener)); + continuous::ContinuousQuery<int32_t, std::string> qry(MakeReference(listener), MakeReference(filter)); { // Continous query scope. Query is closed when scope is left. - continuous::ContinuousQueryHandle<int, std::string> handle = cache.QueryContinuous(qry); + continuous::ContinuousQueryHandle<int32_t, std::string> handle = cache.QueryContinuous(qry); // Add a few more keys and watch more query notifications. - for (int32_t i = keyCnt; i < keyCnt + 5; ++i) + for (int32_t i = keyCnt; i < keyCnt + 10; ++i) { std::stringstream builder;
