|
Summary: The purpose of the custom query pattern is to extend the existing query capabilities by enabling users to write code and execute it as part of the query path.
Motivation for Using Custom QueryGigaSpaces provides various methods for performing queries on the space. The most common are template-based matching, and the SQLQuery. These allow the executing of queries expressively, based on well defined query semantics. Parallel queries are also supported implicitly, when using any of these query methods with a partitioned space. There are cases where the well defined query semantics don't meet specific requirements, or where the requirements are met, but performance isn't adequate. The purpose of the custom query pattern is to extend the existing query capabilities by enabling users to write code and execute it as part of the query path. Similar to a database-stored procedure, a custom query needs to be running close to the data. The custom query should be triggered during the normal query execution path, and should therefore be able to effect the result while the query is taking place. Currently, the component in GigaSpaces that enables such intervention is the space filter. However, the filters are relatively static – they are configured as part of the space configuration and can not be changed during runtime. To overcome this limitation, you need to build a generic filter that gets the custom query commands as parts of its arguments. It uses these arguments to execute multiple custom queries without changing its behavior or implementation – instead of having a different filter per query, one filter is used for all queries, which acts as the hosting environment for query commands that are received as arguments. In the diagram below, the flow of events is as follows:
Executing Custom QueryThe sequence diagram below shows the flow of events once the template gets into the filter's execution:
Code ExampleThe example below populates objects into the space and then executes the following query: "select sum(accountNumber) from Entity where flags <= 5 "
The example consists of two parts, a generic framework and a specific custom query example that implements the query statement above. The generic framework classes are not specific to this example. They include the filter implementation, and an AbstractQueryTask, which is used to provide common framework for the creation of QueryTasks. It also includes space schema (QueryFilter-space-schema) configuration, that is set to run the filter as part of the space startup, and is already configured to intercept readMultiple and takeMultiple operations. The space schema using this filter should have the following added: <filter-names>DefaultSecurityFilter, Statistics, QueryFilter</filter-names> <QueryFilter> <enabled>true</enabled> <security>false</security> <class>com.gs.example.query.QueryTaskFilter</class> <url>none</url> <priority>1</priority> <operation-code>2,3,11,13,51</operation-code> </QueryFilter> The generic framework needs to be included as part of the space server setup. The example classes: SumTask – implements the custom query logic. Installing and Running the ExampleTo install the example, download the CustomQuery.zip archive and extract it to your <GigaSpaces Root>/examples/Advanced/Data_Grid directory. To run the example, execute the run command from the example directory. Upon a successful run, you should see the following output: INFO: java:// protocol. Get <IMDG> space from <IMDG_container> container
---> Start write 1000 entries
---> Finish write 1000 entries
---> Start execute select sum(accountNumber) from Entity where flags <= 5 1000 entries
---> Read Multiple
SumTask: Executing custom SUM query
--> sum = 15
---> Total Sum = 15
---> Take Multiple
SumTask: Executing custom SUM query
--> sum = 15
---> sum(accountNumber) = 15
---> Total Sum = 15
Writing your Own Custom QueryTo run your own custom query, write a Task class that extends the AbstractQueryTask.java class, similar to the implementation of the SumTask class. Write the querie's business logic as part of the class's execute method. Using Custom Query Framework Outside Context of the ExampleTo use the custom query framework outside the environment, put the CustomQuery.zip file in your space and application classpath. When you start the space, make sure you start with a schema argument set to QueryFilter in the following way: SpaceFinder.find("/./myspace?schema=QueryFilter");
|
(works on Firefox 2 and Internet Explorer 7)
