Custom Matching

Search Solutions & Best Practices
Searching Solutions and Best Practices
Browse Solutions & Best Practices

                                                              

Summary: Custom Matching Implementation
Author: Shay Hassidim, Deputy CTO, GigaSpaces
Recently tested with GigaSpaces version: XAP 7.0
Last Update: January 2010
Your Rating: Results: PatheticBadOKGoodOutstanding! 5 rates

Overview

Usually you index and execute queries using primitive fields (long, float, string, etc). The fields may be within the root level of the space object, or embedded within nested objects within the space object. You may construct a query using a template object or SQL to specify the criteria you would like to use when the matching phase is performed within the space when looking for the relevant objects.

In some cases you might want to use a custom data type with a custom business logic to find matching objects within the space, rather the usual primitive data type comparison. To allow the space to invoke your business logic when the matching process is conducted, the Comparable interface should be implemented for a class that stores the data you would like to use with your custom business logic.

The Comparable implementation should not be done for the space class itself, but for one of its fields.

Such custom business logic might be useful when comparing vector data (2 dimensional Cartesian space). These may represent sound, maps, pictures or any other 2 or 3 dimensional artifacts. You may use this technique to query data based on any other mathematical or financial related formulas such as Time value of money like Present Value of a Cash Flow Series, Future Value of a Cash Flow Series, etc. Other areas where such custom matching is relevant, are Pattern recognition, Sequence analysis, Surveillance, Forensic, Social network behavior etc.

In some cases, you may index the data to speed up the custom matching process. To enable this, you should index the field so that its class implements the Comparable interface using the EXTENDED index type as part of the space class. See the Indexing page for additional information about how to enable the EXTENDED index. Indexing the custom type field should be used carefully since it does not support a Comparable.compareTo implementation that performs relative-based matching, as demonstrated by the following example.

Vector Compare Example

The following example illustrates a business logic implementation used to query for vector data (an array of Integer values), using the Euclidean distance formula:

The object that holds the array implements the Comparable interface. The Space class has a getter method for this field indexing, using the EXTENDED index. The actual query involves indexed fields for several sample data points within the vector, together with the custom field:

SQLQuery<Vector> query= new SQLQuery<Vector>(Vector.class , 
		"(x0 > ? AND x0 < ? ) " +
		"AND (x1 > ? AND x1 < ? ) " +
		"AND (x2 > ? AND x2 < ? ) " +
		"AND (x3 > ? AND x3 < ? ) " +
		"AND (x4 > ? AND x4 < ? ) " +
		"AND (x5 > ? AND x5 < ? ) " +
		"AND (x6 > ? AND x6 < ? ) " +
		"AND (x7 > ? AND x7 < ? ) " +
		"AND (x8 > ? AND x8 < ? ) " +
		"AND (x9 > ? AND x9 < ? ) " +
		"AND (x10 > ? AND x10 < ? ) " +
		"AND vectordata <= ?");

Here is an example of a target vector, and a matching vector found using the custom matching implementation illustrated below:

To scale the system you should deploy the space using the partitioned cluster schema. This will allow queries (i.e. matching) to be executed across all the partitions in parallel, speeding up the query execution time.

See The Application tab for the full query usage. This allows the Comparable.compareTo implementation to be performed on a smaller candidate subset of objects.

Rate this page:
Your Rating: Results: PatheticBadOKGoodOutstanding! 5 rates

Additional resources: XAP Application Server | XAP Data Grid | XAP for Cloud Computing | XAP J2EE Support

Labels

 
(None)