How are ENUMs treated in the expression of an index in Gemfire?
search cancel

How are ENUMs treated in the expression of an index in Gemfire?

book

Article ID: 294061

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

The VMware GemFire query engine supports indexing. An index can provide significant performance gains for query execution. A query run without the aid of an index iterates through every object in the collection. If an index is available that matches part or all of the query specification, the query iterates only over the indexed set, and query processing time can be reduced. The Gemfire document (https://docs.vmware.com/en/VMware-GemFire/9.15/gf/developing-query_index-query_index.html) tells about many different ways to work with indexes. 


Enumeration or ENUM is a special kind of data type defined by the user. ENUMs are treated as data types, and users can use them to create sets of constants for use with variables and properties. In GemFire, ENUMs are treated as their underlying ordinal values when they are used in the expression of an index. An index in GemFire allows for efficient lookup and retrieval of data based on specified criteria.

This article will explain how ENUMs are treated in the expression of an index.

Resolution

When an ENUM is used as part of the index expression, GemFire internally uses the ordinal value of the ENUM as the indexed value. The ordinal value represents the position of each ENUM constant within the ENUM declaration, starting from zero.

Here's an example to illustrate how ENUMs are treated in the expression of an index in GemFire:

Suppose we have an ENUM called "Color" defined as follows:
 

public enum Color {
  RED,
  BLUE,
  GREEN
}

And we have a Region in GemFire where each entry has a field called "color" of type Color. We want to create an index on this field for efficient querying.

To create an index on the "color" field, we can use the following GemFire API:
 

Region region = cache.getRegion("yourRegionName");
region.createIndex("colorIndex", "color", "/yourRegionName");

In this case, GemFire will internally use the ordinal values of the Color enum constants (RED, BLUE, GREEN) as the indexed values. The ordinal values are 0, 1, and 2, respectively. The index will enable efficient lookups based on these ordinal values.

It's important to note that when using ENUMs in index expressions, care should be taken to avoid modifying the order or adding/removing constants to the ENUM declaration. Any changes to the ENUM declaration can alter the ordinal values, potentially affecting the correctness of the index lookup.

Additionally, it's worth considering using a more stable and consistent index key, such as a unique identifier or an integer value, instead of relying on ENUMs directly for indexing if there is a possibility of changes to the ENUM declaration.