This is an automated email from the ASF dual-hosted git repository. dbarnes pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode-native.git
commit cabe2825df49861aa3bd5c4c974f86ebcfff6b93 Author: Dave Barnes <[email protected]> AuthorDate: Fri Oct 5 14:21:11 2018 -0700 GEODE-4338, GEODE-4347: User Guide - incorporate CQ examples --- .../continuous-queries.html.md.erb | 189 ++++++++++++++++----- 1 file changed, 146 insertions(+), 43 deletions(-) diff --git a/docs/geode-native-docs/continuous-querying/continuous-queries.html.md.erb b/docs/geode-native-docs/continuous-querying/continuous-queries.html.md.erb index 40d58c0..4190d99 100644 --- a/docs/geode-native-docs/continuous-querying/continuous-queries.html.md.erb +++ b/docs/geode-native-docs/continuous-querying/continuous-queries.html.md.erb @@ -50,68 +50,171 @@ Events that change the query result are sent to the client immediately. 1. The client creates the CQ. This sets up everything for running the query and provides the client with a `CqQuery` object, but does not execute the CQ. At this point, the query is in a `STOPPED `state, ready to be closed or run. 2. The client runs the CQ with an API call to one of the `CqQuery execute*` methods. This puts the query into a `RUNNING` state on the client and on the server. +1. The CqListener fields events and takes action accordingly. Events are not result sets. If the action requires doing something with the data, the data must first be retrieved. 3. The CQ is closed by a client call to `CqQuery.close`. This de-allocates all resources in use for the CQ on the client and server. At this point, the cycle could begin again with the creation of a new `CqQuery` instance. -## CQ steps +### <a id="ExecutingACQ"></a>Executing a Continuous Query from the Client + +The essential steps to create and execute a continuous query are: + +1. Create an instance of the `QueryService` class. If you are using the pool API (recommended), you should obtain the `QueryService` from the pool. +1. Create a `CqListener` to field events sent from the server. +1. Use the `Query.execute()` method to submit the query string to the cache server. The server + remotely evaluates the query string, then monitors those results and notifies the client if they change. +1. Iterate through the returned objects. +1. When finished, close down the continuous query. + +### <a id="DotNetCQExample"></a>.NET Continuous Query Example + +These C# code excerpts are from the `examples\dotnet\ContinuousQueryCs` example included in your client +distribution. See the example for full context. + +Following the steps listed above, + +1. Create a query service: + + ``` + var cqAttributesFactory = new CqAttributesFactory<string, Order>(); + ``` + +1. Create a CQ Listener: + + ``` + var cqListener = new MyCqListener<string, Order>(); + ``` + +1. Insert the CQ Listener into a CQ attributes object: + + ``` + cqAttributesFactory.AddCqListener(cqListener); + + var cqAttributes = cqAttributesFactory.Create(); + ``` + +1. Create a Continuous Query using the query service and the CQ attributes: + + ``` + var query = queryService.NewCq("MyCq", "SELECT * FROM /example_orderobject WHERE quantity > 30", cqAttributes, false); + ``` + +1. Wait for events and do something with them. + + ``` + /* Excerpt from the CqListener */ + + /* Determine OP Type */ + switch (ev.getQueryOperation()) + { + case CqOperation.OP_TYPE_CREATE: + operationType = "CREATE"; + break; + case CqOperation.OP_TYPE_UPDATE: + operationType = "UPDATE"; + break; + case CqOperation.OP_TYPE_DESTROY: + operationType = "DESTROY"; + break; + default: + Console.WriteLine("Unexpected operation encountered {0}", ev.getQueryOperation()); + break; + } + + if (val != null) + { + Console.WriteLine("MyCqListener::OnEvent({0}) called with key {1}, value {2}", operationType, key, val.ToString()); + } + else + { + Console.WriteLine("MyCqListener::OnEvent({0}) called with key {1}, value null", operationType, key); + } + ... + + /* Take action based on OP Type */ + + ``` + +1. When finished, close up shop. + + ``` + query.Stop(); + query.Close(); + ... + cache.Close(); + ``` + +### <a id="CppCQExample"></a>C++ Continuous Query Example + +These C++ code excerpts are from the `examples/cpp/continuousquery` example included in your client +distribution. See the example for full context. + +Following the steps listed above, 1. Create a query service: -``` - auto queryService = pool->getQueryService(); -``` + ``` + auto queryService = pool->getQueryService(); + ``` 1. Create a CQ Listener: -``` -class MyCqListener : public CqListener { - /* the CqListener fields events and takes action accordingly */ - /* Note that events are not result sets. If the action requires - doing something with the data, it must first be retrieved */ -} -``` + ``` + class MyCqListener : public CqListener { + ``` -1. Insert the CQ Listener in a CQ attributes object: +1. Insert the CQ Listener into a CQ attributes object: -``` - CqAttributesFactory cqFactory; + ``` + CqAttributesFactory cqFactory; - auto cqListener = std::make_shared<MyCqListener>(); + auto cqListener = std::make_shared<MyCqListener>(); - cqFactory.addCqListener(cqListener); - auto cqAttributes = cqFactory.create(); -``` + cqFactory.addCqListener(cqListener); + auto cqAttributes = cqFactory.create(); + ``` 1. Create a Continuous Query using the query service and the CQ attributes: -``` - auto query = queryService->newCq( - "MyCq", "SELECT * FROM /custom_orders c WHERE c.quantity > 30", - cqAttributes); -``` + ``` + auto query = queryService->newCq( + "MyCq", "SELECT * FROM /custom_orders c WHERE c.quantity > 30", + cqAttributes); + ``` 1. Wait for events and do something with them. -``` - /* Excerpt from the CqListener */ - - /* Determine OP Type */ - switch (cqEvent.getQueryOperation()) { - case CqOperation::OP_TYPE_CREATE: - opStr = "CREATE"; - break; - case CqOperation::OP_TYPE_UPDATE: - opStr = "UPDATE"; - break; - case CqOperation::OP_TYPE_DESTROY: - opStr = "DESTROY"; - break; - default: - break; - } + ``` + /* Excerpt from the CqListener */ - ... + /* Determine OP Type */ + switch (cqEvent.getQueryOperation()) { + case CqOperation::OP_TYPE_CREATE: + opStr = "CREATE"; + break; + case CqOperation::OP_TYPE_UPDATE: + opStr = "UPDATE"; + break; + case CqOperation::OP_TYPE_DESTROY: + opStr = "DESTROY"; + break; + default: + break; + } + + ... + + /* Take action based on OP Type */ + + ``` + +1. When finished, close up shop. + + ``` + query->execute(); - /* Take action based on OP Type */ + ... (field events as they arrive) -``` + query->stop(); + query->close(); + cache.close(); + ```
