[ 
https://issues.apache.org/jira/browse/GEODE-5845?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16645479#comment-16645479
 ] 

Bill commented on GEODE-5845:
-----------------------------

To point (1): it isn't clear to me, why drawing key values from a set of named 
constants would, in general, be a bad idea. Is it bad because we envision that 
set (of keys) to be relatively small?

I don't see anything particularly wrong with a key having a small number of 
distinct values. It seems perfectly reasonable to create a region to hold days 
of the week, months of the year, or countries on planet earth.

Point (2) is on point. I have a couple minor suggestions:

I think this would be clearer:

"The hash of an enum is derived from a local memory address, resulting in 
different hash values on different servers."

And it seems to me the good advice should be something like:
 * don't use an {{enum}} type as a key (for a region)
 * don't use the {{hashCode()}} of an {{enum}} type in computing the hash code 
of a (non-enum) key type

(Geode could conceivably enforce that first bit of advice at runtime. The 
second would be a lot harder.)

Then there's the [{{Region}} class 
Javadoc|https://geode.apache.org/releases/latest/javadoc/index.html?org/apache/geode/cache/Region.html]:
{quote}Region entries are identified by their key. Any Object can be used as a 
key as long as the key Object is region-wide unique and implements both the 
equals and hashCode methods. For regions with distributed scope, the key must 
also be Serializable.
{quote}
That Javadoc is correct. But as GEODE-5666 illustrates, it's incomplete. Per 
the existing Javadoc: the key type {{K}}, used in a {{Region<K,V>}} must define 
{{equals()}} and {{hashCode()}} methods. What we need to stipulate, 
additionally is: if the keys are for a partitioned region, then {{hashCode()}} 
must _return the same results when applied to the same key value, in every JVM_.

As alluded to in the first comment, this would not be an insurmountable problem 
(with {{enum}} types) if the user could override the {{hashCode()}} method on 
an {{enum}} type. Alas, the {{hashCode()}} method on an {{enum}} type is 
{{final}}—it cannot be overridden. As a result {{enum}} types should not be 
used as key types for regions.

> Document that an enum should never be used for keys
> ---------------------------------------------------
>
>                 Key: GEODE-5845
>                 URL: https://issues.apache.org/jira/browse/GEODE-5845
>             Project: Geode
>          Issue Type: Improvement
>          Components: docs
>            Reporter: Karen Smoler Miller
>            Assignee: Karen Smoler Miller
>            Priority: Major
>
> Maybe it should be obvious, but an enum type should never be used for an 
> entry key. To make this explicitly clear, add this advice to the docs.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to